Skip to content

Commit c0cca91

Browse files
authored
Convert x86/sse41.rs intrinsics to const generics (#1026)
1 parent 894e91c commit c0cca91

File tree

3 files changed

+125
-174
lines changed

3 files changed

+125
-174
lines changed

crates/core_arch/src/macros.rs

+27-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,39 @@
11
//! Utility macros.
22
3-
// Helper struct used to trigger const eval errors when a const generic immediate value is
4-
// out of range.
5-
pub(crate) struct ValidateConstImm8<const imm8: i32>();
6-
impl<const imm8: i32> ValidateConstImm8<imm8> {
3+
// Helper struct used to trigger const eval errors when the const generic immediate value `imm` is
4+
// out of `bits`-bit range.
5+
pub(crate) struct ValidateConstImm<const imm: i32, const bits: i32>;
6+
impl<const imm: i32, const bits: i32> ValidateConstImm<imm, bits> {
77
pub(crate) const VALID: () = {
8-
let _ = 1 / ((imm8 >= 0 && imm8 <= 255) as usize);
8+
let _ = 1 / ((imm >= 0 && imm < (1 << bits)) as usize);
9+
};
10+
}
11+
12+
#[allow(unused)]
13+
macro_rules! static_assert_imm2 {
14+
($imm:ident) => {
15+
let _ = $crate::core_arch::macros::ValidateConstImm::<$imm, 2>::VALID;
16+
};
17+
}
18+
19+
#[allow(unused)]
20+
macro_rules! static_assert_imm3 {
21+
($imm:ident) => {
22+
let _ = $crate::core_arch::macros::ValidateConstImm::<$imm, 3>::VALID;
23+
};
24+
}
25+
26+
#[allow(unused)]
27+
macro_rules! static_assert_imm4 {
28+
($imm:ident) => {
29+
let _ = $crate::core_arch::macros::ValidateConstImm::<$imm, 4>::VALID;
930
};
1031
}
1132

1233
#[allow(unused)]
1334
macro_rules! static_assert_imm8 {
1435
($imm:ident) => {
15-
let _ = $crate::core_arch::macros::ValidateConstImm8::<$imm>::VALID;
36+
let _ = $crate::core_arch::macros::ValidateConstImm::<$imm, 8>::VALID;
1637
};
1738
}
1839

crates/core_arch/src/x86/avx2.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -4415,14 +4415,14 @@ mod tests {
44154415

44164416
#[simd_test(enable = "avx2")]
44174417
unsafe fn test_mm_broadcastb_epi8() {
4418-
let a = _mm_insert_epi8(_mm_set1_epi8(0x00), 0x2a, 0);
4418+
let a = _mm_insert_epi8::<0>(_mm_set1_epi8(0x00), 0x2a);
44194419
let res = _mm_broadcastb_epi8(a);
44204420
assert_eq_m128i(res, _mm_set1_epi8(0x2a));
44214421
}
44224422

44234423
#[simd_test(enable = "avx2")]
44244424
unsafe fn test_mm256_broadcastb_epi8() {
4425-
let a = _mm_insert_epi8(_mm_set1_epi8(0x00), 0x2a, 0);
4425+
let a = _mm_insert_epi8::<0>(_mm_set1_epi8(0x00), 0x2a);
44264426
let res = _mm256_broadcastb_epi8(a);
44274427
assert_eq_m256i(res, _mm256_set1_epi8(0x2a));
44284428
}
@@ -5204,7 +5204,7 @@ mod tests {
52045204
#[simd_test(enable = "avx2")]
52055205
unsafe fn test_mm256_sll_epi32() {
52065206
let a = _mm256_set1_epi32(0xFFFF);
5207-
let b = _mm_insert_epi32(_mm_set1_epi32(0), 4, 0);
5207+
let b = _mm_insert_epi32::<0>(_mm_set1_epi32(0), 4);
52085208
let r = _mm256_sll_epi32(a, b);
52095209
assert_eq_m256i(r, _mm256_set1_epi32(0xFFFF0));
52105210
}
@@ -5295,7 +5295,7 @@ mod tests {
52955295
#[simd_test(enable = "avx2")]
52965296
unsafe fn test_mm256_sra_epi32() {
52975297
let a = _mm256_set1_epi32(-1);
5298-
let b = _mm_insert_epi32(_mm_set1_epi32(0), 1, 0);
5298+
let b = _mm_insert_epi32::<0>(_mm_set1_epi32(0), 1);
52995299
let r = _mm256_sra_epi32(a, b);
53005300
assert_eq_m256i(r, _mm256_set1_epi32(-1));
53015301
}
@@ -5365,7 +5365,7 @@ mod tests {
53655365
#[simd_test(enable = "avx2")]
53665366
unsafe fn test_mm256_srl_epi32() {
53675367
let a = _mm256_set1_epi32(0xFFFF);
5368-
let b = _mm_insert_epi32(_mm_set1_epi32(0), 4, 0);
5368+
let b = _mm_insert_epi32::<0>(_mm_set1_epi32(0), 4);
53695369
let r = _mm256_srl_epi32(a, b);
53705370
assert_eq_m256i(r, _mm256_set1_epi32(0xFFF));
53715371
}

0 commit comments

Comments
 (0)