crous-simd 1.1.0

Optional SIMD-accelerated routines for Crous encoding/decoding
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
//! # crous-simd
//!
//! Optional SIMD-accelerated routines for Crous encoding/decoding.
//!
//! This crate provides optimized implementations of performance-critical
//! operations. On aarch64 (Apple Silicon, etc.) it uses NEON intrinsics.
//! On x86_64 it uses SSE2/AVX2 when available.
//! All functions have scalar fallbacks for unsupported platforms.
//!
//! # Feature flags
//! - `simd-varint` — enable SIMD-accelerated varint boundary pre-scan.
//!   Idea: https://github.com/as-com/varint-simd
//!
//! # Provided routines
//! - `batch_decode_varints` — decode multiple LEB128 varints sequentially
//! - `batch_decode_varints_simd` — SIMD pre-scan variant (feature `simd-varint`)
//! - `find_byte` — locate first occurrence of a byte (SIMD-accelerated)
//! - `count_byte` — count occurrences of a byte (SIMD-accelerated)
//! - `find_non_ascii` — locate first non-ASCII byte (for fast UTF-8 pre-scan)

/// Batch-decode multiple varints from a contiguous buffer (scalar path).
///
/// Returns a vector of `(value, bytes_consumed)` pairs.
pub fn batch_decode_varints(data: &[u8], count: usize) -> Vec<(u64, usize)> {
    let mut results = Vec::with_capacity(count);
    let mut offset = 0;
    for _ in 0..count {
        if offset >= data.len() {
            break;
        }
        match crous_core::varint::decode_varint(data, offset) {
            Ok((val, consumed)) => {
                results.push((val, consumed));
                offset += consumed;
            }
            Err(_) => break,
        }
    }
    results
}

/// Total bytes consumed by a batch decode.
pub fn batch_decode_total_consumed(data: &[u8], count: usize) -> usize {
    let mut offset = 0;
    for _ in 0..count {
        if offset >= data.len() {
            break;
        }
        match crous_core::varint::decode_varint(data, offset) {
            Ok((_val, consumed)) => offset += consumed,
            Err(_) => break,
        }
    }
    offset
}

// ── SIMD varint boundary pre-scan (feature = "simd-varint") ──────────
//
// The key insight from varint-simd: we can use SIMD to scan for the
// continuation bit (0x80) across 16 bytes at once to quickly find
// varint termination bytes, then extract values with scalar code.
// Citation: https://github.com/as-com/varint-simd

#[cfg(all(feature = "simd-varint", target_arch = "aarch64"))]
mod simd_varint_neon {
    use std::arch::aarch64::*;

    /// SIMD pre-scan: find the offset of the first byte without the high bit
    /// set (i.e., a varint terminator) starting from `offset`.
    ///
    /// Returns the length of the varint starting at `offset` (1..=10), or
    /// None if no terminator found in the next 16 bytes (malformed).
    ///
    /// # Safety
    /// NEON always available on aarch64.
    #[inline]
    pub(crate) unsafe fn varint_len_neon(data: &[u8], offset: usize) -> Option<usize> {
        let remaining = data.len() - offset;
        if remaining == 0 {
            return None;
        }

        if remaining >= 16 {
            let ptr = data.as_ptr().add(offset);
            let chunk = unsafe { vld1q_u8(ptr) };
            let high_bits = unsafe { vshrq_n_u8::<7>(chunk) }; // isolate bit 7
            // We want the first lane where bit 7 is 0 (terminator)
            let zero_vec = unsafe { vdupq_n_u8(0) };
            let is_terminator = unsafe { vceqq_u8(high_bits, zero_vec) };
            let max_val = unsafe { vmaxvq_u8(is_terminator) };
            if max_val != 0 {
                let mut mask = [0u8; 16];
                unsafe { vst1q_u8(mask.as_mut_ptr(), is_terminator) };
                for (j, &m) in mask.iter().enumerate() {
                    if m != 0 {
                        let len = j + 1;
                        if len <= 10 {
                            return Some(len);
                        } else {
                            return None; // overflow
                        }
                    }
                }
            }
            None
        } else {
            // Scalar fallback for tail
            scalar_varint_len(data, offset)
        }
    }

    fn scalar_varint_len(data: &[u8], offset: usize) -> Option<usize> {
        for i in 0..10.min(data.len() - offset) {
            if data[offset + i] & 0x80 == 0 {
                return Some(i + 1);
            }
        }
        None
    }
}

