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
//! Lane-wise arithmetic operations.

macro_rules! impl_arithmetic_ops {
    ($id:ident) => {
        impl ops::Add for $id {
            type Output = Self;
            #[inline]
            fn add(self, other: Self) -> Self {
                unsafe { simd_add(self, other) }
            }
        }

        impl ops::Sub for $id {
            type Output = Self;
            #[inline]
            fn sub(self, other: Self) -> Self {
                unsafe { simd_sub(self, other) }
            }
        }

        impl ops::Mul for $id {
            type Output = Self;
            #[inline]
            fn mul(self, other: Self) -> Self {
                unsafe { simd_mul(self, other) }
            }
        }

        impl ops::Div for $id {
            type Output = Self;
            #[inline]
            fn div(self, other: Self) -> Self {
                unsafe { simd_div(self, other) }
            }
        }

        impl ops::Rem for $id {
            type Output = Self;
            #[inline]
            fn rem(self, other: Self) -> Self {
                unsafe { simd_rem(self, other) }
            }
        }

        impl ops::AddAssign for $id {
            #[inline]
            fn add_assign(&mut self, other: Self) {
                *self = *self + other;
            }
        }

        impl ops::SubAssign for $id {
            #[inline]
            fn sub_assign(&mut self, other: Self) {
                *self = *self - other;
            }
        }

        impl ops::MulAssign for $id {
            #[inline]
            fn mul_assign(&mut self, other: Self) {
                *self = *self * other;
            }
        }

        impl ops::DivAssign for $id {
            #[inline]
            fn div_assign(&mut self, other: Self) {
                *self = *self / other;
            }
        }

        impl ops::RemAssign for $id {
            #[inline]
            fn rem_assign(&mut self, other: Self) {
                *self = *self % other;
            }
        }
    }
}

#[cfg(test)]
#[macro_export]
macro_rules! test_arithmetic_ops {
    ($id:ident, $elem_ty:ident) => {
        #[test]
        fn arithmetic() {
            use ::coresimd::simd::$id;
            let z = $id::splat(0 as $elem_ty);
            let o = $id::splat(1 as $elem_ty);
            let t = $id::splat(2 as $elem_ty);
            let f = $id::splat(4 as $elem_ty);

            // add
            assert_eq!(z + z, z);
            assert_eq!(o + z, o);
            assert_eq!(t + z, t);
            assert_eq!(t + t, f);
            // sub
            assert_eq!(z - z, z);
            assert_eq!(o - z, o);
            assert_eq!(t - z, t);
            assert_eq!(f - t, t);
            assert_eq!(f - o - o, t);
            // mul
            assert_eq!(z * z, z);
            assert_eq!(z * o, z);
            assert_eq!(z * t, z);
            assert_eq!(o * t, t);
            assert_eq!(t * t, f);
            // div
            assert_eq!(z / o, z);
            assert_eq!(t / o, t);
            assert_eq!(f / o, f);
            assert_eq!(t / t, o);
            assert_eq!(f / t, t);
            // rem
            assert_eq!(o % o, z);
            assert_eq!(f % t, z);

            {
                let mut v = z;
                assert_eq!(v, z);
                v += o;  // add_assign
                assert_eq!(v, o);
                v -= o; // sub_assign
                assert_eq!(v, z);
                v = t;
                v *= o; // mul_assign
                assert_eq!(v, t);
                v *= t;
                assert_eq!(v, f);
                v /= o; // div_assign
                assert_eq!(v, f);
                v /= t;
                assert_eq!(v, t);
                v %= t; // rem_assign
                assert_eq!(v, z);
            }
        }
    };
}