sassy 0.2.1

Approximate string matching using SIMD
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
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
use std::ops::{Add, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, Shl, Shr, Sub};
use wide::{CmpEq, CmpGt, i8x32, u8x32, u16x16, u32x8, u64x4};

#[cfg(all(target_arch = "x86_64", target_feature = "avx512f"))]
use wide::{u16x32, u32x16, u64x8};

#[cfg(all(target_arch = "x86_64", target_feature = "avx512bw"))]
use std::arch::x86_64::*;

#[derive(Clone, Copy)]
pub struct LaneArray<T: Copy, const N: usize>(pub [T; N]);

impl<T: Copy, const N: usize> From<[T; N]> for LaneArray<T, N> {
    #[inline(always)]
    fn from(value: [T; N]) -> Self {
        Self(value)
    }
}

impl<T: Copy, const N: usize> From<LaneArray<T, N>> for [T; N] {
    #[inline(always)]
    fn from(value: LaneArray<T, N>) -> Self {
        value.0
    }
}

impl<T: Copy + Default, const N: usize> Default for LaneArray<T, N> {
    #[inline(always)]
    fn default() -> Self {
        Self([T::default(); N])
    }
}

impl<T: Copy, const N: usize> AsRef<[T]> for LaneArray<T, N> {
    #[inline(always)]
    fn as_ref(&self) -> &[T] {
        &self.0
    }
}

impl<T: Copy, const N: usize> AsMut<[T]> for LaneArray<T, N> {
    #[inline(always)]
    fn as_mut(&mut self) -> &mut [T] {
        &mut self.0
    }
}

pub trait SimdBackend: Copy + 'static + Send + Sync + Default + std::fmt::Debug {
    type Simd: Copy
        + Add<Output = Self::Simd>
        + Sub<Output = Self::Simd>
        + BitAnd<Output = Self::Simd>
        + BitOr<Output = Self::Simd>
        + BitAndAssign<Self::Simd>
        + BitOrAssign<Self::Simd>
        + BitXor<Output = Self::Simd>
        + Shl<i32, Output = Self::Simd>
        + Shr<i32, Output = Self::Simd>
        + Shl<u32, Output = Self::Simd>
        + Shr<u32, Output = Self::Simd>
        + CmpEq<Output = Self::Simd>
        + PartialEq;

    type Scalar: Copy + PartialEq + std::fmt::Debug;
    type LaneArray: AsRef<[Self::Scalar]> + AsMut<[Self::Scalar]> + Copy + Default;

    const LANES: usize;
    const LIMB_BITS: usize;

    fn mask_word_to_scalar(word: u64) -> Self::Scalar;
    fn scalar_from_i64(value: i64) -> Self::Scalar;
    fn from_array(arr: Self::LaneArray) -> Self::Simd;
    fn to_array(vec: Self::Simd) -> Self::LaneArray;
    fn simd_gt(lhs: Self::Simd, rhs: Self::Simd) -> Self::Simd;
    fn scalar_to_u64(value: Self::Scalar) -> u64;
    fn splat_all_ones() -> Self::Simd;
    fn splat_zero() -> Self::Simd;
    fn splat_one() -> Self::Simd;
    fn splat_scalar(value: Self::Scalar) -> Self::Simd;
    fn lanes_with_zero(vec: Self::Simd) -> u64;
}

macro_rules! impl_wide_backend {
    ($name:ident, $simd_ty:ty, $scalar:ty, $lanes:expr, $bits:expr, $pattern:ty) => {
        #[derive(Clone, Copy, Debug, Default)]
        pub struct $name;

        impl SimdBackend for $name {
            type Simd = $simd_ty;
            type Scalar = $scalar;
            type LaneArray = LaneArray<$scalar, $lanes>;

            const LANES: usize = $lanes;
            const LIMB_BITS: usize = $bits;

            #[inline(always)]
            fn mask_word_to_scalar(word: u64) -> Self::Scalar {
                word as $scalar
            }
            #[inline(always)]
            fn scalar_from_i64(value: i64) -> Self::Scalar {
                value as $scalar
            }
            #[inline(always)]
            fn from_array(arr: Self::LaneArray) -> Self::Simd {
                <$simd_ty>::new(arr.into())
            }
            #[inline(always)]
            fn to_array(vec: Self::Simd) -> Self::LaneArray {
                LaneArray::from(vec.to_array())
            }
            #[inline(always)]
            fn splat_all_ones() -> Self::Simd {
                <$simd_ty>::splat(!0 as $scalar)
            }
            #[inline(always)]
            fn splat_zero() -> Self::Simd {
                <$simd_ty>::splat(0)
            }
            #[inline(always)]
            fn splat_one() -> Self::Simd {
                <$simd_ty>::splat(1)
            }
            #[inline(always)]
            fn splat_scalar(value: Self::Scalar) -> Self::Simd {
                <$simd_ty>::splat(value)
            }
            #[inline(always)]
            fn scalar_to_u64(value: Self::Scalar) -> u64 {
                value as u64
            }

            #[inline(always)]
            fn simd_gt(lhs: Self::Simd, rhs: Self::Simd) -> Self::Simd {
                lhs.simd_gt(rhs)
            }

            #[inline(always)]
            fn lanes_with_zero(vec: Self::Simd) -> u64 {
                vec.simd_eq(<$simd_ty>::splat(0)).to_bitmask() as u64
            }
        }
    };
}

