/rust/registry/src/index.crates.io-6f17d22bba15001f/httparse-1.10.1/src/simd/avx2.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use crate::iter::Bytes; |
2 | | |
3 | | #[inline] |
4 | | #[target_feature(enable = "avx2")] |
5 | 0 | pub unsafe fn match_uri_vectored(bytes: &mut Bytes) { |
6 | 0 | while bytes.as_ref().len() >= 32 { |
7 | | |
8 | 0 | let advance = match_url_char_32_avx(bytes.as_ref()); |
9 | 0 |
|
10 | 0 | bytes.advance(advance); |
11 | 0 |
|
12 | 0 | if advance != 32 { |
13 | 0 | return; |
14 | 0 | } |
15 | | } |
16 | | // NOTE: use SWAR for <32B, more efficient than falling back to SSE4.2 |
17 | 0 | super::swar::match_uri_vectored(bytes) |
18 | 0 | } |
19 | | |
20 | | #[inline(always)] |
21 | | #[allow(non_snake_case, overflowing_literals)] |
22 | | #[allow(unused)] |
23 | 0 | unsafe fn match_url_char_32_avx(buf: &[u8]) -> usize { |
24 | 0 | // NOTE: This check might be not necessary since this function is only used in |
25 | 0 | // `match_uri_vectored` where buffer overflow is taken care of. |
26 | 0 | debug_assert!(buf.len() >= 32); |
27 | | |
28 | | #[cfg(target_arch = "x86")] |
29 | | use core::arch::x86::*; |
30 | | #[cfg(target_arch = "x86_64")] |
31 | | use core::arch::x86_64::*; |
32 | | |
33 | | // pointer to buffer |
34 | 0 | let ptr = buf.as_ptr(); |
35 | 0 |
|
36 | 0 | // %x21-%x7e %x80-%xff |
37 | 0 | // |
38 | 0 | // Character ranges allowed by this function, can also be interpreted as: |
39 | 0 | // 33 =< (x != 127) =< 255 |
40 | 0 | // |
41 | 0 | // Create a vector full of DEL (0x7f) characters. |
42 | 0 | let DEL: __m256i = _mm256_set1_epi8(0x7f); |
43 | 0 | // Create a vector full of exclamation mark (!) (0x21) characters. |
44 | 0 | // Used as lower threshold, characters in URLs cannot be smaller than this. |
45 | 0 | let LOW: __m256i = _mm256_set1_epi8(0x21); |
46 | 0 |
|
47 | 0 | // Load a chunk of 32 bytes from `ptr` as a vector. |
48 | 0 | // We can check 32 bytes in parallel at most with AVX2 since |
49 | 0 | // YMM registers can only have 256 bits most. |
50 | 0 | let dat = _mm256_lddqu_si256(ptr as *const _); |
51 | 0 |
|
52 | 0 | // unsigned comparison dat >= LOW |
53 | 0 | // |
54 | 0 | // `_mm256_max_epu8` creates a new vector by comparing vectors `dat` and `LOW` |
55 | 0 | // and picks the max. values from each for all indices. |
56 | 0 | // So if a byte in `dat` is <= 32, it'll be represented as 33 |
57 | 0 | // which is the smallest valid character. |
58 | 0 | // |
59 | 0 | // Then, we compare the new vector with `dat` for equality. |
60 | 0 | // |
61 | 0 | // `_mm256_cmpeq_epi8` returns a new vector where; |
62 | 0 | // * matching bytes are set to 0xFF (all bits set), |
63 | 0 | // * nonmatching bytes are set to 0 (no bits set). |
64 | 0 | let low = _mm256_cmpeq_epi8(_mm256_max_epu8(dat, LOW), dat); |
65 | 0 | // Similar to what we did before, but now invalid characters are set to 0xFF. |
66 | 0 | let del = _mm256_cmpeq_epi8(dat, DEL); |
67 | 0 |
|
68 | 0 | // We glue the both comparisons via `_mm256_andnot_si256`. |
69 | 0 | // |
70 | 0 | // Since the representation of truthiness differ in these comparisons, |
71 | 0 | // we are in need of bitwise NOT to convert valid characters of `del`. |
72 | 0 | let bit = _mm256_andnot_si256(del, low); |
73 | 0 | // This creates a bitmask from the most significant bit of each byte. |
74 | 0 | // Simply, we're converting a vector value to scalar value here. |
75 | 0 | let res = _mm256_movemask_epi8(bit) as u32; |
76 | 0 |
|
77 | 0 | // Count trailing zeros to find the first encountered invalid character. |
78 | 0 | // Bitwise NOT is required once again to flip truthiness. |
79 | 0 | // TODO: use .trailing_ones() once MSRV >= 1.46 |
80 | 0 | (!res).trailing_zeros() as usize |
81 | 0 | } |
82 | | |
83 | | #[target_feature(enable = "avx2")] |
84 | 0 | pub unsafe fn match_header_value_vectored(bytes: &mut Bytes) { |
85 | 0 | while bytes.as_ref().len() >= 32 { |
86 | 0 | let advance = match_header_value_char_32_avx(bytes.as_ref()); |
87 | 0 | bytes.advance(advance); |
88 | 0 |
|
89 | 0 | if advance != 32 { |
90 | 0 | return; |
91 | 0 | } |
92 | | } |
93 | | // NOTE: use SWAR for <32B, more efficient than falling back to SSE4.2 |
94 | 0 | super::swar::match_header_value_vectored(bytes) |
95 | 0 | } |
96 | | |
97 | | #[inline(always)] |
98 | | #[allow(non_snake_case)] |
99 | | #[allow(unused)] |
100 | 0 | unsafe fn match_header_value_char_32_avx(buf: &[u8]) -> usize { |
101 | 0 | debug_assert!(buf.len() >= 32); |
102 | | |
103 | | #[cfg(target_arch = "x86")] |
104 | | use core::arch::x86::*; |
105 | | #[cfg(target_arch = "x86_64")] |
106 | | use core::arch::x86_64::*; |
107 | | |
108 | 0 | let ptr = buf.as_ptr(); |
109 | 0 |
|
110 | 0 | // %x09 %x20-%x7e %x80-%xff |
111 | 0 | // Create a vector full of horizontal tab (\t) (0x09) characters. |
112 | 0 | let TAB: __m256i = _mm256_set1_epi8(0x09); |
113 | 0 | // Create a vector full of DEL (0x7f) characters. |
114 | 0 | let DEL: __m256i = _mm256_set1_epi8(0x7f); |
115 | 0 | // Create a vector full of space (0x20) characters. |
116 | 0 | let LOW: __m256i = _mm256_set1_epi8(0x20); |
117 | 0 |
|
118 | 0 | // Load a chunk of 32 bytes from `ptr` as a vector. |
119 | 0 | let dat = _mm256_lddqu_si256(ptr as *const _); |
120 | 0 |
|
121 | 0 | // unsigned comparison dat >= LOW |
122 | 0 | // |
123 | 0 | // Same as what we do in `match_url_char_32_avx`. |
124 | 0 | // This time the lower threshold is set to space character though. |
125 | 0 | let low = _mm256_cmpeq_epi8(_mm256_max_epu8(dat, LOW), dat); |
126 | 0 | // Check if `dat` includes `TAB` characters. |
127 | 0 | let tab = _mm256_cmpeq_epi8(dat, TAB); |
128 | 0 | // Check if `dat` includes `DEL` characters. |
129 | 0 | let del = _mm256_cmpeq_epi8(dat, DEL); |
130 | 0 |
|
131 | 0 | // Combine all comparisons together, notice that we're also using OR |
132 | 0 | // to connect `low` and `tab` but flip bits of `del`. |
133 | 0 | // |
134 | 0 | // In the end, this is simply: |
135 | 0 | // ~del & (low | tab) |
136 | 0 | let bit = _mm256_andnot_si256(del, _mm256_or_si256(low, tab)); |
137 | 0 | // This creates a bitmask from the most significant bit of each byte. |
138 | 0 | // Creates a scalar value from vector value. |
139 | 0 | let res = _mm256_movemask_epi8(bit) as u32; |
140 | 0 |
|
141 | 0 | // Count trailing zeros to find the first encountered invalid character. |
142 | 0 | // Bitwise NOT is required once again to flip truthiness. |
143 | 0 | // TODO: use .trailing_ones() once MSRV >= 1.46 |
144 | 0 | (!res).trailing_zeros() as usize |
145 | 0 | } |
146 | | |
147 | | #[test] |
148 | | fn avx2_code_matches_uri_chars_table() { |
149 | | if !is_x86_feature_detected!("avx2") { |
150 | | return; |
151 | | } |
152 | | |
153 | | #[allow(clippy::undocumented_unsafe_blocks)] |
154 | | unsafe { |
155 | | assert!(byte_is_allowed(b'_', match_uri_vectored)); |
156 | | |
157 | | for (b, allowed) in crate::URI_MAP.iter().cloned().enumerate() { |
158 | | assert_eq!( |
159 | | byte_is_allowed(b as u8, match_uri_vectored), allowed, |
160 | | "byte_is_allowed({:?}) should be {:?}", b, allowed, |
161 | | ); |
162 | | } |
163 | | } |
164 | | } |
165 | | |
166 | | #[test] |
167 | | fn avx2_code_matches_header_value_chars_table() { |
168 | | if !is_x86_feature_detected!("avx2") { |
169 | | return; |
170 | | } |
171 | | |
172 | | #[allow(clippy::undocumented_unsafe_blocks)] |
173 | | unsafe { |
174 | | assert!(byte_is_allowed(b'_', match_header_value_vectored)); |
175 | | |
176 | | for (b, allowed) in crate::HEADER_VALUE_MAP.iter().cloned().enumerate() { |
177 | | assert_eq!( |
178 | | byte_is_allowed(b as u8, match_header_value_vectored), allowed, |
179 | | "byte_is_allowed({:?}) should be {:?}", b, allowed, |
180 | | ); |
181 | | } |
182 | | } |
183 | | } |
184 | | |
185 | | #[cfg(test)] |
186 | | unsafe fn byte_is_allowed(byte: u8, f: unsafe fn(bytes: &mut Bytes<'_>)) -> bool { |
187 | | let slice = [ |
188 | | b'_', b'_', b'_', b'_', |
189 | | b'_', b'_', b'_', b'_', |
190 | | b'_', b'_', b'_', b'_', |
191 | | b'_', b'_', b'_', b'_', |
192 | | b'_', b'_', b'_', b'_', |
193 | | b'_', b'_', b'_', b'_', |
194 | | b'_', b'_', byte, b'_', |
195 | | b'_', b'_', b'_', b'_', |
196 | | ]; |
197 | | let mut bytes = Bytes::new(&slice); |
198 | | |
199 | | f(&mut bytes); |
200 | | |
201 | | match bytes.pos() { |
202 | | 32 => true, |
203 | | 26 => false, |
204 | | _ => unreachable!(), |
205 | | } |
206 | | } |