Skip to content

Commit

Permalink
Use static assertions to bound-check const generics (#1019)
Browse files Browse the repository at this point in the history
  • Loading branch information
Amanieu committed Feb 27, 2021
1 parent 8d50178 commit 3b52e06
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 13 additions & 0 deletions crates/core_arch/src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
//! Utility macros.

#[allow(unused)]
macro_rules! static_assert {
($imm:ident : $ty:ty where $e:expr) => {
struct Validate<const $imm: $ty>();
impl<const $imm: $ty> Validate<$imm> {
const VALID: () = {
let _ = 1 / ($e as usize);
};
}
let _ = Validate::<$imm>::VALID;
};
}

#[allow(unused)]
macro_rules! constify_imm8 {
($imm8:expr, $expand:ident) => {
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 {
assert!(mask >= 0 && mask <= 255);
static_assert!(mask: i32 where mask >= 0 && mask <= 255);
simd_shuffle4(
a,
b,
Expand Down

0 comments on commit 3b52e06

Please sign in to comment.