nosj 0.2.2

Pull, push and fused-cursor JSON parsers plus a streaming writer, for hosts that build native values during the parse — SIMD-accelerated (NEON, SSE2, runtime-detected AVX2)
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
//! Shared byte-scanning primitives: the "does this byte need attention"
//! predicate in every form the crate needs: per-architecture SIMD hit
//! masks, a SWAR mask for 8-byte steps, a per-byte class table for scalar
//! tails, and the page-boundary guard that makes overreading vector tails
//! safe.
//!
//! Both the parser's string scan ([`crate::scalars`]) and the emitter's
//! escape scan ([`crate::emit`]) classify against the same base set
//! (`"`, `\`, and control characters below 0x20), so the predicate lives
//! here exactly once per architecture. The emitter adds two optional sets
//! via the `MODE` const parameter; the parser always scans
//! `MODE_STANDARD`.

// Const-generic discriminants for [`crate::emit::EscapeMode`]: stable Rust
// only allows primitive const-generic parameters, so the public enum is
// lowered to these at the dispatch point in `emit::escape_into`. Replace
// with `const MODE: EscapeMode` once `adt_const_params` stabilizes.
pub(crate) const MODE_STANDARD: u8 = 0;
pub(crate) const MODE_SCRIPT_SAFE: u8 = 1;
pub(crate) const MODE_ASCII_ONLY: u8 = 2;
pub(crate) const MODE_HTML_SAFE: u8 = 3;
pub(crate) const MODE_HTML_ENTITIES: u8 = 4;
pub(crate) const MODE_JS_SEPARATORS: u8 = 5;

// U+2028 LINE SEPARATOR and U+2029 PARAGRAPH SEPARATOR (the JS line
// separators) encode in UTF-8 as E2 80 A8 and E2 80 A9. The modes that
// escape them scan for the shared lead byte and inspect the two
// continuation bytes at escape time.
pub(crate) const JS_SEP_LEAD: u8 = 0xE2;
pub(crate) const JS_SEP_CONT: u8 = 0x80;
pub(crate) const JS_SEP_LAST_2028: u8 = 0xA8;
pub(crate) const JS_SEP_LAST_2029: u8 = 0xA9;

// Per-byte class bits in [`ESCAPE_CLASS`].
pub(crate) const CLASS_STANDARD: u8 = 1 << 0;
pub(crate) const CLASS_SCRIPT_SAFE_EXTRA: u8 = 1 << 1;
pub(crate) const CLASS_ASCII_ONLY_EXTRA: u8 = 1 << 2;
pub(crate) const CLASS_HTML_SAFE_EXTRA: u8 = 1 << 3;
pub(crate) const CLASS_HTML_ENTITIES_EXTRA: u8 = 1 << 4;
pub(crate) const CLASS_JS_SEPARATORS_EXTRA: u8 = 1 << 5;

/// The [`ESCAPE_CLASS`] bits that need attention under `MODE`.
pub(crate) const fn class_bits<const MODE: u8>() -> u8 {
    match MODE {
        MODE_SCRIPT_SAFE => CLASS_STANDARD | CLASS_SCRIPT_SAFE_EXTRA,
        MODE_ASCII_ONLY => CLASS_STANDARD | CLASS_ASCII_ONLY_EXTRA,
        MODE_HTML_SAFE => CLASS_STANDARD | CLASS_HTML_SAFE_EXTRA,
        MODE_HTML_ENTITIES => CLASS_STANDARD | CLASS_HTML_ENTITIES_EXTRA,
        MODE_JS_SEPARATORS => CLASS_STANDARD | CLASS_JS_SEPARATORS_EXTRA,
        _ => CLASS_STANDARD,
    }
}

