simply-simd 0.2.0

No-boilerplate portable SIMD library for raw registers with runtime feature detection
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
use std::fmt::Debug;

use num_traits::{NumCast, NumOps};

use crate::architectures::interface::Arch;
use crate::simd_array::SimdToArray;

mod private {
    pub trait SealedTypes {}
    impl SealedTypes for f64 {}
    impl SealedTypes for f32 {}
    impl SealedTypes for i64 {}
    impl SealedTypes for i32 {}
    impl SealedTypes for i16 {}
    impl SealedTypes for i8 {}
    impl SealedTypes for u64 {}
    impl SealedTypes for u32 {}
    impl SealedTypes for u16 {}
    impl SealedTypes for u8 {}

    pub trait SealedSizes {}
    impl SealedSizes for super::B64 {}
    impl SealedSizes for super::B32 {}
    impl SealedSizes for super::B16 {}
    impl SealedSizes for super::B8 {}
}

pub enum BitSize {
    Size64,
    Size32,
    Size16,
    Size8,
}

pub enum PrimitiveType {
    Float,
    SignedInt,
    UnsignedInt,
}

pub enum SimdType {
    F64,
    F32,
    I64,
    I32,
    I16,
    I8,
    U64,
    U32,
    U16,
    U8,
}

pub struct B64;
pub struct B32;
pub struct B16;
pub struct B8;

pub trait BitWidth: private::SealedSizes {
    const BIT_SIZE: usize;
}

impl BitWidth for B64 {
    const BIT_SIZE: usize = 64;
}
impl BitWidth for B32 {
    const BIT_SIZE: usize = 32;
}
impl BitWidth for B16 {
    const BIT_SIZE: usize = 16;
}
impl BitWidth for B8 {
    const BIT_SIZE: usize = 8;
}

pub trait SafeAdd {
    fn safe_add(self, rhs: Self) -> Self;
}

impl SafeAdd for f64 {
    fn safe_add(self, rhs: Self) -> Self {
        self + rhs
    }
}

impl SafeAdd for f32 {
    fn safe_add(self, rhs: Self) -> Self {
        self + rhs
    }
}

impl SafeAdd for u64 {
    fn safe_add(self, rhs: Self) -> Self {
        self.wrapping_add(rhs)
    }
}

impl SafeAdd for u32 {
    fn safe_add(self, rhs: Self) -> Self {
        self.wrapping_add(rhs)
    }
}

impl SafeAdd for u16 {
    fn safe_add(self, rhs: Self) -> Self {
        self.wrapping_add(rhs)
    }
}

impl SafeAdd for u8 {
    fn safe_add(self, rhs: Self) -> Self {
        self.wrapping_add(rhs)
    }
}

impl SafeAdd for i64 {
    fn safe_add(self, rhs: Self) -> Self {
        self.wrapping_add(rhs)
    }
}

impl SafeAdd for i32 {
    fn safe_add(self, rhs: Self) -> Self {
        self.wrapping_add(rhs)
    }
}

impl SafeAdd for i16 {
    fn safe_add(self, rhs: Self) -> Self {
        self.wrapping_add(rhs)
    }
}

impl SafeAdd for i8 {
    fn safe_add(self, rhs: Self) -> Self {
        self.wrapping_add(rhs)
    }
}

// pub trait Array<T> {
//     fn from_fn(f: impl AnMut(usize) -> T) -> Self;
//     fn get_array<const M: usize>() -> [T; M];
//     fn as_slice(&self) -> &[T];
//     fn as_array<const M: usize>(self) -> [T; M];
// }

// impl<const N: usize, T: Default> Array<T> for [T; N] {
//     fn from_fn(f: impl AnMut(usize) -> T) -> [T; N] {
//         std::array::from_fn(f)
//     }

//     fn get_array<const M: usize>() -> [T; M] {
//         std::array::from_fn(|_| T::default())
//     }

//     fn as_slice(&self) -> &[T] {
//         self.as_slice()
//     }

//     fn as_array<const M: usize>(self) -> [T; M] {
//         assert_eq!(N, M, "size mismatch");
//         unsafe { std::mem::transmute_copy(&self) }
//     }
// }

// Need both enum and associated type for matching and bounds.
pub trait SimdElement:
    private::SealedTypes + PartialEq + Sized + Default + Copy + NumCast + NumOps + Debug + SafeAdd
{
    const BIT_SIZE: BitSize;
    const PRIMITIVE_TYPE: PrimitiveType;
    const TYPE: SimdType;
    type BitWidthType: BitWidth;
    type Array<A: Arch>: Debug + Copy + SimdToArray<Self>; // Array wrapper to get around const generics limitations.
    type UType: SimdElement;
}

