memspan 0.1.0

SIMD-accelerated byte-class scanning for lexers and parsers. Backends: AVX-512, AVX2, SSE4.1, NEON, WASM SIMD128. no_std compatible.
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
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
532
533
534
535
536
/// SIMD and vendor intrinsics module.
pub mod arch;

mod sealed {
  pub trait Sealed {}

  impl<T> Sealed for &T where T: Sealed + ?Sized {}
  impl Sealed for u8 {}
  impl Sealed for [u8] {}
  impl<const N: usize> Sealed for [u8; N] {}
}

macro_rules! tail_find_fixed {
  ($name:ident, $($needle:ident),+) => {
    #[cfg_attr(not(tarpaulin), inline(always))]
    #[allow(clippy::too_many_arguments)]
    fn $name(tail: &[u8], $($needle: u8),+) -> Option<usize> {
      for (idx, &byte) in tail.iter().enumerate() {
        if false $(|| byte == $needle)+ {
          return Some(idx);
        }
      }

      None
    }
  };
}

tail_find_fixed!(tail_find1, a);
tail_find_fixed!(tail_find2, a, b);
tail_find_fixed!(tail_find3, a, b, c);
tail_find_fixed!(tail_find4, a, b, c, d);
tail_find_fixed!(tail_find5, a, b, c, d, e);
tail_find_fixed!(tail_find6, a, b, c, d, e, f);
tail_find_fixed!(tail_find7, a, b, c, d, e, f, g);
tail_find_fixed!(tail_find8, a, b, c, d, e, f, g, h);

macro_rules! prefix_len_fixed {
  ($name:ident, $($needle:ident),+) => {
    #[cfg_attr(not(tarpaulin), inline(always))]
    #[allow(clippy::too_many_arguments)]
    fn $name(input: &[u8], $($needle: u8),+) -> usize {
      for (idx, &byte) in input.iter().enumerate() {
        if !(false $(|| byte == $needle)+) {
          return idx;
        }
      }

      input.len()
    }
  };
}

prefix_len_fixed!(prefix_len1, a);
prefix_len_fixed!(prefix_len2, a, b);
prefix_len_fixed!(prefix_len3, a, b, c);
prefix_len_fixed!(prefix_len4, a, b, c, d);
prefix_len_fixed!(prefix_len5, a, b, c, d, e);
prefix_len_fixed!(prefix_len6, a, b, c, d, e, f);
prefix_len_fixed!(prefix_len7, a, b, c, d, e, f, g);
prefix_len_fixed!(prefix_len8, a, b, c, d, e, f, g, h);

/// Needle types for SIMD-accelerated searching.
pub trait Needles: sealed::Sealed {
  /// Number of needles in this set.
  ///
  /// The dispatcher uses this to pick between the scalar path (for inputs
  /// shorter than one SIMD chunk) and the wider SIMD loop.
  fn needle_count(&self) -> usize;

  /// Whether the needles are empty (e.g., an empty slice).
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn is_empty(&self) -> bool {
    self.needle_count() == 0
  }

  /// Find the first occurrence of any of the needles in `tail`, returning
  /// the index (relative to the start of `tail`) if found.
  ///
  /// This is the scalar primitive that backs [`crate::skip::skip_until`]'s
  /// fallback path. Uses an unrolled per-byte loop that LLVM auto-vectorizes
  /// well for 1–8 needles; falls back to a per-byte `contains` check beyond 8.
  ///
  /// Calling this directly bypasses the per-call dispatcher overhead in
  /// [`crate::skip::skip_until`] (one length check + one runtime feature
  /// check). That overhead is invisible on long inputs but measurable on tight
  /// loops over very short slices (≲32 bytes). Prefer the dispatcher for
  /// general use; reach for this when you have known-short inputs and the
  /// extra few cycles per call matter.
  fn tail_find(&self, tail: &[u8]) -> Option<usize>;

