faster-hex 1.0.0

Fast, checked hex encoding and decoding with SIMD and no_std support.
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
//! SSE4.1, AVX2 and AVX-512 checking and decoding.
//!
//! Callers establish CPU/OS support. Checkers accept any source length.
//! Checked decoders require even input and exactly half as much output space;
//! they validate the complete input before writing. Raw decoders additionally
//! require valid hex input.

// SIMD decoding includes work derived from fast-hex under the MIT license.
// See https://github.com/zbjornson/fast-hex and LICENSE-THIRD-PARTY/fast-hex.
// ALSW lookup is adapted from vsimd; saturating nibble mapping from const-hex.
// See LICENSE-THIRD-PARTY/vsimd and LICENSE-THIRD-PARTY/const-hex.

#[cfg(target_arch = "x86")]
use core::arch::x86::*;
#[cfg(target_arch = "x86_64")]
use core::arch::x86_64::*;

use super::{hex_check_fallback_with_case, hex_decode_fallback, CheckCase};

// Both AVX widths repeat these 16-entry hash and decode tables in every lane.
#[inline]
#[target_feature(enable = "sse2")]
fn alsw_tables() -> (__m128i, __m128i) {
    let hash = _mm_setr_epi8(1, 1, 1, 1, 1, 1, 1, 11, 11, 11, 15, 15, 15, 15, 15, 15);
    let decode = _mm_setr_epi8(
        -128, -128, -128, -128, -48, -55, -128, -87, -128, -48, -128, -128, -128, -128, -128, -128,
    );
    (hash, decode)
}

// The ALSW hash separates digits, uppercase and lowercase ASCII ranges.
// Saturating addition with each range's negative start marks invalid bytes
// with a sign bit. Slots 5 and 7 select the uppercase and lowercase ranges.
// Non-ASCII bytes remain negative under signed saturating addition.
#[inline]
fn decode_check_offsets(case: CheckCase) -> &'static [i8; 16] {
    const MIXED: [i8; 16] = [
        -128, -128, -128, -128, -48, -65, -128, -97, -128, -55, -128, -128, -128, -128, -128, -128,
    ];
    const LOWER: [i8; 16] = {
        let mut offsets = MIXED;
        offsets[5] = -128;
        offsets
    };
    const UPPER: [i8; 16] = {
        let mut offsets = MIXED;
        offsets[7] = -128;
        offsets
    };
    match case {
        CheckCase::None => &MIXED,
        CheckCase::Lower => &LOWER,
        CheckCase::Upper => &UPPER,
    }
}

// Add a wrapping bias so the accepted unsigned ASCII interval starts at -128.
// A signed comparison then rejects both bytes below the interval and above it.
#[inline]
#[target_feature(enable = "sse4.1")]
unsafe fn valid_sse(bytes: __m128i, case: CheckCase) -> __m128i {
    let digit = _mm_cmpgt_epi8(_mm_set1_epi8(-118), _mm_add_epi8(bytes, _mm_set1_epi8(80)));
    let fold = if case == CheckCase::None { 0x20 } else { 0 };
    let first = if case == CheckCase::Upper { b'A' } else { b'a' };
    let letters = _mm_or_si128(bytes, _mm_set1_epi8(fold));
    let letter = _mm_cmpgt_epi8(
        _mm_set1_epi8(-122),
        _mm_add_epi8(letters, _mm_set1_epi8(128u8.wrapping_sub(first) as i8)),
    );
    _mm_or_si128(digit, letter)
}

#[target_feature(enable = "sse4.1")]
pub(crate) unsafe fn hex_check_sse_with_case(src: &[u8], case: CheckCase) -> bool {
    if src.len() < 16 {
        return hex_check_fallback_with_case(src, case);
    }
    let (blocks, tail) = src.as_chunks::<16>();
    for block in blocks {
        if _mm_movemask_epi8(valid_sse(_mm_loadu_si128(block.as_ptr().cast()), case)) != 0xffff {
            return false;
        }
    }
    if !tail.is_empty() {
        if let Some(last) = src.last_chunk::<16>() {
            return _mm_movemask_epi8(valid_sse(_mm_loadu_si128(last.as_ptr().cast()), case))
                == 0xffff;
        }
    }
    true
}