macro_rules! impl_u8_wrapper {
    ($name:ident, $wide_ty:ty, $signed_ty:ty, $lanes:expr) => {
        #[derive(Clone, Copy, Debug, Default, PartialEq)]
        #[repr(transparent)]
        pub struct $name(pub $wide_ty);

        impl Add for $name {
            type Output = Self;
            #[inline(always)]
            fn add(self, rhs: Self) -> Self {
                Self(self.0 + rhs.0)
            }
        }
        impl Sub for $name {
            type Output = Self;
            #[inline(always)]
            fn sub(self, rhs: Self) -> Self {
                Self(self.0 - rhs.0)
            }
        }
        impl BitAnd for $name {
            type Output = Self;
            #[inline(always)]
            fn bitand(self, rhs: Self) -> Self {
                Self(self.0 & rhs.0)
            }
        }
        impl BitOr for $name {
            type Output = Self;
            #[inline(always)]
            fn bitor(self, rhs: Self) -> Self {
                Self(self.0 | rhs.0)
            }
        }
        impl BitXor for $name {
            type Output = Self;
            #[inline(always)]
            fn bitxor(self, rhs: Self) -> Self {
                Self(self.0 ^ rhs.0)
            }
        }
        impl BitAndAssign for $name {
            #[inline(always)]
            fn bitand_assign(&mut self, rhs: Self) {
                self.0 &= rhs.0;
            }
        }
        impl BitOrAssign for $name {
            #[inline(always)]
            fn bitor_assign(&mut self, rhs: Self) {
                self.0 |= rhs.0;
            }
        }
        impl CmpEq for $name {
            type Output = Self;
            #[inline(always)]
            fn simd_eq(self, rhs: Self) -> Self {
                Self(self.0.simd_eq(rhs.0))
            }
        }
        impl Shl<u32> for $name {
            type Output = Self;
            #[inline(always)]
            fn shl(self, rhs: u32) -> Self {
                if rhs >= 8 {
                    return Self::splat(0);
                }
                let mut arr = self.to_array();
                for byte in &mut arr {
                    *byte = byte.wrapping_shl(rhs);
                }
                Self(<$wide_ty>::new(arr))
            }
        }
        impl Shr<u32> for $name {
            type Output = Self;
            #[inline(always)]
            fn shr(self, rhs: u32) -> Self {
                if rhs >= 8 {
                    return Self::splat(0);
                }
                let mut arr = self.to_array();
                for byte in &mut arr {
                    *byte = byte.wrapping_shr(rhs);
                }
                Self(<$wide_ty>::new(arr))
            }
        }
        impl Shl<i32> for $name {
            type Output = Self;
            #[inline(always)]
            fn shl(self, rhs: i32) -> Self {
                self << (rhs as u32)
            }
        }
        impl Shr<i32> for $name {
            type Output = Self;
            #[inline(always)]
            fn shr(self, rhs: i32) -> Self {
                self >> (rhs as u32)
            }
        }

        impl $name {
            #[inline(always)]
            pub fn new(arr: [u8; $lanes]) -> Self {
                Self(<$wide_ty>::new(arr))
            }
            #[inline(always)]
            pub fn to_array(self) -> [u8; $lanes] {
                self.0.to_array()
            }
            #[inline(always)]
            pub fn splat(val: u8) -> Self {
                Self(<$wide_ty>::splat(val))
            }
            #[inline(always)]
            pub fn simd_gt(self, rhs: Self) -> Self {
                use std::mem::transmute;
                let mask = <$signed_ty>::splat(1 << 7);
                let a: $signed_ty = unsafe { transmute(self.0) };
                let b: $signed_ty = unsafe { transmute(rhs.0) };
                Self(unsafe { transmute(CmpGt::simd_gt(a ^ mask, b ^ mask)) })
            }
            #[inline(always)]
            pub fn to_bitmask(self) -> u64 {
                self.0.to_bitmask() as u64
            }
        }
    };
}