  /// Returns the length of the longest prefix where every byte matches one of
  /// the needles.
  ///
  /// This is the scalar primitive that backs [`crate::skip::skip_while`]'s
  /// fallback path. For 1 needle and for 2–8 needle sets it uses tight,
  /// LLVM-auto-vectorized per-byte loops; for >8 needles it falls back to a
  /// per-byte `contains` check.
  ///
  /// Calling this directly bypasses the per-call dispatcher overhead in
  /// [`crate::skip::skip_while`]. That overhead is structural — the
  /// dispatcher's body holds both a scalar branch and a NEON branch, which
  /// LLVM doesn't fully inline at the call site — and it is the floor on
  /// throughput for inputs in the 16–32 byte range. If you call `skip_while`
  /// in a hot loop where every input is known to be short (e.g. you're
  /// already chunking elsewhere), calling `prefix_len` directly here recovers
  /// roughly 1.7× throughput at len=16. Prefer the dispatcher for general use.
  fn prefix_len(&self, input: &[u8]) -> usize;

  /// Returns a NEON byte mask where matching lanes are `0xFF` and non-matching
  /// lanes are `0x00`.
  #[cfg(target_arch = "aarch64")]
  fn eq_any_mask_neon(
    &self,
    chunk: core::arch::aarch64::uint8x16_t,
  ) -> core::arch::aarch64::uint8x16_t;

  /// Returns an SSE2 byte mask (`__m128i`) where matching lanes are `0xFF` and
  /// non-matching lanes are `0x00`.
  #[cfg(target_arch = "x86")]
  fn eq_any_mask_sse2(&self, chunk: core::arch::x86::__m128i) -> core::arch::x86::__m128i;

  /// Returns an SSE2 byte mask (`__m128i`) where matching lanes are `0xFF` and
  /// non-matching lanes are `0x00`.
  #[cfg(target_arch = "x86_64")]
  fn eq_any_mask_sse2(&self, chunk: core::arch::x86_64::__m128i) -> core::arch::x86_64::__m128i;

  /// Returns an AVX2 byte mask (`__m256i`) where matching lanes are `0xFF` and
  /// non-matching lanes are `0x00`.
  #[cfg(target_arch = "x86_64")]
  fn eq_any_mask_avx2(&self, chunk: core::arch::x86_64::__m256i) -> core::arch::x86_64::__m256i;

  /// Returns a 64-bit AVX-512BW bitmask where bit `i` is set if byte lane `i`
  /// of `chunk` matches any needle. Unlike the SSE/AVX variants this is a
  /// direct `__mmask64` rather than a byte-per-lane vector.
  #[cfg(target_arch = "x86_64")]
  fn eq_any_mask_avx512(&self, chunk: core::arch::x86_64::__m512i) -> u64;

  /// Returns a WASM SIMD128 byte mask (`v128`) where matching lanes are `0xFF`
  /// and non-matching lanes are `0x00`.
  #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
  fn eq_any_mask_simd128(&self, chunk: core::arch::wasm32::v128) -> core::arch::wasm32::v128;
}

impl<T> Needles for &T
where
  T: Needles + ?Sized,
{
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn needle_count(&self) -> usize {
    (**self).needle_count()
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn tail_find(&self, tail: &[u8]) -> Option<usize> {
    (**self).tail_find(tail)
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn prefix_len(&self, input: &[u8]) -> usize {
    (**self).prefix_len(input)
  }

  #[cfg(target_arch = "aarch64")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn eq_any_mask_neon(
    &self,
    chunk: core::arch::aarch64::uint8x16_t,
  ) -> core::arch::aarch64::uint8x16_t {
    (**self).eq_any_mask_neon(chunk)
  }

  #[cfg(target_arch = "x86")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn eq_any_mask_sse2(&self, chunk: core::arch::x86::__m128i) -> core::arch::x86::__m128i {
    (**self).eq_any_mask_sse2(chunk)
  }

  #[cfg(target_arch = "x86_64")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn eq_any_mask_sse2(&self, chunk: core::arch::x86_64::__m128i) -> core::arch::x86_64::__m128i {
    (**self).eq_any_mask_sse2(chunk)
  }

  #[cfg(target_arch = "x86_64")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn eq_any_mask_avx2(&self, chunk: core::arch::x86_64::__m256i) -> core::arch::x86_64::__m256i {
    (**self).eq_any_mask_avx2(chunk)
  }

  #[cfg(target_arch = "x86_64")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn eq_any_mask_avx512(&self, chunk: core::arch::x86_64::__m512i) -> u64 {
    (**self).eq_any_mask_avx512(chunk)
  }

  #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn eq_any_mask_simd128(&self, chunk: core::arch::wasm32::v128) -> core::arch::wasm32::v128 {
    (**self).eq_any_mask_simd128(chunk)
  }
}

