simd-normalizer 0.1.1

SIMD-accelerated Unicode normalization (NFC, NFD, NFKC, NFKD)
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
//! SIMD-accelerated byte scanning and dispatch.
//!
//! This module provides the scanner infrastructure that produces per-chunk
//! bitmasks for the normalizer's main loop. The scanner identifies byte
//! positions >= a passthrough bound, flagging them for scalar normalization.
//!
//! Backends:
//! - `scalar` -- always available, no special CPU features
//! - `x86_64::sse42`, `x86_64::avx2`, `x86_64::avx512`
//! - `aarch64::neon`
//! - `wasm32::simd128`
//!
//! The dispatch layer selects the best backend per architecture:
//! - x86_64 + std: runtime CPUID via `OnceLock<&'static SimdVTable>`
//! - x86_64 + no_std: compile-time via `cfg(target_feature)`
//! - aarch64 + std: runtime SVE2 probe cached in an `AtomicU8`, with the
//!   public `scan_chunk` / `scan_and_prefetch` wrappers branching directly
//!   into the chosen backend so the NEON path inlines into the bit-walk
//! - aarch64 + no_std: compile-time via `cfg(target_feature = "sve2")`
//! - wasm32: simd128 if compiled with the feature, else scalar
//! - other: scalar fallback

pub(crate) mod prefetch;
pub(crate) mod scanner;

#[cfg(not(any(test, feature = "internal-test-api")))]
pub(crate) mod scalar;

#[cfg(any(test, feature = "internal-test-api"))]
#[doc(hidden)]
pub mod scalar;

#[cfg(all(target_arch = "x86_64", not(any(test, feature = "internal-test-api"))))]
pub(crate) mod x86_64;

#[cfg(all(target_arch = "x86_64", any(test, feature = "internal-test-api")))]
#[doc(hidden)]
pub mod x86_64;

#[cfg(all(target_arch = "aarch64", not(any(test, feature = "internal-test-api"))))]
pub(crate) mod aarch64;

#[cfg(all(target_arch = "aarch64", any(test, feature = "internal-test-api")))]
#[doc(hidden)]
pub mod aarch64;

#[cfg(all(target_arch = "wasm32", not(any(test, feature = "internal-test-api"))))]
pub(crate) mod wasm32;

#[cfg(all(target_arch = "wasm32", any(test, feature = "internal-test-api")))]
#[doc(hidden)]
pub mod wasm32;

// ---------------------------------------------------------------------------
// SimdVTable -- extensible dispatch table for all SIMD operations
// ---------------------------------------------------------------------------

/// VTable holding all SIMD-dispatched function pointers.
///
/// A single `&'static SimdVTable` reference is resolved once (at runtime on
/// x86_64+std, at compile time elsewhere) and reused for every subsequent call.
/// Future fused pipeline operations (e.g. scan + case-fold) will be added here.
///
/// On aarch64 the public `scan_chunk` / `scan_and_prefetch` entry points
/// branch directly to the chosen backend (NEON or SVE2) without going through
/// this struct, so its fields appear unread there. This is intentional: the
/// direct branch keeps the NEON intrinsics inlinable into the bit-walk loop.
#[allow(dead_code)]
pub(crate) struct SimdVTable {
    pub scan_chunk: unsafe fn(*const u8, u8) -> u64,
    pub scan_and_prefetch: unsafe fn(*const u8, *const u8, *const u8, u8) -> u64,
}

// ---------------------------------------------------------------------------
// Static VTable instances -- one per architecture level
// ---------------------------------------------------------------------------

#[allow(dead_code)]
static VTABLE_SCALAR: SimdVTable = SimdVTable {
    scan_chunk: scalar::scan_chunk,
    scan_and_prefetch: scalar::scan_and_prefetch,
};

#[cfg(target_arch = "x86_64")]
#[allow(dead_code)]
static VTABLE_SSE42: SimdVTable = SimdVTable {
    scan_chunk: x86_64::sse42::scan_chunk,
    scan_and_prefetch: x86_64::sse42::scan_and_prefetch,
};

#[cfg(target_arch = "x86_64")]
#[allow(dead_code)]
static VTABLE_AVX2: SimdVTable = SimdVTable {
    scan_chunk: x86_64::avx2::scan_chunk,
    scan_and_prefetch: x86_64::avx2::scan_and_prefetch,
};

#[cfg(target_arch = "x86_64")]
#[allow(dead_code)]
static VTABLE_AVX512: SimdVTable = SimdVTable {
    scan_chunk: x86_64::avx512::scan_chunk,
    scan_and_prefetch: x86_64::avx512::scan_and_prefetch,
};

