|
| 1 | +macro_rules! impl_uint_arith { |
| 2 | + ($(($name:ident, $n:ty)),+) => { |
| 3 | + $( impl<const LANES: usize> $name<LANES> where Self: crate::LanesAtMost64 { |
| 4 | + |
| 5 | + /// Lanewise saturating add. |
| 6 | + /// |
| 7 | + /// # Examples |
| 8 | + /// ``` |
| 9 | + /// # use core_simd::*; |
| 10 | + #[doc = concat!("# use core::", stringify!($n), "::MAX;")] |
| 11 | + #[doc = concat!("let x = ", stringify!($name), "::from_array([2, 1, 0, MAX]);")] |
| 12 | + #[doc = concat!("let max = ", stringify!($name), "::splat(MAX);")] |
| 13 | + /// let unsat = x + max; |
| 14 | + /// let sat = x.saturating_add(max); |
| 15 | + /// assert_eq!(x - 1, unsat); |
| 16 | + /// assert_eq!(sat, max); |
| 17 | + /// ``` |
| 18 | + #[inline] |
| 19 | + pub fn saturating_add(self, second: Self) -> Self { |
| 20 | + unsafe { crate::intrinsics::simd_saturating_add(self, second) } |
| 21 | + } |
| 22 | + |
| 23 | + /// Lanewise saturating subtract. |
| 24 | + /// |
| 25 | + /// # Examples |
| 26 | + /// ``` |
| 27 | + /// # use core_simd::*; |
| 28 | + #[doc = concat!("# use core::", stringify!($n), "::MAX;")] |
| 29 | + #[doc = concat!("let x = ", stringify!($name), "::from_array([2, 1, 0, MAX]);")] |
| 30 | + #[doc = concat!("let max = ", stringify!($name), "::splat(MAX);")] |
| 31 | + /// let unsat = x - max; |
| 32 | + /// let sat = x.saturating_sub(max); |
| 33 | + /// assert_eq!(unsat, x + 1); |
| 34 | + #[doc = concat!("assert_eq!(sat, ", stringify!($name), "::splat(0));")] |
| 35 | + #[inline] |
| 36 | + pub fn saturating_sub(self, second: Self) -> Self { |
| 37 | + unsafe { crate::intrinsics::simd_saturating_sub(self, second) } |
| 38 | + } |
| 39 | + })+ |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +macro_rules! impl_int_arith { |
| 44 | + ($(($name:ident, $n:ty)),+) => { |
| 45 | + $( impl<const LANES: usize> $name<LANES> where Self: crate::LanesAtMost64 { |
| 46 | + |
| 47 | + /// Lanewise saturating add. |
| 48 | + /// |
| 49 | + /// # Examples |
| 50 | + /// ``` |
| 51 | + /// # use core_simd::*; |
| 52 | + #[doc = concat!("# use core::", stringify!($n), "::{MIN, MAX};")] |
| 53 | + #[doc = concat!("let x = ", stringify!($name), "::from_array([MIN, 0, 1, MAX]);")] |
| 54 | + #[doc = concat!("let max = ", stringify!($name), "::splat(MAX);")] |
| 55 | + /// let unsat = x + max; |
| 56 | + /// let sat = x.saturating_add(max); |
| 57 | + #[doc = concat!("assert_eq!(unsat, ", stringify!($name), "::from_array([-1, MAX, MIN, -2]));")] |
| 58 | + #[doc = concat!("assert_eq!(sat, ", stringify!($name), "::from_array([-1, MAX, MAX, MAX]));")] |
| 59 | + /// ``` |
| 60 | + #[inline] |
| 61 | + pub fn saturating_add(self, second: Self) -> Self { |
| 62 | + unsafe { crate::intrinsics::simd_saturating_add(self, second) } |
| 63 | + } |
| 64 | + |
| 65 | + /// Lanewise saturating subtract. |
| 66 | + /// |
| 67 | + /// # Examples |
| 68 | + /// ``` |
| 69 | + /// # use core_simd::*; |
| 70 | + #[doc = concat!("# use core::", stringify!($n), "::{MIN, MAX};")] |
| 71 | + #[doc = concat!("let x = ", stringify!($name), "::from_array([MIN, -2, -1, MAX]);")] |
| 72 | + #[doc = concat!("let max = ", stringify!($name), "::splat(MAX);")] |
| 73 | + /// let unsat = x - max; |
| 74 | + /// let sat = x.saturating_sub(max); |
| 75 | + #[doc = concat!("assert_eq!(unsat, ", stringify!($name), "::from_array([1, MAX, MIN, 0]));")] |
| 76 | + #[doc = concat!("assert_eq!(sat, ", stringify!($name), "::from_array([MIN, MIN, MIN, 0]));")] |
| 77 | + #[inline] |
| 78 | + pub fn saturating_sub(self, second: Self) -> Self { |
| 79 | + unsafe { crate::intrinsics::simd_saturating_sub(self, second) } |
| 80 | + } |
| 81 | + })+ |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +use crate::vector::*; |
| 86 | + |
| 87 | +impl_uint_arith! { (SimdU8, u8), (SimdU16, u16), (SimdU32, u32), (SimdU64, u64), (SimdUsize, usize) } |
| 88 | +impl_int_arith! { (SimdI8, i8), (SimdI16, i16), (SimdI32, i32), (SimdI64, i64), (SimdIsize, isize) } |
0 commit comments