impl Needles for u8 {
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn needle_count(&self) -> usize {
    1
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn tail_find(&self, tail: &[u8]) -> Option<usize> {
    tail_find1(tail, *self)
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn prefix_len(&self, input: &[u8]) -> usize {
    prefix_len1(input, *self)
  }

  #[cfg(target_arch = "aarch64")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn eq_any_mask_neon(
    &self,
    chunk: core::arch::aarch64::uint8x16_t,
  ) -> core::arch::aarch64::uint8x16_t {
    let target = unsafe { core::arch::aarch64::vdupq_n_u8(*self) };
    unsafe { core::arch::aarch64::vceqq_u8(chunk, target) }
  }

  #[cfg(target_arch = "x86")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn eq_any_mask_sse2(&self, chunk: core::arch::x86::__m128i) -> core::arch::x86::__m128i {
    unsafe {
      use core::arch::x86::*;
      _mm_cmpeq_epi8(chunk, _mm_set1_epi8(*self as i8))
    }
  }

  #[cfg(target_arch = "x86_64")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn eq_any_mask_sse2(&self, chunk: core::arch::x86_64::__m128i) -> core::arch::x86_64::__m128i {
    unsafe {
      use core::arch::x86_64::*;
      _mm_cmpeq_epi8(chunk, _mm_set1_epi8(*self as i8))
    }
  }

  #[cfg(target_arch = "x86_64")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn eq_any_mask_avx2(&self, chunk: core::arch::x86_64::__m256i) -> core::arch::x86_64::__m256i {
    unsafe {
      use core::arch::x86_64::*;
      _mm256_cmpeq_epi8(chunk, _mm256_set1_epi8(*self as i8))
    }
  }

  #[cfg(target_arch = "x86_64")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn eq_any_mask_avx512(&self, chunk: core::arch::x86_64::__m512i) -> u64 {
    unsafe {
      use core::arch::x86_64::*;
      _mm512_cmpeq_epi8_mask(chunk, _mm512_set1_epi8(*self as i8))
    }
  }

  #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn eq_any_mask_simd128(&self, chunk: core::arch::wasm32::v128) -> core::arch::wasm32::v128 {
    use core::arch::wasm32::*;
    i8x16_eq(chunk, i8x16_splat(*self as i8))
  }
}