// Add a wrapping bias so the accepted unsigned ASCII interval starts at -128.
// A signed comparison then rejects both bytes below the interval and above it.
#[inline]
#[target_feature(enable = "avx2")]
unsafe fn valid_avx2(bytes: __m256i, case: CheckCase) -> __m256i {
    let digit = _mm256_cmpgt_epi8(
        _mm256_set1_epi8(-118),
        _mm256_add_epi8(bytes, _mm256_set1_epi8(80)),
    );
    let fold = if case == CheckCase::None { 0x20 } else { 0 };
    let first = if case == CheckCase::Upper { b'A' } else { b'a' };
    let letters = _mm256_or_si256(bytes, _mm256_set1_epi8(fold));
    let letter = _mm256_cmpgt_epi8(
        _mm256_set1_epi8(-122),
        _mm256_add_epi8(letters, _mm256_set1_epi8(128u8.wrapping_sub(first) as i8)),
    );
    _mm256_or_si256(digit, letter)
}

#[target_feature(enable = "avx2")]
#[inline]
pub(crate) unsafe fn hex_check_avx2_with_case(src: &[u8], case: CheckCase) -> bool {
    if src.len() < 32 {
        return hex_check_sse_with_case(src, case);
    }
    // Reduce four vectors together so long inputs need fewer mask tests.
    let (batches, rest) = src.as_chunks::<128>();
    for batch in batches {
        let mut valid = _mm256_set1_epi8(-1);
        for block in batch.as_chunks::<32>().0 {
            valid = _mm256_and_si256(
                valid,
                valid_avx2(_mm256_loadu_si256(block.as_ptr().cast()), case),
            );
        }
        if _mm256_movemask_epi8(valid) != -1 {
            return false;
        }
    }
    let (blocks, tail) = rest.as_chunks::<32>();
    let mut valid = _mm256_set1_epi8(-1);
    for block in blocks {
        valid = _mm256_and_si256(
            valid,
            valid_avx2(_mm256_loadu_si256(block.as_ptr().cast()), case),
        );
    }
    if !tail.is_empty() {
        if let Some(last) = src.last_chunk::<32>() {
            valid = _mm256_and_si256(
                valid,
                valid_avx2(_mm256_loadu_si256(last.as_ptr().cast()), case),
            );
        }
    }
    _mm256_movemask_epi8(valid) == -1
}

#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
unsafe fn valid_avx512(bytes: __m512i, case: CheckCase) -> u64 {
    let digit = _mm512_cmple_epu8_mask(
        _mm512_sub_epi8(bytes, _mm512_set1_epi8(b'0' as i8)),
        _mm512_set1_epi8(9),
    );
    let fold = if case == CheckCase::None { 0x20 } else { 0 };
    let first = if case == CheckCase::Upper { b'A' } else { b'a' };
    let letters = _mm512_or_si512(bytes, _mm512_set1_epi8(fold));
    let letter = _mm512_cmple_epu8_mask(
        _mm512_sub_epi8(letters, _mm512_set1_epi8(first as i8)),
        _mm512_set1_epi8(5),
    );
    digit | letter
}

#[target_feature(enable = "avx512f,avx512bw")]
#[inline]
pub(crate) unsafe fn hex_check_avx512_with_case(src: &[u8], case: CheckCase) -> bool {
    if src.len() < 64 {
        return hex_check_avx2_with_case(src, case);
    }
    let (blocks, tail) = src.as_chunks::<64>();
    for block in blocks {
        if valid_avx512(_mm512_loadu_si512(block.as_ptr().cast()), case) != u64::MAX {
            return false;
        }
    }
    if !tail.is_empty() {
        if let Some(last) = src.last_chunk::<64>() {
            return valid_avx512(_mm512_loadu_si512(last.as_ptr().cast()), case) == u64::MAX;
        }
    }
    true
}