/// Per-byte `CLASS_*` membership bits. One table load replaces five
/// compares on the scalar path.
pub(crate) static ESCAPE_CLASS: [u8; 256] = {
    let mut t = [0u8; 256];
    let mut i = 0;
    while i < 256 {
        let b = i as u8;
        if b == b'"' || b == b'\\' || b < 0x20 {
            t[i] |= CLASS_STANDARD;
        }
        if b == b'/' || b == JS_SEP_LEAD {
            t[i] |= CLASS_SCRIPT_SAFE_EXTRA;
        }
        if b >= 0x80 {
            t[i] |= CLASS_ASCII_ONLY_EXTRA;
        }
        if b == b'<' || b == b'>' || b == b'&' || b == JS_SEP_LEAD {
            t[i] |= CLASS_HTML_SAFE_EXTRA;
        }
        if b == b'<' || b == b'>' || b == b'&' {
            t[i] |= CLASS_HTML_ENTITIES_EXTRA;
        }
        if b == JS_SEP_LEAD {
            t[i] |= CLASS_JS_SEPARATORS_EXTRA;
        }
        i += 1;
    }
    t
};

/// Copy `n <= 32` bytes with a pair of overlapping word moves instead of a
/// `memcpy` call: tiny-copy call overhead is measurable on short-string
/// workloads (14% of a string-heavy generation profile; see
/// [`crate::emit::push_short`]).
///
/// # Safety
///
/// `n` readable bytes at `src`, `n` writable bytes at `dst`, and the two
/// ranges must not overlap.
#[inline(always)]
pub(crate) unsafe fn copy_small(src: *const u8, dst: *mut u8, n: usize) {
    debug_assert!(n <= 32);
    /// One overlapping pair: word-size bytes at offset 0 and at `n - size`
    /// cover `size..=2*size` bytes; the overlapped middle is written twice
    /// with identical data.
    macro_rules! word_pair {
        ($t:ty) => {{
            const SIZE: usize = size_of::<$t>();
            dst.cast::<$t>()
                .write_unaligned(src.cast::<$t>().read_unaligned());
            dst.add(n - SIZE)
                .cast::<$t>()
                .write_unaligned(src.add(n - SIZE).cast::<$t>().read_unaligned());
        }};
    }
    // SAFETY: each taken arm's word size was just tested `<= n`, so every
    // access lands within the caller-guaranteed `n` bytes at each pointer.
    // All accesses are explicitly unaligned.
    unsafe {
        if n >= 16 {
            word_pair!(u128);
        } else if n >= 8 {
            word_pair!(u64);
        } else if n >= 4 {
            word_pair!(u32);
        } else if n >= 2 {
            word_pair!(u16);
        } else if n == 1 {
            dst.write(src.read());
        }
    }
}

/// Stage a partial final chunk (`tail.len() < W`) in a zero-padded stack
/// buffer so full-width vector loads never read past the source
/// allocation. Reading past a slice's end is undefined behavior in Rust's
/// allocation model even when it cannot fault at the hardware level (the
/// page-guard trick this replaced), so tails load from this copy instead.
///
/// Zero padding classifies as a control character in every scan mode, so
/// callers must truncate result masks to the real remainder, which they
/// already do.
#[inline(always)]
pub(crate) fn padded_tail<const W: usize>(tail: &[u8]) -> [u8; W] {
    debug_assert!(tail.len() < W);
    let mut buf = [0u8; W];
    // SAFETY: `tail.len()` readable bytes at the slice's pointer,
    // `tail.len() < W` writable bytes in `buf`; distinct allocations.
    unsafe { copy_small(tail.as_ptr(), buf.as_mut_ptr(), tail.len()) };
    buf
}

/// Byte-parallel needs-attention test over 8 bytes: the escape scan for
/// hosts without SIMD, and the tests' cross-check reference. Both
/// detectors are the exact bit-hack forms (no false positives):
/// `hasvalue` for equality, `hasless(n<=128)` for the control range. One
/// bit set in the high bit of each matching byte; position is
/// `trailing_zeros() >> 3`.
#[cfg(any(test, not(any(target_arch = "aarch64", target_arch = "x86_64"))))]
#[inline(always)]
pub(crate) fn swar_escape_mask<const MODE: u8>(w: u64) -> u64 {
    const LO: u64 = 0x0101_0101_0101_0101;
    const HI: u64 = 0x8080_8080_8080_8080;
    #[inline(always)]
    fn eq(w: u64, b: u8) -> u64 {
        let v = w ^ (LO * u64::from(b));
        v.wrapping_sub(LO) & !v & HI
    }
    let mut m = eq(w, b'"') | eq(w, b'\\') | (w.wrapping_sub(LO * 0x20) & !w & HI);
    if MODE == MODE_SCRIPT_SAFE {
        m |= eq(w, b'/') | eq(w, JS_SEP_LEAD);
    }
    if MODE == MODE_ASCII_ONLY {
        m |= w & HI;
    }
    if MODE == MODE_HTML_SAFE {
        m |= eq(w, b'<') | eq(w, b'>') | eq(w, b'&') | eq(w, JS_SEP_LEAD);
    }
    if MODE == MODE_HTML_ENTITIES {
        m |= eq(w, b'<') | eq(w, b'>') | eq(w, b'&');
    }
    if MODE == MODE_JS_SEPARATORS {
        m |= eq(w, JS_SEP_LEAD);
    }
    m
}