/// Batch-decode varints using SIMD pre-scan to determine boundaries first.
///
/// This amortizes branch misprediction by scanning continuation bits in bulk.
/// Falls back to `batch_decode_varints` when the `simd-varint` feature is disabled
/// or the platform is unsupported.
///
/// Citation: SIMD varint idea — https://github.com/as-com/varint-simd
pub fn batch_decode_varints_simd(data: &[u8], count: usize) -> Vec<(u64, usize)> {
    #[cfg(all(feature = "simd-varint", target_arch = "aarch64"))]
    {
        let mut results = Vec::with_capacity(count);
        let mut offset = 0;
        for _ in 0..count {
            if offset >= data.len() {
                break;
            }
            // Use SIMD to find varint length, then decode scalar
            let vlen = unsafe { simd_varint_neon::varint_len_neon(data, offset) };
            match vlen {
                Some(len) => {
                    // Fast scalar decode knowing the exact length
                    match crous_core::varint::decode_varint(data, offset) {
                        Ok((val, consumed)) => {
                            debug_assert_eq!(consumed, len);
                            results.push((val, consumed));
                            offset += consumed;
                        }
                        Err(_) => break,
                    }
                }
                None => {
                    // Fallback to scalar
                    match crous_core::varint::decode_varint(data, offset) {
                        Ok((val, consumed)) => {
                            results.push((val, consumed));
                            offset += consumed;
                        }
                        Err(_) => break,
                    }
                }
            }
        }
        results
    }
    #[cfg(not(all(feature = "simd-varint", target_arch = "aarch64")))]
    {
        batch_decode_varints(data, count)
    }
}

// ── SIMD byte scanning (aarch64 NEON) ────────────────────────────────

#[cfg(target_arch = "aarch64")]
mod neon {
    use std::arch::aarch64::*;

    /// Find the first occurrence of `needle` in `data` using NEON.
    ///
    /// # Safety
    /// Caller must ensure NEON is available (always true on aarch64).
    #[inline]
    pub(crate) unsafe fn find_byte_neon(data: &[u8], needle: u8) -> Option<usize> {
        let len = data.len();
        let ptr = data.as_ptr();
        let needle_vec = unsafe { vdupq_n_u8(needle) };
        let mut i = 0;

        // Process 16-byte chunks
        while i + 16 <= len {
            let chunk = unsafe { vld1q_u8(ptr.add(i)) };
            let cmp = unsafe { vceqq_u8(chunk, needle_vec) };
            // Check if any byte matched
            let max = unsafe { vmaxvq_u8(cmp) };
            if max != 0 {
                // Find the exact position
                let mut mask_bytes = [0u8; 16];
                unsafe { vst1q_u8(mask_bytes.as_mut_ptr(), cmp) };
                for (j, &m) in mask_bytes.iter().enumerate() {
                    if m != 0 {
                        return Some(i + j);
                    }
                }
            }
            i += 16;
        }

        // Scalar tail
        while i < len {
            if unsafe { *ptr.add(i) } == needle {
                return Some(i);
            }
            i += 1;
        }
        None
    }

    /// Count occurrences of `needle` in `data` using NEON.
    ///
    /// # Safety
    /// Caller must ensure NEON is available.
    #[inline]
    pub(crate) unsafe fn count_byte_neon(data: &[u8], needle: u8) -> usize {
        let len = data.len();
        let ptr = data.as_ptr();
        let needle_vec = unsafe { vdupq_n_u8(needle) };
        let mut total: usize = 0;
        let mut i = 0;

        // Process 16-byte chunks; accumulate per-lane counts
        // Use vaddlvq_u8 on the mask (0xFF = match, 0 = no match).
        // Each match contributes 0xFF = 255, and we need count, so divide by 255.
        while i + 16 <= len {
            let chunk = unsafe { vld1q_u8(ptr.add(i)) };
            let cmp = unsafe { vceqq_u8(chunk, needle_vec) };
            // Each matching lane has value 0xFF. Sum all lanes.
            // We want count of matches = sum / 255.
            let sum = unsafe { vaddlvq_u8(cmp) } as usize;
            total += sum / 255;
            i += 16;
        }

        // Scalar tail
        while i < len {
            if unsafe { *ptr.add(i) } == needle {
                total += 1;
            }
            i += 1;
        }
        total
    }

    /// Find the first non-ASCII byte (byte >= 0x80) using NEON.
    ///
    /// Returns `None` if all bytes are ASCII.
    ///
    /// # Safety
    /// Caller must ensure NEON is available.
    #[inline]
    pub(crate) unsafe fn find_non_ascii_neon(data: &[u8]) -> Option<usize> {
        let len = data.len();
        let ptr = data.as_ptr();
        let threshold = unsafe { vdupq_n_u8(0x80) };
        let mut i = 0;

        while i + 16 <= len {
            let chunk = unsafe { vld1q_u8(ptr.add(i)) };
            // Compare >= 0x80 means high bit set
            let high_bits = unsafe { vcgeq_u8(chunk, threshold) };
            let max = unsafe { vmaxvq_u8(high_bits) };
            if max != 0 {
                let mut mask_bytes = [0u8; 16];
                unsafe { vst1q_u8(mask_bytes.as_mut_ptr(), high_bits) };
                for (j, &m) in mask_bytes.iter().enumerate() {
                    if m != 0 {
                        return Some(i + j);
                    }
                }
            }
            i += 16;
        }

        while i < len {
            if unsafe { *ptr.add(i) } >= 0x80 {
                return Some(i);
            }
            i += 1;
        }
        None
    }
}

