|
| 1 | +//! Lane-wise arithmetic operations. |
| 2 | +#![allow(unused)] |
| 3 | + |
| 4 | +macro_rules! impl_arithmetic_scalar_ops { |
| 5 | + ($id:ident, $elem_ty:ident) => { |
| 6 | + impl ::ops::Add<$elem_ty> for $id { |
| 7 | + type Output = Self; |
| 8 | + #[inline] |
| 9 | + fn add(self, other: $elem_ty) -> Self { |
| 10 | + self + $id::splat(other) |
| 11 | + } |
| 12 | + } |
| 13 | + impl ::ops::Add<$id> for $elem_ty { |
| 14 | + type Output = $id; |
| 15 | + #[inline] |
| 16 | + fn add(self, other: $id) -> $id { |
| 17 | + $id::splat(self) + other |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + impl ::ops::Sub<$elem_ty> for $id { |
| 22 | + type Output = Self; |
| 23 | + #[inline] |
| 24 | + fn sub(self, other: $elem_ty) -> Self { |
| 25 | + self - $id::splat(other) |
| 26 | + } |
| 27 | + } |
| 28 | + impl ::ops::Sub<$id> for $elem_ty { |
| 29 | + type Output = $id; |
| 30 | + #[inline] |
| 31 | + fn sub(self, other: $id) -> $id { |
| 32 | + $id::splat(self) - other |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + impl ::ops::Mul<$elem_ty> for $id { |
| 37 | + type Output = Self; |
| 38 | + #[inline] |
| 39 | + fn mul(self, other: $elem_ty) -> Self { |
| 40 | + self * $id::splat(other) |
| 41 | + } |
| 42 | + } |
| 43 | + impl ::ops::Mul<$id> for $elem_ty { |
| 44 | + type Output = $id; |
| 45 | + #[inline] |
| 46 | + fn mul(self, other: $id) -> $id { |
| 47 | + $id::splat(self) * other |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + impl ::ops::Div<$elem_ty> for $id { |
| 52 | + type Output = Self; |
| 53 | + #[inline] |
| 54 | + fn div(self, other: $elem_ty) -> Self { |
| 55 | + self / $id::splat(other) |
| 56 | + } |
| 57 | + } |
| 58 | + impl ::ops::Div<$id> for $elem_ty { |
| 59 | + type Output = $id; |
| 60 | + #[inline] |
| 61 | + fn div(self, other: $id) -> $id { |
| 62 | + $id::splat(self) / other |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + impl ::ops::Rem<$elem_ty> for $id { |
| 67 | + type Output = Self; |
| 68 | + #[inline] |
| 69 | + fn rem(self, other: $elem_ty) -> Self { |
| 70 | + self % $id::splat(other) |
| 71 | + } |
| 72 | + } |
| 73 | + impl ::ops::Rem<$id> for $elem_ty { |
| 74 | + type Output = $id; |
| 75 | + #[inline] |
| 76 | + fn rem(self, other: $id) -> $id { |
| 77 | + $id::splat(self) % other |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + impl ::ops::AddAssign<$elem_ty> for $id { |
| 82 | + #[inline] |
| 83 | + fn add_assign(&mut self, other: $elem_ty) { |
| 84 | + *self = *self + other; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + impl ::ops::SubAssign<$elem_ty> for $id { |
| 89 | + #[inline] |
| 90 | + fn sub_assign(&mut self, other: $elem_ty) { |
| 91 | + *self = *self - other; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + impl ::ops::MulAssign<$elem_ty> for $id { |
| 96 | + #[inline] |
| 97 | + fn mul_assign(&mut self, other: $elem_ty) { |
| 98 | + *self = *self * other; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + impl ::ops::DivAssign<$elem_ty> for $id { |
| 103 | + #[inline] |
| 104 | + fn div_assign(&mut self, other: $elem_ty) { |
| 105 | + *self = *self / other; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + impl ::ops::RemAssign<$elem_ty> for $id { |
| 110 | + #[inline] |
| 111 | + fn rem_assign(&mut self, other: $elem_ty) { |
| 112 | + *self = *self % other; |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +#[cfg(test)] |
| 119 | +macro_rules! test_arithmetic_scalar_ops { |
| 120 | + ($id:ident, $elem_ty:ident) => { |
| 121 | + #[test] |
| 122 | + fn arithmetic_scalar() { |
| 123 | + use ::coresimd::simd::$id; |
| 124 | + let zi = 0 as $elem_ty; |
| 125 | + let oi = 1 as $elem_ty; |
| 126 | + let ti = 2 as $elem_ty; |
| 127 | + let fi = 4 as $elem_ty; |
| 128 | + let z = $id::splat(zi); |
| 129 | + let o = $id::splat(oi); |
| 130 | + let t = $id::splat(ti); |
| 131 | + let f = $id::splat(fi); |
| 132 | + |
| 133 | + // add |
| 134 | + assert_eq!(zi + z, z); |
| 135 | + assert_eq!(z + zi, z); |
| 136 | + assert_eq!(oi + z, o); |
| 137 | + assert_eq!(o + zi, o); |
| 138 | + assert_eq!(ti + z, t); |
| 139 | + assert_eq!(t + zi, t); |
| 140 | + assert_eq!(ti + t, f); |
| 141 | + assert_eq!(t + ti, f); |
| 142 | + // sub |
| 143 | + assert_eq!(zi - z, z); |
| 144 | + assert_eq!(z - zi, z); |
| 145 | + assert_eq!(oi - z, o); |
| 146 | + assert_eq!(o - zi, o); |
| 147 | + assert_eq!(ti - z, t); |
| 148 | + assert_eq!(t - zi, t); |
| 149 | + assert_eq!(fi - t, t); |
| 150 | + assert_eq!(f - ti, t); |
| 151 | + assert_eq!(f - o - o, t); |
| 152 | + assert_eq!(f - oi - oi, t); |
| 153 | + // mul |
| 154 | + assert_eq!(zi * z, z); |
| 155 | + assert_eq!(z * zi, z); |
| 156 | + assert_eq!(zi * o, z); |
| 157 | + assert_eq!(z * oi, z); |
| 158 | + assert_eq!(zi * t, z); |
| 159 | + assert_eq!(z * ti, z); |
| 160 | + assert_eq!(oi * t, t); |
| 161 | + assert_eq!(o * ti, t); |
| 162 | + assert_eq!(ti * t, f); |
| 163 | + assert_eq!(t * ti, f); |
| 164 | + // div |
| 165 | + assert_eq!(zi / o, z); |
| 166 | + assert_eq!(z / oi, z); |
| 167 | + assert_eq!(ti / o, t); |
| 168 | + assert_eq!(t / oi, t); |
| 169 | + assert_eq!(fi / o, f); |
| 170 | + assert_eq!(f / oi, f); |
| 171 | + assert_eq!(ti / t, o); |
| 172 | + assert_eq!(t / ti, o); |
| 173 | + assert_eq!(fi / t, t); |
| 174 | + assert_eq!(f / ti, t); |
| 175 | + // rem |
| 176 | + assert_eq!(oi % o, z); |
| 177 | + assert_eq!(o % oi, z); |
| 178 | + assert_eq!(fi % t, z); |
| 179 | + assert_eq!(f % ti, z); |
| 180 | + |
| 181 | + { |
| 182 | + let mut v = z; |
| 183 | + assert_eq!(v, z); |
| 184 | + v += oi; // add_assign |
| 185 | + assert_eq!(v, o); |
| 186 | + v -= oi; // sub_assign |
| 187 | + assert_eq!(v, z); |
| 188 | + v = t; |
| 189 | + v *= oi; // mul_assign |
| 190 | + assert_eq!(v, t); |
| 191 | + v *= ti; |
| 192 | + assert_eq!(v, f); |
| 193 | + v /= oi; // div_assign |
| 194 | + assert_eq!(v, f); |
| 195 | + v /= ti; |
| 196 | + assert_eq!(v, t); |
| 197 | + v %= ti; // rem_assign |
| 198 | + assert_eq!(v, z); |
| 199 | + } |
| 200 | + } |
| 201 | + }; |
| 202 | +} |
0 commit comments