#[cfg(target_arch = "aarch64")]
pub(crate) mod neon {
    use super::{
        JS_SEP_LEAD, MODE_ASCII_ONLY, MODE_HTML_ENTITIES, MODE_HTML_SAFE, MODE_JS_SEPARATORS,
        MODE_SCRIPT_SAFE,
    };
    use std::arch::aarch64::{
        uint8x16_t, vceqq_u8, vcgeq_u8, vcltq_u8, vdupq_n_u8, vget_lane_u64, vld1q_u8, vorrq_u8,
        vreinterpret_u64_u8, vreinterpretq_u16_u8, vshrn_n_u16, vst1q_u8,
    };

    /// Per-lane needs-attention flags (all-ones lanes) for 16 bytes.
    /// Kept separate from [`nib_mask`] so callers scanning wide can OR two
    /// hit vectors and early-exit on `vmaxvq_u8 == 0` (one instruction)
    /// before paying for any mask extraction.
    #[inline(always)]
    pub(crate) unsafe fn hit_vec<const MODE: u8>(v: uint8x16_t) -> uint8x16_t {
        // SAFETY: register-only NEON operations on an already-loaded
        // vector; NEON is baseline on aarch64.
        unsafe {
            let mut hit = vorrq_u8(
                vorrq_u8(
                    vceqq_u8(v, vdupq_n_u8(b'"')),
                    vceqq_u8(v, vdupq_n_u8(b'\\')),
                ),
                vcltq_u8(v, vdupq_n_u8(0x20)),
            );
            if MODE == MODE_SCRIPT_SAFE {
                hit = vorrq_u8(
                    hit,
                    vorrq_u8(
                        vceqq_u8(v, vdupq_n_u8(b'/')),
                        vceqq_u8(v, vdupq_n_u8(JS_SEP_LEAD)),
                    ),
                );
            }
            if MODE == MODE_ASCII_ONLY {
                hit = vorrq_u8(hit, vcgeq_u8(v, vdupq_n_u8(0x80)));
            }
            if MODE == MODE_HTML_SAFE {
                hit = vorrq_u8(
                    hit,
                    vorrq_u8(
                        vorrq_u8(vceqq_u8(v, vdupq_n_u8(b'<')), vceqq_u8(v, vdupq_n_u8(b'>'))),
                        vorrq_u8(
                            vceqq_u8(v, vdupq_n_u8(b'&')),
                            vceqq_u8(v, vdupq_n_u8(JS_SEP_LEAD)),
                        ),
                    ),
                );
            }
            if MODE == MODE_HTML_ENTITIES {
                hit = vorrq_u8(
                    hit,
                    vorrq_u8(
                        vorrq_u8(vceqq_u8(v, vdupq_n_u8(b'<')), vceqq_u8(v, vdupq_n_u8(b'>'))),
                        vceqq_u8(v, vdupq_n_u8(b'&')),
                    ),
                );
            }
            if MODE == MODE_JS_SEPARATORS {
                hit = vorrq_u8(hit, vceqq_u8(v, vdupq_n_u8(JS_SEP_LEAD)));
            }
            hit
        }
    }

    /// NEON has no movemask; the standard substitute narrows each 16-bit
    /// pair to its middle 4 bits (`vshrn` by 4), yielding a u64 with 4 bits
    /// per lane. Position of the first hit: `trailing_zeros() / 4`.
    #[inline(always)]
    pub(crate) unsafe fn nib_mask(hit: uint8x16_t) -> u64 {
        // SAFETY: register-only NEON operations; NEON is baseline on
        // aarch64.
        unsafe {
            vget_lane_u64::<0>(vreinterpret_u64_u8(vshrn_n_u16::<4>(vreinterpretq_u16_u8(
                hit,
            ))))
        }
    }