// Map valid ASCII to 0..=15 and every invalid byte to >=16. This is the
// saturating-arithmetic idea in Muła and Langdale's hex parser, with a strict
// case policy and validation completed before any output is written:
// http://0x80.pl/notesen/2022-01-17-validating-hex-parse.html
// Wrapping (x - 58), saturating subtraction of 6, then wrapping +16 maps
// '0'..='9' to 0..=9 and everything else to >=16. The letter candidate maps
// its first six values to 10..=15; unsigned min chooses the valid candidate.
#[inline]
#[target_feature(enable = "sse4.1")]
unsafe fn decode_sse41_nibbles(bytes: __m128i, case: CheckCase) -> __m128i {
    let digit = _mm_sub_epi8(
        _mm_subs_epu8(_mm_add_epi8(bytes, _mm_set1_epi8(-58)), _mm_set1_epi8(6)),
        _mm_set1_epi8(-16),
    );
    let fold = if case == CheckCase::None { 0x20 } else { 0 };
    let first = if case == CheckCase::Upper { b'A' } else { b'a' };
    let letter = _mm_sub_epi8(
        _mm_or_si128(bytes, _mm_set1_epi8(fold)),
        _mm_set1_epi8(first as i8),
    );
    _mm_min_epu8(digit, _mm_adds_epu8(letter, _mm_set1_epi8(10)))
}

// Returns (nibbles, validity bytes). Nibbles are valid only when no validity
// byte has its sign bit set. Check the complete input before storing output.
// The word-sized shift leaves neighbor bits in the hash, but for ASCII they
// only affect index bits ignored by the 16-entry byte shuffles.
#[inline]
#[target_feature(enable = "avx2")]
unsafe fn decode_avx2_nibbles(bytes: __m256i, case: CheckCase) -> (__m256i, __m256i) {
    let (hash_table, decode_offsets) = alsw_tables();
    let hash_table = _mm256_broadcastsi128_si256(hash_table);
    let check_offsets =
        _mm256_broadcastsi128_si256(_mm_loadu_si128(decode_check_offsets(case).as_ptr().cast()));
    let decode_offsets = _mm256_broadcastsi128_si256(decode_offsets);
    let hash = _mm256_avg_epu8(
        _mm256_srli_epi32::<3>(bytes),
        _mm256_shuffle_epi8(hash_table, bytes),
    );
    let checked = _mm256_adds_epi8(bytes, _mm256_shuffle_epi8(check_offsets, hash));
    let nibbles = _mm256_add_epi8(bytes, _mm256_shuffle_epi8(decode_offsets, hash));
    (nibbles, checked)
}

// Same nibble/validity contract as decode_avx2_nibbles, for 64 input bytes.
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
unsafe fn decode_avx512_nibbles(bytes: __m512i, case: CheckCase) -> (__m512i, __m512i) {
    let (hash_table, decode_offsets) = alsw_tables();
    let hash_table = _mm512_broadcast_i32x4(hash_table);
    let check_offsets =
        _mm512_broadcast_i32x4(_mm_loadu_si128(decode_check_offsets(case).as_ptr().cast()));
    let decode_offsets = _mm512_broadcast_i32x4(decode_offsets);
    let hash = _mm512_avg_epu8(
        _mm512_srli_epi32::<3>(bytes),
        _mm512_shuffle_epi8(hash_table, bytes),
    );
    let checked = _mm512_adds_epi8(bytes, _mm512_shuffle_epi8(check_offsets, hash));
    let nibbles = _mm512_add_epi8(bytes, _mm512_shuffle_epi8(decode_offsets, hash));
    (nibbles, checked)
}

