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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
use super::transmute::*;
use crate::{Simd, SimdBaseOps};
use core::ops::*;
pub trait SimdBitMask: Copy {
const WORDS: usize;
fn word(self, index: usize) -> u32;
#[inline(always)]
fn to_u32_lossy(self) -> u32 {
self.word(0)
}
#[inline(always)]
fn to_u64_lossy(self) -> u64 {
let mut out = 0u64;
let words = if Self::WORDS < 2 { Self::WORDS } else { 2 };
let mut index = 0;
while index < words {
out |= (self.word(index) as u64) << (index * 32);
index += 1;
}
out
}
#[inline(always)]
fn to_u128_lossy(self) -> u128 {
let mut out = 0u128;
let words = if Self::WORDS < 4 { Self::WORDS } else { 4 };
let mut index = 0;
while index < words {
out |= (self.word(index) as u128) << (index * 32);
index += 1;
}
out
}
#[inline(always)]
fn write_to_u32_slice(self, dst: &mut [u32]) -> usize {
let words = if Self::WORDS < dst.len() {
Self::WORDS
} else {
dst.len()
};
let mut index = 0;
while index < words {
dst[index] = self.word(index);
index += 1;
}
words
}
#[inline(always)]
fn valid_bits(index: usize, bit_len: usize) -> u32 {
let start = index * 32;
if start >= bit_len {
return 0;
}
let remaining = bit_len - start;
if remaining >= 32 {
u32::MAX
} else {
(1u32 << remaining) - 1
}
}
#[inline(always)]
fn any_in_mask(self, bit_len: usize) -> bool {
for index in 0..Self::WORDS {
if (self.word(index) & Self::valid_bits(index, bit_len)) != 0 {
return true;
}
}
false
}
#[inline(always)]
fn all_in_mask(self, bit_len: usize) -> bool {
for index in 0..Self::WORDS {
let valid = Self::valid_bits(index, bit_len);
if (self.word(index) & valid) != valid {
return false;
}
}
true
}
#[inline(always)]
fn first_set_in_mask(self, bit_len: usize) -> Option<usize> {
for index in 0..Self::WORDS {
let word = self.word(index) & Self::valid_bits(index, bit_len);
if word != 0 {
return Some(index * 32 + word.trailing_zeros() as usize);
}
}
None
}
#[inline(always)]
fn first_unset_in_mask(self, bit_len: usize) -> Option<usize> {
for index in 0..Self::WORDS {
let valid = Self::valid_bits(index, bit_len);
let word = (!self.word(index)) & valid;
if word != 0 {
return Some(index * 32 + word.trailing_zeros() as usize);
}
}
None
}
#[inline(always)]
fn last_set_in_mask(self, bit_len: usize) -> Option<usize> {
for index in (0..Self::WORDS).rev() {
let word = self.word(index) & Self::valid_bits(index, bit_len);
if word != 0 {
return Some(index * 32 + (31 - word.leading_zeros()) as usize);
}
}
None
}
#[inline(always)]
fn last_unset_in_mask(self, bit_len: usize) -> Option<usize> {
for index in (0..Self::WORDS).rev() {
let valid = Self::valid_bits(index, bit_len);
let word = (!self.word(index)) & valid;
if word != 0 {
return Some(index * 32 + (31 - word.leading_zeros()) as usize);
}
}
None
}
}
impl SimdBitMask for u32 {
const WORDS: usize = 1;
#[inline(always)]
fn word(self, index: usize) -> u32 {
debug_assert_eq!(index, 0);
if index == 0 {
self
} else {
0
}
}
}
impl<const N: usize> SimdBitMask for [u32; N] {
const WORDS: usize = N;
#[inline(always)]
fn word(self, index: usize) -> u32 {
self[index]
}
}
/// Operations shared by 16 and 32 bit int types
pub trait SimdInt:
SimdBaseOps + Shl<i32, Output = Self> + ShlAssign<i32> + Shr<i32, Output = Self> + ShrAssign<i32>
{
/// Shift each value left by n bits.
///
/// Shift counts use wrapping semantics (`rhs mod lane_bit_width`) so behavior is
/// defined for negative and out-of-range counts across all backends.
///
/// For 64 bits, this operations is missing in most implementations
/// and is emulated here under SSE2, SSE4.1, and AVX2.
fn shl(self, rhs: i32) -> Self;
/// Shift each value right by n bits.
///
/// Shift counts use wrapping semantics (`rhs mod lane_bit_width`) so behavior is
/// defined for negative and out-of-range counts across all backends.
///
/// For 64 bits, this operations is missing in most implementations
/// and is emulated here under SSE2, SSE4.1, and AVX2.
fn shr(self, rhs: i32) -> Self;
/// Shift each value left by a constant n bits. This operation is faster in some instruction sets.
///
/// For 64 bits, this operations is missing in most implementations
/// and is emulated here under SSE2, SSE4.1, and AVX2.
#[inline(always)]
fn shl_const<const BY: i32>(self) -> Self {
SimdInt::shl(self, BY)
}
/// Shift each value right by a constant n bits. This operation is faster in some instruction sets.
///
/// For 64 bits, this operations is missing in most implementations
/// and is emulated here under SSE2, SSE4.1, and AVX2.
#[inline(always)]
fn shr_const<const BY: i32>(self) -> Self {
SimdInt::shr(self, BY)
}
/// Add every number in the vector together in unsigned arithmetic. When expanding the size of each number,
/// it treats the numbers as unsigned, meaning the sign bit doesn't get moved around.
fn horizontal_unsigned_add(self) -> Self::HorizontalAddScalar;
fn from_i64(value: i64) -> Self;
}
/// Operations shared by 8 bit int types
pub trait SimdInt8: SimdInt<Scalar = i8, HorizontalAddScalar = i64> + SimdTransmuteI8 {
type BitMask: SimdBitMask;
/// Splits the vector into two halves, then extends them both to be i16. This is useful for horizontal adding.
fn extend_to_i16(self) -> (<Self::Engine as Simd>::Vi16, <Self::Engine as Simd>::Vi16);
/// Splits the vector into two halves, then extends them both to be i16. This is useful for horizontal adding.
/// The numbers are treated as unsigned, so the sign bit isn't moved. This is more efficient on some instruction sets.
fn unsigned_extend_to_i16(self)
-> (<Self::Engine as Simd>::Vi16, <Self::Engine as Simd>::Vi16);
/// Adds (arbitrary) pairs of values in the vector, returning a i16 version of the vector.
/// The way the pairs are chosen is implementation-defined.
#[inline(always)]
fn partial_horizontal_add(self) -> <Self::Engine as Simd>::Vi16 {
let (a, b) = self.extend_to_i16();
a + b
}
/// Adds (arbitrary) pairs of values in the vector, returning a i16 version of the vector.
/// When extending the numbers, they're treated as unsigned wich performs more efficiently on some instruction sets.
/// The way the pairs are chosen is implementation-defined.
#[inline(always)]
fn partial_horizontal_unsigned_add(self) -> <Self::Engine as Simd>::Vi16 {
let (a, b) = self.unsigned_extend_to_i16();
a + b
}
/// Gets the lane truthiness mask for the vector.
///
/// Use the helper methods on [`SimdBitMask`] (or the default methods below) instead of assuming
/// that all lanes fit into a single integer. This keeps the API usable for wider future SIMD backends.
///
/// Portably, these mask helpers are intended for **canonical compare masks**: each truthy lane should
/// be all bits set (`-1`) and each falsy lane should be zero. Compare operations in simdeez already
/// produce masks in that form.
fn get_mask(self) -> Self::BitMask;
/// Checks if any element in the vector is truthy. A value is truthy either if the highest bit is one, or if any bit is one,
/// depending on the instruction set being used. For portable behavior, prefer canonical compare masks where truthy lanes
/// are `-1` and falsy lanes are `0`.
#[inline(always)]
fn is_any_truthy(self) -> bool {
self.get_mask().any_in_mask(Self::WIDTH)
}
/// Checks if all elements in the vector are truthy. A value is truthy either if the highest bit is one, or if any bit is one,
/// depending on the instruction set being used. For portable behavior, prefer canonical compare masks where truthy lanes
/// are `-1` and falsy lanes are `0`.
#[inline(always)]
fn is_truthy(self) -> bool {
self.get_mask().all_in_mask(Self::WIDTH)
}
/// Grabs the index of the last value that matches the given value. If no value matches, returns None.
/// Index will always be smaller than Self::WIDTH.
#[inline(always)]
fn index_of_last_truthy(self) -> Option<usize> {
self.get_mask().last_set_in_mask(Self::WIDTH)
}
/// Grabs the index of the last value that matches the given value. If no value matches, returns None.
/// Index will always be smaller than Self::WIDTH.
#[inline(always)]
fn index_of_last_falsy(self) -> Option<usize> {
self.get_mask().last_unset_in_mask(Self::WIDTH)
}
/// Grabs the index of the first value that matches the given value. If no value matches, returns None.
/// Index will always be smaller than Self::WIDTH.
#[inline(always)]
fn index_of_first_truthy(self) -> Option<usize> {
self.get_mask().first_set_in_mask(Self::WIDTH)
}
/// Grabs the index of the first value that matches the given value. If no value matches, returns None.
/// Index will always be smaller than Self::WIDTH.
#[inline(always)]
fn index_of_first_falsy(self) -> Option<usize> {
self.get_mask().first_unset_in_mask(Self::WIDTH)
}
/// Grabs the index of the first value that matches the given value. If no value matches, returns None.
/// Index will always be smaller than Self::WIDTH.
#[inline(always)]
fn index_of_first_eq(self, value: i8) -> Option<usize> {
let value = Self::set1(value);
let mask = self.cmp_eq(value);
mask.index_of_first_truthy()
}
}
/// Operations shared by 16 bit int types
pub trait SimdInt16: SimdInt<Scalar = i16, HorizontalAddScalar = i64> + SimdTransmuteI16 {
/// Splits the vector into two halves, then extends them both to be i32. This is useful for horizontal adding.
fn extend_to_i32(self) -> (<Self::Engine as Simd>::Vi32, <Self::Engine as Simd>::Vi32);
/// Splits the vector into two halves, then extends them both to be i32. This is useful for horizontal adding.
/// The numbers are treated as unsigned, so the sign bit isn't moved. This is more efficient on some instruction sets.
fn unsigned_extend_to_i32(self)
-> (<Self::Engine as Simd>::Vi32, <Self::Engine as Simd>::Vi32);
/// Adds (arbitrary) pairs of values in the vector, returning a i32 version of the vector.
/// The way the pairs are chosen is implementation-defined.
#[inline(always)]
fn partial_horizontal_add(self) -> <Self::Engine as Simd>::Vi32 {
let (a, b) = self.extend_to_i32();
a + b
}
/// Adds (arbitrary) pairs of values in the vector, returning a i32 version of the vector.
/// When extending the numbers, they're treated as unsigned wich performs more efficiently on some instruction sets.
/// The way the pairs are chosen is implementation-defined.
#[inline(always)]
fn partial_horizontal_unsigned_add(self) -> <Self::Engine as Simd>::Vi32 {
let (a, b) = self.unsigned_extend_to_i32();
a + b
}
}
/// Operations shared by 32 bit int types
pub trait SimdInt32: SimdInt<Scalar = i32, HorizontalAddScalar = i64> + SimdTransmuteI32 {
/// Bit cast to f32.
/// This function is only used for compilation and does not generate any instructions, thus it has zero latency.
fn bitcast_f32(self) -> <Self::Engine as Simd>::Vf32;
/// Element-wise cast to f32
fn cast_f32(self) -> <Self::Engine as Simd>::Vf32;
/// Splits the vector into two halves, then extends them both to be i64. This is useful for horizontal adding.
fn extend_to_i64(self) -> (<Self::Engine as Simd>::Vi64, <Self::Engine as Simd>::Vi64);
/// Splits the vector into two halves, then extends them both to be i32. This is useful for horizontal adding.
/// The numbers are treated as unsigned, so the sign bit isn't moved. This is more efficient on some instruction sets.
fn unsigned_extend_to_i64(self)
-> (<Self::Engine as Simd>::Vi64, <Self::Engine as Simd>::Vi64);
/// Adds (arbitrary) pairs of values in the vector, returning a i64 version of the vector.
/// The way the pairs are chosen is implementation-defined.
#[inline(always)]
fn partial_horizontal_add(self) -> <Self::Engine as Simd>::Vi64 {
let (a, b) = self.extend_to_i64();
a + b
}
/// Adds (arbitrary) pairs of values in the vector, returning a i64 version of the vector.
/// When extending the numbers, they're treated as unsigned wich performs more efficiently on some instruction sets.
/// The way the pairs are chosen is implementation-defined.
#[inline(always)]
fn partial_horizontal_unsigned_add(self) -> <Self::Engine as Simd>::Vi64 {
let (a, b) = self.unsigned_extend_to_i64();
a + b
}
}
/// Operations shared by 64 bt int types
pub trait SimdInt64: SimdInt<Scalar = i64, HorizontalAddScalar = i64> + SimdTransmuteI64 {
/// Bit cast to f64.
/// This function is only used for compilation and does not generate any instructions, thus it has zero latency.
fn bitcast_f64(self) -> <Self::Engine as Simd>::Vf64;
/// Element-wise cast to f64
fn cast_f64(self) -> <Self::Engine as Simd>::Vf64;
fn partial_horizontal_add(self) -> i64;
}
/// Operations shared by f32 and f64 floating point types
pub trait SimdFloat:
SimdBaseOps
+ Div<Self, Output = Self>
+ DivAssign<Self>
+ Div<Self::Scalar, Output = Self>
+ DivAssign<Self::Scalar> //
{
/// Element-wise divide between two vectors
fn div(self, rhs: Self) -> Self;
/// Element-wise ceilings between two vectors
fn ceil(self) -> Self;
/// Element-wise floors between two vectors
fn floor(self) -> Self;
/// Element-wise rounds between two vectors
fn round(self) -> Self;
/// Alternative element-wise ceilings between two vectors.
/// When using Sse2, this uses a faster version of ceiling
/// that only works on floating point values small enough to fit in
/// an i32. This is a big performance boost if you don't need
/// a complete ceiling.
fn fast_ceil(self) -> Self;
/// Alternative element-wise floors between two vectors.
/// When using Sse2, this uses a faster version of floor
/// that only works on floating point values small enough to fit in
/// an i32. This is a big performance boost if you don't need
/// a complete floor.
fn fast_floor(self) -> Self;
/// Alternative element-wise rounds between two vectors.
/// When using Sse2, this uses a faster version of round
/// that only works on floating point values small enough to fit in
/// an i32. This is a big performance boost if you don't need
/// a complete round.
fn fast_round(self) -> Self;
/// Element-wise multiply add. This performs `Self * A + B`
fn mul_add(self, a: Self, b: Self) -> Self;
/// Element-wise multiply subtract. This performs `Self * A - B`
fn mul_sub(self, a: Self, b: Self) -> Self;
/// Element-wise negative multiply add. This performs `-(Self * A) + B`
fn neg_mul_add(self, a: Self, b: Self) -> Self;
/// Element-wise negative multiply subtract. This performs `-(Self * A) - B`
fn neg_mul_sub(self, a: Self, b: Self) -> Self;
/// Element-wise square root
fn sqrt(self) -> Self;
/// Element-wise approximate inverse square root.
///
/// Accuracy and edge-case handling are backend dependent, especially for non-positive,
/// subnormal, infinite, and NaN inputs. Only finite positive lanes are guaranteed to
/// produce a meaningful approximation of `1.0 / sqrt(x)`.
fn rsqrt(self) -> Self;
fn from_f64(value: f64) -> Self;
}
/// Operations shared by 32 bit float types
pub trait SimdFloat32:
SimdFloat<Scalar = f32, HorizontalAddScalar = f32> + SimdTransmuteF32
{
/// Bit cast to i32.
/// This function is only used for compilation and does not generate any instructions, thus it has zero latency.
fn bitcast_i32(self) -> <Self::Engine as Simd>::Vi32;
/// Element-wise cast to i32 (rounded to nearest, ties to even; not floored).
/// Note, this may cause undefined behavior when casting from numbers outside the range of i32.
/// E.g. a very large positive float may become i32::MIN.
fn cast_i32(self) -> <Self::Engine as Simd>::Vi32;
/// Element-wise fast reciprocal (1.0 / x)
fn fast_inverse(self) -> Self;
}
/// Operations shared by 64 bit float types
pub trait SimdFloat64:
SimdFloat<Scalar = f64, HorizontalAddScalar = f64> + SimdTransmuteF64
{
/// Bit cast to i64.
/// This function is only used for compilation and does not generate any instructions, thus it has zero latency.
fn bitcast_i64(self) -> <Self::Engine as Simd>::Vi64;
/// Element-wise cast to i64 (rounded to nearest, ties to even; not floored).
fn cast_i64(self) -> <Self::Engine as Simd>::Vi64;
}
#[cfg(test)]
mod tests {
use super::SimdBitMask;
#[test]
fn bitmask_helpers_work_for_single_word_masks() {
let mask = 0b0010_1000u32;
assert!(mask.any_in_mask(6));
assert!(!mask.all_in_mask(6));
assert_eq!(mask.first_set_in_mask(6), Some(3));
assert_eq!(mask.last_set_in_mask(6), Some(5));
assert_eq!(mask.first_unset_in_mask(6), Some(0));
assert_eq!(mask.last_unset_in_mask(6), Some(4));
assert_eq!(mask.to_u32_lossy(), 0b0010_1000);
assert_eq!(mask.to_u64_lossy(), 0b0010_1000);
assert_eq!(mask.to_u128_lossy(), 0b0010_1000);
}
#[test]
fn bitmask_can_be_written_to_word_slices() {
let mask = [0x89AB_CDEFu32, 0x0123_4567u32];
let mut out = [0u32; 4];
let written = mask.write_to_u32_slice(&mut out);
assert_eq!(written, 2);
assert_eq!(out, [0x89AB_CDEF, 0x0123_4567, 0, 0]);
}
#[test]
fn bitmask_lossy_integer_exports_pack_words_in_order() {
let mask = [
0x89AB_CDEFu32,
0x0123_4567u32,
0xDEAD_BEEFu32,
0xCAFE_BABEu32,
0xFFFF_FFFFu32,
];
assert_eq!(mask.to_u32_lossy(), 0x89AB_CDEF);
assert_eq!(mask.to_u64_lossy(), 0x0123_4567_89AB_CDEFu64);
assert_eq!(
mask.to_u128_lossy(),
0xCAFE_BABE_DEAD_BEEF_0123_4567_89AB_CDEFu128
);
}
#[test]
fn bitmask_helpers_work_across_multiple_words() {
let mask = [0u32, 1u32 << 5];
assert!(mask.any_in_mask(64));
assert_eq!(mask.first_set_in_mask(64), Some(37));
assert_eq!(mask.last_set_in_mask(64), Some(37));
assert_eq!(mask.first_unset_in_mask(64), Some(0));
assert_eq!(mask.last_unset_in_mask(64), Some(63));
}
#[test]
fn bitmask_helpers_ignore_padding_bits_outside_mask() {
let mask = [u32::MAX, u32::MAX];
assert!(mask.all_in_mask(40));
assert_eq!(mask.first_unset_in_mask(40), None);
assert_eq!(mask.last_unset_in_mask(40), None);
}
}