    /// [`hit_vec`] + [`nib_mask`] in one call, for 16-byte-at-a-time loops.
    #[inline(always)]
    pub(crate) unsafe fn hit_mask<const MODE: u8>(v: uint8x16_t) -> u64 {
        // SAFETY: register-only composition of the two functions above.
        unsafe { nib_mask(hit_vec::<MODE>(v)) }
    }

    /// The fused scan+store step: load 16 bytes from `src`, store them to
    /// `dst`, return the needs-attention mask (4 bits per lane). Shared by
    /// the emitter's fused escape loop and the parser's fused decode.
    ///
    /// # Safety
    ///
    /// 16 readable bytes at `src`, 16 writable bytes at `dst`.
    #[inline(always)]
    pub(crate) unsafe fn copy_scan<const MODE: u8>(src: *const u8, dst: *mut u8) -> u64 {
        // SAFETY: per this function's contract.
        unsafe {
            let v = vld1q_u8(src);
            vst1q_u8(dst, v);
            hit_mask::<MODE>(v)
        }
    }

    /// First needs-attention byte in the tail `[from, input.len())`, or
    /// `None`. Loads the buffer's LAST 16 bytes (fully in bounds,
    /// overlapping bytes before `from` the caller already scanned clean)
    /// and drops mask lanes before `from`, so a stale hit in the overlap
    /// can never surface.
    #[inline(always)]
    pub(crate) fn tail_find<const MODE: u8>(input: &[u8], from: usize) -> Option<usize> {
        let back = input.len().checked_sub(16)?;
        // Whole-vector remainders belong in the caller's main loop; the
        // lane shift below relies on `0 < from - back <= 15`.
        debug_assert!(from > back && from < input.len());
        // SAFETY: `back + 16 == input.len()`, so the load is entirely
        // inside `input`.
        let mask = unsafe { hit_mask::<MODE>(vld1q_u8(input.as_ptr().add(back))) }
            & (u64::MAX << (4 * (from - back)));
        if mask == 0 {
            None
        } else {
            Some(back + (mask.trailing_zeros() as usize) / 4)
        }
    }
}

#[cfg(target_arch = "x86_64")]
pub(crate) mod x86 {
    use super::{
        JS_SEP_LEAD, MODE_ASCII_ONLY, MODE_HTML_ENTITIES, MODE_HTML_SAFE, MODE_JS_SEPARATORS,
        MODE_SCRIPT_SAFE,
    };
    use std::arch::x86_64::{
        __m128i, __m256i, __m512i, _mm_cmpeq_epi8, _mm_loadu_si128, _mm_min_epu8,
        _mm_movemask_epi8, _mm_or_si128, _mm_set1_epi8, _mm_storeu_si128, _mm256_cmpeq_epi8,
        _mm256_loadu_si256, _mm256_min_epu8, _mm256_movemask_epi8, _mm256_or_si256,
        _mm256_set1_epi8, _mm256_storeu_si256, _mm512_cmpeq_epi8_mask, _mm512_cmplt_epu8_mask,
        _mm512_loadu_si512, _mm512_movepi8_mask, _mm512_set1_epi8, _mm512_storeu_si512,
    };

