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