Skip to main content

simply_simd/register/
integer.rs

1use std::ops::*;
2
3use num_traits::NumCast;
4
5use crate::architectures::interface::*;
6use crate::register::Simd;
7use crate::simd_types::*;
8
9impl<T: SimdInteger, A: Arch> BitAnd for Simd<T, A> {
10    type Output = Self;
11    #[inline(always)]
12    fn bitand(self, rhs: Self) -> Self {
13        unsafe { Self::new(A::Vec::and(self.data, rhs.data)) }
14    }
15}
16
17impl<T: SimdInteger, A: Arch> BitOr for Simd<T, A> {
18    type Output = Self;
19    #[inline(always)]
20    fn bitor(self, rhs: Self) -> Self {
21        unsafe { Self::new(A::Vec::or(self.data, rhs.data)) }
22    }
23}
24
25impl<T: SimdInteger, A: Arch> BitXor for Simd<T, A> {
26    type Output = Self;
27    #[inline(always)]
28    fn bitxor(self, rhs: Self) -> Self {
29        unsafe { Self::new(A::Vec::xor(self.data, rhs.data)) }
30    }
31}
32
33impl<T: SimdInteger, A: Arch> Simd<T, A> {
34    #[inline(always)]
35    pub fn andnot(self, rhs: Self) -> Self {
36        unsafe { Self::new(A::Vec::and_not(self.data, rhs.data)) }
37    }
38}
39
40impl<T: SimdInteger, A: Arch> Not for Simd<T, A> {
41    type Output = Self;
42    #[inline(always)]
43    fn not(self) -> Self {
44        unsafe { Self::new(A::Vec::not(self.data)) }
45    }
46}
47
48// === Shifts ===
49impl<T: SimdIntegerNotByte, A: Arch> Shl<Simd<<T as SimdInteger>::Unsigned, A>>
50    for Simd<T, A>
51{
52    type Output = Self;
53    #[inline(always)]
54    fn shl(self, rhs: Simd<<T as SimdInteger>::Unsigned, A>) -> Self {
55        Self::new(unsafe {
56            match T::BIT_SIZE {
57                BitSize::Size64 => self.data.sllv_64(rhs.data),
58                BitSize::Size32 => self.data.sllv_32(rhs.data),
59                BitSize::Size16 => self.data.sllv_16(rhs.data),
60                _ => unreachable!(),
61            }
62        })
63    }
64}
65
66impl<T: SimdIntegerNotByte, A: Arch> Shr<Simd<<T as SimdInteger>::Unsigned, A>>
67    for Simd<T, A>
68{
69    type Output = Self;
70    #[inline(always)]
71    fn shr(self, rhs: Simd<<T as SimdInteger>::Unsigned, A>) -> Self {
72        unsafe {
73            Self::new(match T::TYPE {
74                SimdType::U64 => self.data.srlv_64(rhs.data),
75                SimdType::U32 => self.data.srlv_32(rhs.data),
76                SimdType::U16 => self.data.srlv_16(rhs.data),
77                SimdType::I64 => self.data.srav_64(rhs.data),
78                SimdType::I32 => self.data.srav_32(rhs.data),
79                SimdType::I16 => self.data.srav_16(rhs.data),
80                _ => unreachable!(),
81            })
82        }
83    }
84}
85
86// === Scalar shifts ===
87
88impl<T: SimdIntegerNotByte, A: Arch> Shl<usize> for Simd<T, A> {
89    type Output = Self;
90    #[inline(always)]
91    fn shl(self, rhs: usize) -> Self {
92        let shift = Simd::<<T as SimdInteger>::Unsigned, A>::splat(NumCast::from(rhs).unwrap());
93        self << shift
94    }
95}
96
97impl<T: SimdIntegerNotByte, A: Arch> Shr<usize> for Simd<T, A> {
98    type Output = Self;
99    #[inline(always)]
100    fn shr(self, rhs: usize) -> Self {
101        let shift = Simd::<<T as SimdInteger>::Unsigned, A>::splat(NumCast::from(rhs).unwrap());
102        self >> shift
103    }
104}
105
106// === Addition ===
107
108impl<T: SimdMulType, A: Arch> Mul for Simd<T, A> {
109    type Output = Self;
110    #[inline(always)]
111    fn mul(self, rhs: Self) -> Self {
112        unsafe {
113            Self::new(match T::TYPE {
114                SimdType::F64 => self.data.f64_mul(rhs.data),
115                SimdType::F32 => self.data.f32_mul(rhs.data),
116                SimdType::I32 => self.data.i32_mul(rhs.data),
117                SimdType::I16 => self.data.i16_mul(rhs.data),
118                SimdType::U32 => self.data.i32_mul(rhs.data),
119                SimdType::U16 => self.data.i16_mul(rhs.data),
120                _ => unreachable!(),
121            })
122        }
123    }
124}
125
126impl<T: SimdFloat, A: Arch> Div for Simd<T, A> {
127    type Output = Self;
128    #[inline(always)]
129    fn div(self, rhs: Self) -> Self {
130        unsafe {
131            Self::new(match T::TYPE {
132                SimdType::F64 => self.data.f64_div(rhs.data),
133                SimdType::F32 => self.data.f32_div(rhs.data),
134                _ => unreachable!(),
135            })
136        }
137    }
138}
139
140// TODO TODO
141// impl<T: SimdInteger, A: SimdAamily> Rem for Simd<T, A> {
142//     type Output = Self;
143//     #[inline(always)]
144//     fn rem(self, rhs: Self) -> Self {
145//         Self::new(
146//             match T::TYPE {
147//                 SimdType::F64 => self.data.f64_sub(rhs.data),
148//                 SimdType::F32 => self.data.f32_sub(rhs.data),
149//                 SimdType::I64 => self.data.i64_sub(rhs.data),
150//                 SimdType::I32 => self.data.i32_sub(rhs.data),
151//                 SimdType::I16 => self.data.i16_sub(rhs.data),
152//                 SimdType::I8 => self.data.i8_sub(rhs.data),
153//                 SimdType::U64 => self.data.i64_sub(rhs.data),
154//                 SimdType::U32 => self.data.i32_sub(rhs.data),
155//                 SimdType::U16 => self.data.i16_sub(rhs.data),
156//                 SimdType::U8 => self.data.i8_sub(rhs.data),
157//             }
158//         )
159//     }
160// }
161
162// === Casts ===
163
164impl<T: SimdInteger + HasSigned, A: Arch> Simd<T, A> {
165    #[inline(always)]
166    pub fn cast_signed(self) -> Simd<<T as SimdInteger>::Signed, A> {
167        Simd::new(self.data)
168    }
169}
170
171impl<T: SimdInteger + HasUnsigned, A: Arch> Simd<T, A> {
172    #[inline(always)]
173    pub fn cast_unsigned(self) -> Simd<<T as SimdInteger>::Unsigned, A> {
174        Simd::new(self.data)
175    }
176}
177
178impl<T: SimdInteger + HasFloat, A: Arch> Simd<T, A> {
179    #[inline(always)]
180    pub fn cast_float(self) -> Simd<<T as HasFloat>::Float, A> {
181        unsafe { Simd::new(self.data.int_to_float()) }
182    }
183}
184
185// === Clamp ===
186
187// impl<T: SimdElement, A: SimdAamily> Simd<T, A> {
188//     #[inline(always)]
189//     pub fn clamp(self, min: T, max: T) -> Self {
190//         let min_vec = Self::splat(min);
191//         let max_vec = Self::splat(max);
192//         self.blendself.simd_lt(min_vec)
193//     }
194// }