1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// This file is part of faster, the SIMD library for humans.
// Copyright 2017 Adam Niederer <adam.niederer@gmail.com>
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
pub trait Merge {
/// Return a vector with the first half populated by the first half of
/// `self`, and the second half populated by the second half of `other`.
///
/// ```
/// extern crate faster;
/// use faster::*;
///
/// # fn main() {
/// assert_eq!(u8s(2).merge_halves(u8s(3)), u8s::halfs(2, 3));
/// # }
/// ```
fn merge_halves(&self, other: Self) -> Self;
/// Return a vector containing the even elements of `self` interleaved with
/// the odd elements of other, starting with the first element of `self`.
///
/// ```
/// extern crate faster;
/// use faster::*;
///
/// # fn main() {
/// assert_eq!(u8s(2).merge_interleaved(u8s(3)), u8s::interleave(2, 3));
/// # }
/// ```
fn merge_interleaved(&self, other: Self) -> Self;
/// Return a vector containing the first `offset` elements of `self`, then
/// the last `(Self::WIDTH - offset)` elements of `other`.
///
/// ```
/// extern crate faster;
/// use faster::*;
///
/// # fn main() {
/// assert_eq!(u8s(2).merge_partitioned(u8s(3), 2), u8s::partition(2u8, 3u8, 2));
/// # }
/// ```
fn merge_partitioned(&self, other: Self, offset: usize) -> Self;
}
macro_rules! impl_packed_merge {
($vec:ty, $uvec:tt, $uscl:tt, $mmfn:expr, $feat:expr, ($($a:expr),*), ($($b:expr),*), $($na:expr, $nb:expr),*) => {
#[cfg(not(target_feature = $feat))]
impl Merge for $vec {
#[inline(always)]
fn merge_halves(&self, other: Self) -> Self {
fallback!();
unsafe {
Self::new($(self.extract_unchecked($a)),*,
$(other.extract_unchecked($b)),*)
}
}
#[inline(always)]
fn merge_interleaved(&self, other: Self) -> Self {
fallback!();
unsafe {
Self::new($(self.extract_unchecked($na), other.extract_unchecked($nb)),*)
}
}
#[inline(always)]
fn merge_partitioned(&self, other: Self, offset: usize) -> Self {
fallback!();
assert!(offset < Self::WIDTH);
let mut ret = self.clone();
for i in offset..Self::WIDTH {
unsafe {
ret = ret.replace_unchecked(i, other.extract_unchecked(i));
}
}
ret
}
}
#[cfg(target_feature = $feat)]
impl Merge for $vec {
#[inline(always)]
fn merge_halves(&self, other: Self) -> Self {
unsafe {
transmute($mmfn(
self.be_i8s(), other.be_i8s(),
transmute($uvec::halfs($uscl::min_value(), $uscl::max_value()))))
}
}
#[inline(always)]
fn merge_interleaved(&self, other: Self) -> Self {
unsafe {
transmute($mmfn(
self.be_i8s(), other.be_i8s(),
transmute($uvec::interleave($uscl::min_value(), $uscl::max_value()))))
}
}
#[inline(always)]
fn merge_partitioned(&self, other: Self, offset: usize) -> Self {
unsafe {
transmute($mmfn(
self.be_i8s(), other.be_i8s(),
transmute(Self::partition_mask(offset))))
}
}
}
}
}
macro_rules! test_packed_merge {
(($($vec:tt),*), ($($fn:ident),*)) => {
$(
#[test]
fn $fn() {
let asc = 30i32 as <$vec as Packed>::Scalar;
let bsc = 5i32 as <$vec as Packed>::Scalar;
let a = $vec::splat(asc);
let b = $vec::splat(bsc);
assert_eq!(a.merge_interleaved(b), $vec::interleave(asc, bsc));
assert_eq!(b.merge_interleaved(a), $vec::interleave(bsc, asc));
assert_eq!(a.merge_halves(b), $vec::halfs(asc, bsc));
assert_eq!(b.merge_halves(a), $vec::halfs(bsc, asc));
for i in 0..$vec::WIDTH {
assert_eq!(a.merge_partitioned(b, i), $vec::partition(asc, bsc, i));
assert_eq!(b.merge_partitioned(a, i), $vec::partition(bsc, asc, i));
}
}
)*
}
}