frizbee 0.10.0

Fast typo-resistant fuzzy matching via SIMD smith waterman, similar algorithm to FZF/FZY
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
493
494
495
496
497
//! Scalar backend used when no SIMD instruction set is available (non-x86, non-ARM)

use crate::smith_waterman::algo::{ascii_gap, unicode_gap};

use super::{Backend, BytesVec, MaskVec, ScoreVec};

/// Doubles as both the byte vector and the comparison mask (a mask lane is 0xFF
/// for true, 0x00 for false)
#[derive(Debug, Clone, Copy)]
pub struct ScalarBytes<const LANES: usize>([u8; LANES]);

#[derive(Debug, Clone, Copy)]
pub struct ScalarScoreU16<const LANES: usize>([u16; LANES]);

#[derive(Debug, Clone, Copy)]
pub struct ScalarScoreU8<const LANES: usize>([u8; LANES]);

impl<const LANES: usize> ScalarScoreU16<LANES> {
    /// Widen a comparison mask (0xFF/0x00 per byte) to full-width score lanes.
    #[inline(always)]
    fn widen(mask: ScalarBytes<LANES>) -> Self {
        let mut out = [0u16; LANES];
        for (idx, lane) in out.iter_mut().enumerate() {
            *lane = if mask.0[idx] != 0 { u16::MAX } else { 0 };
        }
        Self(out)
    }
}

impl<const LANES: usize> ScalarScoreU8<LANES> {
    /// Widen a comparison mask (0xFF/0x00 per byte) to full-width score lanes.
    #[inline(always)]
    fn widen(mask: ScalarBytes<LANES>) -> Self {
        let mut out = [0u8; LANES];
        for (idx, lane) in out.iter_mut().enumerate() {
            *lane = if mask.0[idx] != 0 { u8::MAX } else { 0 };
        }
        Self(out)
    }
}

impl<const LANES: usize> BytesVec for ScalarBytes<LANES> {
    type Mask = ScalarBytes<LANES>;

    #[inline(always)]
    unsafe fn splat(value: u8) -> Self {
        Self([value; LANES])
    }

    #[inline(always)]
    unsafe fn eq(self, other: Self) -> Self::Mask {
        let mut out = [0u8; LANES];
        for (idx, lane) in out.iter_mut().enumerate() {
            *lane = if self.0[idx] == other.0[idx] { 0xFF } else { 0 };
        }
        Self(out)
    }

    #[inline(always)]
    unsafe fn gt(self, other: Self) -> Self::Mask {
        let mut out = [0u8; LANES];
        for (idx, lane) in out.iter_mut().enumerate() {
            *lane = if self.0[idx] > other.0[idx] { 0xFF } else { 0 };
        }
        Self(out)
    }

    #[inline(always)]
    unsafe fn lt(self, other: Self) -> Self::Mask {
        let mut out = [0u8; LANES];
        for (idx, lane) in out.iter_mut().enumerate() {
            *lane = if self.0[idx] < other.0[idx] { 0xFF } else { 0 };
        }
        Self(out)
    }

    #[inline(always)]
    unsafe fn load_partial(data: *const u8, start: usize, len: usize) -> Self {
        let mut out = [0u8; LANES];
        let take = len.saturating_sub(start).min(LANES);
        for (idx, lane) in out.iter_mut().take(take).enumerate() {
            *lane = unsafe { *data.add(start + idx) };
        }
        Self(out)
    }

    #[cfg(test)]
    fn from_lanes(values: &[u8]) -> Self {
        assert_eq!(values.len(), LANES);
        let mut out = [0u8; LANES];
        out.copy_from_slice(values);
        Self(out)
    }

    #[cfg(test)]
    fn to_lanes(self) -> Vec<u8> {
        self.0.to_vec()
    }
}

impl<const LANES: usize> MaskVec for ScalarBytes<LANES> {
    #[inline(always)]
    unsafe fn zero() -> Self {
        Self([0; LANES])
    }

    #[inline(always)]
    unsafe fn and(self, other: Self) -> Self {
        let mut out = [0u8; LANES];
        for (idx, lane) in out.iter_mut().enumerate() {
            *lane = self.0[idx] & other.0[idx];
        }
        Self(out)
    }

    #[inline(always)]
    unsafe fn or(self, other: Self) -> Self {
        let mut out = [0u8; LANES];
        for (idx, lane) in out.iter_mut().enumerate() {
            *lane = self.0[idx] | other.0[idx];
        }
        Self(out)
    }

    #[inline(always)]
    unsafe fn not(self) -> Self {
        let mut out = [0u8; LANES];
        for (idx, lane) in out.iter_mut().enumerate() {
            *lane = !self.0[idx];
        }
        Self(out)
    }

    #[inline(always)]
    unsafe fn is_zero(self) -> bool {
        self.0.iter().all(|&v| v == 0)
    }

    #[inline(always)]
    unsafe fn shift_right_padded_1(self, prev: Self) -> Self {
        let mut out = [0u8; LANES];
        out[0] = prev.0[LANES - 1];
        out[1..LANES].copy_from_slice(&self.0[..(LANES - 1)]);
        Self(out)
    }

