Skip to main content

simply_simd/architectures/
interface.rs

1#![allow(clippy::missing_safety_doc)]
2use std::{fmt::Debug, ops::{Deref, DerefMut}};
3
4use crate::{Architecture, SimdElement, simd_array::SimdToArray, register::Simd};
5
6pub trait Arch: Clone + Copy + Default {
7    const SIMD_WIDTH: usize;
8    const NUM_SIMD_REG: usize;
9    const ARCHITECTURE: Architecture;
10
11    type Block2<T: SimdElement>: Deref<Target = [Simd<T, Self>]>
12        + DerefMut
13        + Default;
14
15    type Block4<T: SimdElement>: Deref<Target = [Simd<T, Self>]>
16        + DerefMut
17        + Default;
18
19    type Vec: SimdArch
20        + Copy
21        + Clone
22        + SimdLoadImpl<MaskType = Self::Mask>
23        + SimdStoreImpl<MaskType = Self::Mask>
24        + SimdPartialOrdImpl<MaskType = Self::Mask>;
25    type Mask: MaskArch + Copy + Clone + SimdVariableBlendImpl<VecType = Self::Vec>;
26    type ScalarArch: Arch;
27
28    type Array64<T: Debug + Copy>: Debug + Copy + SimdToArray<T>;
29    type Array32<T: Debug + Copy>: Debug + Copy + SimdToArray<T>;
30    type Array16<T: Debug + Copy>: Debug + Copy + SimdToArray<T>;
31    type Array8<T: Debug + Copy>: Debug + Copy + SimdToArray<T>;
32}
33
34pub trait SimdArch:
35    Copy +
36    Clone +
37    SimdAddImpl +
38    SimdSubImpl +
39    SimdMulImpl +
40    SimdDivImpl +
41    SimdBitwiseImpl +
42    SimdShiftImpl +
43    SimdLoadImpl +
44    SimdStoreImpl +
45    SimdZeroImpl +
46    SimdFloatCastsImpl +
47    SimdIntCastsImpl +
48    SimdPermuteImpl +
49    SimdMulAddImpl +
50    SimdRoundImpl +
51    SimdPartialOrdImpl +
52    SimdSplatImpl +
53    SimdGatherImpl +
54    SimdSqrtImpl +
55    SimdNegateImpl +
56    SimdBlockShiftImpl +
57    SimdImmediateBlendImpl +
58    SimdLaneShiftImpl +
59{}
60
61pub trait MaskArch:
62    Copy + Clone + SimdBitwiseImpl + SimdAllBitsImpl + SimdVariableBlendImpl + SimdMaskBitConversion
63{
64}
65
66#[repr(transparent)]
67pub struct Block<V, const N: usize>([V; N]);
68
69impl<V: Default, const N: usize> Default for Block<V, N> {
70    #[inline(always)]
71    fn default() -> Self {
72        Block(core::array::from_fn(|_| V::default()))
73    }
74}
75
76impl<V, const N: usize> Deref for Block<V, N> {
77    type Target = [V];
78    #[inline(always)]
79    fn deref(&self) -> &[V] { &self.0 }
80}
81
82impl<V, const N: usize> DerefMut for Block<V, N> {
83    #[inline(always)]
84    fn deref_mut(&mut self) -> &mut [V] { &mut self.0 }
85}
86
87// pub trait BlockExt<V> {
88//     /// # Safety
89//     /// `index` must be in bounds.
90//     unsafe fn get_unchecked(&self, index: usize) -> &V;
91//     /// # Safety
92//     /// `index` must be in bounds.
93//     unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut V;
94// }
95//
96// impl<V, B> BlockExt<V> for B
97// where
98//     B: AsRef<[V]> + AsMut<[V]>,
99// {
100//     #[inline(always)]
101//     unsafe fn get_unchecked(&self, index: usize) -> &V {
102//         unsafe { self.as_ref().get_unchecked(index) }
103//     }
104//
105//     #[inline(always)]
106//     unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut V {
107//         unsafe { self.as_mut().get_unchecked_mut(index) }
108//     }
109// }
110
111// === Arithmetic ===
112pub trait SimdAddImpl {
113    unsafe fn f64_add(self, rhs: Self) -> Self;
114    unsafe fn f32_add(self, rhs: Self) -> Self;
115    unsafe fn i64_add(self, rhs: Self) -> Self;
116    unsafe fn i32_add(self, rhs: Self) -> Self;
117    unsafe fn i16_add(self, rhs: Self) -> Self;
118    unsafe fn i8_add(self, rhs: Self) -> Self;
119}
120
121pub trait SimdSubImpl {
122    unsafe fn f64_sub(self, rhs: Self) -> Self;
123    unsafe fn f32_sub(self, rhs: Self) -> Self;
124    unsafe fn i64_sub(self, rhs: Self) -> Self;
125    unsafe fn i32_sub(self, rhs: Self) -> Self;
126    unsafe fn i16_sub(self, rhs: Self) -> Self;
127    unsafe fn i8_sub(self, rhs: Self) -> Self;
128}
129
130pub trait SimdMulImpl {
131    unsafe fn f64_mul(self, rhs: Self) -> Self;
132    unsafe fn f32_mul(self, rhs: Self) -> Self;
133    unsafe fn i32_mul(self, rhs: Self) -> Self;
134    unsafe fn i16_mul(self, rhs: Self) -> Self;
135}
136
137pub trait SimdDivImpl {
138    unsafe fn f64_div(self, rhs: Self) -> Self;
139    unsafe fn f32_div(self, rhs: Self) -> Self;
140}
141
142pub trait SimdBitwiseImpl {
143    unsafe fn and(self, rhs: Self) -> Self;
144    unsafe fn or(self, rhs: Self) -> Self;
145    unsafe fn xor(self, rhs: Self) -> Self;
146    unsafe fn not(self) -> Self;
147    unsafe fn and_not(self, rhs: Self) -> Self;
148}
149
150pub trait SimdShiftImpl {
151    unsafe fn sllv_64(self, shift: Self) -> Self;
152    unsafe fn srlv_64(self, shift: Self) -> Self;
153    unsafe fn srav_64(self, shift: Self) -> Self;
154    unsafe fn sllv_32(self, shift: Self) -> Self;
155    unsafe fn srlv_32(self, shift: Self) -> Self;
156    unsafe fn srav_32(self, shift: Self) -> Self;
157    unsafe fn sllv_16(self, shift: Self) -> Self;
158    unsafe fn srlv_16(self, shift: Self) -> Self;
159    unsafe fn srav_16(self, shift: Self) -> Self;
160}
161
162pub trait SimdLoadImpl {
163    type MaskType;
164    unsafe fn load_aligned<T>(ptr: *const T) -> Self;
165    unsafe fn load_unaligned<T>(ptr: *const T) -> Self;
166    unsafe fn masked_load_64<T>(ptr: *const T, mask: Self::MaskType) -> Self;
167    unsafe fn masked_load_32<T>(ptr: *const T, mask: Self::MaskType) -> Self;
168    // TODO: There are byte and short mask loaders as well.
169}
170
171pub trait SimdStoreImpl {
172    type MaskType;
173    unsafe fn store_aligned<T>(self, ptr: *mut T);
174    unsafe fn store_unaligned<T>(self, ptr: *mut T);
175    unsafe fn masked_store_64<T>(self, ptr: *mut T, mask: Self::MaskType);
176    unsafe fn masked_store_32<T>(self, ptr: *mut T, mask: Self::MaskType);
177    // TODO: There are byte and short mask storers as well.
178}
179
180// pub trait SimdExtractImpl {
181//     unsafe fn extract_64<T, const N: i32>(self) -> T;
182//     unsafe fn extract_32<T, const N: i32>(self) -> T;
183//     unsafe fn extract_16<T, const N: i32>(self) -> T;
184//     unsafe fn extract_8<T, const N: i32>(self) -> T;
185// }
186
187// pub trait SimdInsertImpl {
188//     unsafe fn insert_64<T, const N: i32>(self, val: T) -> Self;
189//     unsafe fn insert_32<T, const N: i32>(self, val: T) -> Self;
190//     unsafe fn insert_16<T, const N: i32>(self, val: T) -> Self;
191//     unsafe fn insert_8<T, const N: i32>(self, val: T) -> Self;
192// }
193
194pub trait SimdZeroImpl {
195    unsafe fn zero() -> Self;
196}
197
198pub trait SimdFloatCastsImpl {
199    unsafe fn float_to_int_trunc(self) -> Self;
200    unsafe fn float_to_int_round(self) -> Self;
201}
202
203pub trait SimdIntCastsImpl {
204    unsafe fn int_to_float(self) -> Self;
205}
206
207pub trait SimdPermuteImpl {
208    // type BlockVec;
209    unsafe fn permute_32(self, rhs: Self) -> Self;
210    unsafe fn permute_8(self, rhs: Self) -> Self;
211    // unsafe fn imm_permute_64<const M: i32>(self);
212    // unsafe fn imm_permute_32_lo<const M: i32>(self);
213    // unsafe fn imm_permute_16_lo<const M: i32>(self);
214}
215
216pub trait SimdVariableBlendImpl {
217    type VecType;
218    unsafe fn vblend_64(self, true_values: Self::VecType, false_values: Self::VecType) -> Self::VecType;
219    unsafe fn vblend_32(self, true_values: Self::VecType, false_values: Self::VecType) -> Self::VecType;
220    unsafe fn vblend_8(self, true_values: Self::VecType, false_values: Self::VecType) -> Self::VecType;
221}
222
223pub trait SimdImmediateBlendImpl {
224    unsafe fn blend_64<const N: i32>(self, false_values: Self) -> Self;
225    unsafe fn blend_32<const N: i32>(self, false_values: Self) -> Self;
226}
227pub trait SimdMulAddImpl {
228    unsafe fn mul_add_f64(self, mult: Self, add: Self) -> Self;
229    unsafe fn mul_sub_f64(self, mult: Self, sub: Self) -> Self;
230    unsafe fn negated_mul_add_f64(self, mult: Self, add: Self) -> Self;
231    unsafe fn negated_mul_sub_f64(self, mult: Self, sub: Self) -> Self;
232    unsafe fn mul_add_f32(self, mult: Self, add: Self) -> Self;
233    unsafe fn mul_sub_f32(self, mult: Self, sub: Self) -> Self;
234    unsafe fn negated_mul_add_f32(self, mult: Self, add: Self) -> Self;
235    unsafe fn negated_mul_sub_f32(self, mult: Self, sub: Self) -> Self;
236}
237
238pub trait SimdRoundImpl {
239    unsafe fn round_f64(self) -> Self;
240    unsafe fn round_f32(self) -> Self;
241    unsafe fn floor_f64(self) -> Self;
242    unsafe fn floor_f32(self) -> Self;
243    unsafe fn ceil_f64(self) -> Self;
244    unsafe fn ceil_f32(self) -> Self;
245}
246
247pub trait SimdPartialOrdImpl {
248    type MaskType;
249    unsafe fn cmp_f64_eq(self, rhs: Self) -> Self::MaskType;
250    unsafe fn cmp_f64_lt(self, rhs: Self) -> Self::MaskType;
251    unsafe fn cmp_f64_le(self, rhs: Self) -> Self::MaskType;
252    unsafe fn cmp_f64_gt(self, rhs: Self) -> Self::MaskType;
253    unsafe fn cmp_f64_ge(self, rhs: Self) -> Self::MaskType;
254    unsafe fn cmp_f64_neq(self, rhs: Self) -> Self::MaskType;
255    unsafe fn cmp_f32_eq(self, rhs: Self) -> Self::MaskType;
256    unsafe fn cmp_f32_lt(self, rhs: Self) -> Self::MaskType;
257    unsafe fn cmp_f32_le(self, rhs: Self) -> Self::MaskType;
258    unsafe fn cmp_f32_gt(self, rhs: Self) -> Self::MaskType;
259    unsafe fn cmp_f32_ge(self, rhs: Self) -> Self::MaskType;
260    unsafe fn cmp_f32_neq(self, rhs: Self) -> Self::MaskType;
261    unsafe fn cmp_i64_eq(self, rhs: Self) -> Self::MaskType;
262    unsafe fn cmp_i64_gt(self, rhs: Self) -> Self::MaskType;
263    unsafe fn cmp_i32_eq(self, rhs: Self) -> Self::MaskType;
264    unsafe fn cmp_i32_gt(self, rhs: Self) -> Self::MaskType;
265    unsafe fn cmp_i16_eq(self, rhs: Self) -> Self::MaskType;
266    unsafe fn cmp_i16_gt(self, rhs: Self) -> Self::MaskType;
267    unsafe fn cmp_i8_eq(self, rhs: Self) -> Self::MaskType;
268    unsafe fn cmp_i8_gt(self, rhs: Self) -> Self::MaskType;
269
270    unsafe fn max_f64(self, rhs: Self) -> Self;
271    unsafe fn min_f64(self, rhs: Self) -> Self;
272    unsafe fn max_f32(self, rhs: Self) -> Self;
273    unsafe fn min_f32(self, rhs: Self) -> Self;
274    unsafe fn max_i32(self, rhs: Self) -> Self;
275    unsafe fn min_i32(self, rhs: Self) -> Self;
276    unsafe fn max_i16(self, rhs: Self) -> Self;
277    unsafe fn min_i16(self, rhs: Self) -> Self;
278    unsafe fn max_i8(self, rhs: Self) -> Self;
279    unsafe fn min_i8(self, rhs: Self) -> Self;
280    unsafe fn max_u32(self, rhs: Self) -> Self;
281    unsafe fn min_u32(self, rhs: Self) -> Self;
282    unsafe fn max_u16(self, rhs: Self) -> Self;
283    unsafe fn min_u16(self, rhs: Self) -> Self;
284    unsafe fn max_u8(self, rhs: Self) -> Self;
285    unsafe fn min_u8(self, rhs: Self) -> Self;
286}
287
288pub trait SimdSplatImpl {
289    unsafe fn splat_64<T>(val: T) -> Self;
290    unsafe fn splat_32<T>(val: T) -> Self;
291    unsafe fn splat_16<T>(val: T) -> Self;
292    unsafe fn splat_8<T>(val: T) -> Self;
293}
294
295pub trait SimdGatherImpl {
296    unsafe fn gather_32_from_32<T, const B: i32>(self, ptr: *const T) -> Self;
297    // unsafe fn gather_64_from_32<T, const B: i32>(ptr: *const T, indicies: Self) -> Self;
298    // unsafe fn gather_32_from_64<T, const B: i32>(ptr: *const T, indicies: Self) -> Self;
299    unsafe fn gather_64_from_64<T, const B: i32>(self, ptr: *const T) -> Self;
300}
301
302pub trait SimdSqrtImpl {
303    unsafe fn sqrt_f64(self) -> Self;
304    unsafe fn sqrt_f32(self) -> Self;
305    // unsafe fn rsqrt_f64(self) -> Self;
306    unsafe fn rsqrt_f32(self) -> Self;
307}
308
309pub trait SimdAllBitsImpl {
310    unsafe fn all_zero(self) -> bool;
311}
312
313pub trait SimdNegateImpl {
314    unsafe fn negate_f64(self) -> Self;
315    unsafe fn negate_f32(self) -> Self;
316}
317
318pub trait SimdBlockShiftImpl {
319    unsafe fn block_right_byte_shift<const N: i32>(self) -> Self;
320    unsafe fn block_left_byte_shift<const N: i32>(self) -> Self;
321}
322
323// TODO: Add to_bits_16 and from_bits.
324pub trait SimdMaskBitConversion {
325    unsafe fn to_bits_64(self) -> u64;
326    unsafe fn to_bits_32(self) -> u64;
327    // unsafe fn to_bits_16(self) -> u64;
328    unsafe fn to_bits_8(self) -> u64;
329    unsafe fn from_bits_64(bitmask: u64) -> Self;
330    unsafe fn from_bits_32(bitmask: u64) -> Self;
331    unsafe fn from_bits_16(bitmask: u64) -> Self;
332    unsafe fn from_bits_8(bitmask: u64) -> Self;
333}
334
335pub trait SimdLaneShiftImpl {
336    unsafe fn right_lane_shift_32(self, n: u32) -> Self;
337    unsafe fn left_lane_shift_32(self, n: u32) -> Self;
338}