#[cfg(target_arch = "aarch64")]
#[allow(dead_code)]
static VTABLE_NEON: SimdVTable = SimdVTable {
    scan_chunk: aarch64::neon::scan_chunk,
    scan_and_prefetch: aarch64::neon::scan_and_prefetch,
};

#[cfg(target_arch = "aarch64")]
#[allow(dead_code)]
static VTABLE_SVE2: SimdVTable = SimdVTable {
    scan_chunk: aarch64::sve2::scan_chunk,
    scan_and_prefetch: aarch64::sve2::scan_and_prefetch,
};

#[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
static VTABLE_SIMD128: SimdVTable = SimdVTable {
    scan_chunk: wasm32::simd128::scan_chunk,
    scan_and_prefetch: wasm32::simd128::scan_and_prefetch,
};

// ===========================================================================
// x86_64 + std: runtime dispatch via OnceLock
// ===========================================================================
#[cfg(all(feature = "std", target_arch = "x86_64"))]
mod dispatch {
    use super::SimdVTable;
    use std::sync::OnceLock;

    static VTABLE: OnceLock<&'static SimdVTable> = OnceLock::new();

    fn detect_best() -> &'static SimdVTable {
        if std::is_x86_feature_detected!("avx512bw") {
            return &super::VTABLE_AVX512;
        }
        if std::is_x86_feature_detected!("avx2") {
            return &super::VTABLE_AVX2;
        }
        if std::is_x86_feature_detected!("sse4.2") {
            return &super::VTABLE_SSE42;
        }
        &super::VTABLE_SCALAR
    }

    #[inline]
    pub(crate) fn get_vtable() -> &'static SimdVTable {
        VTABLE.get_or_init(detect_best)
    }
}

// ===========================================================================
// x86_64 + no_std: compile-time dispatch via cfg(target_feature)
// ===========================================================================
#[cfg(all(not(feature = "std"), target_arch = "x86_64"))]
mod dispatch {
    use super::SimdVTable;

    #[inline]
    pub(crate) fn get_vtable() -> &'static SimdVTable {
        #[cfg(target_feature = "avx512bw")]
        {
            return &super::VTABLE_AVX512;
        }
        #[cfg(all(target_feature = "avx2", not(target_feature = "avx512bw")))]
        {
            return &super::VTABLE_AVX2;
        }
        #[cfg(all(
            target_feature = "sse4.2",
            not(target_feature = "avx2"),
            not(target_feature = "avx512bw")
        ))]
        {
            return &super::VTABLE_SSE42;
        }
        #[cfg(not(any(
            target_feature = "sse4.2",
            target_feature = "avx2",
            target_feature = "avx512bw"
        )))]
        {
            &super::VTABLE_SCALAR
        }
    }
}

// ===========================================================================
// aarch64 + std: runtime dispatch via direct branch (NEON or SVE2)
// ===========================================================================
//
// We deliberately avoid an `OnceLock<&'static SimdVTable>` here because that
// hides the chosen backend behind a function pointer that LLVM cannot inline
// across the SIMD-hit bit-walk on every chunk. Instead we cache the SVE2
// detection result in a `AtomicU8` and have the public `scan_chunk` /
// `scan_and_prefetch` wrappers branch directly to the named backend, so the
// NEON fast path inlines the NEON intrinsic into the bit-walk (matching the
// pre-SVE2-dispatch shape). The atomic is initialized exactly once on the
// first call; thereafter it is a single relaxed load + branch.
#[cfg(all(feature = "std", target_arch = "aarch64"))]
mod dispatch {
    use core::sync::atomic::{AtomicU8, Ordering};

    /// 0 = not yet detected, 1 = NEON only, 2 = SVE2 available.
    static STATE: AtomicU8 = AtomicU8::new(0);

    #[cold]
    fn detect_and_store() -> u8 {
        let chosen = if std::arch::is_aarch64_feature_detected!("sve2") {
            2
        } else {
            1
        };
        // Relaxed is sufficient: STATE is a hint, all branches lead to a
        // semantically-equivalent scanner. Multiple writes converge to the
        // same value.
        STATE.store(chosen, Ordering::Relaxed);
        chosen
    }

    /// Returns `true` if SVE2 is available. Initialises on first call.
    #[inline]
    pub(crate) fn sve2_enabled() -> bool {
        let v = STATE.load(Ordering::Relaxed);
        if v == 0 {
            return detect_and_store() == 2;
        }
        v == 2
    }
}

// ===========================================================================
// aarch64 + no_std: compile-time dispatch via cfg(target_feature)
// ===========================================================================
#[cfg(all(not(feature = "std"), target_arch = "aarch64"))]
mod dispatch {
    /// Compile-time decision; this function const-folds completely.
    #[inline(always)]
    pub(crate) fn sve2_enabled() -> bool {
        cfg!(target_feature = "sve2")
    }
}