impl SimdElement for f64 {
    const BIT_SIZE: BitSize = BitSize::Size64;
    const PRIMITIVE_TYPE: PrimitiveType = PrimitiveType::Float;
    const TYPE: SimdType = SimdType::F64;
    type BitWidthType = B64;
    type Array<A: Arch> = A::Array64<f64>;
    type UType = u64;
}
impl SimdElement for f32 {
    const BIT_SIZE: BitSize = BitSize::Size32;
    const PRIMITIVE_TYPE: PrimitiveType = PrimitiveType::Float;
    const TYPE: SimdType = SimdType::F32;
    type BitWidthType = B32;
    type Array<A: Arch> = A::Array32<f32>;
    type UType = u32;
}
impl SimdElement for i64 {
    const BIT_SIZE: BitSize = BitSize::Size64;
    const PRIMITIVE_TYPE: PrimitiveType = PrimitiveType::SignedInt;
    const TYPE: SimdType = SimdType::I64;
    type BitWidthType = B64;
    type Array<A: Arch> = A::Array64<i64>;
    type UType = u64;
}
impl SimdElement for i32 {
    const BIT_SIZE: BitSize = BitSize::Size32;
    const PRIMITIVE_TYPE: PrimitiveType = PrimitiveType::SignedInt;
    const TYPE: SimdType = SimdType::I32;
    type BitWidthType = B32;
    type Array<A: Arch> = A::Array32<i32>;
    type UType = u32;
}
impl SimdElement for i16 {
    const BIT_SIZE: BitSize = BitSize::Size16;
    const PRIMITIVE_TYPE: PrimitiveType = PrimitiveType::SignedInt;
    const TYPE: SimdType = SimdType::I16;
    type BitWidthType = B16;
    type Array<A: Arch> = A::Array16<i16>;
    type UType = u16;
}
impl SimdElement for i8 {
    const BIT_SIZE: BitSize = BitSize::Size8;
    const PRIMITIVE_TYPE: PrimitiveType = PrimitiveType::SignedInt;
    const TYPE: SimdType = SimdType::I8;
    type BitWidthType = B8;
    type Array<A: Arch> = A::Array8<i8>;
    type UType = u8;
}
impl SimdElement for u64 {
    const BIT_SIZE: BitSize = BitSize::Size64;
    const PRIMITIVE_TYPE: PrimitiveType = PrimitiveType::UnsignedInt;
    const TYPE: SimdType = SimdType::U64;
    type BitWidthType = B64;
    type Array<A: Arch> = A::Array64<u64>;
    type UType = u64;
}
impl SimdElement for u32 {
    const BIT_SIZE: BitSize = BitSize::Size32;
    const PRIMITIVE_TYPE: PrimitiveType = PrimitiveType::UnsignedInt;
    const TYPE: SimdType = SimdType::U32;
    type BitWidthType = B32;
    type Array<A: Arch> = A::Array32<u32>;
    type UType = u32;
}
impl SimdElement for u16 {
    const BIT_SIZE: BitSize = BitSize::Size16;
    const PRIMITIVE_TYPE: PrimitiveType = PrimitiveType::UnsignedInt;
    const TYPE: SimdType = SimdType::U16;
    type BitWidthType = B16;
    type Array<A: Arch> = A::Array16<u16>;
    type UType = u16;
}
impl SimdElement for u8 {
    const BIT_SIZE: BitSize = BitSize::Size8;
    const PRIMITIVE_TYPE: PrimitiveType = PrimitiveType::UnsignedInt;
    const TYPE: SimdType = SimdType::U8;
    type BitWidthType = B8;
    type Array<A: Arch> = A::Array8<u8>;
    type UType = u8;
}

// pub trait BitSize: private::Sealed + Sized {
//     const BITS: usize;
//     type BitSize;
// }

// impl BitSize for f64 { const BITS: usize = 64; type BitSize = BitSize64; }
// impl BitSize for f32 { const BITS: usize = 32; type BitSize = BitSize32; }
// impl BitSize for i64 { const BITS: usize = 64; type BitSize = BitSize64; }
// impl BitSize for i32 { const BITS: usize = 32; type BitSize = BitSize32; }
// impl BitSize for i16 { const BITS: usize = 16; type BitSize = BitSize16; }
// impl BitSize for i8 { const BITS: usize = 8; type BitSize = BitSize8; }
// impl BitSize for u64 { const BITS: usize = 64; type BitSize = BitSize64; }
// impl BitSize for u32 { const BITS: usize = 32; type BitSize = BitSize32; }
// impl BitSize for u16 { const BITS: usize = 16; type BitSize = BitSize16; }
// impl BitSize for u8 { const BITS: usize = 8; type BitSize = BitSize8; }

pub struct Signed;
pub struct Unsigned;