    #[cfg(test)]
    fn from_lanes(values: &[bool]) -> Self {
        assert_eq!(values.len(), LANES);
        let mut out = [0u8; LANES];
        for (idx, lane) in out.iter_mut().enumerate() {
            *lane = if values[idx] { 0xFF } else { 0 };
        }
        Self(out)
    }

    #[cfg(test)]
    fn to_lanes(self) -> Vec<bool> {
        self.0.iter().map(|&v| v != 0).collect()
    }
}

impl<const LANES: usize> ScoreVec for ScalarScoreU16<LANES> {
    #[inline(always)]
    unsafe fn zero() -> Self {
        Self([0; LANES])
    }

    #[inline(always)]
    unsafe fn splat(value: u16) -> Self {
        Self([value; LANES])
    }

    #[inline(always)]
    unsafe fn first_lane(value: u16) -> Self {
        let mut out = [0u16; LANES];
        out[0] = value;
        Self(out)
    }

    #[inline(always)]
    unsafe fn max(self, other: Self) -> Self {
        let mut out = [0u16; LANES];
        for (idx, lane) in out.iter_mut().enumerate() {
            *lane = self.0[idx].max(other.0[idx]);
        }
        Self(out)
    }

    #[inline(always)]
    unsafe fn horizontal_max(self) -> u16 {
        *self.0.iter().max().unwrap()
    }

    #[inline(always)]
    unsafe fn add(self, other: Self) -> Self {
        let mut out = [0u16; LANES];
        for (idx, lane) in out.iter_mut().enumerate() {
            *lane = self.0[idx].wrapping_add(other.0[idx]);
        }
        Self(out)
    }

    #[inline(always)]
    unsafe fn subs(self, other: Self) -> Self {
        let mut out = [0u16; LANES];
        for (idx, lane) in out.iter_mut().enumerate() {
            *lane = self.0[idx].saturating_sub(other.0[idx]);
        }
        Self(out)
    }

    #[inline(always)]
    unsafe fn and(self, other: Self) -> Self {
        let mut out = [0u16; LANES];
        for (idx, lane) in out.iter_mut().enumerate() {
            *lane = self.0[idx] & other.0[idx];
        }
        Self(out)
    }

    #[inline(always)]
    unsafe fn shift_right_padded<const L: i32>(self, prev: Self) -> Self {
        const { assert!(L >= 0 && (L as usize) <= LANES) };
        let n = L as usize;
        let mut out = [0u16; LANES];
        for (idx, item) in out.iter_mut().enumerate().take(n) {
            *item = prev.0[LANES - n + idx];
        }
        out[n..LANES].copy_from_slice(&self.0[..(LANES - n)]);
        Self(out)
    }

    #[inline(always)]
    unsafe fn find_lane(self, search: u16) -> usize {
        for (idx, &lane) in self.0.iter().enumerate() {
            if lane == search {
                return idx;
            }
        }
        LANES
    }

    #[cfg(test)]
    fn from_lanes(values: &[u16]) -> Self {
        assert_eq!(values.len(), LANES);
        let mut out = [0u16; LANES];
        out.copy_from_slice(values);
        Self(out)
    }

    #[cfg(test)]
    fn to_lanes(self) -> Vec<u16> {
        self.0.to_vec()
    }
}

impl<const LANES: usize> ScoreVec for ScalarScoreU8<LANES> {
    #[inline(always)]
    unsafe fn zero() -> Self {
        Self([0; LANES])
    }

    #[inline(always)]
    unsafe fn splat(value: u16) -> Self {
        Self([value as u8; LANES])
    }

    #[inline(always)]
    unsafe fn first_lane(value: u16) -> Self {
        let mut out = [0u8; LANES];
        out[0] = value as u8;
        Self(out)
    }

    #[inline(always)]
    unsafe fn max(self, other: Self) -> Self {
        let mut out = [0u8; LANES];
        for (idx, lane) in out.iter_mut().enumerate() {
            *lane = self.0[idx].max(other.0[idx]);
        }
        Self(out)
    }

    #[inline(always)]
    unsafe fn horizontal_max(self) -> u16 {
        *self.0.iter().max().unwrap() as u16
    }

    #[inline(always)]
    unsafe fn add(self, other: Self) -> Self {
        let mut out = [0u8; LANES];
        for (idx, lane) in out.iter_mut().enumerate() {
            *lane = self.0[idx].wrapping_add(other.0[idx]);
        }
        Self(out)
    }

    #[inline(always)]
    unsafe fn subs(self, other: Self) -> Self {
        let mut out = [0u8; LANES];
        for (idx, lane) in out.iter_mut().enumerate() {
            *lane = self.0[idx].saturating_sub(other.0[idx]);
        }
        Self(out)
    }

    #[inline(always)]
    unsafe fn and(self, other: Self) -> Self {
        let mut out = [0u8; LANES];
        for (idx, lane) in out.iter_mut().enumerate() {
            *lane = self.0[idx] & other.0[idx];
        }
        Self(out)
    }

