Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert some SSE2 intrinsics to const generics #1021

Merged
merged 8 commits into from
Feb 28, 2021
58 changes: 16 additions & 42 deletions crates/core_arch/src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
//! Utility macros.

// Helper struct used to trigger const eval errors when a const generic immediate value is
// out of range.
pub(crate) struct ValidateConstImm8<const imm8: i32>();
impl<const imm8: i32> ValidateConstImm8<imm8> {
pub(crate) const VALID: () = {
let _ = 1 / ((imm8 >= 0 && imm8 <= 255) as usize);
};
}

#[allow(unused)]
macro_rules! static_assert_imm8 {
($imm:ident) => {
let _ = $crate::core_arch::macros::ValidateConstImm8::<$imm>::VALID;
};
}

#[allow(unused)]
macro_rules! static_assert {
($imm:ident : $ty:ty where $e:expr) => {
Expand Down Expand Up @@ -320,48 +336,6 @@ macro_rules! constify_imm5 {
};
}

//immediate value: -16:15
#[allow(unused)]
macro_rules! constify_imm5 {
($imm8:expr, $expand:ident) => {
#[allow(overflowing_literals)]
match ($imm8) & 0b1_1111 {
0 => $expand!(0),
1 => $expand!(1),
2 => $expand!(2),
3 => $expand!(3),
4 => $expand!(4),
5 => $expand!(5),
6 => $expand!(6),
7 => $expand!(7),
8 => $expand!(8),
9 => $expand!(9),
10 => $expand!(10),
11 => $expand!(11),
12 => $expand!(12),
13 => $expand!(13),
14 => $expand!(14),
15 => $expand!(15),
16 => $expand!(16),
17 => $expand!(17),
18 => $expand!(18),
19 => $expand!(19),
20 => $expand!(20),
21 => $expand!(21),
22 => $expand!(22),
23 => $expand!(23),
24 => $expand!(24),
25 => $expand!(25),
26 => $expand!(26),
27 => $expand!(27),
28 => $expand!(28),
29 => $expand!(29),
30 => $expand!(30),
_ => $expand!(31),
}
};
}

//immediate value: 0:16
#[allow(unused)]
macro_rules! constify_imm4 {
Expand Down
12 changes: 6 additions & 6 deletions crates/core_arch/src/x86/avx512bw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5858,7 +5858,7 @@ pub unsafe fn _mm256_maskz_srai_epi16(k: __mmask16, a: __m256i, imm8: u32) -> __
pub unsafe fn _mm_mask_srai_epi16(src: __m128i, k: __mmask8, a: __m128i, imm8: u32) -> __m128i {
macro_rules! call {
($imm8:expr) => {
_mm_srai_epi16(a, $imm8)
_mm_srai_epi16::<$imm8>(a)
};
}
let shf = constify_imm8_sae!(imm8, call);
Expand All @@ -5875,7 +5875,7 @@ pub unsafe fn _mm_mask_srai_epi16(src: __m128i, k: __mmask8, a: __m128i, imm8: u
pub unsafe fn _mm_maskz_srai_epi16(k: __mmask8, a: __m128i, imm8: u32) -> __m128i {
macro_rules! call {
($imm8:expr) => {
_mm_srai_epi16(a, $imm8)
_mm_srai_epi16::<$imm8>(a)
};
}
let shf = constify_imm8_sae!(imm8, call);
Expand Down Expand Up @@ -7414,7 +7414,7 @@ pub unsafe fn _mm_mask_shufflelo_epi16(
) -> __m128i {
macro_rules! call {
($imm8:expr) => {
_mm_shufflelo_epi16(a, $imm8)
_mm_shufflelo_epi16::<$imm8>(a)
};
}
let shuffle = constify_imm8_sae!(imm8, call);
Expand All @@ -7431,7 +7431,7 @@ pub unsafe fn _mm_mask_shufflelo_epi16(
pub unsafe fn _mm_maskz_shufflelo_epi16(k: __mmask8, a: __m128i, imm8: i32) -> __m128i {
macro_rules! call {
($imm8:expr) => {
_mm_shufflelo_epi16(a, $imm8)
_mm_shufflelo_epi16::<$imm8>(a)
};
}
let shuffle = constify_imm8_sae!(imm8, call);
Expand Down Expand Up @@ -7592,7 +7592,7 @@ pub unsafe fn _mm_mask_shufflehi_epi16(
) -> __m128i {
macro_rules! call {
($imm8:expr) => {
_mm_shufflehi_epi16(a, $imm8)
_mm_shufflehi_epi16::<$imm8>(a)
};
}
let shuffle = constify_imm8_sae!(imm8, call);
Expand All @@ -7609,7 +7609,7 @@ pub unsafe fn _mm_mask_shufflehi_epi16(
pub unsafe fn _mm_maskz_shufflehi_epi16(k: __mmask8, a: __m128i, imm8: i32) -> __m128i {
macro_rules! call {
($imm8:expr) => {
_mm_shufflehi_epi16(a, $imm8)
_mm_shufflehi_epi16::<$imm8>(a)
};
}
let shuffle = constify_imm8_sae!(imm8, call);
Expand Down
8 changes: 4 additions & 4 deletions crates/core_arch/src/x86/avx512f.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19238,7 +19238,7 @@ pub unsafe fn _mm256_maskz_srai_epi32(k: __mmask8, a: __m256i, imm8: u32) -> __m
pub unsafe fn _mm_mask_srai_epi32(src: __m128i, k: __mmask8, a: __m128i, imm8: u32) -> __m128i {
macro_rules! call {
($imm8:expr) => {
_mm_srai_epi32(a, $imm8)
_mm_srai_epi32::<$imm8>(a)
};
}
let shf = constify_imm8_sae!(imm8, call);
Expand All @@ -19255,7 +19255,7 @@ pub unsafe fn _mm_mask_srai_epi32(src: __m128i, k: __mmask8, a: __m128i, imm8: u
pub unsafe fn _mm_maskz_srai_epi32(k: __mmask8, a: __m128i, imm8: u32) -> __m128i {
macro_rules! call {
($imm8:expr) => {
_mm_srai_epi32(a, $imm8)
_mm_srai_epi32::<$imm8>(a)
};
}
let shf = constify_imm8_sae!(imm8, call);
Expand Down Expand Up @@ -22495,7 +22495,7 @@ pub unsafe fn _mm_mask_shuffle_epi32(
) -> __m128i {
macro_rules! call {
($imm8:expr) => {
_mm_shuffle_epi32(a, $imm8)
_mm_shuffle_epi32::<$imm8>(a)
};
}
let r = constify_imm8_sae!(imm8, call);
Expand All @@ -22512,7 +22512,7 @@ pub unsafe fn _mm_mask_shuffle_epi32(
pub unsafe fn _mm_maskz_shuffle_epi32(k: __mmask8, a: __m128i, imm8: _MM_PERM_ENUM) -> __m128i {
macro_rules! call {
($imm8:expr) => {
_mm_shuffle_epi32(a, $imm8)
_mm_shuffle_epi32::<$imm8>(a)
};
}
let r = constify_imm8_sae!(imm8, call);
Expand Down
2 changes: 1 addition & 1 deletion crates/core_arch/src/x86/sse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,7 @@ pub const fn _MM_SHUFFLE(z: u32, y: u32, x: u32, w: u32) -> i32 {
#[rustc_legacy_const_generics(2)]
#[stable(feature = "simd_x86", since = "1.27.0")]
pub unsafe fn _mm_shuffle_ps<const mask: i32>(a: __m128, b: __m128) -> __m128 {
static_assert!(mask: i32 where mask >= 0 && mask <= 255);
static_assert_imm8!(mask);
simd_shuffle4(
a,
b,
Expand Down
Loading