pub trait SimdInteger: SimdElement {
    type Type;
    type Unsigned: SimdElement;
    type Signed: SimdElement;
}

impl SimdInteger for i64 {
    type Type = Signed;
    type Unsigned = u64;
    type Signed = i64;
}
impl SimdInteger for i32 {
    type Type = Signed;
    type Unsigned = u32;
    type Signed = i32;
}
impl SimdInteger for i16 {
    type Type = Signed;
    type Unsigned = u16;
    type Signed = i16;
}
impl SimdInteger for i8 {
    type Type = Signed;
    type Unsigned = u8;
    type Signed = i8;
}
impl SimdInteger for u64 {
    type Type = Unsigned;
    type Unsigned = u64;
    type Signed = i64;
}
impl SimdInteger for u32 {
    type Type = Unsigned;
    type Unsigned = u32;
    type Signed = i32;
}
impl SimdInteger for u16 {
    type Type = Unsigned;
    type Unsigned = u16;
    type Signed = i16;
}
impl SimdInteger for u8 {
    type Type = Unsigned;
    type Unsigned = u8;
    type Signed = i8;
}

pub trait SimdFloat: SimdElement + HasSigned + HasUnsigned + SimdMulType {
    const SIGN_MASK: usize;
    type IntType: SimdElement + SimdInteger + SimdIntegerNotByte;
    type UintType: SimdElement + SimdInteger + SimdIntegerNotByte;
}

impl SimdFloat for f64 {
    const SIGN_MASK: usize = 0x7FFFFFFFFFFFFFFF;
    type IntType = i64;
    type UintType = u64;
}
impl SimdFloat for f32 {
    const SIGN_MASK: usize = 0x7FFFFFFF;
    type IntType = i32;
    type UintType = u32;
}

pub trait SimdWideType: SimdElement {}
impl SimdWideType for f64 {}
impl SimdWideType for f32 {}
impl SimdWideType for i64 {}
impl SimdWideType for i32 {}
impl SimdWideType for u64 {}
impl SimdWideType for u32 {}

pub trait SimdIntegerNotByte: SimdInteger {}
impl SimdIntegerNotByte for i64 {}
impl SimdIntegerNotByte for i32 {}
impl SimdIntegerNotByte for u64 {}
impl SimdIntegerNotByte for u32 {}
impl SimdIntegerNotByte for i16 {}
impl SimdIntegerNotByte for u16 {}

pub trait SimdMulType: SimdElement {}
impl SimdMulType for f64 {}
impl SimdMulType for f32 {}
impl SimdMulType for i32 {}
impl SimdMulType for i16 {}
impl SimdMulType for u32 {}
impl SimdMulType for u16 {}

// pub trait BitSize64: Sized + private::Sealed {}
// impl BitSize64 for f64 {}
// impl BitSize64 for i64 {}
// impl BitSize64 for u64 {}

// pub trait BitSize32: Sized + private::Sealed {}
// impl BitSize32 for f32 {}
// impl BitSize32 for i32 {}
// impl BitSize32 for u32 {}

// pub trait BitSize16: Sized + private::Sealed {}
// impl BitSize16 for i16 {}
// impl BitSize16 for u16 {}

// pub trait BitSize8: Sized + private::Sealed {}
// impl BitSize8 for i8 {}
// impl BitSize8 for u8 {}

pub trait HasFloat: SimdElement {
    type Float: SimdElement + SimdFloat;
}

pub trait HasSigned: SimdElement {
    type Signed: SimdElement + SimdInteger;
}

pub trait HasUnsigned: SimdElement {
    type Unsigned: SimdElement + SimdInteger;
}

// Floats.
impl HasSigned for f64 {
    type Signed = i64;
}
impl HasUnsigned for f64 {
    type Unsigned = u64;
}
impl HasSigned for f32 {
    type Signed = i32;
}
impl HasUnsigned for f32 {
    type Unsigned = u32;
}

// Signed Integers.
impl HasFloat for i64 {
    type Float = f64;
}
impl HasUnsigned for i64 {
    type Unsigned = u64;
}
impl HasFloat for i32 {
    type Float = f32;
}
impl HasUnsigned for i32 {
    type Unsigned = u32;
}
impl HasUnsigned for i16 {
    type Unsigned = u16;
}
impl HasUnsigned for i8 {
    type Unsigned = u8;
}

// Unsigned Integers.
impl HasFloat for u64 {
    type Float = f64;
}
impl HasSigned for u64 {
    type Signed = i64;
}
impl HasFloat for u32 {
    type Float = f32;
}
impl HasSigned for u32 {
    type Signed = i32;
}
impl HasSigned for u16 {
    type Signed = i16;
}
impl HasSigned for u8 {
    type Signed = i8;
}