// Checked SIMD decoders require even input of at least 16 bytes and the exact
// 2:1 input/output ratio. Dispatch handles shorter input with the scalar kernel.
// Bounded inputs stay in registers until every byte passes validation; the
// general path validates the complete input before writing.
// 8..=16 output bytes fit in two input registers, possibly overlapping.
#[inline]
#[target_feature(enable = "sse4.1")]
unsafe fn hex_decode_short_sse41(src: &[u8], dst: &mut [u8], case: CheckCase) -> Result<(), ()> {
    debug_assert!((16..=32).contains(&src.len()));
    let a = decode_sse41_nibbles(_mm_loadu_si128(src.as_ptr().cast()), case);
    // An eight-byte output fits in this one load; no overlapping tail is needed.
    if src.len() == 16 {
        if _mm_testz_si128(a, _mm_set1_epi8(-16)) == 0 {
            return Err(());
        }
        _mm_storel_epi64(dst.as_mut_ptr().cast(), pack_sse41(a, a));
        return Ok(());
    }
    let b = decode_sse41_nibbles(
        _mm_loadu_si128(src.as_ptr().add(src.len() - 16).cast()),
        case,
    );
    if _mm_testz_si128(_mm_or_si128(a, b), _mm_set1_epi8(-16)) == 0 {
        return Err(());
    }
    let decoded = pack_sse41(a, b);
    _mm_storel_epi64(dst.as_mut_ptr().cast(), decoded);
    _mm_storel_epi64(
        dst.as_mut_ptr().add(dst.len() - 8).cast(),
        _mm_srli_si128::<8>(decoded),
    );
    Ok(())
}

#[target_feature(enable = "sse4.1")]
pub(crate) unsafe fn hex_decode_sse41_checked(
    src: &[u8],
    dst: &mut [u8],
    case: CheckCase,
) -> Result<(), ()> {
    debug_assert!(src.len() >= 16);
    if src.len() <= 32 {
        return hex_decode_short_sse41(src, dst, case);
    }
    if src.len() <= 64 {
        let a = decode_sse41_nibbles(_mm_loadu_si128(src.as_ptr().cast()), case);
        let b = decode_sse41_nibbles(_mm_loadu_si128(src.as_ptr().add(16).cast()), case);
        let c = decode_sse41_nibbles(
            _mm_loadu_si128(src.as_ptr().add(src.len() - 32).cast()),
            case,
        );
        let d = decode_sse41_nibbles(
            _mm_loadu_si128(src.as_ptr().add(src.len() - 16).cast()),
            case,
        );
        let combined = _mm_or_si128(_mm_or_si128(a, b), _mm_or_si128(c, d));
        if _mm_testz_si128(combined, _mm_set1_epi8(-16)) == 0 {
            return Err(());
        }
        _mm_storeu_si128(dst.as_mut_ptr().cast(), pack_sse41(a, b));
        _mm_storeu_si128(
            dst.as_mut_ptr().add(dst.len() - 16).cast(),
            pack_sse41(c, d),
        );
    } else {
        if !hex_check_sse_with_case(src, case) {
            return Err(());
        }
        hex_decode_sse41(src, dst);
    }
    Ok(())
}

