From a95c0714602e7316a32b3cea317199d7a27fb1e4 Mon Sep 17 00:00:00 2001 From: Folkert de Vries Date: Mon, 26 Jan 2026 10:38:04 +0100 Subject: [PATCH 1/2] use `simd_splat` --- crates/core_arch/src/macros.rs | 11 ++--------- crates/core_arch/src/powerpc/altivec.rs | 6 +++--- crates/core_arch/src/simd.rs | 16 ++++------------ 3 files changed, 9 insertions(+), 24 deletions(-) diff --git a/crates/core_arch/src/macros.rs b/crates/core_arch/src/macros.rs index 849297b1fc..353829633f 100644 --- a/crates/core_arch/src/macros.rs +++ b/crates/core_arch/src/macros.rs @@ -90,17 +90,10 @@ macro_rules! types { pub struct $name($v [$elem_type; $len]); impl $name { - /// Using `my_simd([x; N])` seemingly fails tests, - /// so use this internal helper for it instead. + /// Put the same value in every lane. #[inline(always)] $v fn splat(value: $elem_type) -> $name { - #[derive(Copy, Clone)] - #[repr(simd)] - struct JustOne([$elem_type; 1]); - let one = JustOne([value]); - // SAFETY: 0 is always in-bounds because we're shuffling - // a simd type with exactly one element. - unsafe { simd_shuffle!(one, one, [0; $len]) } + unsafe { $crate::intrinsics::simd::simd_splat(value) } } /// Returns an array reference containing the entire SIMD vector. diff --git a/crates/core_arch/src/powerpc/altivec.rs b/crates/core_arch/src/powerpc/altivec.rs index b9a25a9d85..fb1a9d8ed9 100644 --- a/crates/core_arch/src/powerpc/altivec.rs +++ b/crates/core_arch/src/powerpc/altivec.rs @@ -1457,7 +1457,7 @@ mod sealed { #[cfg_attr(test, assert_instr(vspltb, IMM4 = 15))] unsafe fn vspltb(a: vector_signed_char) -> vector_signed_char { static_assert_uimm_bits!(IMM4, 4); - simd_shuffle(a, a, const { u32x16::from_array([IMM4; 16]) }) + simd_shuffle(a, a, const { u32x16::splat(IMM4) }) } #[inline] @@ -1465,7 +1465,7 @@ mod sealed { #[cfg_attr(test, assert_instr(vsplth, IMM3 = 7))] unsafe fn vsplth(a: vector_signed_short) -> vector_signed_short { static_assert_uimm_bits!(IMM3, 3); - simd_shuffle(a, a, const { u32x8::from_array([IMM3; 8]) }) + simd_shuffle(a, a, const { u32x8::splat(IMM3) }) } #[inline] @@ -1474,7 +1474,7 @@ mod sealed { #[cfg_attr(all(test, target_feature = "vsx"), assert_instr(xxspltw, IMM2 = 3))] unsafe fn vspltw(a: vector_signed_int) -> vector_signed_int { static_assert_uimm_bits!(IMM2, 2); - simd_shuffle(a, a, const { u32x4::from_array([IMM2; 4]) }) + simd_shuffle(a, a, const { u32x4::splat(IMM2) }) } #[unstable(feature = "stdarch_powerpc", issue = "111145")] diff --git a/crates/core_arch/src/simd.rs b/crates/core_arch/src/simd.rs index b670092f5b..313c474792 100644 --- a/crates/core_arch/src/simd.rs +++ b/crates/core_arch/src/simd.rs @@ -50,14 +50,10 @@ impl Simd { Self(elements) } - // FIXME: Workaround rust@60637 - #[inline(always)] + #[inline] #[rustc_const_unstable(feature = "stdarch_const_helpers", issue = "none")] pub(crate) const fn splat(value: T) -> Self { - let one = Simd([value]); - // SAFETY: 0 is always in-bounds because we're shuffling - // a simd type with exactly one element. - unsafe { simd_shuffle!(one, one, [0; N]) } + unsafe { crate::intrinsics::simd::simd_splat(value) } } /// Extract the element at position `index`. Note that `index` is not a constant so this @@ -182,14 +178,10 @@ impl SimdM { [zeros, ones][x as usize] } - // FIXME: Workaround rust@60637 - #[inline(always)] + #[inline] #[rustc_const_unstable(feature = "stdarch_const_helpers", issue = "none")] pub(crate) const fn splat(value: bool) -> Self { - let one = SimdM([Self::bool_to_internal(value)]); - // SAFETY: 0 is always in-bounds because we're shuffling - // a simd type with exactly one element. - unsafe { simd_shuffle!(one, one, [0; N]) } + unsafe { crate::intrinsics::simd::simd_splat(value) } } #[inline] From 93f1c45b7be893343139a40123043a6d59775e56 Mon Sep 17 00:00:00 2001 From: Folkert de Vries Date: Mon, 26 Jan 2026 20:42:22 +0100 Subject: [PATCH 2/2] use `simd_splat` for the `set1` functions --- crates/core_arch/src/x86/avx.rs | 18 ++++++------------ crates/core_arch/src/x86/sse.rs | 2 +- crates/core_arch/src/x86/sse2.rs | 8 ++++---- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/crates/core_arch/src/x86/avx.rs b/crates/core_arch/src/x86/avx.rs index e0e01ae6d0..c8cddad2ef 100644 --- a/crates/core_arch/src/x86/avx.rs +++ b/crates/core_arch/src/x86/avx.rs @@ -2746,7 +2746,7 @@ pub const fn _mm256_setr_epi64x(a: i64, b: i64, c: i64, d: i64) -> __m256i { #[stable(feature = "simd_x86", since = "1.27.0")] #[rustc_const_unstable(feature = "stdarch_const_x86", issue = "149298")] pub const fn _mm256_set1_pd(a: f64) -> __m256d { - _mm256_setr_pd(a, a, a, a) + f64x4::splat(a).as_m256d() } /// Broadcasts single-precision (32-bit) floating-point value `a` to all @@ -2759,7 +2759,7 @@ pub const fn _mm256_set1_pd(a: f64) -> __m256d { #[stable(feature = "simd_x86", since = "1.27.0")] #[rustc_const_unstable(feature = "stdarch_const_x86", issue = "149298")] pub const fn _mm256_set1_ps(a: f32) -> __m256 { - _mm256_setr_ps(a, a, a, a, a, a, a, a) + f32x8::splat(a).as_m256() } /// Broadcasts 8-bit integer `a` to all elements of returned vector. @@ -2772,13 +2772,7 @@ pub const fn _mm256_set1_ps(a: f32) -> __m256 { #[stable(feature = "simd_x86", since = "1.27.0")] #[rustc_const_unstable(feature = "stdarch_const_x86", issue = "149298")] pub const fn _mm256_set1_epi8(a: i8) -> __m256i { - #[rustfmt::skip] - _mm256_setr_epi8( - a, a, a, a, a, a, a, a, - a, a, a, a, a, a, a, a, - a, a, a, a, a, a, a, a, - a, a, a, a, a, a, a, a, - ) + i8x32::splat(a).as_m256i() } /// Broadcasts 16-bit integer `a` to all elements of returned vector. @@ -2793,7 +2787,7 @@ pub const fn _mm256_set1_epi8(a: i8) -> __m256i { #[stable(feature = "simd_x86", since = "1.27.0")] #[rustc_const_unstable(feature = "stdarch_const_x86", issue = "149298")] pub const fn _mm256_set1_epi16(a: i16) -> __m256i { - _mm256_setr_epi16(a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a) + i16x16::splat(a).as_m256i() } /// Broadcasts 32-bit integer `a` to all elements of returned vector. @@ -2806,7 +2800,7 @@ pub const fn _mm256_set1_epi16(a: i16) -> __m256i { #[stable(feature = "simd_x86", since = "1.27.0")] #[rustc_const_unstable(feature = "stdarch_const_x86", issue = "149298")] pub const fn _mm256_set1_epi32(a: i32) -> __m256i { - _mm256_setr_epi32(a, a, a, a, a, a, a, a) + i32x8::splat(a).as_m256i() } /// Broadcasts 64-bit integer `a` to all elements of returned vector. @@ -2821,7 +2815,7 @@ pub const fn _mm256_set1_epi32(a: i32) -> __m256i { #[stable(feature = "simd_x86", since = "1.27.0")] #[rustc_const_unstable(feature = "stdarch_const_x86", issue = "149298")] pub const fn _mm256_set1_epi64x(a: i64) -> __m256i { - _mm256_setr_epi64x(a, a, a, a) + i64x4::splat(a).as_m256i() } /// Cast vector of type __m256d to type __m256. diff --git a/crates/core_arch/src/x86/sse.rs b/crates/core_arch/src/x86/sse.rs index f167e8381d..38b309c1cb 100644 --- a/crates/core_arch/src/x86/sse.rs +++ b/crates/core_arch/src/x86/sse.rs @@ -932,7 +932,7 @@ pub const fn _mm_set_ss(a: f32) -> __m128 { #[stable(feature = "simd_x86", since = "1.27.0")] #[rustc_const_unstable(feature = "stdarch_const_x86", issue = "149298")] pub const fn _mm_set1_ps(a: f32) -> __m128 { - __m128([a, a, a, a]) + f32x4::splat(a).as_m128() } /// Alias for [`_mm_set1_ps`](fn._mm_set1_ps.html) diff --git a/crates/core_arch/src/x86/sse2.rs b/crates/core_arch/src/x86/sse2.rs index fc010e8467..d712c4f4c5 100644 --- a/crates/core_arch/src/x86/sse2.rs +++ b/crates/core_arch/src/x86/sse2.rs @@ -1176,7 +1176,7 @@ pub const fn _mm_set_epi8( #[stable(feature = "simd_x86", since = "1.27.0")] #[rustc_const_unstable(feature = "stdarch_const_x86", issue = "149298")] pub const fn _mm_set1_epi64x(a: i64) -> __m128i { - _mm_set_epi64x(a, a) + i64x2::splat(a).as_m128i() } /// Broadcasts 32-bit integer `a` to all elements. @@ -1188,7 +1188,7 @@ pub const fn _mm_set1_epi64x(a: i64) -> __m128i { #[stable(feature = "simd_x86", since = "1.27.0")] #[rustc_const_unstable(feature = "stdarch_const_x86", issue = "149298")] pub const fn _mm_set1_epi32(a: i32) -> __m128i { - _mm_set_epi32(a, a, a, a) + i32x4::splat(a).as_m128i() } /// Broadcasts 16-bit integer `a` to all elements. @@ -1200,7 +1200,7 @@ pub const fn _mm_set1_epi32(a: i32) -> __m128i { #[stable(feature = "simd_x86", since = "1.27.0")] #[rustc_const_unstable(feature = "stdarch_const_x86", issue = "149298")] pub const fn _mm_set1_epi16(a: i16) -> __m128i { - _mm_set_epi16(a, a, a, a, a, a, a, a) + i16x8::splat(a).as_m128i() } /// Broadcasts 8-bit integer `a` to all elements. @@ -1212,7 +1212,7 @@ pub const fn _mm_set1_epi16(a: i16) -> __m128i { #[stable(feature = "simd_x86", since = "1.27.0")] #[rustc_const_unstable(feature = "stdarch_const_x86", issue = "149298")] pub const fn _mm_set1_epi8(a: i8) -> __m128i { - _mm_set_epi8(a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a) + i8x16::splat(a).as_m128i() } /// Sets packed 32-bit integers with the supplied values in reverse order.