impl Needles for [u8] {
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn needle_count(&self) -> usize {
    self.len()
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn tail_find(&self, tail: &[u8]) -> Option<usize> {
    match self {
      [] => None,
      [a] => tail_find1(tail, *a),
      [a, b] => tail_find2(tail, *a, *b),
      [a, b, c] => tail_find3(tail, *a, *b, *c),
      [a, b, c, d] => tail_find4(tail, *a, *b, *c, *d),
      [a, b, c, d, e] => tail_find5(tail, *a, *b, *c, *d, *e),
      [a, b, c, d, e, f] => tail_find6(tail, *a, *b, *c, *d, *e, *f),
      [a, b, c, d, e, f, g] => tail_find7(tail, *a, *b, *c, *d, *e, *f, *g),
      [a, b, c, d, e, f, g, h] => tail_find8(tail, *a, *b, *c, *d, *e, *f, *g, *h),
      _ => tail.iter().position(|byte| self.contains(byte)),
    }
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn prefix_len(&self, input: &[u8]) -> usize {
    match self {
      [] => 0,
      [a] => prefix_len1(input, *a),
      [a, b] => prefix_len2(input, *a, *b),
      [a, b, c] => prefix_len3(input, *a, *b, *c),
      [a, b, c, d] => prefix_len4(input, *a, *b, *c, *d),
      [a, b, c, d, e] => prefix_len5(input, *a, *b, *c, *d, *e),
      [a, b, c, d, e, f] => prefix_len6(input, *a, *b, *c, *d, *e, *f),
      [a, b, c, d, e, f, g] => prefix_len7(input, *a, *b, *c, *d, *e, *f, *g),
      [a, b, c, d, e, f, g, h] => prefix_len8(input, *a, *b, *c, *d, *e, *f, *g, *h),
      _ => input
        .iter()
        .position(|byte| !self.contains(byte))
        .unwrap_or(input.len()),
    }
  }

  #[cfg(target_arch = "aarch64")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn eq_any_mask_neon(
    &self,
    chunk: core::arch::aarch64::uint8x16_t,
  ) -> core::arch::aarch64::uint8x16_t {
    match self {
      [] => arch::aarch64::eq_any_mask_const(chunk, []),
      [a] => arch::aarch64::eq_any_mask_const(chunk, [*a]),
      [a, b] => arch::aarch64::eq_any_mask_const(chunk, [*a, *b]),
      [a, b, c] => arch::aarch64::eq_any_mask_const(chunk, [*a, *b, *c]),
      [a, b, c, d] => arch::aarch64::eq_any_mask_const(chunk, [*a, *b, *c, *d]),
      [a, b, c, d, e] => arch::aarch64::eq_any_mask_const(chunk, [*a, *b, *c, *d, *e]),
      [a, b, c, d, e, f] => arch::aarch64::eq_any_mask_const(chunk, [*a, *b, *c, *d, *e, *f]),
      [a, b, c, d, e, f, g] => {
        arch::aarch64::eq_any_mask_const(chunk, [*a, *b, *c, *d, *e, *f, *g])
      }
      [a, b, c, d, e, f, g, h] => {
        arch::aarch64::eq_any_mask_const(chunk, [*a, *b, *c, *d, *e, *f, *g, *h])
      }
      _ => arch::aarch64::eq_any_mask_dynamic(chunk, self),
    }
  }

  #[cfg(target_arch = "x86")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn eq_any_mask_sse2(&self, chunk: core::arch::x86::__m128i) -> core::arch::x86::__m128i {
    match self {
      [] => unsafe { core::arch::x86::_mm_setzero_si128() },
      [a] => unsafe { arch::x86::eq_any_mask_const_sse2(chunk, [*a]) },
      [a, b] => unsafe { arch::x86::eq_any_mask_const_sse2(chunk, [*a, *b]) },
      [a, b, c] => unsafe { arch::x86::eq_any_mask_const_sse2(chunk, [*a, *b, *c]) },
      [a, b, c, d] => unsafe { arch::x86::eq_any_mask_const_sse2(chunk, [*a, *b, *c, *d]) },
      [a, b, c, d, e] => unsafe { arch::x86::eq_any_mask_const_sse2(chunk, [*a, *b, *c, *d, *e]) },
      [a, b, c, d, e, f] => unsafe {
        arch::x86::eq_any_mask_const_sse2(chunk, [*a, *b, *c, *d, *e, *f])
      },
      [a, b, c, d, e, f, g] => unsafe {
        arch::x86::eq_any_mask_const_sse2(chunk, [*a, *b, *c, *d, *e, *f, *g])
      },
      [a, b, c, d, e, f, g, h] => unsafe {
        arch::x86::eq_any_mask_const_sse2(chunk, [*a, *b, *c, *d, *e, *f, *g, *h])
      },
      _ => unsafe { arch::x86::eq_any_mask_dynamic_sse2(chunk, self) },
    }
  }