    /// Needs-attention movemask for 16 bytes (1 bit per byte).
    #[inline(always)]
    pub(crate) unsafe fn sse2_hit_mask<const MODE: u8>(v: __m128i) -> u32 {
        // SAFETY: register-only SSE2 operations on an already-loaded
        // vector; SSE2 is baseline on x86_64.
        unsafe {
            // Unsigned v <= 0x1F via min(v, 0x1F) == v.
            let is_ctrl = _mm_cmpeq_epi8(_mm_min_epu8(v, _mm_set1_epi8(0x1F)), v);
            let mut hit = _mm_or_si128(
                _mm_or_si128(
                    _mm_cmpeq_epi8(v, _mm_set1_epi8(b'"' as i8)),
                    _mm_cmpeq_epi8(v, _mm_set1_epi8(b'\\' as i8)),
                ),
                is_ctrl,
            );
            if MODE == MODE_SCRIPT_SAFE {
                hit = _mm_or_si128(
                    hit,
                    _mm_or_si128(
                        _mm_cmpeq_epi8(v, _mm_set1_epi8(b'/' as i8)),
                        _mm_cmpeq_epi8(v, _mm_set1_epi8(JS_SEP_LEAD as i8)),
                    ),
                );
            }
            if MODE == MODE_HTML_SAFE {
                hit = _mm_or_si128(
                    hit,
                    _mm_or_si128(
                        _mm_or_si128(
                            _mm_cmpeq_epi8(v, _mm_set1_epi8(b'<' as i8)),
                            _mm_cmpeq_epi8(v, _mm_set1_epi8(b'>' as i8)),
                        ),
                        _mm_or_si128(
                            _mm_cmpeq_epi8(v, _mm_set1_epi8(b'&' as i8)),
                            _mm_cmpeq_epi8(v, _mm_set1_epi8(JS_SEP_LEAD as i8)),
                        ),
                    ),
                );
            }
            if MODE == MODE_HTML_ENTITIES {
                hit = _mm_or_si128(
                    hit,
                    _mm_or_si128(
                        _mm_or_si128(
                            _mm_cmpeq_epi8(v, _mm_set1_epi8(b'<' as i8)),
                            _mm_cmpeq_epi8(v, _mm_set1_epi8(b'>' as i8)),
                        ),
                        _mm_cmpeq_epi8(v, _mm_set1_epi8(b'&' as i8)),
                    ),
                );
            }
            if MODE == MODE_JS_SEPARATORS {
                hit = _mm_or_si128(hit, _mm_cmpeq_epi8(v, _mm_set1_epi8(JS_SEP_LEAD as i8)));
            }
            let mut mask = _mm_movemask_epi8(hit) as u32;
            if MODE == MODE_ASCII_ONLY {
                // The sign bit doubles as the >= 0x80 test.
                mask |= _mm_movemask_epi8(v) as u32;
            }
            mask
        }
    }

    /// Needs-attention movemask for 32 bytes. Callers must have verified
    /// AVX2 at runtime (universal on production x86 since Haswell/Zen 1).
    #[target_feature(enable = "avx2")]
    #[inline]
    pub(crate) unsafe fn avx2_hit_mask<const MODE: u8>(v: __m256i) -> u32 {
        let is_ctrl = _mm256_cmpeq_epi8(_mm256_min_epu8(v, _mm256_set1_epi8(0x1F)), v);
        let mut hit = _mm256_or_si256(
            _mm256_or_si256(
                _mm256_cmpeq_epi8(v, _mm256_set1_epi8(b'"' as i8)),
                _mm256_cmpeq_epi8(v, _mm256_set1_epi8(b'\\' as i8)),
            ),
            is_ctrl,
        );
        if MODE == MODE_SCRIPT_SAFE {
            hit = _mm256_or_si256(
                hit,
                _mm256_or_si256(
                    _mm256_cmpeq_epi8(v, _mm256_set1_epi8(b'/' as i8)),
                    _mm256_cmpeq_epi8(v, _mm256_set1_epi8(JS_SEP_LEAD as i8)),
                ),
            );
        }
        if MODE == MODE_HTML_SAFE {
            hit = _mm256_or_si256(
                hit,
                _mm256_or_si256(
                    _mm256_or_si256(
                        _mm256_cmpeq_epi8(v, _mm256_set1_epi8(b'<' as i8)),
                        _mm256_cmpeq_epi8(v, _mm256_set1_epi8(b'>' as i8)),
                    ),
                    _mm256_or_si256(
                        _mm256_cmpeq_epi8(v, _mm256_set1_epi8(b'&' as i8)),
                        _mm256_cmpeq_epi8(v, _mm256_set1_epi8(JS_SEP_LEAD as i8)),
                    ),
                ),
            );
        }
        if MODE == MODE_HTML_ENTITIES {
            hit = _mm256_or_si256(
                hit,
                _mm256_or_si256(
                    _mm256_or_si256(
                        _mm256_cmpeq_epi8(v, _mm256_set1_epi8(b'<' as i8)),
                        _mm256_cmpeq_epi8(v, _mm256_set1_epi8(b'>' as i8)),
                    ),
                    _mm256_cmpeq_epi8(v, _mm256_set1_epi8(b'&' as i8)),
                ),
            );
        }
        if MODE == MODE_JS_SEPARATORS {
            hit = _mm256_or_si256(
                hit,
                _mm256_cmpeq_epi8(v, _mm256_set1_epi8(JS_SEP_LEAD as i8)),
            );
        }
        let mut mask = _mm256_movemask_epi8(hit) as u32;
        if MODE == MODE_ASCII_ONLY {
            mask |= _mm256_movemask_epi8(v) as u32;
        }
        mask
    }