#[target_feature(enable = "avx2")]
#[inline]
pub(crate) unsafe fn hex_decode_avx2_checked(
    src: &[u8],
    dst: &mut [u8],
    case: CheckCase,
) -> Result<(), ()> {
    debug_assert!(src.len() >= 16);
    // Common fixed sizes use constant offsets and avoid duplicate tail loads.
    // Every branch validates the complete input before its first store.
    if src.len() == 64 {
        let (a, a_checked) = decode_avx2_nibbles(_mm256_loadu_si256(src.as_ptr().cast()), case);
        let (b, b_checked) =
            decode_avx2_nibbles(_mm256_loadu_si256(src.as_ptr().add(32).cast()), case);
        if _mm256_movemask_epi8(_mm256_or_si256(a_checked, b_checked)) != 0 {
            return Err(());
        }
        _mm256_storeu_si256(dst.as_mut_ptr().cast(), pack_avx2(a, b));
        return Ok(());
    }
    if src.len() == 32 {
        let (nibbles, checked) = decode_avx2_nibbles(_mm256_loadu_si256(src.as_ptr().cast()), case);
        if _mm256_movemask_epi8(checked) != 0 {
            return Err(());
        }
        _mm_storeu_si128(
            dst.as_mut_ptr().cast(),
            _mm256_castsi256_si128(pack_avx2(nibbles, nibbles)),
        );
        return Ok(());
    }
    if src.len() < 32 {
        return hex_decode_short_sse41(src, dst, case);
    }
    if src.len() < 64 {
        let (a, a_checked) = decode_avx2_nibbles(_mm256_loadu_si256(src.as_ptr().cast()), case);
        let (b, b_checked) = decode_avx2_nibbles(
            _mm256_loadu_si256(src.as_ptr().add(src.len() - 32).cast()),
            case,
        );
        if _mm256_movemask_epi8(_mm256_or_si256(a_checked, b_checked)) != 0 {
            return Err(());
        }
        let packed = pack_avx2(a, b);
        _mm_storeu_si128(dst.as_mut_ptr().cast(), _mm256_castsi256_si128(packed));
        _mm_storeu_si128(
            dst.as_mut_ptr().add(dst.len() - 16).cast(),
            _mm256_extracti128_si256::<1>(packed),
        );
    } else if src.len() <= 128 {
        let (a, a_checked) = decode_avx2_nibbles(_mm256_loadu_si256(src.as_ptr().cast()), case);
        let (b, b_checked) =
            decode_avx2_nibbles(_mm256_loadu_si256(src.as_ptr().add(32).cast()), case);
        let (c, c_checked) = decode_avx2_nibbles(
            _mm256_loadu_si256(src.as_ptr().add(src.len() - 64).cast()),
            case,
        );
        let (d, d_checked) = decode_avx2_nibbles(
            _mm256_loadu_si256(src.as_ptr().add(src.len() - 32).cast()),
            case,
        );
        let checked = _mm256_or_si256(
            _mm256_or_si256(a_checked, b_checked),
            _mm256_or_si256(c_checked, d_checked),
        );
        if _mm256_movemask_epi8(checked) != 0 {
            return Err(());
        }
        _mm256_storeu_si256(dst.as_mut_ptr().cast(), pack_avx2(a, b));
        _mm256_storeu_si256(dst.as_mut_ptr().add(dst.len() - 32).cast(), pack_avx2(c, d));
    } else if src.len() <= 192 {
        let (a, a_checked) = decode_avx2_nibbles(_mm256_loadu_si256(src.as_ptr().cast()), case);
        let (b, b_checked) =
            decode_avx2_nibbles(_mm256_loadu_si256(src.as_ptr().add(32).cast()), case);
        let (c, c_checked) =
            decode_avx2_nibbles(_mm256_loadu_si256(src.as_ptr().add(64).cast()), case);
        let (d, d_checked) =
            decode_avx2_nibbles(_mm256_loadu_si256(src.as_ptr().add(96).cast()), case);
        let (e, e_checked) = decode_avx2_nibbles(
            _mm256_loadu_si256(src.as_ptr().add(src.len() - 64).cast()),
            case,
        );
        let (f, f_checked) = decode_avx2_nibbles(
            _mm256_loadu_si256(src.as_ptr().add(src.len() - 32).cast()),
            case,
        );
        let checked = _mm256_or_si256(
            _mm256_or_si256(a_checked, b_checked),
            _mm256_or_si256(
                _mm256_or_si256(c_checked, d_checked),
                _mm256_or_si256(e_checked, f_checked),
            ),
        );
        if _mm256_movemask_epi8(checked) != 0 {
            return Err(());
        }
        _mm256_storeu_si256(dst.as_mut_ptr().cast(), pack_avx2(a, b));
        _mm256_storeu_si256(dst.as_mut_ptr().add(32).cast(), pack_avx2(c, d));
        _mm256_storeu_si256(dst.as_mut_ptr().add(dst.len() - 32).cast(), pack_avx2(e, f));
    } else {
        if !hex_check_avx2_with_case(src, case) {
            return Err(());
        }
        hex_decode_avx2(src, dst);
    }
    Ok(())
}