// ── Public API ───────────────────────────────────────────────────────

/// Scan a byte slice for a specific byte using SIMD where available.
///
/// On aarch64, uses NEON intrinsics for 16-byte-at-a-time scanning.
/// Falls back to a scalar scan on other architectures.
#[inline]
pub fn find_byte(data: &[u8], needle: u8) -> Option<usize> {
    #[cfg(target_arch = "aarch64")]
    {
        // NEON is always available on aarch64
        unsafe { neon::find_byte_neon(data, needle) }
    }
    #[cfg(not(target_arch = "aarch64"))]
    {
        data.iter().position(|&b| b == needle)
    }
}

/// Count the number of occurrences of `needle` in `data`.
///
/// On aarch64, uses NEON intrinsics for fast counting.
#[inline]
pub fn count_byte(data: &[u8], needle: u8) -> usize {
    #[cfg(target_arch = "aarch64")]
    {
        unsafe { neon::count_byte_neon(data, needle) }
    }
    #[cfg(not(target_arch = "aarch64"))]
    {
        data.iter().filter(|&&b| b == needle).count()
    }
}

/// Find the first byte with the high bit set (non-ASCII).
///
/// This is useful for fast UTF-8 pre-scanning: if this returns `None`,
/// the entire slice is pure ASCII and valid UTF-8.
#[inline]
pub fn find_non_ascii(data: &[u8]) -> Option<usize> {
    #[cfg(target_arch = "aarch64")]
    {
        unsafe { neon::find_non_ascii_neon(data) }
    }
    #[cfg(not(target_arch = "aarch64"))]
    {
        data.iter().position(|&b| b >= 0x80)
    }
}

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

    #[test]
    fn batch_decode_basic() {
        let mut data = Vec::new();
        for v in [0u64, 1, 127, 128, 300] {
            crous_core::varint::encode_varint_vec(v, &mut data);
        }
        let results = batch_decode_varints(&data, 5);
        assert_eq!(results.len(), 5);
        assert_eq!(results[0].0, 0);
        assert_eq!(results[1].0, 1);
        assert_eq!(results[2].0, 127);
        assert_eq!(results[3].0, 128);
        assert_eq!(results[4].0, 300);
    }

    #[test]
    fn batch_decode_simd_matches_scalar() {
        let mut data = Vec::new();
        let values = [0u64, 1, 42, 127, 128, 255, 300, 16384, u64::MAX];
        for v in &values {
            crous_core::varint::encode_varint_vec(*v, &mut data);
        }
        let scalar = batch_decode_varints(&data, values.len());
        let simd = batch_decode_varints_simd(&data, values.len());
        assert_eq!(scalar.len(), simd.len());
        for (s, d) in scalar.iter().zip(simd.iter()) {
            assert_eq!(s.0, d.0, "value mismatch");
            assert_eq!(s.1, d.1, "consumed mismatch");
        }
    }

    #[test]
    fn find_byte_basic() {
        assert_eq!(find_byte(b"hello", b'l'), Some(2));
        assert_eq!(find_byte(b"hello", b'z'), None);
    }

    #[test]
    fn find_byte_long() {
        // Test with data longer than 16 bytes to exercise SIMD path
        let data: Vec<u8> = (0..256).map(|i| i as u8).collect();
        assert_eq!(find_byte(&data, 0), Some(0));
        assert_eq!(find_byte(&data, 42), Some(42));
        assert_eq!(find_byte(&data, 255), Some(255));

        let zeros = vec![0u8; 100];
        assert_eq!(find_byte(&zeros, 1), None);
    }

    #[test]
    fn count_byte_basic() {
        assert_eq!(count_byte(b"hello", b'l'), 2);
        assert_eq!(count_byte(b"hello", b'z'), 0);
        assert_eq!(count_byte(b"hello", b'o'), 1);
    }

    #[test]
    fn count_byte_long() {
        let data = vec![0xABu8; 200];
        assert_eq!(count_byte(&data, 0xAB), 200);
        assert_eq!(count_byte(&data, 0x00), 0);
    }

    #[test]
    fn find_non_ascii_basic() {
        assert_eq!(find_non_ascii(b"hello"), None);
        assert_eq!(find_non_ascii(b"hello\x80"), Some(5));
        assert_eq!(find_non_ascii(b"\xff"), Some(0));
    }

    #[test]
    fn find_non_ascii_long() {
        let mut data = vec![b'a'; 100];
        assert_eq!(find_non_ascii(&data), None);
        data[50] = 0x80;
        assert_eq!(find_non_ascii(&data), Some(50));
    }
}