// ===========================================================================
// wasm32: simd128 is a compile-time decision
// ===========================================================================
#[cfg(target_arch = "wasm32")]
mod dispatch {
    use super::SimdVTable;

    #[inline]
    pub(crate) fn get_vtable() -> &'static SimdVTable {
        #[cfg(target_feature = "simd128")]
        {
            return &super::VTABLE_SIMD128;
        }
        #[cfg(not(target_feature = "simd128"))]
        {
            return &super::VTABLE_SCALAR;
        }
    }
}

// ===========================================================================
// Fallback: any other architecture -> scalar
// ===========================================================================
#[cfg(not(any(
    target_arch = "x86_64",
    target_arch = "aarch64",
    target_arch = "wasm32",
)))]
mod dispatch {
    use super::SimdVTable;

    #[inline]
    pub(crate) fn get_vtable() -> &'static SimdVTable {
        &super::VTABLE_SCALAR
    }
}

// ===========================================================================
// Public dispatch API
// ===========================================================================

/// Scan a 64-byte chunk, returning a bitmask of bytes >= `bound`.
///
/// Dispatches to the best available SIMD backend for the current platform.
/// On x86_64 with `std`, the first call triggers runtime CPUID detection via
/// `OnceLock`; subsequent calls are a single pointer dereference. On aarch64
/// with `std` the path is a single `AtomicU8` load + branch into the chosen
/// backend, so the NEON intrinsics inline into the bit-walk loop.
///
/// # Safety
/// - `ptr` must be valid for 64 bytes of read access.
#[inline]
pub(crate) unsafe fn scan_chunk(ptr: *const u8, bound: u8) -> u64 {
    #[cfg(target_arch = "aarch64")]
    {
        if dispatch::sve2_enabled() {
            // SAFETY: caller guarantees `ptr` is valid for 64 bytes.
            unsafe { aarch64::sve2::scan_chunk(ptr, bound) }
        } else {
            // SAFETY: caller guarantees `ptr` is valid for 64 bytes.
            unsafe { aarch64::neon::scan_chunk(ptr, bound) }
        }
    }
    #[cfg(not(target_arch = "aarch64"))]
    {
        let vt = dispatch::get_vtable();
        // SAFETY: caller guarantees `ptr` is valid for 64 bytes.
        unsafe { (vt.scan_chunk)(ptr, bound) }
    }
}

/// Scan a 64-byte chunk and issue prefetch hints for upcoming data.
///
/// Dispatches to the best available SIMD backend for the current platform.
///
/// # Safety
/// - `ptr` must be valid for 64 bytes of read access.
/// - Prefetch pointers may be out-of-bounds (prefetch is a non-faulting hint
///   on all supported architectures).
#[allow(dead_code)]
#[inline]
pub(crate) unsafe fn scan_and_prefetch(
    ptr: *const u8,
    prefetch_l1: *const u8,
    prefetch_l2: *const u8,
    bound: u8,
) -> u64 {
    #[cfg(target_arch = "aarch64")]
    {
        if dispatch::sve2_enabled() {
            // SAFETY: caller guarantees `ptr` is valid for 64 bytes; prefetch
            // pointers are non-faulting hints.
            unsafe { aarch64::sve2::scan_and_prefetch(ptr, prefetch_l1, prefetch_l2, bound) }
        } else {
            // SAFETY: see above.
            unsafe { aarch64::neon::scan_and_prefetch(ptr, prefetch_l1, prefetch_l2, bound) }
        }
    }
    #[cfg(not(target_arch = "aarch64"))]
    {
        let vt = dispatch::get_vtable();
        // SAFETY: caller guarantees `ptr` is valid for 64 bytes; prefetch
        // pointers are non-faulting hints.
        unsafe { (vt.scan_and_prefetch)(ptr, prefetch_l1, prefetch_l2, bound) }
    }
}

