Debian Codegraph @

debian/rust-vendor/blake3/src/platform.rs

raw
1 use crate::{BLOCK_LEN, CVWords, IncrementCounter, portable};
2
3 cfg_if::cfg_if! {
4 if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
5 cfg_if::cfg_if! {
6 if #[cfg(blake3_avx512_ffi)] {
7 pub const MAX_SIMD_DEGREE: usize = 16;
8 } else {
9 pub const MAX_SIMD_DEGREE: usize = 8;
10 }
11 }
12 } else if #[cfg(blake3_neon)] {
13 pub const MAX_SIMD_DEGREE: usize = 4;
14 } else if #[cfg(blake3_wasm32_simd)] {
15 pub const MAX_SIMD_DEGREE: usize = 4;
16 } else {
17 pub const MAX_SIMD_DEGREE: usize = 1;
18 }
19 }
20
21 // There are some places where we want a static size that's equal to the
22 // MAX_SIMD_DEGREE, but also at least 2. Constant contexts aren't currently
23 // allowed to use cmp::max, so we have to hardcode this additional constant
24 // value. Get rid of this once cmp::max is a const fn.
25 cfg_if::cfg_if! {
26 if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
27 cfg_if::cfg_if! {
28 if #[cfg(blake3_avx512_ffi)] {
29 pub const MAX_SIMD_DEGREE_OR_2: usize = 16;
30 } else {
31 pub const MAX_SIMD_DEGREE_OR_2: usize = 8;
32 }
33 }
34 } else if #[cfg(blake3_neon)] {
35 pub const MAX_SIMD_DEGREE_OR_2: usize = 4;
36 } else if #[cfg(blake3_wasm32_simd)] {
37 pub const MAX_SIMD_DEGREE_OR_2: usize = 4;
38 } else {
39 pub const MAX_SIMD_DEGREE_OR_2: usize = 2;
40 }
41 }
42
43 #[derive(Clone, Copy, Debug)]
44 pub enum Platform {
45 Portable,
46 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
47 SSE2,
48 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
49 SSE41,
50 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
51 AVX2,
52 #[cfg(blake3_avx512_ffi)]
53 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
54 AVX512,
55 #[cfg(blake3_neon)]
56 NEON,
57 #[cfg(blake3_wasm32_simd)]
58 #[allow(non_camel_case_types)]
59 WASM32_SIMD,
60 }
61
62 impl Platform {
63 #[allow(unreachable_code)]
64 pub fn detect() -> Self {
65 #[cfg(miri)]
66 {
67 return Platform::Portable;
68 }
69
70 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
71 {
72 #[cfg(blake3_avx512_ffi)]
73 {
74 if avx512_detected() {
75 return Platform::AVX512;
76 }
77 }
78 if avx2_detected() {
79 return Platform::AVX2;
80 }
81 if sse41_detected() {
82 return Platform::SSE41;
83 }
84 if sse2_detected() {
85 return Platform::SSE2;
86 }
87 }
88 // We don't use dynamic feature detection for NEON. If the "neon"
89 // feature is on, NEON is assumed to be supported.
90 #[cfg(blake3_neon)]
91 {
92 return Platform::NEON;
93 }
94 #[cfg(blake3_wasm32_simd)]
95 {
96 return Platform::WASM32_SIMD;
97 }
98 Platform::Portable
99 }
100
101 pub fn simd_degree(&self) -> usize {
102 let degree = match self {
103 Platform::Portable => 1,
104 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
105 Platform::SSE2 => 4,
106 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
107 Platform::SSE41 => 4,
108 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
109 Platform::AVX2 => 8,
110 #[cfg(blake3_avx512_ffi)]
111 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
112 Platform::AVX512 => 16,
113 #[cfg(blake3_neon)]
114 Platform::NEON => 4,
115 #[cfg(blake3_wasm32_simd)]
116 Platform::WASM32_SIMD => 4,
117 };
118 debug_assert!(degree <= MAX_SIMD_DEGREE);
119 degree
120 }
121
122 pub fn compress_in_place(
123 &self,
124 cv: &mut CVWords,
125 block: &[u8; BLOCK_LEN],
126 block_len: u8,
127 counter: u64,
128 flags: u8,
129 ) {
130 match self {
131 Platform::Portable => portable::compress_in_place(cv, block, block_len, counter, flags),
132 // Safe because detect() checked for platform support.
133 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
134 Platform::SSE2 => unsafe {
135 crate::sse2::compress_in_place(cv, block, block_len, counter, flags)
136 },
137 // Safe because detect() checked for platform support.
138 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
139 Platform::SSE41 | Platform::AVX2 => unsafe {
140 crate::sse41::compress_in_place(cv, block, block_len, counter, flags)
141 },
142 // Safe because detect() checked for platform support.
143 #[cfg(blake3_avx512_ffi)]
144 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
145 Platform::AVX512 => unsafe {
146 crate::avx512::compress_in_place(cv, block, block_len, counter, flags)
147 },
148 // No NEON compress_in_place() implementation yet.
149 #[cfg(blake3_neon)]
150 Platform::NEON => portable::compress_in_place(cv, block, block_len, counter, flags),
151 #[cfg(blake3_wasm32_simd)]
152 Platform::WASM32_SIMD => {
153 crate::wasm32_simd::compress_in_place(cv, block, block_len, counter, flags)
154 }
155 }
156 }
157
158 pub fn compress_xof(
159 &self,
160 cv: &CVWords,
161 block: &[u8; BLOCK_LEN],
162 block_len: u8,
163 counter: u64,
164 flags: u8,
165 ) -> [u8; 64] {
166 match self {
167 Platform::Portable => portable::compress_xof(cv, block, block_len, counter, flags),
168 // Safe because detect() checked for platform support.
169 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
170 Platform::SSE2 => unsafe {
171 crate::sse2::compress_xof(cv, block, block_len, counter, flags)
172 },
173 // Safe because detect() checked for platform support.
174 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
175 Platform::SSE41 | Platform::AVX2 => unsafe {
176 crate::sse41::compress_xof(cv, block, block_len, counter, flags)
177 },
178 // Safe because detect() checked for platform support.
179 #[cfg(blake3_avx512_ffi)]
180 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
181 Platform::AVX512 => unsafe {
182 crate::avx512::compress_xof(cv, block, block_len, counter, flags)
183 },
184 // No NEON compress_xof() implementation yet.
185 #[cfg(blake3_neon)]
186 Platform::NEON => portable::compress_xof(cv, block, block_len, counter, flags),
187 #[cfg(blake3_wasm32_simd)]
188 Platform::WASM32_SIMD => {
189 crate::wasm32_simd::compress_xof(cv, block, block_len, counter, flags)
190 }
191 }
192 }
193
194 // IMPLEMENTATION NOTE
195 // ===================
196 // hash_many() applies two optimizations. The critically important
197 // optimization is the high-performance parallel SIMD hashing mode,
198 // described in detail in the spec. This more than doubles throughput per
199 // thread. Another optimization is keeping the state vectors transposed
200 // from block to block within a chunk. When state vectors are transposed
201 // after every block, there's a small but measurable performance loss.
202 // Compressing chunks with a dedicated loop avoids this.
203
204 pub fn hash_many<const N: usize>(
205 &self,
206 inputs: &[&[u8; N]],
207 key: &CVWords,
208 counter: u64,
209 increment_counter: IncrementCounter,
210 flags: u8,
211 flags_start: u8,
212 flags_end: u8,
213 out: &mut [u8],
214 ) {
215 match self {
216 Platform::Portable => portable::hash_many(
217 inputs,
218 key,
219 counter,
220 increment_counter,
221 flags,
222 flags_start,
223 flags_end,
224 out,
225 ),
226 // Safe because detect() checked for platform support.
227 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
228 Platform::SSE2 => unsafe {
229 crate::sse2::hash_many(
230 inputs,
231 key,
232 counter,
233 increment_counter,
234 flags,
235 flags_start,
236 flags_end,
237 out,
238 )
239 },
240 // Safe because detect() checked for platform support.
241 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
242 Platform::SSE41 => unsafe {
243 crate::sse41::hash_many(
244 inputs,
245 key,
246 counter,
247 increment_counter,
248 flags,
249 flags_start,
250 flags_end,
251 out,
252 )
253 },
254 // Safe because detect() checked for platform support.
255 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
256 Platform::AVX2 => unsafe {
257 crate::avx2::hash_many(
258 inputs,
259 key,
260 counter,
261 increment_counter,
262 flags,
263 flags_start,
264 flags_end,
265 out,
266 )
267 },
268 // Safe because detect() checked for platform support.
269 #[cfg(blake3_avx512_ffi)]
270 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
271 Platform::AVX512 => unsafe {
272 crate::avx512::hash_many(
273 inputs,
274 key,
275 counter,
276 increment_counter,
277 flags,
278 flags_start,
279 flags_end,
280 out,
281 )
282 },
283 // Assumed to be safe if the "neon" feature is on.
284 #[cfg(blake3_neon)]
285 Platform::NEON => unsafe {
286 crate::neon::hash_many(
287 inputs,
288 key,
289 counter,
290 increment_counter,
291 flags,
292 flags_start,
293 flags_end,
294 out,
295 )
296 },
297 // Assumed to be safe if the "wasm32_simd" feature is on.
298 #[cfg(blake3_wasm32_simd)]
299 Platform::WASM32_SIMD => unsafe {
300 crate::wasm32_simd::hash_many(
301 inputs,
302 key,
303 counter,
304 increment_counter,
305 flags,
306 flags_start,
307 flags_end,
308 out,
309 )
310 },
311 }
312 }
313
314 pub fn xof_many(
315 &self,
316 cv: &CVWords,
317 block: &[u8; BLOCK_LEN],
318 block_len: u8,
319 mut counter: u64,
320 flags: u8,
321 out: &mut [u8],
322 ) {
323 debug_assert_eq!(0, out.len() % BLOCK_LEN, "whole blocks only");
324 if out.is_empty() {
325 // The current assembly implementation always outputs at least 1 block.
326 return;
327 }
328 match self {
329 // Safe because detect() checked for platform support.
330 #[cfg(blake3_avx512_ffi)]
331 #[cfg(unix)]
332 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
333 Platform::AVX512 => unsafe {
334 crate::avx512::xof_many(cv, block, block_len, counter, flags, out)
335 },
336 _ => {
337 // For platforms without an optimized xof_many, fall back to a loop over
338 // compress_xof. This is still faster than portable code.
339 for out_block in out.chunks_exact_mut(BLOCK_LEN) {
340 // TODO: Use array_chunks_mut here once that's stable.
341 let out_array: &mut [u8; BLOCK_LEN] = out_block.try_into().unwrap();
342 *out_array = self.compress_xof(cv, block, block_len, counter, flags);
343 counter += 1;
344 }
345 }
346 }
347 }
348
349 // Explicit platform constructors, for benchmarks.
350
351 pub fn portable() -> Self {
352 Self::Portable
353 }
354
355 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
356 pub fn sse2() -> Option<Self> {
357 if sse2_detected() {
358 Some(Self::SSE2)
359 } else {
360 None
361 }
362 }
363
364 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
365 pub fn sse41() -> Option<Self> {
366 if sse41_detected() {
367 Some(Self::SSE41)
368 } else {
369 None
370 }
371 }
372
373 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
374 pub fn avx2() -> Option<Self> {
375 if avx2_detected() {
376 Some(Self::AVX2)
377 } else {
378 None
379 }
380 }
381
382 #[cfg(blake3_avx512_ffi)]
383 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
384 pub fn avx512() -> Option<Self> {
385 if avx512_detected() {
386 Some(Self::AVX512)
387 } else {
388 None
389 }
390 }
391
392 #[cfg(blake3_neon)]
393 pub fn neon() -> Option<Self> {
394 // Assumed to be safe if the "neon" feature is on.
395 Some(Self::NEON)
396 }
397
398 #[cfg(blake3_wasm32_simd)]
399 pub fn wasm32_simd() -> Option<Self> {
400 // Assumed to be safe if the "wasm32_simd" feature is on.
401 Some(Self::WASM32_SIMD)
402 }
403 }
404
405 // Note that AVX-512 is divided into multiple featuresets, and we use two of
406 // them, F and VL.
407 #[cfg(blake3_avx512_ffi)]
408 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
409 #[inline(always)]
410 #[allow(deprecated)] // TODO: revert this after https://github.com/RustCrypto/utils/pull/1515 is released
411 pub fn avx512_detected() -> bool {
412 if cfg!(miri) {
413 return false;
414 }
415
416 // A testing-only short-circuit.
417 if cfg!(feature = "no_avx512") {
418 return false;
419 }
420
421 cpufeatures::new!(has_avx512, "avx512f", "avx512vl");
422 has_avx512::get()
423 }
424
425 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
426 #[inline(always)]
427 #[allow(deprecated)] // TODO: revert this after https://github.com/RustCrypto/utils/pull/1515 is released
428 pub fn avx2_detected() -> bool {
429 if cfg!(miri) {
430 return false;
431 }
432
433 // A testing-only short-circuit.
434 if cfg!(feature = "no_avx2") {
435 return false;
436 }
437
438 cpufeatures::new!(has_avx2, "avx2");
439 has_avx2::get()
440 }
441
442 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
443 #[inline(always)]
444 #[allow(deprecated)] // TODO: revert this after https://github.com/RustCrypto/utils/pull/1515 is released
445 pub fn sse41_detected() -> bool {
446 if cfg!(miri) {
447 return false;
448 }
449
450 // A testing-only short-circuit.
451 if cfg!(feature = "no_sse41") {
452 return false;
453 }
454
455 cpufeatures::new!(has_sse41, "sse4.1");
456 has_sse41::get()
457 }
458
459 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
460 #[inline(always)]
461 #[allow(deprecated)] // TODO: revert this after https://github.com/RustCrypto/utils/pull/1515 is released
462 pub fn sse2_detected() -> bool {
463 if cfg!(miri) {
464 return false;
465 }
466
467 // A testing-only short-circuit.
468 if cfg!(feature = "no_sse2") {
469 return false;
470 }
471
472 cpufeatures::new!(has_sse2, "sse2");
473 has_sse2::get()
474 }
475
476 #[inline(always)]
477 pub fn words_from_le_bytes_32(bytes: &[u8; 32]) -> [u32; 8] {
478 let mut out = [0; 8];
479 out[0] = u32::from_le_bytes(bytes[0 * 4..][..4].try_into().unwrap());
480 out[1] = u32::from_le_bytes(bytes[1 * 4..][..4].try_into().unwrap());
481 out[2] = u32::from_le_bytes(bytes[2 * 4..][..4].try_into().unwrap());
482 out[3] = u32::from_le_bytes(bytes[3 * 4..][..4].try_into().unwrap());
483 out[4] = u32::from_le_bytes(bytes[4 * 4..][..4].try_into().unwrap());
484 out[5] = u32::from_le_bytes(bytes[5 * 4..][..4].try_into().unwrap());
485 out[6] = u32::from_le_bytes(bytes[6 * 4..][..4].try_into().unwrap());
486 out[7] = u32::from_le_bytes(bytes[7 * 4..][..4].try_into().unwrap());
487 out
488 }
489
490 #[inline(always)]
491 pub fn words_from_le_bytes_64(bytes: &[u8; 64]) -> [u32; 16] {
492 let mut out = [0; 16];
493 out[0] = u32::from_le_bytes(bytes[0 * 4..][..4].try_into().unwrap());
494 out[1] = u32::from_le_bytes(bytes[1 * 4..][..4].try_into().unwrap());
495 out[2] = u32::from_le_bytes(bytes[2 * 4..][..4].try_into().unwrap());
496 out[3] = u32::from_le_bytes(bytes[3 * 4..][..4].try_into().unwrap());
497 out[4] = u32::from_le_bytes(bytes[4 * 4..][..4].try_into().unwrap());
498 out[5] = u32::from_le_bytes(bytes[5 * 4..][..4].try_into().unwrap());
499 out[6] = u32::from_le_bytes(bytes[6 * 4..][..4].try_into().unwrap());
500 out[7] = u32::from_le_bytes(bytes[7 * 4..][..4].try_into().unwrap());
501 out[8] = u32::from_le_bytes(bytes[8 * 4..][..4].try_into().unwrap());
502 out[9] = u32::from_le_bytes(bytes[9 * 4..][..4].try_into().unwrap());
503 out[10] = u32::from_le_bytes(bytes[10 * 4..][..4].try_into().unwrap());
504 out[11] = u32::from_le_bytes(bytes[11 * 4..][..4].try_into().unwrap());
505 out[12] = u32::from_le_bytes(bytes[12 * 4..][..4].try_into().unwrap());
506 out[13] = u32::from_le_bytes(bytes[13 * 4..][..4].try_into().unwrap());
507 out[14] = u32::from_le_bytes(bytes[14 * 4..][..4].try_into().unwrap());
508 out[15] = u32::from_le_bytes(bytes[15 * 4..][..4].try_into().unwrap());
509 out
510 }
511
512 #[inline(always)]
513 pub fn le_bytes_from_words_32(words: &[u32; 8]) -> [u8; 32] {
514 let mut out = [0; 32];
515 *<&mut [u8; 4]>::try_from(&mut out[0 * 4..][..4]).unwrap() = words[0].to_le_bytes();
516 *<&mut [u8; 4]>::try_from(&mut out[1 * 4..][..4]).unwrap() = words[1].to_le_bytes();
517 *<&mut [u8; 4]>::try_from(&mut out[2 * 4..][..4]).unwrap() = words[2].to_le_bytes();
518 *<&mut [u8; 4]>::try_from(&mut out[3 * 4..][..4]).unwrap() = words[3].to_le_bytes();
519 *<&mut [u8; 4]>::try_from(&mut out[4 * 4..][..4]).unwrap() = words[4].to_le_bytes();
520 *<&mut [u8; 4]>::try_from(&mut out[5 * 4..][..4]).unwrap() = words[5].to_le_bytes();
521 *<&mut [u8; 4]>::try_from(&mut out[6 * 4..][..4]).unwrap() = words[6].to_le_bytes();
522 *<&mut [u8; 4]>::try_from(&mut out[7 * 4..][..4]).unwrap() = words[7].to_le_bytes();
523 out
524 }
525
526 #[inline(always)]
527 pub fn le_bytes_from_words_64(words: &[u32; 16]) -> [u8; 64] {
528 let mut out = [0; 64];
529 *<&mut [u8; 4]>::try_from(&mut out[0 * 4..][..4]).unwrap() = words[0].to_le_bytes();
530 *<&mut [u8; 4]>::try_from(&mut out[1 * 4..][..4]).unwrap() = words[1].to_le_bytes();
531 *<&mut [u8; 4]>::try_from(&mut out[2 * 4..][..4]).unwrap() = words[2].to_le_bytes();
532 *<&mut [u8; 4]>::try_from(&mut out[3 * 4..][..4]).unwrap() = words[3].to_le_bytes();
533 *<&mut [u8; 4]>::try_from(&mut out[4 * 4..][..4]).unwrap() = words[4].to_le_bytes();
534 *<&mut [u8; 4]>::try_from(&mut out[5 * 4..][..4]).unwrap() = words[5].to_le_bytes();
535 *<&mut [u8; 4]>::try_from(&mut out[6 * 4..][..4]).unwrap() = words[6].to_le_bytes();
536 *<&mut [u8; 4]>::try_from(&mut out[7 * 4..][..4]).unwrap() = words[7].to_le_bytes();
537 *<&mut [u8; 4]>::try_from(&mut out[8 * 4..][..4]).unwrap() = words[8].to_le_bytes();
538 *<&mut [u8; 4]>::try_from(&mut out[9 * 4..][..4]).unwrap() = words[9].to_le_bytes();
539 *<&mut [u8; 4]>::try_from(&mut out[10 * 4..][..4]).unwrap() = words[10].to_le_bytes();
540 *<&mut [u8; 4]>::try_from(&mut out[11 * 4..][..4]).unwrap() = words[11].to_le_bytes();
541 *<&mut [u8; 4]>::try_from(&mut out[12 * 4..][..4]).unwrap() = words[12].to_le_bytes();
542 *<&mut [u8; 4]>::try_from(&mut out[13 * 4..][..4]).unwrap() = words[13].to_le_bytes();
543 *<&mut [u8; 4]>::try_from(&mut out[14 * 4..][..4]).unwrap() = words[14].to_le_bytes();
544 *<&mut [u8; 4]>::try_from(&mut out[15 * 4..][..4]).unwrap() = words[15].to_le_bytes();
545 out
546 }