Skip to main content

core_simd/simd/num/
int.rs

1use crate::simd::{
2    Mask, Select, Simd, SimdCast, SimdElement, cmp::SimdOrd, cmp::SimdPartialOrd, num::SimdUint,
3};
4
5/// Operations on SIMD vectors of signed integers.
6pub impl(self) trait SimdInt: Copy {
7    /// Mask type used for manipulating this SIMD vector type.
8    type Mask;
9
10    /// Scalar type contained by this SIMD vector type.
11    type Scalar;
12
13    /// A SIMD vector of unsigned integers with the same element size.
14    type Unsigned;
15
16    /// A SIMD vector with a different element type.
17    type Cast<T: SimdElement>;
18
19    /// Performs elementwise conversion of this vector's elements to another SIMD-valid type.
20    ///
21    /// This follows the semantics of Rust's `as` conversion for casting integers (wrapping to
22    /// other integer types, and saturating to float types).
23    #[must_use]
24    fn cast<T: SimdCast>(self) -> Self::Cast<T>;
25
26    /// Lanewise saturating add.
27    ///
28    /// # Examples
29    /// ```
30    /// # #![feature(portable_simd)]
31    /// # #[cfg(feature = "as_crate")] use core_simd::simd;
32    /// # #[cfg(not(feature = "as_crate"))] use core::simd;
33    /// # use simd::prelude::*;
34    /// let x = Simd::from_array([i32::MIN, 0, 1, i32::MAX]);
35    /// let max = Simd::splat(i32::MAX);
36    /// let unsat = x + max;
37    /// let sat = x.saturating_add(max);
38    /// assert_eq!(unsat, Simd::from_array([-1, i32::MAX, i32::MIN, -2]));
39    /// assert_eq!(sat, Simd::from_array([-1, i32::MAX, i32::MAX, i32::MAX]));
40    /// ```
41    fn saturating_add(self, second: Self) -> Self;
42
43    /// Lanewise saturating subtract.
44    ///
45    /// # Examples
46    /// ```
47    /// # #![feature(portable_simd)]
48    /// # #[cfg(feature = "as_crate")] use core_simd::simd;
49    /// # #[cfg(not(feature = "as_crate"))] use core::simd;
50    /// # use simd::prelude::*;
51    /// let x = Simd::from_array([i32::MIN, -2, -1, i32::MAX]);
52    /// let max = Simd::splat(i32::MAX);
53    /// let unsat = x - max;
54    /// let sat = x.saturating_sub(max);
55    /// assert_eq!(unsat, Simd::from_array([1, i32::MAX, i32::MIN, 0]));
56    /// assert_eq!(sat, Simd::from_array([i32::MIN, i32::MIN, i32::MIN, 0]));
57    /// ```
58    fn saturating_sub(self, second: Self) -> Self;
59
60    /// Lanewise absolute value, implemented in Rust.
61    /// Every element becomes its absolute value.
62    ///
63    /// # Examples
64    /// ```
65    /// # #![feature(portable_simd)]
66    /// # #[cfg(feature = "as_crate")] use core_simd::simd;
67    /// # #[cfg(not(feature = "as_crate"))] use core::simd;
68    /// # use simd::prelude::*;
69    /// let xs = Simd::from_array([i32::MIN, i32::MIN + 1, -5, 0]);
70    /// assert_eq!(xs.abs(), Simd::from_array([i32::MIN, i32::MAX, 5, 0]));
71    /// ```
72    fn abs(self) -> Self;
73
74    /// Lanewise absolute difference.
75    /// Every element becomes the absolute difference of `self` and `second`.
76    ///
77    /// # Examples
78    /// ```
79    /// # #![feature(portable_simd)]
80    /// # #[cfg(feature = "as_crate")] use core_simd::simd;
81    /// # #[cfg(not(feature = "as_crate"))] use core::simd;
82    /// # use simd::prelude::*;
83    /// let a = Simd::from_array([i32::MIN, i32::MAX, 100, -100]);
84    /// let b = Simd::from_array([i32::MAX, i32::MIN, -80, -120]);
85    /// assert_eq!(a.abs_diff(b), Simd::from_array([u32::MAX, u32::MAX, 180, 20]));
86    /// ```
87    fn abs_diff(self, second: Self) -> Self::Unsigned;
88
89    /// Lanewise saturating absolute value, implemented in Rust.
90    /// As abs(), except the MIN value becomes MAX instead of itself.
91    ///
92    /// # Examples
93    /// ```
94    /// # #![feature(portable_simd)]
95    /// # #[cfg(feature = "as_crate")] use core_simd::simd;
96    /// # #[cfg(not(feature = "as_crate"))] use core::simd;
97    /// # use simd::prelude::*;
98    /// let xs = Simd::from_array([i32::MIN, -2, 0, 3]);
99    /// let unsat = xs.abs();
100    /// let sat = xs.saturating_abs();
101    /// assert_eq!(unsat, Simd::from_array([i32::MIN, 2, 0, 3]));
102    /// assert_eq!(sat, Simd::from_array([i32::MAX, 2, 0, 3]));
103    /// ```
104    fn saturating_abs(self) -> Self;
105
106    /// Lanewise saturating negation, implemented in Rust.
107    /// As neg(), except the MIN value becomes MAX instead of itself.
108    ///
109    /// # Examples
110    /// ```
111    /// # #![feature(portable_simd)]
112    /// # #[cfg(feature = "as_crate")] use core_simd::simd;
113    /// # #[cfg(not(feature = "as_crate"))] use core::simd;
114    /// # use simd::prelude::*;
115    /// let x = Simd::from_array([i32::MIN, -2, 3, i32::MAX]);
116    /// let unsat = -x;
117    /// let sat = x.saturating_neg();
118    /// assert_eq!(unsat, Simd::from_array([i32::MIN, 2, -3, i32::MIN + 1]));
119    /// assert_eq!(sat, Simd::from_array([i32::MAX, 2, -3, i32::MIN + 1]));
120    /// ```
121    fn saturating_neg(self) -> Self;
122
123    /// Returns true for each positive element and false if it is zero or negative.
124    fn is_positive(self) -> Self::Mask;
125
126    /// Returns true for each negative element and false if it is zero or positive.
127    fn is_negative(self) -> Self::Mask;
128
129    /// Returns numbers representing the sign of each element.
130    /// * `0` if the number is zero
131    /// * `1` if the number is positive
132    /// * `-1` if the number is negative
133    fn signum(self) -> Self;
134
135    /// Returns the sum of the elements of the vector, with wrapping addition.
136    ///
137    /// # Examples
138    ///
139    /// ```
140    /// # #![feature(portable_simd)]
141    /// # #[cfg(feature = "as_crate")] use core_simd::simd;
142    /// # #[cfg(not(feature = "as_crate"))] use core::simd;
143    /// # use simd::prelude::*;
144    /// let v = i32x4::from_array([1, 2, 3, 4]);
145    /// assert_eq!(v.reduce_sum(), 10);
146    ///
147    /// // SIMD integer addition is always wrapping
148    /// let v = i32x4::from_array([i32::MAX, 1, 0, 0]);
149    /// assert_eq!(v.reduce_sum(), i32::MIN);
150    /// ```
151    fn reduce_sum(self) -> Self::Scalar;
152
153    /// Returns the product of the elements of the vector, with wrapping multiplication.
154    ///
155    /// # Examples
156    ///
157    /// ```
158    /// # #![feature(portable_simd)]
159    /// # #[cfg(feature = "as_crate")] use core_simd::simd;
160    /// # #[cfg(not(feature = "as_crate"))] use core::simd;
161    /// # use simd::prelude::*;
162    /// let v = i32x4::from_array([1, 2, 3, 4]);
163    /// assert_eq!(v.reduce_product(), 24);
164    ///
165    /// // SIMD integer multiplication is always wrapping
166    /// let v = i32x4::from_array([i32::MAX, 2, 1, 1]);
167    /// assert!(v.reduce_product() < i32::MAX);
168    /// ```
169    fn reduce_product(self) -> Self::Scalar;
170
171    /// Returns the maximum element in the vector.
172    ///
173    /// # Examples
174    ///
175    /// ```
176    /// # #![feature(portable_simd)]
177    /// # #[cfg(feature = "as_crate")] use core_simd::simd;
178    /// # #[cfg(not(feature = "as_crate"))] use core::simd;
179    /// # use simd::prelude::*;
180    /// let v = i32x4::from_array([1, 2, 3, 4]);
181    /// assert_eq!(v.reduce_max(), 4);
182    /// ```
183    fn reduce_max(self) -> Self::Scalar;
184
185    /// Returns the minimum element in the vector.
186    ///
187    /// # Examples
188    ///
189    /// ```
190    /// # #![feature(portable_simd)]
191    /// # #[cfg(feature = "as_crate")] use core_simd::simd;
192    /// # #[cfg(not(feature = "as_crate"))] use core::simd;
193    /// # use simd::prelude::*;
194    /// let v = i32x4::from_array([1, 2, 3, 4]);
195    /// assert_eq!(v.reduce_min(), 1);
196    /// ```
197    fn reduce_min(self) -> Self::Scalar;
198
199    /// Returns the cumulative bitwise "and" across the elements of the vector.
200    fn reduce_and(self) -> Self::Scalar;
201
202    /// Returns the cumulative bitwise "or" across the elements of the vector.
203    fn reduce_or(self) -> Self::Scalar;
204
205    /// Returns the cumulative bitwise "xor" across the elements of the vector.
206    fn reduce_xor(self) -> Self::Scalar;
207
208    /// Reverses the byte order of each element.
209    fn swap_bytes(self) -> Self;
210
211    /// Reverses the order of bits in each elemnent.
212    /// The least significant bit becomes the most significant bit, second least-significant bit becomes second most-significant bit, etc.
213    fn reverse_bits(self) -> Self;
214
215    /// Returns the number of ones in the binary representation of each element.
216    fn count_ones(self) -> Self::Unsigned;
217
218    /// Returns the number of zeros in the binary representation of each element.
219    fn count_zeros(self) -> Self::Unsigned;
220
221    /// Returns the number of leading zeros in the binary representation of each element.
222    fn leading_zeros(self) -> Self::Unsigned;
223
224    /// Returns the number of trailing zeros in the binary representation of each element.
225    fn trailing_zeros(self) -> Self::Unsigned;
226
227    /// Returns the number of leading ones in the binary representation of each element.
228    fn leading_ones(self) -> Self::Unsigned;
229
230    /// Returns the number of trailing ones in the binary representation of each element.
231    fn trailing_ones(self) -> Self::Unsigned;
232}
233
234macro_rules! impl_trait {
235    { $($ty:ident ($unsigned:ident)),* } => {
236        $(
237        impl<const N: usize> SimdInt for Simd<$ty, N> {
238            type Mask = Mask<<$ty as SimdElement>::Mask, N>;
239            type Scalar = $ty;
240            type Unsigned = Simd<$unsigned, N>;
241            type Cast<T: SimdElement> = Simd<T, N>;
242
243            #[inline]
244            fn cast<T: SimdCast>(self) -> Self::Cast<T> {
245                // Safety: supported types are guaranteed by SimdCast
246                unsafe { core::intrinsics::simd::simd_as(self) }
247            }
248
249            #[inline]
250            fn saturating_add(self, second: Self) -> Self {
251                // Safety: `self` is a vector
252                unsafe { core::intrinsics::simd::simd_saturating_add(self, second) }
253            }
254
255            #[inline]
256            fn saturating_sub(self, second: Self) -> Self {
257                // Safety: `self` is a vector
258                unsafe { core::intrinsics::simd::simd_saturating_sub(self, second) }
259            }
260
261            #[inline]
262            fn abs(self) -> Self {
263                const SHR: $ty = <$ty>::BITS as $ty - 1;
264                let m = self >> Simd::splat(SHR);
265                (self^m) - m
266            }
267
268            #[inline]
269            fn abs_diff(self, second: Self) -> Self::Unsigned {
270                let max = self.simd_max(second);
271                let min = self.simd_min(second);
272                (max - min).cast()
273            }
274
275            #[inline]
276            fn saturating_abs(self) -> Self {
277                // arith shift for -1 or 0 mask based on sign bit, giving 2s complement
278                const SHR: $ty = <$ty>::BITS as $ty - 1;
279                let m = self >> Simd::splat(SHR);
280                (self^m).saturating_sub(m)
281            }
282
283            #[inline]
284            fn saturating_neg(self) -> Self {
285                Self::splat(0).saturating_sub(self)
286            }
287
288            #[inline]
289            fn is_positive(self) -> Self::Mask {
290                self.simd_gt(Self::splat(0))
291            }
292
293            #[inline]
294            fn is_negative(self) -> Self::Mask {
295                self.simd_lt(Self::splat(0))
296            }
297
298            #[inline]
299            fn signum(self) -> Self {
300                self.is_positive().select(
301                    Self::splat(1),
302                    self.is_negative().select(Self::splat(-1), Self::splat(0))
303                )
304            }
305
306            #[inline]
307            fn reduce_sum(self) -> Self::Scalar {
308                // Safety: `self` is an integer vector
309                unsafe { core::intrinsics::simd::simd_reduce_add_ordered(self, 0) }
310            }
311
312            #[inline]
313            fn reduce_product(self) -> Self::Scalar {
314                // Safety: `self` is an integer vector
315                unsafe { core::intrinsics::simd::simd_reduce_mul_ordered(self, 1) }
316            }
317
318            #[inline]
319            fn reduce_max(self) -> Self::Scalar {
320                // Safety: `self` is an integer vector
321                unsafe { core::intrinsics::simd::simd_reduce_max(self) }
322            }
323
324            #[inline]
325            fn reduce_min(self) -> Self::Scalar {
326                // Safety: `self` is an integer vector
327                unsafe { core::intrinsics::simd::simd_reduce_min(self) }
328            }
329
330            #[inline]
331            fn reduce_and(self) -> Self::Scalar {
332                // Safety: `self` is an integer vector
333                unsafe { core::intrinsics::simd::simd_reduce_and(self) }
334            }
335
336            #[inline]
337            fn reduce_or(self) -> Self::Scalar {
338                // Safety: `self` is an integer vector
339                unsafe { core::intrinsics::simd::simd_reduce_or(self) }
340            }
341
342            #[inline]
343            fn reduce_xor(self) -> Self::Scalar {
344                // Safety: `self` is an integer vector
345                unsafe { core::intrinsics::simd::simd_reduce_xor(self) }
346            }
347
348            #[inline]
349            fn swap_bytes(self) -> Self {
350                // Safety: `self` is an integer vector
351                unsafe { core::intrinsics::simd::simd_bswap(self) }
352            }
353
354            #[inline]
355            fn reverse_bits(self) -> Self {
356                // Safety: `self` is an integer vector
357                unsafe { core::intrinsics::simd::simd_bitreverse(self) }
358            }
359
360            #[inline]
361            fn count_ones(self) -> Self::Unsigned {
362                self.cast::<$unsigned>().count_ones()
363            }
364
365            #[inline]
366            fn count_zeros(self) -> Self::Unsigned {
367                self.cast::<$unsigned>().count_zeros()
368            }
369
370            #[inline]
371            fn leading_zeros(self) -> Self::Unsigned {
372                self.cast::<$unsigned>().leading_zeros()
373            }
374
375            #[inline]
376            fn trailing_zeros(self) -> Self::Unsigned {
377                self.cast::<$unsigned>().trailing_zeros()
378            }
379
380            #[inline]
381            fn leading_ones(self) -> Self::Unsigned {
382                self.cast::<$unsigned>().leading_ones()
383            }
384
385            #[inline]
386            fn trailing_ones(self) -> Self::Unsigned {
387                self.cast::<$unsigned>().trailing_ones()
388            }
389        }
390        )*
391    }
392}
393
394impl_trait! { i8 (u8), i16 (u16), i32 (u32), i64 (u64), isize (usize) }