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
//! Implements mask's `select`.

/// Implements mask select method
macro_rules! impl_select {
    ([$elem_ty:ident; $elem_count:expr]: $id:ident | $test_tt:tt) => {
        impl $id {
            /// Selects elements of `a` and `b` using mask.
            ///
            /// The lanes of the result for which the mask is `true` contain
            /// the values of `a`. The remaining lanes contain the values of
            /// `b`.
            #[inline]
            pub fn select<T>(self, a: Simd<T>, b: Simd<T>) -> Simd<T>
            where
                T: sealed::SimdArray<
                    NT = <[$elem_ty; $elem_count] as sealed::SimdArray>::NT,
                >,
            {
                use crate::llvm::simd_select;
                Simd(unsafe { simd_select(self.0, a.0, b.0) })
            }
        }

        test_select!(bool, $id, $id, (false, true) | $test_tt);
    };
}

macro_rules! test_select {
    (
        $elem_ty:ident,
        $mask_ty:ident,
        $vec_ty:ident,($small:expr, $large:expr) |
        $test_tt:tt
    ) => {
        test_if! {
            $test_tt:
            paste::item! {
                pub mod [<$vec_ty _select>] {
                    use super::*;

                    #[cfg_attr(not(target_arch = "wasm32"), test)] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
                    fn select() {
                        let o = $small as $elem_ty;
                        let t = $large as $elem_ty;

                        let a = $vec_ty::splat(o);
                        let b = $vec_ty::splat(t);
                        let m = a.lt(b);
                        assert_eq!(m.select(a, b), a);

                        let m = b.lt(a);
                        assert_eq!(m.select(b, a), a);

                        let mut c = a;
                        let mut d = b;
                        let mut m_e = $mask_ty::splat(false);
                        for i in 0..$vec_ty::lanes() {
                            if i % 2 == 0 {
                                let c_tmp = c.extract(i);
                                c = c.replace(i, d.extract(i));
                                d = d.replace(i, c_tmp);
                            } else {
                                m_e = m_e.replace(i, true);
                            }
                        }

                        let m = c.lt(d);
                        assert_eq!(m_e, m);
                        assert_eq!(m.select(c, d), a);
                    }
                }
            }
        }
    };
}