rten_simd/
elem.rs

1//! Traits for elements of SIMD vectors.
2
3/// Types used as elements (or _lanes_) of SIMD vectors.
4pub trait Elem: Copy + Default + WrappingAdd<Output = Self> {
5    /// Return the 1 value of this type.
6    fn one() -> Self;
7}
8
9impl Elem for f32 {
10    fn one() -> Self {
11        1.
12    }
13}
14
15macro_rules! impl_elem_for_int {
16    ($int:ty) => {
17        impl Elem for $int {
18            fn one() -> Self {
19                1
20            }
21        }
22    };
23}
24
25impl_elem_for_int!(i32);
26impl_elem_for_int!(i16);
27impl_elem_for_int!(i8);
28impl_elem_for_int!(u8);
29impl_elem_for_int!(u16);
30impl_elem_for_int!(u32);
31
32/// Wrapping addition of numbers.
33///
34/// For float types, this is the same as [`std::ops::Add`]. For integer types,
35/// this is the same as the type's inherent `wrapping_add` method.
36pub trait WrappingAdd: Sized {
37    type Output;
38
39    fn wrapping_add(self, x: Self) -> Self;
40}
41
42macro_rules! impl_wrapping_add {
43    ($type:ty) => {
44        impl WrappingAdd for $type {
45            type Output = Self;
46
47            fn wrapping_add(self, x: Self) -> Self {
48                Self::wrapping_add(self, x)
49            }
50        }
51    };
52}
53
54impl_wrapping_add!(i32);
55impl_wrapping_add!(i16);
56impl_wrapping_add!(i8);
57impl_wrapping_add!(u8);
58impl_wrapping_add!(u16);
59impl_wrapping_add!(u32);
60
61impl WrappingAdd for f32 {
62    type Output = Self;
63
64    fn wrapping_add(self, x: f32) -> f32 {
65        self + x
66    }
67}