Skip to main content

simply_simd/
simd_types.rs

1use std::fmt::Debug;
2
3use num_traits::{NumCast, NumOps};
4
5use crate::architectures::interface::Arch;
6use crate::simd_array::SimdToArray;
7
8mod private {
9    pub trait SealedTypes {}
10    impl SealedTypes for f64 {}
11    impl SealedTypes for f32 {}
12    impl SealedTypes for i64 {}
13    impl SealedTypes for i32 {}
14    impl SealedTypes for i16 {}
15    impl SealedTypes for i8 {}
16    impl SealedTypes for u64 {}
17    impl SealedTypes for u32 {}
18    impl SealedTypes for u16 {}
19    impl SealedTypes for u8 {}
20
21    pub trait SealedSizes {}
22    impl SealedSizes for super::B64 {}
23    impl SealedSizes for super::B32 {}
24    impl SealedSizes for super::B16 {}
25    impl SealedSizes for super::B8 {}
26}
27
28pub enum BitSize {
29    Size64,
30    Size32,
31    Size16,
32    Size8,
33}
34
35pub enum PrimitiveType {
36    Float,
37    SignedInt,
38    UnsignedInt,
39}
40
41pub enum SimdType {
42    F64,
43    F32,
44    I64,
45    I32,
46    I16,
47    I8,
48    U64,
49    U32,
50    U16,
51    U8,
52}
53
54pub struct B64;
55pub struct B32;
56pub struct B16;
57pub struct B8;
58
59pub trait BitWidth: private::SealedSizes {
60    const BIT_SIZE: usize;
61}
62
63impl BitWidth for B64 {
64    const BIT_SIZE: usize = 64;
65}
66impl BitWidth for B32 {
67    const BIT_SIZE: usize = 32;
68}
69impl BitWidth for B16 {
70    const BIT_SIZE: usize = 16;
71}
72impl BitWidth for B8 {
73    const BIT_SIZE: usize = 8;
74}
75
76pub trait SafeAdd {
77    fn safe_add(self, rhs: Self) -> Self;
78}
79
80impl SafeAdd for f64 {
81    fn safe_add(self, rhs: Self) -> Self {
82        self + rhs
83    }
84}
85
86impl SafeAdd for f32 {
87    fn safe_add(self, rhs: Self) -> Self {
88        self + rhs
89    }
90}
91
92impl SafeAdd for u64 {
93    fn safe_add(self, rhs: Self) -> Self {
94        self.wrapping_add(rhs)
95    }
96}
97
98impl SafeAdd for u32 {
99    fn safe_add(self, rhs: Self) -> Self {
100        self.wrapping_add(rhs)
101    }
102}
103
104impl SafeAdd for u16 {
105    fn safe_add(self, rhs: Self) -> Self {
106        self.wrapping_add(rhs)
107    }
108}
109
110impl SafeAdd for u8 {
111    fn safe_add(self, rhs: Self) -> Self {
112        self.wrapping_add(rhs)
113    }
114}
115
116impl SafeAdd for i64 {
117    fn safe_add(self, rhs: Self) -> Self {
118        self.wrapping_add(rhs)
119    }
120}
121
122impl SafeAdd for i32 {
123    fn safe_add(self, rhs: Self) -> Self {
124        self.wrapping_add(rhs)
125    }
126}
127
128impl SafeAdd for i16 {
129    fn safe_add(self, rhs: Self) -> Self {
130        self.wrapping_add(rhs)
131    }
132}
133
134impl SafeAdd for i8 {
135    fn safe_add(self, rhs: Self) -> Self {
136        self.wrapping_add(rhs)
137    }
138}
139
140// pub trait Array<T> {
141//     fn from_fn(f: impl AnMut(usize) -> T) -> Self;
142//     fn get_array<const M: usize>() -> [T; M];
143//     fn as_slice(&self) -> &[T];
144//     fn as_array<const M: usize>(self) -> [T; M];
145// }
146
147// impl<const N: usize, T: Default> Array<T> for [T; N] {
148//     fn from_fn(f: impl AnMut(usize) -> T) -> [T; N] {
149//         std::array::from_fn(f)
150//     }
151
152//     fn get_array<const M: usize>() -> [T; M] {
153//         std::array::from_fn(|_| T::default())
154//     }
155
156//     fn as_slice(&self) -> &[T] {
157//         self.as_slice()
158//     }
159
160//     fn as_array<const M: usize>(self) -> [T; M] {
161//         assert_eq!(N, M, "size mismatch");
162//         unsafe { std::mem::transmute_copy(&self) }
163//     }
164// }
165
166// Need both enum and associated type for matching and bounds.
167pub trait SimdElement:
168    private::SealedTypes + PartialEq + Sized + Default + Copy + NumCast + NumOps + Debug + SafeAdd
169{
170    const BIT_SIZE: BitSize;
171    const PRIMITIVE_TYPE: PrimitiveType;
172    const TYPE: SimdType;
173    type BitWidthType: BitWidth;
174    type Array<A: Arch>: Debug + Copy + SimdToArray<Self>; // Array wrapper to get around const generics limitations.
175    type UType: SimdElement;
176}
177
178impl SimdElement for f64 {
179    const BIT_SIZE: BitSize = BitSize::Size64;
180    const PRIMITIVE_TYPE: PrimitiveType = PrimitiveType::Float;
181    const TYPE: SimdType = SimdType::F64;
182    type BitWidthType = B64;
183    type Array<A: Arch> = A::Array64<f64>;
184    type UType = u64;
185}
186impl SimdElement for f32 {
187    const BIT_SIZE: BitSize = BitSize::Size32;
188    const PRIMITIVE_TYPE: PrimitiveType = PrimitiveType::Float;
189    const TYPE: SimdType = SimdType::F32;
190    type BitWidthType = B32;
191    type Array<A: Arch> = A::Array32<f32>;
192    type UType = u32;
193}
194impl SimdElement for i64 {
195    const BIT_SIZE: BitSize = BitSize::Size64;
196    const PRIMITIVE_TYPE: PrimitiveType = PrimitiveType::SignedInt;
197    const TYPE: SimdType = SimdType::I64;
198    type BitWidthType = B64;
199    type Array<A: Arch> = A::Array64<i64>;
200    type UType = u64;
201}
202impl SimdElement for i32 {
203    const BIT_SIZE: BitSize = BitSize::Size32;
204    const PRIMITIVE_TYPE: PrimitiveType = PrimitiveType::SignedInt;
205    const TYPE: SimdType = SimdType::I32;
206    type BitWidthType = B32;
207    type Array<A: Arch> = A::Array32<i32>;
208    type UType = u32;
209}
210impl SimdElement for i16 {
211    const BIT_SIZE: BitSize = BitSize::Size16;
212    const PRIMITIVE_TYPE: PrimitiveType = PrimitiveType::SignedInt;
213    const TYPE: SimdType = SimdType::I16;
214    type BitWidthType = B16;
215    type Array<A: Arch> = A::Array16<i16>;
216    type UType = u16;
217}
218impl SimdElement for i8 {
219    const BIT_SIZE: BitSize = BitSize::Size8;
220    const PRIMITIVE_TYPE: PrimitiveType = PrimitiveType::SignedInt;
221    const TYPE: SimdType = SimdType::I8;
222    type BitWidthType = B8;
223    type Array<A: Arch> = A::Array8<i8>;
224    type UType = u8;
225}
226impl SimdElement for u64 {
227    const BIT_SIZE: BitSize = BitSize::Size64;
228    const PRIMITIVE_TYPE: PrimitiveType = PrimitiveType::UnsignedInt;
229    const TYPE: SimdType = SimdType::U64;
230    type BitWidthType = B64;
231    type Array<A: Arch> = A::Array64<u64>;
232    type UType = u64;
233}
234impl SimdElement for u32 {
235    const BIT_SIZE: BitSize = BitSize::Size32;
236    const PRIMITIVE_TYPE: PrimitiveType = PrimitiveType::UnsignedInt;
237    const TYPE: SimdType = SimdType::U32;
238    type BitWidthType = B32;
239    type Array<A: Arch> = A::Array32<u32>;
240    type UType = u32;
241}
242impl SimdElement for u16 {
243    const BIT_SIZE: BitSize = BitSize::Size16;
244    const PRIMITIVE_TYPE: PrimitiveType = PrimitiveType::UnsignedInt;
245    const TYPE: SimdType = SimdType::U16;
246    type BitWidthType = B16;
247    type Array<A: Arch> = A::Array16<u16>;
248    type UType = u16;
249}
250impl SimdElement for u8 {
251    const BIT_SIZE: BitSize = BitSize::Size8;
252    const PRIMITIVE_TYPE: PrimitiveType = PrimitiveType::UnsignedInt;
253    const TYPE: SimdType = SimdType::U8;
254    type BitWidthType = B8;
255    type Array<A: Arch> = A::Array8<u8>;
256    type UType = u8;
257}
258
259// pub trait BitSize: private::Sealed + Sized {
260//     const BITS: usize;
261//     type BitSize;
262// }
263
264// impl BitSize for f64 { const BITS: usize = 64; type BitSize = BitSize64; }
265// impl BitSize for f32 { const BITS: usize = 32; type BitSize = BitSize32; }
266// impl BitSize for i64 { const BITS: usize = 64; type BitSize = BitSize64; }
267// impl BitSize for i32 { const BITS: usize = 32; type BitSize = BitSize32; }
268// impl BitSize for i16 { const BITS: usize = 16; type BitSize = BitSize16; }
269// impl BitSize for i8 { const BITS: usize = 8; type BitSize = BitSize8; }
270// impl BitSize for u64 { const BITS: usize = 64; type BitSize = BitSize64; }
271// impl BitSize for u32 { const BITS: usize = 32; type BitSize = BitSize32; }
272// impl BitSize for u16 { const BITS: usize = 16; type BitSize = BitSize16; }
273// impl BitSize for u8 { const BITS: usize = 8; type BitSize = BitSize8; }
274
275pub struct Signed;
276pub struct Unsigned;
277
278pub trait SimdInteger: SimdElement {
279    type Type;
280    type Unsigned: SimdElement;
281    type Signed: SimdElement;
282}
283
284impl SimdInteger for i64 {
285    type Type = Signed;
286    type Unsigned = u64;
287    type Signed = i64;
288}
289impl SimdInteger for i32 {
290    type Type = Signed;
291    type Unsigned = u32;
292    type Signed = i32;
293}
294impl SimdInteger for i16 {
295    type Type = Signed;
296    type Unsigned = u16;
297    type Signed = i16;
298}
299impl SimdInteger for i8 {
300    type Type = Signed;
301    type Unsigned = u8;
302    type Signed = i8;
303}
304impl SimdInteger for u64 {
305    type Type = Unsigned;
306    type Unsigned = u64;
307    type Signed = i64;
308}
309impl SimdInteger for u32 {
310    type Type = Unsigned;
311    type Unsigned = u32;
312    type Signed = i32;
313}
314impl SimdInteger for u16 {
315    type Type = Unsigned;
316    type Unsigned = u16;
317    type Signed = i16;
318}
319impl SimdInteger for u8 {
320    type Type = Unsigned;
321    type Unsigned = u8;
322    type Signed = i8;
323}
324
325pub trait SimdFloat: SimdElement + HasSigned + HasUnsigned + SimdMulType {
326    const SIGN_MASK: usize;
327    type IntType: SimdElement + SimdInteger + SimdIntegerNotByte;
328    type UintType: SimdElement + SimdInteger + SimdIntegerNotByte;
329}
330
331impl SimdFloat for f64 {
332    const SIGN_MASK: usize = 0x7FFFFFFFFFFFFFFF;
333    type IntType = i64;
334    type UintType = u64;
335}
336impl SimdFloat for f32 {
337    const SIGN_MASK: usize = 0x7FFFFFFF;
338    type IntType = i32;
339    type UintType = u32;
340}
341
342pub trait SimdWideType: SimdElement {}
343impl SimdWideType for f64 {}
344impl SimdWideType for f32 {}
345impl SimdWideType for i64 {}
346impl SimdWideType for i32 {}
347impl SimdWideType for u64 {}
348impl SimdWideType for u32 {}
349
350pub trait SimdIntegerNotByte: SimdInteger {}
351impl SimdIntegerNotByte for i64 {}
352impl SimdIntegerNotByte for i32 {}
353impl SimdIntegerNotByte for u64 {}
354impl SimdIntegerNotByte for u32 {}
355impl SimdIntegerNotByte for i16 {}
356impl SimdIntegerNotByte for u16 {}
357
358pub trait SimdMulType: SimdElement {}
359impl SimdMulType for f64 {}
360impl SimdMulType for f32 {}
361impl SimdMulType for i32 {}
362impl SimdMulType for i16 {}
363impl SimdMulType for u32 {}
364impl SimdMulType for u16 {}
365
366// pub trait BitSize64: Sized + private::Sealed {}
367// impl BitSize64 for f64 {}
368// impl BitSize64 for i64 {}
369// impl BitSize64 for u64 {}
370
371// pub trait BitSize32: Sized + private::Sealed {}
372// impl BitSize32 for f32 {}
373// impl BitSize32 for i32 {}
374// impl BitSize32 for u32 {}
375
376// pub trait BitSize16: Sized + private::Sealed {}
377// impl BitSize16 for i16 {}
378// impl BitSize16 for u16 {}
379
380// pub trait BitSize8: Sized + private::Sealed {}
381// impl BitSize8 for i8 {}
382// impl BitSize8 for u8 {}
383
384pub trait HasFloat: SimdElement {
385    type Float: SimdElement + SimdFloat;
386}
387
388pub trait HasSigned: SimdElement {
389    type Signed: SimdElement + SimdInteger;
390}
391
392pub trait HasUnsigned: SimdElement {
393    type Unsigned: SimdElement + SimdInteger;
394}
395
396// Floats.
397impl HasSigned for f64 {
398    type Signed = i64;
399}
400impl HasUnsigned for f64 {
401    type Unsigned = u64;
402}
403impl HasSigned for f32 {
404    type Signed = i32;
405}
406impl HasUnsigned for f32 {
407    type Unsigned = u32;
408}
409
410// Signed Integers.
411impl HasFloat for i64 {
412    type Float = f64;
413}
414impl HasUnsigned for i64 {
415    type Unsigned = u64;
416}
417impl HasFloat for i32 {
418    type Float = f32;
419}
420impl HasUnsigned for i32 {
421    type Unsigned = u32;
422}
423impl HasUnsigned for i16 {
424    type Unsigned = u16;
425}
426impl HasUnsigned for i8 {
427    type Unsigned = u8;
428}
429
430// Unsigned Integers.
431impl HasFloat for u64 {
432    type Float = f64;
433}
434impl HasSigned for u64 {
435    type Signed = i64;
436}
437impl HasFloat for u32 {
438    type Float = f32;
439}
440impl HasSigned for u32 {
441    type Signed = i32;
442}
443impl HasSigned for u16 {
444    type Signed = i16;
445}
446impl HasSigned for u8 {
447    type Signed = i8;
448}