  #[cfg(target_arch = "x86_64")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn eq_any_mask_sse2(&self, chunk: core::arch::x86_64::__m128i) -> core::arch::x86_64::__m128i {
    match self {
      [] => unsafe { core::arch::x86_64::_mm_setzero_si128() },
      [a] => unsafe { arch::x86::eq_any_mask_const_sse2(chunk, [*a]) },
      [a, b] => unsafe { arch::x86::eq_any_mask_const_sse2(chunk, [*a, *b]) },
      [a, b, c] => unsafe { arch::x86::eq_any_mask_const_sse2(chunk, [*a, *b, *c]) },
      [a, b, c, d] => unsafe { arch::x86::eq_any_mask_const_sse2(chunk, [*a, *b, *c, *d]) },
      [a, b, c, d, e] => unsafe { arch::x86::eq_any_mask_const_sse2(chunk, [*a, *b, *c, *d, *e]) },
      [a, b, c, d, e, f] => unsafe {
        arch::x86::eq_any_mask_const_sse2(chunk, [*a, *b, *c, *d, *e, *f])
      },
      [a, b, c, d, e, f, g] => unsafe {
        arch::x86::eq_any_mask_const_sse2(chunk, [*a, *b, *c, *d, *e, *f, *g])
      },
      [a, b, c, d, e, f, g, h] => unsafe {
        arch::x86::eq_any_mask_const_sse2(chunk, [*a, *b, *c, *d, *e, *f, *g, *h])
      },
      _ => unsafe { arch::x86::eq_any_mask_dynamic_sse2(chunk, self) },
    }
  }

  #[cfg(target_arch = "x86_64")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn eq_any_mask_avx2(&self, chunk: core::arch::x86_64::__m256i) -> core::arch::x86_64::__m256i {
    match self {
      [] => unsafe { core::arch::x86_64::_mm256_setzero_si256() },
      [a] => unsafe { arch::x86::eq_any_mask_const_avx2(chunk, [*a]) },
      [a, b] => unsafe { arch::x86::eq_any_mask_const_avx2(chunk, [*a, *b]) },
      [a, b, c] => unsafe { arch::x86::eq_any_mask_const_avx2(chunk, [*a, *b, *c]) },
      [a, b, c, d] => unsafe { arch::x86::eq_any_mask_const_avx2(chunk, [*a, *b, *c, *d]) },
      [a, b, c, d, e] => unsafe { arch::x86::eq_any_mask_const_avx2(chunk, [*a, *b, *c, *d, *e]) },
      [a, b, c, d, e, f] => unsafe {
        arch::x86::eq_any_mask_const_avx2(chunk, [*a, *b, *c, *d, *e, *f])
      },
      [a, b, c, d, e, f, g] => unsafe {
        arch::x86::eq_any_mask_const_avx2(chunk, [*a, *b, *c, *d, *e, *f, *g])
      },
      [a, b, c, d, e, f, g, h] => unsafe {
        arch::x86::eq_any_mask_const_avx2(chunk, [*a, *b, *c, *d, *e, *f, *g, *h])
      },
      _ => unsafe { arch::x86::eq_any_mask_dynamic_avx2(chunk, self) },
    }
  }

  #[cfg(target_arch = "x86_64")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn eq_any_mask_avx512(&self, chunk: core::arch::x86_64::__m512i) -> u64 {
    match self {
      [] => 0u64,
      [a] => unsafe { arch::x86::eq_any_mask_const_avx512(chunk, [*a]) },
      [a, b] => unsafe { arch::x86::eq_any_mask_const_avx512(chunk, [*a, *b]) },
      [a, b, c] => unsafe { arch::x86::eq_any_mask_const_avx512(chunk, [*a, *b, *c]) },
      [a, b, c, d] => unsafe { arch::x86::eq_any_mask_const_avx512(chunk, [*a, *b, *c, *d]) },
      [a, b, c, d, e] => unsafe {
        arch::x86::eq_any_mask_const_avx512(chunk, [*a, *b, *c, *d, *e])
      },
      [a, b, c, d, e, f] => unsafe {
        arch::x86::eq_any_mask_const_avx512(chunk, [*a, *b, *c, *d, *e, *f])
      },
      [a, b, c, d, e, f, g] => unsafe {
        arch::x86::eq_any_mask_const_avx512(chunk, [*a, *b, *c, *d, *e, *f, *g])
      },
      [a, b, c, d, e, f, g, h] => unsafe {
        arch::x86::eq_any_mask_const_avx512(chunk, [*a, *b, *c, *d, *e, *f, *g, *h])
      },
      _ => unsafe { arch::x86::eq_any_mask_dynamic_avx512(chunk, self) },
    }
  }

  #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn eq_any_mask_simd128(&self, chunk: core::arch::wasm32::v128) -> core::arch::wasm32::v128 {
    match self {
      [] => arch::wasm32::eq_any_mask_const_simd128(chunk, []),
      [a] => arch::wasm32::eq_any_mask_const_simd128(chunk, [*a]),
      [a, b] => arch::wasm32::eq_any_mask_const_simd128(chunk, [*a, *b]),
      [a, b, c] => arch::wasm32::eq_any_mask_const_simd128(chunk, [*a, *b, *c]),
      [a, b, c, d] => arch::wasm32::eq_any_mask_const_simd128(chunk, [*a, *b, *c, *d]),
      [a, b, c, d, e] => arch::wasm32::eq_any_mask_const_simd128(chunk, [*a, *b, *c, *d, *e]),
      [a, b, c, d, e, f] => {
        arch::wasm32::eq_any_mask_const_simd128(chunk, [*a, *b, *c, *d, *e, *f])
      }
      [a, b, c, d, e, f, g] => {
        arch::wasm32::eq_any_mask_const_simd128(chunk, [*a, *b, *c, *d, *e, *f, *g])
      }
      [a, b, c, d, e, f, g, h] => {
        arch::wasm32::eq_any_mask_const_simd128(chunk, [*a, *b, *c, *d, *e, *f, *g, *h])
      }
      _ => arch::wasm32::eq_any_mask_dynamic_simd128(chunk, self),
    }
  }
}