    /// The fused scan+store step: load 16 bytes from `src`, store them to
    /// `dst`, return the needs-attention mask (1 bit per byte). Shared by
    /// the emitter's fused escape loop and the parser's fused decode.
    ///
    /// # Safety
    ///
    /// 16 readable bytes at `src`, 16 writable bytes at `dst`.
    #[inline(always)]
    pub(crate) unsafe fn sse2_copy_scan<const MODE: u8>(src: *const u8, dst: *mut u8) -> u64 {
        // SAFETY: per this function's contract.
        unsafe {
            let v = _mm_loadu_si128(src.cast());
            _mm_storeu_si128(dst.cast(), v);
            u64::from(sse2_hit_mask::<MODE>(v))
        }
    }

    /// AVX2 [`sse2_copy_scan`]: 32 bytes per step.
    ///
    /// # Safety
    ///
    /// 32 readable bytes at `src`, 32 writable bytes at `dst`; AVX2
    /// verified at runtime by the dispatch site.
    #[target_feature(enable = "avx2")]
    #[inline]
    pub(crate) unsafe fn avx2_copy_scan<const MODE: u8>(src: *const u8, dst: *mut u8) -> u64 {
        // SAFETY: per this function's contract.
        unsafe {
            let v = _mm256_loadu_si256(src.cast());
            _mm256_storeu_si256(dst.cast(), v);
            u64::from(avx2_hit_mask::<MODE>(v))
        }
    }

    /// Needs-attention mask for 64 bytes, straight into a `__mmask64`:
    /// AVX-512BW compares produce mask registers natively, so there is
    /// no movemask step and the unsigned control-byte compare exists
    /// directly. Callers must have verified AVX-512BW at runtime.
    /// Currently undispatched (emission rejected it; see emit.rs);
    /// kept for scan-only consumers.
    #[target_feature(enable = "avx512bw")]
    #[inline]
    #[allow(dead_code)]
    pub(crate) unsafe fn avx512_hit_mask<const MODE: u8>(v: __m512i) -> u64 {
        let mut mask = _mm512_cmpeq_epi8_mask(v, _mm512_set1_epi8(b'"' as i8))
            | _mm512_cmpeq_epi8_mask(v, _mm512_set1_epi8(b'\\' as i8))
            | _mm512_cmplt_epu8_mask(v, _mm512_set1_epi8(0x20));
        if MODE == MODE_SCRIPT_SAFE {
            mask |= _mm512_cmpeq_epi8_mask(v, _mm512_set1_epi8(b'/' as i8))
                | _mm512_cmpeq_epi8_mask(v, _mm512_set1_epi8(JS_SEP_LEAD as i8));
        }
        if MODE == MODE_HTML_SAFE {
            mask |= _mm512_cmpeq_epi8_mask(v, _mm512_set1_epi8(b'<' as i8))
                | _mm512_cmpeq_epi8_mask(v, _mm512_set1_epi8(b'>' as i8))
                | _mm512_cmpeq_epi8_mask(v, _mm512_set1_epi8(b'&' as i8))
                | _mm512_cmpeq_epi8_mask(v, _mm512_set1_epi8(JS_SEP_LEAD as i8));
        }
        if MODE == MODE_HTML_ENTITIES {
            mask |= _mm512_cmpeq_epi8_mask(v, _mm512_set1_epi8(b'<' as i8))
                | _mm512_cmpeq_epi8_mask(v, _mm512_set1_epi8(b'>' as i8))
                | _mm512_cmpeq_epi8_mask(v, _mm512_set1_epi8(b'&' as i8));
        }
        if MODE == MODE_JS_SEPARATORS {
            mask |= _mm512_cmpeq_epi8_mask(v, _mm512_set1_epi8(JS_SEP_LEAD as i8));
        }
        if MODE == MODE_ASCII_ONLY {
            // The sign bit doubles as the >= 0x80 test.
            mask |= _mm512_movepi8_mask(v) as u64;
        }
        mask
    }

