Skip to content

Commit 9ba0a3f

Browse files
authored
Merge pull request #2003 from folkertdev/use-simd-splat
use `simd_splat`
2 parents 2f9bc39 + 93f1c45 commit 9ba0a3f

6 files changed

Lines changed: 20 additions & 41 deletions

File tree

crates/core_arch/src/macros.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,10 @@ macro_rules! types {
9090
pub struct $name($v [$elem_type; $len]);
9191

9292
impl $name {
93-
/// Using `my_simd([x; N])` seemingly fails tests,
94-
/// so use this internal helper for it instead.
93+
/// Put the same value in every lane.
9594
#[inline(always)]
9695
$v fn splat(value: $elem_type) -> $name {
97-
#[derive(Copy, Clone)]
98-
#[repr(simd)]
99-
struct JustOne([$elem_type; 1]);
100-
let one = JustOne([value]);
101-
// SAFETY: 0 is always in-bounds because we're shuffling
102-
// a simd type with exactly one element.
103-
unsafe { simd_shuffle!(one, one, [0; $len]) }
96+
unsafe { $crate::intrinsics::simd::simd_splat(value) }
10497
}
10598

10699
/// Returns an array reference containing the entire SIMD vector.

crates/core_arch/src/powerpc/altivec.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,15 +1457,15 @@ mod sealed {
14571457
#[cfg_attr(test, assert_instr(vspltb, IMM4 = 15))]
14581458
unsafe fn vspltb<const IMM4: u32>(a: vector_signed_char) -> vector_signed_char {
14591459
static_assert_uimm_bits!(IMM4, 4);
1460-
simd_shuffle(a, a, const { u32x16::from_array([IMM4; 16]) })
1460+
simd_shuffle(a, a, const { u32x16::splat(IMM4) })
14611461
}
14621462

14631463
#[inline]
14641464
#[target_feature(enable = "altivec")]
14651465
#[cfg_attr(test, assert_instr(vsplth, IMM3 = 7))]
14661466
unsafe fn vsplth<const IMM3: u32>(a: vector_signed_short) -> vector_signed_short {
14671467
static_assert_uimm_bits!(IMM3, 3);
1468-
simd_shuffle(a, a, const { u32x8::from_array([IMM3; 8]) })
1468+
simd_shuffle(a, a, const { u32x8::splat(IMM3) })
14691469
}
14701470

14711471
#[inline]
@@ -1474,7 +1474,7 @@ mod sealed {
14741474
#[cfg_attr(all(test, target_feature = "vsx"), assert_instr(xxspltw, IMM2 = 3))]
14751475
unsafe fn vspltw<const IMM2: u32>(a: vector_signed_int) -> vector_signed_int {
14761476
static_assert_uimm_bits!(IMM2, 2);
1477-
simd_shuffle(a, a, const { u32x4::from_array([IMM2; 4]) })
1477+
simd_shuffle(a, a, const { u32x4::splat(IMM2) })
14781478
}
14791479

14801480
#[unstable(feature = "stdarch_powerpc", issue = "111145")]

crates/core_arch/src/simd.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,10 @@ impl<T: SimdElement, const N: usize> Simd<T, N> {
5050
Self(elements)
5151
}
5252

53-
// FIXME: Workaround rust@60637
54-
#[inline(always)]
53+
#[inline]
5554
#[rustc_const_unstable(feature = "stdarch_const_helpers", issue = "none")]
5655
pub(crate) const fn splat(value: T) -> Self {
57-
let one = Simd([value]);
58-
// SAFETY: 0 is always in-bounds because we're shuffling
59-
// a simd type with exactly one element.
60-
unsafe { simd_shuffle!(one, one, [0; N]) }
56+
unsafe { crate::intrinsics::simd::simd_splat(value) }
6157
}
6258

6359
/// Extract the element at position `index`. Note that `index` is not a constant so this
@@ -182,14 +178,10 @@ impl<T: SimdElement, const N: usize> SimdM<T, N> {
182178
[zeros, ones][x as usize]
183179
}
184180

185-
// FIXME: Workaround rust@60637
186-
#[inline(always)]
181+
#[inline]
187182
#[rustc_const_unstable(feature = "stdarch_const_helpers", issue = "none")]
188183
pub(crate) const fn splat(value: bool) -> Self {
189-
let one = SimdM([Self::bool_to_internal(value)]);
190-
// SAFETY: 0 is always in-bounds because we're shuffling
191-
// a simd type with exactly one element.
192-
unsafe { simd_shuffle!(one, one, [0; N]) }
184+
unsafe { crate::intrinsics::simd::simd_splat(value) }
193185
}
194186

195187
#[inline]

crates/core_arch/src/x86/avx.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2746,7 +2746,7 @@ pub const fn _mm256_setr_epi64x(a: i64, b: i64, c: i64, d: i64) -> __m256i {
27462746
#[stable(feature = "simd_x86", since = "1.27.0")]
27472747
#[rustc_const_unstable(feature = "stdarch_const_x86", issue = "149298")]
27482748
pub const fn _mm256_set1_pd(a: f64) -> __m256d {
2749-
_mm256_setr_pd(a, a, a, a)
2749+
f64x4::splat(a).as_m256d()
27502750
}
27512751

27522752
/// Broadcasts single-precision (32-bit) floating-point value `a` to all
@@ -2759,7 +2759,7 @@ pub const fn _mm256_set1_pd(a: f64) -> __m256d {
27592759
#[stable(feature = "simd_x86", since = "1.27.0")]
27602760
#[rustc_const_unstable(feature = "stdarch_const_x86", issue = "149298")]
27612761
pub const fn _mm256_set1_ps(a: f32) -> __m256 {
2762-
_mm256_setr_ps(a, a, a, a, a, a, a, a)
2762+
f32x8::splat(a).as_m256()
27632763
}
27642764

27652765
/// Broadcasts 8-bit integer `a` to all elements of returned vector.
@@ -2772,13 +2772,7 @@ pub const fn _mm256_set1_ps(a: f32) -> __m256 {
27722772
#[stable(feature = "simd_x86", since = "1.27.0")]
27732773
#[rustc_const_unstable(feature = "stdarch_const_x86", issue = "149298")]
27742774
pub const fn _mm256_set1_epi8(a: i8) -> __m256i {
2775-
#[rustfmt::skip]
2776-
_mm256_setr_epi8(
2777-
a, a, a, a, a, a, a, a,
2778-
a, a, a, a, a, a, a, a,
2779-
a, a, a, a, a, a, a, a,
2780-
a, a, a, a, a, a, a, a,
2781-
)
2775+
i8x32::splat(a).as_m256i()
27822776
}
27832777

27842778
/// Broadcasts 16-bit integer `a` to all elements of returned vector.
@@ -2793,7 +2787,7 @@ pub const fn _mm256_set1_epi8(a: i8) -> __m256i {
27932787
#[stable(feature = "simd_x86", since = "1.27.0")]
27942788
#[rustc_const_unstable(feature = "stdarch_const_x86", issue = "149298")]
27952789
pub const fn _mm256_set1_epi16(a: i16) -> __m256i {
2796-
_mm256_setr_epi16(a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a)
2790+
i16x16::splat(a).as_m256i()
27972791
}
27982792

27992793
/// Broadcasts 32-bit integer `a` to all elements of returned vector.
@@ -2806,7 +2800,7 @@ pub const fn _mm256_set1_epi16(a: i16) -> __m256i {
28062800
#[stable(feature = "simd_x86", since = "1.27.0")]
28072801
#[rustc_const_unstable(feature = "stdarch_const_x86", issue = "149298")]
28082802
pub const fn _mm256_set1_epi32(a: i32) -> __m256i {
2809-
_mm256_setr_epi32(a, a, a, a, a, a, a, a)
2803+
i32x8::splat(a).as_m256i()
28102804
}
28112805

28122806
/// Broadcasts 64-bit integer `a` to all elements of returned vector.
@@ -2821,7 +2815,7 @@ pub const fn _mm256_set1_epi32(a: i32) -> __m256i {
28212815
#[stable(feature = "simd_x86", since = "1.27.0")]
28222816
#[rustc_const_unstable(feature = "stdarch_const_x86", issue = "149298")]
28232817
pub const fn _mm256_set1_epi64x(a: i64) -> __m256i {
2824-
_mm256_setr_epi64x(a, a, a, a)
2818+
i64x4::splat(a).as_m256i()
28252819
}
28262820

28272821
/// Cast vector of type __m256d to type __m256.

crates/core_arch/src/x86/sse.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ pub const fn _mm_set_ss(a: f32) -> __m128 {
932932
#[stable(feature = "simd_x86", since = "1.27.0")]
933933
#[rustc_const_unstable(feature = "stdarch_const_x86", issue = "149298")]
934934
pub const fn _mm_set1_ps(a: f32) -> __m128 {
935-
__m128([a, a, a, a])
935+
f32x4::splat(a).as_m128()
936936
}
937937

938938
/// Alias for [`_mm_set1_ps`](fn._mm_set1_ps.html)

crates/core_arch/src/x86/sse2.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,7 +1176,7 @@ pub const fn _mm_set_epi8(
11761176
#[stable(feature = "simd_x86", since = "1.27.0")]
11771177
#[rustc_const_unstable(feature = "stdarch_const_x86", issue = "149298")]
11781178
pub const fn _mm_set1_epi64x(a: i64) -> __m128i {
1179-
_mm_set_epi64x(a, a)
1179+
i64x2::splat(a).as_m128i()
11801180
}
11811181

11821182
/// Broadcasts 32-bit integer `a` to all elements.
@@ -1188,7 +1188,7 @@ pub const fn _mm_set1_epi64x(a: i64) -> __m128i {
11881188
#[stable(feature = "simd_x86", since = "1.27.0")]
11891189
#[rustc_const_unstable(feature = "stdarch_const_x86", issue = "149298")]
11901190
pub const fn _mm_set1_epi32(a: i32) -> __m128i {
1191-
_mm_set_epi32(a, a, a, a)
1191+
i32x4::splat(a).as_m128i()
11921192
}
11931193

11941194
/// Broadcasts 16-bit integer `a` to all elements.
@@ -1200,7 +1200,7 @@ pub const fn _mm_set1_epi32(a: i32) -> __m128i {
12001200
#[stable(feature = "simd_x86", since = "1.27.0")]
12011201
#[rustc_const_unstable(feature = "stdarch_const_x86", issue = "149298")]
12021202
pub const fn _mm_set1_epi16(a: i16) -> __m128i {
1203-
_mm_set_epi16(a, a, a, a, a, a, a, a)
1203+
i16x8::splat(a).as_m128i()
12041204
}
12051205

12061206
/// Broadcasts 8-bit integer `a` to all elements.
@@ -1212,7 +1212,7 @@ pub const fn _mm_set1_epi16(a: i16) -> __m128i {
12121212
#[stable(feature = "simd_x86", since = "1.27.0")]
12131213
#[rustc_const_unstable(feature = "stdarch_const_x86", issue = "149298")]
12141214
pub const fn _mm_set1_epi8(a: i8) -> __m128i {
1215-
_mm_set_epi8(a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a)
1215+
i8x16::splat(a).as_m128i()
12161216
}
12171217

12181218
/// Sets packed 32-bit integers with the supplied values in reverse order.

0 commit comments

Comments
 (0)