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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//! Implements portable arithmetic vector reductions.
#![allow(unused)]

macro_rules! impl_arithmetic_reductions {
    ($id:ident, $elem_ty:ident) => {
        impl $id {
            /// Horizontal sum of the vector elements.
            ///
            /// The intrinsic performs a tree-reduction of the vector elements.
            /// That is, for an 8 element vector:
            ///
            /// > ((x0 + x1) + (x2 + x3)) + ((x4 + x5) + (x6 + x7))
            ///
            /// # Integer vectors
            ///
            /// If an operation overflows it returns the mathematical result
            /// modulo `2^n` where `n` is the number of times it overflows.
            ///
            /// # Floating-point vectors
            ///
            /// If one of the vector element is `NaN` the reduction returns
            /// `NaN`.
            #[cfg(not(target_arch = "aarch64"))]
            #[inline]
            pub fn wrapping_sum(self) -> $elem_ty {
                use coresimd::simd_llvm::simd_reduce_add_ordered;
                unsafe { simd_reduce_add_ordered(self, 0 as $elem_ty) }
            }
            /// Horizontal sum of the vector elements.
            ///
            /// The intrinsic performs a tree-reduction of the vector elements.
            /// That is, for an 8 element vector:
            ///
            /// > ((x0 + x1) + (x2 + x3)) + ((x4 + x5) + (x6 + x7))
            ///
            /// # Integer vectors
            ///
            /// If an operation overflows it returns the mathematical result
            /// modulo `2^n` where `n` is the number of times it overflows.
            ///
            /// # Floating-point vectors
            ///
            /// If one of the vector element is `NaN` the reduction returns
            /// `NaN`.
            #[cfg(target_arch = "aarch64")]
            #[inline]
            pub fn wrapping_sum(self) -> $elem_ty {
                // FIXME: broken on AArch64
                // https://bugs.llvm.org/show_bug.cgi?id=36796
                use super::codegen::wrapping::Wrapping;
                let mut x = self.extract(0) as $elem_ty;
                for i in 1..$id::lanes() {
                    x = Wrapping::add(x, self.extract(i) as $elem_ty);
                }
                x
            }

            /// Horizontal product of the vector elements.
            ///
            /// The intrinsic performs a tree-reduction of the vector elements.
            /// That is, for an 8 element vector:
            ///
            /// > ((x0 * x1) * (x2 * x3)) * ((x4 * x5) * (x6 * x7))
            ///
            /// # Integer vectors
            ///
            /// If an operation overflows it returns the mathematical result
            /// modulo `2^n` where `n` is the number of times it overflows.
            ///
            /// # Floating-point vectors
            ///
            /// If one of the vector element is `NaN` the reduction returns
            /// `NaN`.
            #[cfg(not(target_arch = "aarch64"))]
            #[inline]
            pub fn wrapping_product(self) -> $elem_ty {
                use coresimd::simd_llvm::simd_reduce_mul_ordered;
                unsafe { simd_reduce_mul_ordered(self, 1 as $elem_ty) }
            }
            /// Horizontal product of the vector elements.
            ///
            /// The intrinsic performs a tree-reduction of the vector elements.
            /// That is, for an 8 element vector:
            ///
            /// > ((x0 * x1) * (x2 * x3)) * ((x4 * x5) * (x6 * x7))
            ///
            /// # Integer vectors
            ///
            /// If an operation overflows it returns the mathematical result
            /// modulo `2^n` where `n` is the number of times it overflows.
            ///
            /// # Floating-point vectors
            ///
            /// If one of the vector element is `NaN` the reduction returns
            /// `NaN`.
            #[cfg(target_arch = "aarch64")]
            #[inline]
            pub fn wrapping_product(self) -> $elem_ty {
                // FIXME: broken on AArch64
                // https://bugs.llvm.org/show_bug.cgi?id=36796
                use super::codegen::wrapping::Wrapping;
                let mut x = self.extract(0) as $elem_ty;
                for i in 1..$id::lanes() {
                    x = Wrapping::mul(x, self.extract(i) as $elem_ty);
                }
                x
            }
        }
    };
}

#[cfg(test)]
macro_rules! test_arithmetic_reductions {
    ($id:ident, $elem_ty:ident) => {
        fn alternating(x: usize) -> ::coresimd::simd::$id {
            use coresimd::simd::$id;
            let mut v = $id::splat(1 as $elem_ty);
            for i in 0..$id::lanes() {
                if i % x == 0 {
                    v = v.replace(i, 2 as $elem_ty);
                }
            }
            v
        }

        #[test]
        fn wrapping_sum() {
            use coresimd::simd::$id;
            let v = $id::splat(0 as $elem_ty);
            assert_eq!(v.wrapping_sum(), 0 as $elem_ty);
            let v = $id::splat(1 as $elem_ty);
            assert_eq!(v.wrapping_sum(), $id::lanes() as $elem_ty);
            let v = alternating(2);
            assert_eq!(
                v.wrapping_sum(),
                ($id::lanes() / 2 + $id::lanes()) as $elem_ty
            );
        }
        #[test]
        fn wrapping_product() {
            use coresimd::simd::$id;
            let v = $id::splat(0 as $elem_ty);
            assert_eq!(v.wrapping_product(), 0 as $elem_ty);
            let v = $id::splat(1 as $elem_ty);
            assert_eq!(v.wrapping_product(), 1 as $elem_ty);
            let f = match $id::lanes() {
                64 => 16,
                32 => 8,
                16 => 4,
                _ => 2,
            };
            let v = alternating(f);
            assert_eq!(
                v.wrapping_product(),
                (2_usize.pow(($id::lanes() / f) as u32) as $elem_ty)
            );
        }
    };
}