Use static assertions to bound-check const generics (#1019)

This commit is contained in:
Amanieu d'Antras 2021-02-27 16:16:47 +00:00 committed by GitHub
parent b4023b1ffe
commit 78ab9042cb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 1 deletions

View file

@ -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) => {

View file

@ -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,