#[target_feature(enable = "avx512f,avx512bw")]
#[inline]
pub(crate) unsafe fn hex_decode_avx512_checked(
    src: &[u8],
    dst: &mut [u8],
    case: CheckCase,
) -> Result<(), ()> {
    debug_assert!(src.len() >= 16);
    if src.len() < 64 {
        return hex_decode_avx2_checked(src, dst, case);
    }
    if src.len() == 64 {
        let (nibbles, checked) =
            decode_avx512_nibbles(_mm512_loadu_si512(src.as_ptr().cast()), case);
        if _mm512_movepi8_mask(checked) != 0 {
            return Err(());
        }
        _mm256_storeu_si256(dst.as_mut_ptr().cast(), pack_avx512(nibbles));
    } else if src.len() <= 128 {
        let (a, a_checked) = decode_avx512_nibbles(_mm512_loadu_si512(src.as_ptr().cast()), case);
        let (b, b_checked) = decode_avx512_nibbles(
            _mm512_loadu_si512(src.as_ptr().add(src.len() - 64).cast()),
            case,
        );
        if _mm512_movepi8_mask(_mm512_or_si512(a_checked, b_checked)) != 0 {
            return Err(());
        }
        _mm256_storeu_si256(dst.as_mut_ptr().cast(), pack_avx512(a));
        _mm256_storeu_si256(dst.as_mut_ptr().add(dst.len() - 32).cast(), pack_avx512(b));
    } else if src.len() <= 192 {
        let (a, a_checked) = decode_avx512_nibbles(_mm512_loadu_si512(src.as_ptr().cast()), case);
        let (b, b_checked) =
            decode_avx512_nibbles(_mm512_loadu_si512(src.as_ptr().add(64).cast()), case);
        let (c, c_checked) = decode_avx512_nibbles(
            _mm512_loadu_si512(src.as_ptr().add(src.len() - 64).cast()),
            case,
        );
        if _mm512_movepi8_mask(_mm512_or_si512(
            _mm512_or_si512(a_checked, b_checked),
            c_checked,
        )) != 0
        {
            return Err(());
        }
        _mm256_storeu_si256(dst.as_mut_ptr().cast(), pack_avx512(a));
        _mm256_storeu_si256(dst.as_mut_ptr().add(32).cast(), pack_avx512(b));
        _mm256_storeu_si256(dst.as_mut_ptr().add(dst.len() - 32).cast(), pack_avx512(c));
    } else {
        if !hex_check_avx512_with_case(src, case) {
            return Err(());
        }
        hex_decode_avx512(src, dst);
    }
    Ok(())
}

#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
unsafe fn pack_avx512(nibbles: __m512i) -> __m256i {
    _mm512_cvtepi16_epi8(_mm512_maddubs_epi16(nibbles, _mm512_set1_epi16(0x0110)))
}

#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(super) unsafe fn hex_decode_avx512(src: &[u8], dst: &mut [u8]) {
    if src.len() < 64 {
        return hex_decode_avx2(src, dst);
    }
    let convert = |input: &[u8; 64], output: &mut [u8; 32]| {
        let bytes = _mm512_loadu_si512(input.as_ptr().cast());
        let low = _mm512_and_si512(bytes, _mm512_set1_epi8(15));
        let letters = _mm512_cmpgt_epu8_mask(bytes, _mm512_set1_epi8(b'9' as i8));
        let nibbles = _mm512_mask_add_epi8(low, letters, low, _mm512_set1_epi8(9));
        _mm256_storeu_si256(output.as_mut_ptr().cast(), pack_avx512(nibbles));
    };
    let (blocks, tail) = src.as_chunks::<64>();
    for (input, output) in blocks.iter().zip(dst.as_chunks_mut::<32>().0) {
        convert(input, output);
    }
    if !tail.is_empty() {
        if let (Some(input), Some(output)) = (src.last_chunk::<64>(), dst.last_chunk_mut::<32>()) {
            convert(input, output);
        }
    }
}

#[inline]
#[target_feature(enable = "sse4.1")]
unsafe fn decode_sse41_block(a: __m128i, b: __m128i) -> __m128i {
    let nibble = |bytes| {
        // Valid ASCII letters add nine to their low nibble.
        _mm_add_epi8(
            _mm_and_si128(bytes, _mm_set1_epi8(15)),
            _mm_and_si128(
                _mm_cmpgt_epi8(bytes, _mm_set1_epi8(b'9' as i8)),
                _mm_set1_epi8(9),
            ),
        )
    };
    pack_sse41(nibble(a), nibble(b))
}

#[inline]
#[target_feature(enable = "avx2")]
unsafe fn decode_avx2_block(a: __m256i, b: __m256i) -> __m256i {
    let nibble = |bytes| {
        _mm256_add_epi8(
            _mm256_and_si256(bytes, _mm256_set1_epi8(15)),
            _mm256_and_si256(
                _mm256_cmpgt_epi8(bytes, _mm256_set1_epi8(b'9' as i8)),
                _mm256_set1_epi8(9),
            ),
        )
    };
    pack_avx2(nibble(a), nibble(b))
}

