Skip to content

Commit

Permalink
make blake2 with simd_opt work on nightly (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril committed Aug 25, 2021
1 parent 1cb3dee commit a7a5ad4
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
1 change: 1 addition & 0 deletions blake2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
)]
#![warn(missing_docs, rust_2018_idioms)]
#![cfg_attr(feature = "simd", feature(platform_intrinsics, repr_simd))]
#![cfg_attr(feature = "simd", allow(incomplete_features))]
#![cfg_attr(feature = "simd_asm", feature(asm))]

#[cfg(feature = "std")]
Expand Down
9 changes: 6 additions & 3 deletions blake2/src/simd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ macro_rules! impl_vector4 {
#[inline(always)]
fn shuffle_left_1(self) -> Self {
use crate::simd::simdint::simd_shuffle4;
unsafe { simd_shuffle4(self, self, [1, 2, 3, 0]) }
const IDX: [u32; 4] = [1, 2, 3, 0];
unsafe { simd_shuffle4(self, self, IDX) }
}

#[cfg(not(feature = "simd"))]
Expand All @@ -109,7 +110,8 @@ macro_rules! impl_vector4 {
#[inline(always)]
fn shuffle_left_2(self) -> Self {
use crate::simd::simdint::simd_shuffle4;
unsafe { simd_shuffle4(self, self, [2, 3, 0, 1]) }
const IDX: [u32; 4] = [2, 3, 0, 1];
unsafe { simd_shuffle4(self, self, IDX) }
}

#[cfg(not(feature = "simd"))]
Expand All @@ -122,7 +124,8 @@ macro_rules! impl_vector4 {
#[inline(always)]
fn shuffle_left_3(self) -> Self {
use crate::simd::simdint::simd_shuffle4;
unsafe { simd_shuffle4(self, self, [3, 0, 1, 2]) }
const IDX: [u32; 4] = [3, 0, 1, 2];
unsafe { simd_shuffle4(self, self, IDX) }
}

#[cfg(not(feature = "simd"))]
Expand Down
5 changes: 3 additions & 2 deletions blake2/src/simd/simd_opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
#[allow(unused_macros)]
#[cfg(feature = "simd")]
macro_rules! transmute_shuffle {
($tmp:ident, $shuffle:ident, $vec:expr, $idx:expr) => {
($tmp:ident, $shuffle:ident, $vec:expr, $idx_n:expr, $idx:expr) => {
unsafe {
use crate::simd::simdint::$shuffle;
use crate::simd::simdty::$tmp;
use core::mem::transmute;

const IDX: [u32; $idx_n] = $idx;
let tmp_i: $tmp = transmute($vec);
let tmp_o: $tmp = $shuffle(tmp_i, tmp_i, $idx);
let tmp_o: $tmp = $shuffle(tmp_i, tmp_i, IDX);
transmute(tmp_o)
}
};
Expand Down
4 changes: 3 additions & 1 deletion blake2/src/simd/simd_opt/u32x4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ fn rotate_right_16(vec: u32x4) -> u32x4 {
u8x16,
simd_shuffle16,
vec,
16,
[2, 3, 0, 1, 6, 7, 4, 5, 10, 11, 8, 9, 14, 15, 12, 13]
)
} else if cfg!(any(target_feature = "sse2", target_feature = "neon")) {
// pshuflw+pshufhw (SSE2) / vrev (NEON)
transmute_shuffle!(u16x8, simd_shuffle8, vec, [1, 0, 3, 2, 5, 4, 7, 6])
transmute_shuffle!(u16x8, simd_shuffle8, vec, 8, [1, 0, 3, 2, 5, 4, 7, 6])
} else {
rotate_right_any(vec, 16)
}
Expand All @@ -59,6 +60,7 @@ fn rotate_right_8(vec: u32x4) -> u32x4 {
u8x16,
simd_shuffle16,
vec,
16,
[1, 2, 3, 0, 5, 6, 7, 4, 9, 10, 11, 8, 13, 14, 15, 12]
)
} else {
Expand Down
5 changes: 4 additions & 1 deletion blake2/src/simd/simd_opt/u64x4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn rotate_right_any(vec: u64x4, n: u32) -> u64x4 {
fn rotate_right_32(vec: u64x4) -> u64x4 {
if cfg!(any(target_feature = "sse2", target_feature = "neon")) {
// 2 x pshufd (SSE2) / vpshufd (AVX2) / 2 x vrev (NEON)
transmute_shuffle!(u32x8, simd_shuffle8, vec, [1, 0, 3, 2, 5, 4, 7, 6])
transmute_shuffle!(u32x8, simd_shuffle8, vec, 8, [1, 0, 3, 2, 5, 4, 7, 6])
} else {
rotate_right_any(vec, 32)
}
Expand All @@ -59,6 +59,7 @@ fn rotate_right_24(vec: u64x4) -> u64x4 {
u8x32,
simd_shuffle32,
vec,
32,
[
3, 4, 5, 6, 7, 0, 1, 2, 11, 12, 13, 14, 15, 8, 9, 10, 19, 20, 21, 22, 23, 16, 17,
18, 27, 28, 29, 30, 31, 24, 25, 26
Expand All @@ -85,6 +86,7 @@ fn rotate_right_16(vec: u64x4) -> u64x4 {
u8x32,
simd_shuffle32,
vec,
32,
[
2, 3, 4, 5, 6, 7, 0, 1, 10, 11, 12, 13, 14, 15, 8, 9, 18, 19, 20, 21, 22, 23, 16,
17, 26, 27, 28, 29, 30, 31, 24, 25
Expand All @@ -96,6 +98,7 @@ fn rotate_right_16(vec: u64x4) -> u64x4 {
u16x16,
simd_shuffle16,
vec,
16,
[1, 2, 3, 0, 5, 6, 7, 4, 9, 10, 11, 8, 13, 14, 15, 12]
)
} else {
Expand Down

0 comments on commit a7a5ad4

Please sign in to comment.