/// Scan two adjacent 64-byte chunks software-pipelined, issuing one pair of
/// prefetch hints. Returns `(mask_a, mask_b)`.
///
/// On NEON (aarch64) this dispatches to a custom paired implementation that
/// issues all 8 loads / 8 compares / 8 reductions interleaved across NEON
/// pipes, which buys ~2x throughput on dense-hit chunks.
///
/// On all other targets (including aarch64 SVE2, x86_64, wasm32, scalar)
/// this falls back to two sequential `scan_and_prefetch` calls. The
/// fallback exists only so callers don't need to special-case the
/// architecture; it provides no microarchitectural benefit on non-NEON
/// backends.
///
/// # Safety
/// - Both chunk pointers must be valid for 64 bytes of read access.
/// - Prefetch pointers may be out-of-bounds (non-faulting hint).
#[allow(dead_code)]
#[inline]
pub(crate) unsafe fn scan_pair_and_prefetch(
    ptr_a: *const u8,
    ptr_b: *const u8,
    prefetch_l1: *const u8,
    prefetch_l2: *const u8,
    bound: u8,
) -> (u64, u64) {
    #[cfg(target_arch = "aarch64")]
    {
        if dispatch::sve2_enabled() {
            // SVE2: no paired specialization yet -- fall back to two scans.
            // SAFETY: see scan_and_prefetch.
            unsafe {
                let a = aarch64::sve2::scan_and_prefetch(ptr_a, prefetch_l1, prefetch_l2, bound);
                let b = aarch64::sve2::scan_chunk(ptr_b, bound);
                (a, b)
            }
        } else {
            // SAFETY: caller guarantees both chunks are valid for 64 bytes.
            unsafe {
                aarch64::neon::scan_chunk_pair_and_prefetch(
                    ptr_a,
                    ptr_b,
                    prefetch_l1,
                    prefetch_l2,
                    bound,
                )
            }
        }
    }
    #[cfg(not(target_arch = "aarch64"))]
    {
        let vt = dispatch::get_vtable();
        // SAFETY: see scan_and_prefetch.
        unsafe {
            let a = (vt.scan_and_prefetch)(ptr_a, prefetch_l1, prefetch_l2, bound);
            let b = (vt.scan_chunk)(ptr_b, bound);
            (a, b)
        }
    }
}

/// Find the byte offset of the first byte >= `bound` in `bytes`.
///
/// Uses the dispatched SIMD scanner in 64-byte chunks, then scans the tail
/// byte-by-byte. Returns `bytes.len()` if no such byte exists.
#[allow(dead_code)]
#[inline]
pub(crate) fn find_first_above(bytes: &[u8], bound: u8) -> usize {
    let len = bytes.len();
    let mut offset = 0usize;

    // Process full 64-byte chunks via the dispatch layer.
    while offset + 64 <= len {
        // SAFETY: offset + 64 <= len, so the pointer is valid for 64 bytes.
        let mask = unsafe { scan_chunk(bytes.as_ptr().add(offset), bound) };
        if mask != 0 {
            return offset + mask.trailing_zeros() as usize;
        }
        offset += 64;
    }

    // Scan remaining bytes one at a time.
    while offset < len {
        if bytes[offset] >= bound {
            return offset;
        }
        offset += 1;
    }

    len
}

#[cfg(test)]
mod dispatch_tests;

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

    #[test]
    fn find_first_above_all_below() {
        let data = b"Hello, world! This is pure ASCII and should return len.";
        assert_eq!(find_first_above(data, 0xC0), data.len());
    }

    #[test]
    fn find_first_above_first_byte() {
        let data = [0xC0u8, 0x00, 0x00, 0x00];
        assert_eq!(find_first_above(&data, 0xC0), 0);
    }

    #[test]
    fn find_first_above_in_second_chunk() {
        let mut data = [0u8; 128];
        data[65] = 0xC0;
        assert_eq!(find_first_above(&data, 0xC0), 65);
    }

    #[test]
    fn find_first_above_in_tail() {
        let mut data = [0u8; 70];
        data[68] = 0xC0;
        assert_eq!(find_first_above(&data, 0xC0), 68);
    }

    #[test]
    fn find_first_above_empty() {
        assert_eq!(find_first_above(&[], 0xC0), 0);
    }

    #[test]
    fn find_first_above_exact_chunk_boundary() {
        let mut data = [0u8; 64];
        data[63] = 0xC0;
        assert_eq!(find_first_above(&data, 0xC0), 63);
    }

    #[test]
    fn find_first_above_multi_chunk_utf8() {
        let s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\u{00E9}";
        let bytes = s.as_bytes();
        assert_eq!(find_first_above(bytes, 0xC0), 64);
    }

    #[cfg(not(target_arch = "aarch64"))]
    #[test]
    fn vtable_get_returns_consistent_reference() {
        let vt1 = dispatch::get_vtable();
        let vt2 = dispatch::get_vtable();
        assert!(
            core::ptr::eq(vt1, vt2),
            "get_vtable() must return the same reference"
        );
    }

    #[cfg(target_arch = "aarch64")]
    #[test]
    fn aarch64_dispatch_state_stable() {
        // First call may initialise; both calls must agree.
        let a = dispatch::sve2_enabled();
        let b = dispatch::sve2_enabled();
        assert_eq!(a, b, "sve2_enabled() must be stable across calls");
    }
}