base64_simd/
alsw.rs

1use vsimd::alsw::AlswLut;
2use vsimd::vector::{V128, V256};
3
4struct StandardAlsw;
5
6impl StandardAlsw {
7    #[inline]
8    const fn decode(c: u8) -> u8 {
9        match c {
10            b'A'..=b'Z' => c - b'A',
11            b'a'..=b'z' => c - b'a' + 26,
12            b'0'..=b'9' => c - b'0' + 52,
13            b'+' => 62,
14            b'/' => 63,
15            _ => 0xff,
16        }
17    }
18
19    #[inline]
20    const fn check_hash(i: u8) -> u8 {
21        match i {
22            0 => 5,
23            1..=9 => 2,
24            0xA => 4,
25            0xB => 6,
26            0xC..=0xE => 8,
27            0xF => 6,
28            _ => unreachable!(),
29        }
30    }
31
32    #[inline]
33    const fn decode_hash(i: u8) -> u8 {
34        match i {
35            0xB => 0x07,
36            0xF => 0x08,
37            _ => 0x01,
38        }
39    }
40}
41
42vsimd::impl_alsw!(StandardAlsw);
43
44struct UrlSafeAlsw;
45
46impl UrlSafeAlsw {
47    #[inline]
48    const fn decode(c: u8) -> u8 {
49        match c {
50            b'A'..=b'Z' => c - b'A',
51            b'a'..=b'z' => c - b'a' + 26,
52            b'0'..=b'9' => c - b'0' + 52,
53            b'-' => 62,
54            b'_' => 63,
55            _ => 0xff,
56        }
57    }
58
59    #[inline]
60    const fn check_hash(i: u8) -> u8 {
61        match i {
62            0 => 7,
63            1..=9 => 2,
64            0xA => 4,
65            0xB | 0xC => 6,
66            0xD => 8,
67            0xE => 6,
68            0xF => 6,
69            _ => unreachable!(),
70        }
71    }
72
73    #[inline]
74    const fn decode_hash(i: u8) -> u8 {
75        match i {
76            0xD => 0x01,
77            0xF => 0x05,
78            _ => 0x01,
79        }
80    }
81}
82
83vsimd::impl_alsw!(UrlSafeAlsw);
84
85pub const STANDARD_ALSW_CHECK_X2: AlswLut<V256> = StandardAlsw::check_lut().x2();
86pub const STANDARD_ALSW_DECODE_X2: AlswLut<V256> = StandardAlsw::decode_lut().x2();
87
88pub const URL_SAFE_ALSW_CHECK_X2: AlswLut<V256> = UrlSafeAlsw::check_lut().x2();
89pub const URL_SAFE_ALSW_DECODE_X2: AlswLut<V256> = UrlSafeAlsw::decode_lut().x2();
90
91#[cfg(test)]
92mod algorithm {
93    use super::*;
94
95    #[test]
96    #[ignore]
97    fn standard_alsw() {
98        StandardAlsw::test_check();
99        StandardAlsw::test_decode();
100    }
101
102    #[test]
103    #[ignore]
104    fn url_safe_alsw() {
105        UrlSafeAlsw::test_check();
106        UrlSafeAlsw::test_decode();
107    }
108}