Skip to main content

core_simd/simd/num/
uint.rs

1use crate::simd::{Simd, SimdCast, SimdElement, cmp::SimdOrd};
2
3/// Operations on SIMD vectors of unsigned integers.
4pub impl(self) trait SimdUint: Copy {
5    /// Scalar type contained by this SIMD vector type.
6    type Scalar;
7
8    /// A SIMD vector with a different element type.
9    type Cast<T: SimdElement>;
10
11    /// Performs elementwise conversion of this vector's elements to another SIMD-valid type.
12    ///
13    /// This follows the semantics of Rust's `as` conversion for casting integers (wrapping to
14    /// other integer types, and saturating to float types).
15    #[must_use]
16    fn cast<T: SimdCast>(self) -> Self::Cast<T>;
17
18    /// Wrapping negation.
19    ///
20    /// Like [`u32::wrapping_neg`], all applications of this function will wrap, with the exception
21    /// of `-0`.
22    fn wrapping_neg(self) -> Self;
23
24    /// Lanewise saturating add.
25    ///
26    /// # Examples
27    /// ```
28    /// # #![feature(portable_simd)]
29    /// # #[cfg(feature = "as_crate")] use core_simd::simd;
30    /// # #[cfg(not(feature = "as_crate"))] use core::simd;
31    /// # use simd::prelude::*;
32    /// let x = Simd::from_array([2, 1, 0, u32::MAX]);
33    /// let max = Simd::splat(u32::MAX);
34    /// let unsat = x + max;
35    /// let sat = x.saturating_add(max);
36    /// assert_eq!(unsat, Simd::from_array([1, 0, u32::MAX, u32::MAX - 1]));
37    /// assert_eq!(sat, max);
38    /// ```
39    fn saturating_add(self, second: Self) -> Self;
40
41    /// Lanewise saturating subtract.
42    ///
43    /// # Examples
44    /// ```
45    /// # #![feature(portable_simd)]
46    /// # #[cfg(feature = "as_crate")] use core_simd::simd;
47    /// # #[cfg(not(feature = "as_crate"))] use core::simd;
48    /// # use simd::prelude::*;
49    /// let x = Simd::from_array([2, 1, 0, u32::MAX]);
50    /// let max = Simd::splat(u32::MAX);
51    /// let unsat = x - max;
52    /// let sat = x.saturating_sub(max);
53    /// assert_eq!(unsat, Simd::from_array([3, 2, 1, 0]));
54    /// assert_eq!(sat, Simd::splat(0));
55    /// ```
56    fn saturating_sub(self, second: Self) -> Self;
57
58    /// Lanewise absolute difference.
59    /// Every element becomes the absolute difference of `self` and `second`.
60    ///
61    /// # Examples
62    /// ```
63    /// # #![feature(portable_simd)]
64    /// # #[cfg(feature = "as_crate")] use core_simd::simd;
65    /// # #[cfg(not(feature = "as_crate"))] use core::simd;
66    /// # use simd::prelude::*;
67    /// let a = Simd::from_array([0, u32::MAX, 100, 20]);
68    /// let b = Simd::from_array([u32::MAX, 0, 80, 200]);
69    /// assert_eq!(a.abs_diff(b), Simd::from_array([u32::MAX, u32::MAX, 20, 180]));
70    /// ```
71    fn abs_diff(self, second: Self) -> Self;
72
73    /// Returns the sum of the elements of the vector, with wrapping addition.
74    fn reduce_sum(self) -> Self::Scalar;
75
76    /// Returns the product of the elements of the vector, with wrapping multiplication.
77    fn reduce_product(self) -> Self::Scalar;
78
79    /// Returns the maximum element in the vector.
80    fn reduce_max(self) -> Self::Scalar;
81
82    /// Returns the minimum element in the vector.
83    fn reduce_min(self) -> Self::Scalar;
84
85    /// Returns the cumulative bitwise "and" across the elements of the vector.
86    fn reduce_and(self) -> Self::Scalar;
87
88    /// Returns the cumulative bitwise "or" across the elements of the vector.
89    fn reduce_or(self) -> Self::Scalar;
90
91    /// Returns the cumulative bitwise "xor" across the elements of the vector.
92    fn reduce_xor(self) -> Self::Scalar;
93
94    /// Reverses the byte order of each element.
95    fn swap_bytes(self) -> Self;
96
97    /// Reverses the order of bits in each elemnent.
98    /// The least significant bit becomes the most significant bit, second least-significant bit becomes second most-significant bit, etc.
99    fn reverse_bits(self) -> Self;
100
101    /// Returns the number of ones in the binary representation of each element.
102    fn count_ones(self) -> Self;
103
104    /// Returns the number of zeros in the binary representation of each element.
105    fn count_zeros(self) -> Self;
106
107    /// Returns the number of leading zeros in the binary representation of each element.
108    fn leading_zeros(self) -> Self;
109
110    /// Returns the number of trailing zeros in the binary representation of each element.
111    fn trailing_zeros(self) -> Self;
112
113    /// Returns the number of leading ones in the binary representation of each element.
114    fn leading_ones(self) -> Self;
115
116    /// Returns the number of trailing ones in the binary representation of each element.
117    fn trailing_ones(self) -> Self;
118}
119
120macro_rules! impl_trait {
121    { $($ty:ident ($signed:ident)),* } => {
122        $(
123        impl<const N: usize> SimdUint for Simd<$ty, N>
124        {
125            type Scalar = $ty;
126            type Cast<T: SimdElement> = Simd<T, N>;
127
128            #[inline]
129            fn cast<T: SimdCast>(self) -> Self::Cast<T> {
130                // Safety: supported types are guaranteed by SimdCast
131                unsafe { core::intrinsics::simd::simd_as(self) }
132            }
133
134            #[inline]
135            fn wrapping_neg(self) -> Self {
136                use crate::simd::num::SimdInt;
137                (-self.cast::<$signed>()).cast()
138            }
139
140            #[inline]
141            fn saturating_add(self, second: Self) -> Self {
142                // Safety: `self` is a vector
143                unsafe { core::intrinsics::simd::simd_saturating_add(self, second) }
144            }
145
146            #[inline]
147            fn saturating_sub(self, second: Self) -> Self {
148                // Safety: `self` is a vector
149                unsafe { core::intrinsics::simd::simd_saturating_sub(self, second) }
150            }
151
152            #[inline]
153            fn abs_diff(self, second: Self) -> Self {
154                let max = self.simd_max(second);
155                let min = self.simd_min(second);
156                max - min
157            }
158
159            #[inline]
160            fn reduce_sum(self) -> Self::Scalar {
161                // Safety: `self` is an integer vector
162                unsafe { core::intrinsics::simd::simd_reduce_add_ordered(self, 0) }
163            }
164
165            #[inline]
166            fn reduce_product(self) -> Self::Scalar {
167                // Safety: `self` is an integer vector
168                unsafe { core::intrinsics::simd::simd_reduce_mul_ordered(self, 1) }
169            }
170
171            #[inline]
172            fn reduce_max(self) -> Self::Scalar {
173                // Safety: `self` is an integer vector
174                unsafe { core::intrinsics::simd::simd_reduce_max(self) }
175            }
176
177            #[inline]
178            fn reduce_min(self) -> Self::Scalar {
179                // Safety: `self` is an integer vector
180                unsafe { core::intrinsics::simd::simd_reduce_min(self) }
181            }
182
183            #[inline]
184            fn reduce_and(self) -> Self::Scalar {
185                // Safety: `self` is an integer vector
186                unsafe { core::intrinsics::simd::simd_reduce_and(self) }
187            }
188
189            #[inline]
190            fn reduce_or(self) -> Self::Scalar {
191                // Safety: `self` is an integer vector
192                unsafe { core::intrinsics::simd::simd_reduce_or(self) }
193            }
194
195            #[inline]
196            fn reduce_xor(self) -> Self::Scalar {
197                // Safety: `self` is an integer vector
198                unsafe { core::intrinsics::simd::simd_reduce_xor(self) }
199            }
200
201            #[inline]
202            fn swap_bytes(self) -> Self {
203                // Safety: `self` is an integer vector
204                unsafe { core::intrinsics::simd::simd_bswap(self) }
205            }
206
207            #[inline]
208            fn reverse_bits(self) -> Self {
209                // Safety: `self` is an integer vector
210                unsafe { core::intrinsics::simd::simd_bitreverse(self) }
211            }
212
213            #[inline]
214            fn count_ones(self) -> Self {
215                // Safety: `self` is an integer vector
216                unsafe { core::intrinsics::simd::simd_ctpop(self) }
217            }
218
219            #[inline]
220            fn count_zeros(self) -> Self {
221                (!self).count_ones()
222            }
223
224            #[inline]
225            fn leading_zeros(self) -> Self {
226                // Safety: `self` is an integer vector
227                unsafe { core::intrinsics::simd::simd_ctlz(self) }
228            }
229
230            #[inline]
231            fn trailing_zeros(self) -> Self {
232                // Safety: `self` is an integer vector
233                unsafe { core::intrinsics::simd::simd_cttz(self) }
234            }
235
236            #[inline]
237            fn leading_ones(self) -> Self {
238                (!self).leading_zeros()
239            }
240
241            #[inline]
242            fn trailing_ones(self) -> Self {
243                (!self).trailing_zeros()
244            }
245        }
246        )*
247    }
248}
249
250impl_trait! { u8 (i8), u16 (i16), u32 (i32), u64 (i64), usize (isize) }