impl_u8_wrapper!(WrapperU8x32, u8x32, i8x32, 32);

#[cfg(all(target_arch = "x86_64", target_feature = "avx512bw"))]
#[derive(Clone, Copy, Debug)]
#[repr(transparent)]
pub struct Avx512U8(pub __m512i);

#[cfg(all(target_arch = "x86_64", target_feature = "avx512bw"))]
impl Default for Avx512U8 {
    #[inline(always)]
    fn default() -> Self {
        Self::splat(0)
    }
}

#[cfg(all(target_arch = "x86_64", target_feature = "avx512bw"))]
impl PartialEq for Avx512U8 {
    fn eq(&self, other: &Self) -> bool {
        unsafe {
            let mut a = [0u64; 8];
            let mut b = [0u64; 8];
            _mm512_storeu_si512(a.as_mut_ptr() as *mut _, self.0);
            _mm512_storeu_si512(b.as_mut_ptr() as *mut _, other.0);
            a == b
        }
    }
}

#[cfg(all(target_arch = "x86_64", target_feature = "avx512bw"))]
impl Avx512U8 {
    #[inline(always)]
    pub fn new(arr: [u8; 64]) -> Self {
        unsafe { Self(_mm512_loadu_si512(arr.as_ptr() as *const _)) }
    }

    #[inline(always)]
    pub fn to_array(self) -> [u8; 64] {
        unsafe {
            let mut arr = [0u8; 64];
            _mm512_storeu_si512(arr.as_mut_ptr() as *mut _, self.0);
            arr
        }
    }

    #[inline(always)]
    pub fn splat(val: u8) -> Self {
        unsafe { Self(_mm512_set1_epi8(val as i8)) }
    }

    #[inline(always)]
    pub fn simd_gt(self, rhs: Self) -> Self {
        unsafe {
            let mask = _mm512_cmp_epu8_mask(self.0, rhs.0, 6); // is this safe to assume 6?
            Self(_mm512_maskz_set1_epi8(mask, -1))
        }
    }

    #[inline(always)]
    pub fn to_bitmask(self) -> u64 {
        unsafe { _mm512_movepi8_mask(self.0) as u64 }
    }
}

#[cfg(all(target_arch = "x86_64", target_feature = "avx512bw"))]
impl Add for Avx512U8 {
    type Output = Self;
    #[inline(always)]
    fn add(self, rhs: Self) -> Self {
        unsafe { Self(_mm512_add_epi8(self.0, rhs.0)) }
    }
}

#[cfg(all(target_arch = "x86_64", target_feature = "avx512bw"))]
impl Sub for Avx512U8 {
    type Output = Self;
    #[inline(always)]
    fn sub(self, rhs: Self) -> Self {
        unsafe { Self(_mm512_sub_epi8(self.0, rhs.0)) }
    }
}

#[cfg(all(target_arch = "x86_64", target_feature = "avx512bw"))]
impl BitAnd for Avx512U8 {
    type Output = Self;
    #[inline(always)]
    fn bitand(self, rhs: Self) -> Self {
        unsafe { Self(_mm512_and_si512(self.0, rhs.0)) }
    }
}

#[cfg(all(target_arch = "x86_64", target_feature = "avx512bw"))]
impl BitOr for Avx512U8 {
    type Output = Self;
    #[inline(always)]
    fn bitor(self, rhs: Self) -> Self {
        unsafe { Self(_mm512_or_si512(self.0, rhs.0)) }
    }
}

#[cfg(all(target_arch = "x86_64", target_feature = "avx512bw"))]
impl BitXor for Avx512U8 {
    type Output = Self;
    #[inline(always)]
    fn bitxor(self, rhs: Self) -> Self {
        unsafe { Self(_mm512_xor_si512(self.0, rhs.0)) }
    }
}