    /// AVX-512 [`sse2_copy_scan`]: 64 bytes per step.
    ///
    /// # Safety
    ///
    /// 64 readable bytes at `src`, 64 writable bytes at `dst`;
    /// AVX-512BW verified at runtime by the dispatch site.
    #[target_feature(enable = "avx512bw")]
    #[inline]
    #[allow(dead_code)]
    pub(crate) unsafe fn avx512_copy_scan<const MODE: u8>(src: *const u8, dst: *mut u8) -> u64 {
        // SAFETY: per this function's contract.
        unsafe {
            let v = _mm512_loadu_si512(src.cast());
            _mm512_storeu_si512(dst.cast(), v);
            avx512_hit_mask::<MODE>(v)
        }
    }

    /// First needs-attention byte in the tail `[from, input.len())`, or
    /// `None`. Loads the buffer's LAST 16 bytes (fully in bounds,
    /// overlapping bytes before `from` the caller already scanned clean)
    /// and drops mask lanes before `from`, so a stale hit in the overlap
    /// can never surface.
    #[inline(always)]
    pub(crate) fn sse2_tail_find<const MODE: u8>(input: &[u8], from: usize) -> Option<usize> {
        let back = input.len().checked_sub(16)?;
        // Whole-vector remainders belong in the caller's main loop; the
        // lane shift below relies on `0 < from - back <= 15`.
        debug_assert!(from > back && from < input.len());
        // SAFETY: `back + 16 == input.len()`, so the load is entirely
        // inside `input`.
        let mask =
            unsafe { sse2_hit_mask::<MODE>(_mm_loadu_si128(input.as_ptr().add(back).cast())) }
                & (u32::MAX << (from - back));
        if mask == 0 {
            None
        } else {
            Some(back + mask.trailing_zeros() as usize)
        }
    }

    /// AVX2 [`sse2_tail_find`]: a 32-byte backwards window. AVX2 verified
    /// at runtime by the dispatch site.
    #[target_feature(enable = "avx2")]
    #[inline]
    pub(crate) fn avx2_tail_find<const MODE: u8>(input: &[u8], from: usize) -> Option<usize> {
        let back = input.len().checked_sub(32)?;
        debug_assert!(from > back && from < input.len());
        // SAFETY: `back + 32 == input.len()`, so the load is entirely
        // inside `input`.
        let mask =
            unsafe { avx2_hit_mask::<MODE>(_mm256_loadu_si256(input.as_ptr().add(back).cast())) }
                & (u32::MAX << (from - back));
        if mask == 0 {
            None
        } else {
            Some(back + mask.trailing_zeros() as usize)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// The scalar table is the reference; SWAR must agree byte-for-byte.
    #[test]
    fn swar_agrees_with_class_table() {
        fn check<const MODE: u8>() {
            for b in 0..=255u8 {
                let word = u64::from_le_bytes([b, b'a', b, 0x00, b'z', b, b'a', b]);
                let mask = swar_escape_mask::<MODE>(word);
                for (lane, &byte) in [b, b'a', b, 0x00, b'z', b, b'a', b].iter().enumerate() {
                    let expected = ESCAPE_CLASS[byte as usize] & class_bits::<MODE>() != 0;
                    let got = mask & (0x80 << (8 * lane)) != 0;
                    assert_eq!(got, expected, "mode {MODE} byte {byte:#x} lane {lane}");
                }
            }
        }
        check::<MODE_STANDARD>();
        check::<MODE_SCRIPT_SAFE>();
        check::<MODE_ASCII_ONLY>();
        check::<MODE_HTML_SAFE>();
        check::<MODE_HTML_ENTITIES>();
        check::<MODE_JS_SEPARATORS>();
    }
}