Skip to content

Commit 0f6399b

Browse files
Merge pull request rust-lang#284 from programmerjake/fix_splat
Change `Simd::splat` to not generate a loop
2 parents 3e7a2ed + f7412ad commit 0f6399b

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

crates/core_simd/src/vector.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ pub use uint::*;
99
// Vectors of pointers are not for public use at the current time.
1010
pub(crate) mod ptr;
1111

12-
use crate::simd::intrinsics;
13-
use crate::simd::{LaneCount, Mask, MaskElement, SimdPartialOrd, SupportedLaneCount};
12+
use crate::simd::{
13+
intrinsics, LaneCount, Mask, MaskElement, SimdPartialOrd, SupportedLaneCount, Swizzle,
14+
};
1415

1516
/// A SIMD vector of `LANES` elements of type `T`. `Simd<T, N>` has the same shape as [`[T; N]`](array), but operates like `T`.
1617
///
@@ -123,8 +124,14 @@ where
123124
/// let v = u32x4::splat(8);
124125
/// assert_eq!(v.as_array(), &[8, 8, 8, 8]);
125126
/// ```
126-
pub const fn splat(value: T) -> Self {
127-
Self([value; LANES])
127+
pub fn splat(value: T) -> Self {
128+
// This is a workaround for `[value; LANES]` generating a loop:
129+
// https://github.com/rust-lang/rust/issues/97804
130+
struct Splat;
131+
impl<const LANES: usize> Swizzle<1, LANES> for Splat {
132+
const INDEX: [usize; LANES] = [0; LANES];
133+
}
134+
Splat::swizzle(Simd::<T, 1>::from([value]))
128135
}
129136

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

0 commit comments

Comments
 (0)