    #[inline(always)]
    unsafe fn shift_right_padded<const L: i32>(self, prev: Self) -> Self {
        const { assert!(L >= 0 && (L as usize) <= LANES) };
        let n = L as usize;
        let mut out = [0u8; LANES];
        for (idx, item) in out.iter_mut().enumerate().take(n) {
            *item = prev.0[LANES - n + idx];
        }
        out[n..LANES].copy_from_slice(&self.0[..(LANES - n)]);
        Self(out)
    }

    #[inline(always)]
    unsafe fn find_lane(self, search: u16) -> usize {
        let search = search as u8;
        for (idx, &lane) in self.0.iter().enumerate() {
            if lane == search {
                return idx;
            }
        }
        LANES
    }

    #[cfg(test)]
    fn from_lanes(values: &[u16]) -> Self {
        assert_eq!(values.len(), LANES);
        let mut out = [0u8; LANES];
        for (idx, lane) in out.iter_mut().enumerate() {
            *lane = values[idx] as u8;
        }
        Self(out)
    }

    #[cfg(test)]
    fn to_lanes(self) -> Vec<u16> {
        self.0.iter().map(|&v| v as u16).collect()
    }
}

// Each scalar backend differs only in lane count, score-element width, and
// which lane-count-specialized gap-propagation helper it needs. We use a
// macro to avoid writing these all out by hand.
macro_rules! scalar_backend {
    (
        $backend:ident,
        $lanes:literal,
        $lane_bytes:literal,
        $score:ty,
        $propagate:ident,
        $propagate_unicode:ident
    ) => {
        #[derive(Debug, Clone, Copy)]
        pub struct $backend;

        impl Backend for $backend {
            const LANES: usize = $lanes;
            const LANE_BYTES: usize = $lane_bytes;
            type Bytes = ScalarBytes<$lanes>;
            type Mask = ScalarBytes<$lanes>;
            type Score = $score;

            fn is_available() -> bool {
                true
            }

            #[inline(always)]
            unsafe fn widen_mask(m: Self::Mask) -> Self::Score {
                <$score>::widen(m)
            }

            #[inline(always)]
            unsafe fn propagate_horizontal_gaps(
                row: Self::Score,
                adjacent_row: Self::Score,
                match_mask: Self::Score,
                adjacent_match_mask: Self::Score,
                gap_open_penalty: Self::Score,
                gap_extend_penalty: Self::Score,
            ) -> Self::Score {
                unsafe {
                    ascii_gap::$propagate::<Self>(
                        row,
                        adjacent_row,
                        match_mask,
                        adjacent_match_mask,
                        gap_open_penalty,
                        gap_extend_penalty,
                    )
                }
            }

            #[inline(always)]
            unsafe fn propagate_horizontal_unicode_gaps(
                row: Self::Score,
                adjacent_row: Self::Score,
                pending_gap_open_mask: Self::Score,
                adjacent_pending_gap_open_mask: Self::Score,
                continuation_gap_extend_penalty: Self::Score,
                adjacent_continuation_gap_extend_penalty: Self::Score,
                scalar_end_mask: Self::Score,
                adjacent_scalar_end_mask: Self::Score,
                gap_open_penalty: Self::Score,
                gap_extend_penalty: Self::Score,
            ) -> (Self::Score, Self::Score) {
                unsafe {
                    unicode_gap::$propagate_unicode::<Self>(
                        row,
                        adjacent_row,
                        pending_gap_open_mask,
                        adjacent_pending_gap_open_mask,
                        continuation_gap_extend_penalty,
                        adjacent_continuation_gap_extend_penalty,
                        scalar_end_mask,
                        adjacent_scalar_end_mask,
                        gap_open_penalty,
                        gap_extend_penalty,
                    )
                }
            }
        }
    };
}

scalar_backend!(
    BackendScalar8,
    8,
    2,
    ScalarScoreU16<8>,
    propagate_8_lane,
    propagate_unicode_8_lane
);
scalar_backend!(
    BackendScalar16U8,
    16,
    1,
    ScalarScoreU8<16>,
    propagate_16_lane,
    propagate_unicode_16_lane
);

// Test only backends used to assert correctness in the SIMD backends
#[cfg(target_arch = "x86_64")]
#[cfg(test)]
scalar_backend!(
    TestScalar16,
    16,
    2,
    ScalarScoreU16<16>,
    propagate_16_lane,
    propagate_unicode_16_lane
);
#[cfg(target_arch = "x86_64")]
#[cfg(test)]
scalar_backend!(
    TestScalar32,
    32,
    2,
    ScalarScoreU16<32>,
    propagate_32_lane,
    propagate_unicode_32_lane
);
#[cfg(target_arch = "x86_64")]
#[cfg(test)]
scalar_backend!(
    TestScalar32U8,
    32,
    1,
    ScalarScoreU8<32>,
    propagate_32_lane,
    propagate_unicode_32_lane
);
#[cfg(target_arch = "x86_64")]
#[cfg(test)]
scalar_backend!(
    TestScalar64U8,
    64,
    1,
    ScalarScoreU8<64>,
    propagate_64_lane,
    propagate_unicode_64_lane
);