impl<const N: usize> Needles for [u8; N] {
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn needle_count(&self) -> usize {
    N
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn tail_find(&self, tail: &[u8]) -> Option<usize> {
    match self.as_slice() {
      [] => None,
      [a] => tail_find1(tail, *a),
      [a, b] => tail_find2(tail, *a, *b),
      [a, b, c] => tail_find3(tail, *a, *b, *c),
      [a, b, c, d] => tail_find4(tail, *a, *b, *c, *d),
      [a, b, c, d, e] => tail_find5(tail, *a, *b, *c, *d, *e),
      [a, b, c, d, e, f] => tail_find6(tail, *a, *b, *c, *d, *e, *f),
      [a, b, c, d, e, f, g] => tail_find7(tail, *a, *b, *c, *d, *e, *f, *g),
      [a, b, c, d, e, f, g, h] => tail_find8(tail, *a, *b, *c, *d, *e, *f, *g, *h),
      _ => tail.iter().position(|byte| self.contains(byte)),
    }
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn prefix_len(&self, input: &[u8]) -> usize {
    match self.as_slice() {
      [] => 0,
      [a] => prefix_len1(input, *a),
      [a, b] => prefix_len2(input, *a, *b),
      [a, b, c] => prefix_len3(input, *a, *b, *c),
      [a, b, c, d] => prefix_len4(input, *a, *b, *c, *d),
      [a, b, c, d, e] => prefix_len5(input, *a, *b, *c, *d, *e),
      [a, b, c, d, e, f] => prefix_len6(input, *a, *b, *c, *d, *e, *f),
      [a, b, c, d, e, f, g] => prefix_len7(input, *a, *b, *c, *d, *e, *f, *g),
      [a, b, c, d, e, f, g, h] => prefix_len8(input, *a, *b, *c, *d, *e, *f, *g, *h),
      _ => input
        .iter()
        .position(|byte| !self.contains(byte))
        .unwrap_or(input.len()),
    }
  }

  #[cfg(target_arch = "aarch64")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn eq_any_mask_neon(
    &self,
    chunk: core::arch::aarch64::uint8x16_t,
  ) -> core::arch::aarch64::uint8x16_t {
    arch::aarch64::eq_any_mask_const(chunk, *self)
  }

  #[cfg(target_arch = "x86")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn eq_any_mask_sse2(&self, chunk: core::arch::x86::__m128i) -> core::arch::x86::__m128i {
    unsafe { arch::x86::eq_any_mask_const_sse2(chunk, *self) }
  }

  #[cfg(target_arch = "x86_64")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn eq_any_mask_sse2(&self, chunk: core::arch::x86_64::__m128i) -> core::arch::x86_64::__m128i {
    unsafe { arch::x86::eq_any_mask_const_sse2(chunk, *self) }
  }

  #[cfg(target_arch = "x86_64")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn eq_any_mask_avx2(&self, chunk: core::arch::x86_64::__m256i) -> core::arch::x86_64::__m256i {
    unsafe { arch::x86::eq_any_mask_const_avx2(chunk, *self) }
  }

  #[cfg(target_arch = "x86_64")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn eq_any_mask_avx512(&self, chunk: core::arch::x86_64::__m512i) -> u64 {
    unsafe { arch::x86::eq_any_mask_const_avx512(chunk, *self) }
  }

  #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn eq_any_mask_simd128(&self, chunk: core::arch::wasm32::v128) -> core::arch::wasm32::v128 {
    arch::wasm32::eq_any_mask_const_simd128(chunk, *self)
  }
}