#[inline]
#[target_feature(enable = "sse4.1")]
unsafe fn pack_sse41(a: __m128i, b: __m128i) -> __m128i {
    // PMADDUBSW combines adjacent nibbles with [16, 1]; no result exceeds 255.
    let weights = _mm_set1_epi16(0x0110);
    _mm_packus_epi16(_mm_maddubs_epi16(a, weights), _mm_maddubs_epi16(b, weights))
}

#[inline]
#[target_feature(enable = "avx2")]
unsafe fn pack_avx2(a: __m256i, b: __m256i) -> __m256i {
    let weights = _mm256_set1_epi16(0x0110);
    // Packing is lane-local; restore the original order of the four 8-byte groups.
    _mm256_permute4x64_epi64::<0xd8>(_mm256_packus_epi16(
        _mm256_maddubs_epi16(a, weights),
        _mm256_maddubs_epi16(b, weights),
    ))
}

#[target_feature(enable = "sse4.1")]
pub(super) unsafe fn hex_decode_sse41(src: &[u8], dst: &mut [u8]) {
    if src.len() < 32 {
        return hex_decode_fallback(src, dst);
    }
    let convert = |input: &[u8; 32], output: &mut [u8; 16]| {
        let a = _mm_loadu_si128(input.as_ptr().cast());
        let b = _mm_loadu_si128(input.as_ptr().add(16).cast());
        _mm_storeu_si128(output.as_mut_ptr().cast(), decode_sse41_block(a, b));
    };
    let (blocks, tail) = src.as_chunks::<32>();
    for (input, output) in blocks.iter().zip(dst.as_chunks_mut::<16>().0) {
        convert(input, output);
    }
    if !tail.is_empty() {
        if let (Some(input), Some(output)) = (src.last_chunk::<32>(), dst.last_chunk_mut::<16>()) {
            convert(input, output);
        }
    }
}

#[target_feature(enable = "avx2")]
pub(super) unsafe fn hex_decode_avx2(src: &[u8], dst: &mut [u8]) {
    if src.len() < 64 {
        return hex_decode_sse41(src, dst);
    }
    let convert = |input: &[u8; 64], output: &mut [u8; 32]| {
        let a = _mm256_loadu_si256(input.as_ptr().cast());
        let b = _mm256_loadu_si256(input.as_ptr().add(32).cast());
        _mm256_storeu_si256(output.as_mut_ptr().cast(), decode_avx2_block(a, b));
    };
    let (blocks, tail) = src.as_chunks::<64>();
    for (input, output) in blocks.iter().zip(dst.as_chunks_mut::<32>().0) {
        convert(input, output);
    }
    if !tail.is_empty() {
        if let (Some(input), Some(output)) = (src.last_chunk::<64>(), dst.last_chunk_mut::<32>()) {
            convert(input, output);
        }
    }
}

// The exact-size output is private to an owning caller: earlier valid blocks
// may be written before a later block fails. The feature boundary surrounds
// the loop so each constant-size checked kernel can inline into it.
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn hex_decode_avx2_owned(
    src: &[u8],
    dst: &mut [u8],
    case: CheckCase,
) -> Result<(), ()> {
    let (blocks, tail) = src.as_chunks::<64>();
    let (outputs, rest) = dst.split_at_mut(blocks.len() * 32);
    for (input, output) in blocks.iter().zip(outputs.as_chunks_mut::<32>().0) {
        hex_decode_avx2_checked(input, output, case)?;
    }
    super::decode_checked(tail, rest, case)
}

#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn hex_decode_avx512_owned(
    src: &[u8],
    dst: &mut [u8],
    case: CheckCase,
) -> Result<(), ()> {
    let (blocks, tail) = src.as_chunks::<128>();
    let (outputs, rest) = dst.split_at_mut(blocks.len() * 64);
    for (input, output) in blocks.iter().zip(outputs.as_chunks_mut::<64>().0) {
        hex_decode_avx512_checked(input, output, case)?;
    }
    super::decode_checked(tail, rest, case)
}