#[cfg(all(target_arch = "x86_64", target_feature = "avx512bw"))]
impl BitAndAssign for Avx512U8 {
    #[inline(always)]
    fn bitand_assign(&mut self, rhs: Self) {
        self.0 = unsafe { _mm512_and_si512(self.0, rhs.0) };
    }
}

#[cfg(all(target_arch = "x86_64", target_feature = "avx512bw"))]
impl BitOrAssign for Avx512U8 {
    #[inline(always)]
    fn bitor_assign(&mut self, rhs: Self) {
        self.0 = unsafe { _mm512_or_si512(self.0, rhs.0) };
    }
}

#[cfg(all(target_arch = "x86_64", target_feature = "avx512bw"))]
impl Shl<u32> for Avx512U8 {
    type Output = Self;
    #[inline(always)]
    fn shl(self, rhs: u32) -> Self {
        if rhs >= 8 {
            return Self::splat(0);
        }
        let mut arr = self.to_array();
        for byte in &mut arr {
            *byte = byte.wrapping_shl(rhs);
        }
        Self::new(arr)
    }
}

#[cfg(all(target_arch = "x86_64", target_feature = "avx512bw"))]
impl Shr<u32> for Avx512U8 {
    type Output = Self;
    #[inline(always)]
    fn shr(self, rhs: u32) -> Self {
        if rhs >= 8 {
            return Self::splat(0);
        }
        let mut arr = self.to_array();
        for byte in &mut arr {
            *byte = byte.wrapping_shr(rhs);
        }
        Self::new(arr)
    }
}

#[cfg(all(target_arch = "x86_64", target_feature = "avx512bw"))]
impl Shl<i32> for Avx512U8 {
    type Output = Self;
    #[inline(always)]
    fn shl(self, rhs: i32) -> Self {
        self << (rhs as u32)
    }
}

#[cfg(all(target_arch = "x86_64", target_feature = "avx512bw"))]
impl Shr<i32> for Avx512U8 {
    type Output = Self;
    #[inline(always)]
    fn shr(self, rhs: i32) -> Self {
        self >> (rhs as u32)
    }
}

#[cfg(all(target_arch = "x86_64", target_feature = "avx512bw"))]
impl CmpEq for Avx512U8 {
    type Output = Self;
    #[inline(always)]
    fn simd_eq(self, rhs: Self) -> Self {
        unsafe {
            let mask = _mm512_cmpeq_epu8_mask(self.0, rhs.0);
            Self(_mm512_maskz_set1_epi8(mask, -1))
        }
    }
}

// Define Backends
impl_wide_backend!(I32x8Backend, u32x8, u32, 8, 32, u8x32);
impl_wide_backend!(I64x4Backend, u64x4, u64, 4, 64, [u8; 64]);
impl_wide_backend!(I16x16Backend, u16x16, u16, 16, 16, [u8; 16]);
impl_wide_backend!(I8x32Backend, WrapperU8x32, u8, 32, 8, [u8; 8]);

#[cfg(all(target_arch = "x86_64", target_feature = "avx512f"))]
impl_wide_backend!(I32x16Backend, u32x16, u32, 16, 32, [u8; 32]);
#[cfg(all(target_arch = "x86_64", target_feature = "avx512f"))]
impl_wide_backend!(I64x8Backend, u64x8, u64, 8, 64, [u8; 64]);
#[cfg(all(target_arch = "x86_64", target_feature = "avx512bw"))]
impl_wide_backend!(I16x32Backend, u16x32, u16, 32, 16, [u8; 16]);
#[cfg(all(target_arch = "x86_64", target_feature = "avx512bw"))]
impl_wide_backend!(I8x64Backend, Avx512U8, u8, 64, 8, [u8; 8]);

// --- Aliases ---
pub type U8 = I8x32Backend;
pub type U32 = I32x8Backend;
pub type U64 = I64x4Backend;
pub type U16 = I16x16Backend;

#[cfg(all(target_arch = "x86_64", target_feature = "avx512bw"))]
pub type U8_512 = I8x64Backend;
#[cfg(all(target_arch = "x86_64", target_feature = "avx512f"))]
pub type U32_512 = I32x16Backend;
#[cfg(all(target_arch = "x86_64", target_feature = "avx512f"))]
pub type U64_512 = I64x8Backend;
#[cfg(all(target_arch = "x86_64", target_feature = "avx512bw"))]
pub type U16_512 = I16x32Backend;