base64_simd/
check.rs

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
use crate::alsw::{STANDARD_ALSW_CHECK_X2, URL_SAFE_ALSW_CHECK_X2};
use crate::decode::{decode_ascii4, decode_ascii8, decode_extra};
use crate::decode::{STANDARD_DECODE_TABLE, URL_SAFE_DECODE_TABLE};
use crate::{Config, Error, Kind};

use vsimd::alsw::AlswLut;
use vsimd::vector::V256;
use vsimd::SIMD256;

use core::ptr::null_mut;

#[inline]
pub(crate) unsafe fn check_fallback(mut src: *const u8, mut n: usize, config: Config) -> Result<(), Error> {
    let kind = config.kind;
    let forgiving = config.extra.forgiving();

    let table = match kind {
        Kind::Standard => STANDARD_DECODE_TABLE.as_ptr(),
        Kind::UrlSafe => URL_SAFE_DECODE_TABLE.as_ptr(),
    };

    unsafe {
        // n*3/4 >= 6+2
        while n >= 11 {
            decode_ascii8::<false>(src, null_mut(), table)?;
            src = src.add(8);
            n -= 8;
        }

        while n >= 4 {
            decode_ascii4::<false>(src, null_mut(), table)?;
            src = src.add(4);
            n -= 4;
        }

        decode_extra::<false>(n, src, null_mut(), table, forgiving)
    }
}

#[inline(always)]
pub(crate) unsafe fn check_simd<S: SIMD256>(
    s: S,
    mut src: *const u8,
    mut n: usize,
    config: Config,
) -> Result<(), Error> {
    let kind = config.kind;

    let check_lut = match kind {
        Kind::Standard => STANDARD_ALSW_CHECK_X2,
        Kind::UrlSafe => URL_SAFE_ALSW_CHECK_X2,
    };

    unsafe {
        // n*3/4 >= 24+4
        while n >= 38 {
            let x = s.v256_load_unaligned(src);
            let is_valid = check_ascii32(s, x, check_lut);
            ensure!(is_valid);
            src = src.add(32);
            n -= 32;
        }

        check_fallback(src, n, config)
    }
}

#[inline(always)]
fn check_ascii32<S: SIMD256>(s: S, x: V256, check: AlswLut<V256>) -> bool {
    vsimd::alsw::check_ascii_xn(s, x, check)
}