From ff9ba664e191ac91fc32d96bfc541af729b7917c Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 17 Feb 2024 13:28:37 +0100 Subject: [PATCH] avoid using simd_extract in SimdTy::extract (since the index is not a constant there) --- library/stdarch/crates/core_arch/src/simd.rs | 22 +++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/library/stdarch/crates/core_arch/src/simd.rs b/library/stdarch/crates/core_arch/src/simd.rs index 281fefba425f..b386b17848a3 100644 --- a/library/stdarch/crates/core_arch/src/simd.rs +++ b/library/stdarch/crates/core_arch/src/simd.rs @@ -18,17 +18,28 @@ macro_rules! simd_ty { #[inline(always)] pub(crate) const fn splat(value: $ety) -> Self { $id($({ + // We want this to be repeated for each element. + // So we need to use `elem_name` in a `$(...)`. + // But we don't actually need that name for anything so we use a dummy struct. #[allow(non_camel_case_types, dead_code)] struct $elem_name; value }),*) } + /// Extract the element at position `index`. + /// `index` is not a constant so this is not efficient! + /// Use for testing only. // FIXME: Workaround rust@60637 #[inline(always)] pub(crate) fn extract(self, index: usize) -> $ety { + // Here we assume that there is no padding. + let len = crate::mem::size_of::() / crate::mem::size_of::<$ety>(); + assert!(index < len); + // Now that we know this is in-bounds, use pointer arithmetic to access the right element. + let self_ptr = &self as *const Self as *const $ety; unsafe { - crate::core_arch::simd_llvm::simd_extract(self, index as u32) + self_ptr.add(index).read() } } } @@ -62,15 +73,6 @@ macro_rules! simd_m_ty { Self::bool_to_internal(value) }),*) } - - // FIXME: Workaround rust@60637 - #[inline(always)] - pub(crate) fn extract(self, index: usize) -> bool { - let r: $ety = unsafe { - crate::core_arch::simd_llvm::simd_extract(self, index as u32) - }; - r != 0 - } } } }