Skip to content

Commit c886e69

Browse files
Merge pull request #417 from rust-lang/const-splat
Make splat const fn
2 parents 3125888 + e72b450 commit c886e69

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

crates/core_simd/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![no_std]
22
#![feature(
3+
const_eval_select,
34
const_intrinsic_copy,
45
const_refs_to_cell,
56
const_maybe_uninit_as_mut_ptr,

crates/core_simd/src/vector.rs

+24-7
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,31 @@ where
144144
/// assert_eq!(v.as_array(), &[8, 8, 8, 8]);
145145
/// ```
146146
#[inline]
147-
pub fn splat(value: T) -> Self {
148-
// This is preferred over `[value; N]`, since it's explicitly a splat:
149-
// https://github.com/rust-lang/rust/issues/97804
150-
struct Splat;
151-
impl<const N: usize> Swizzle<N> for Splat {
152-
const INDEX: [usize; N] = [0; N];
147+
pub const fn splat(value: T) -> Self {
148+
const fn splat_const<T, const N: usize>(value: T) -> Simd<T, N>
149+
where
150+
T: SimdElement,
151+
LaneCount<N>: SupportedLaneCount,
152+
{
153+
Simd::from_array([value; N])
153154
}
154-
Splat::swizzle::<T, 1>(Simd::<T, 1>::from([value]))
155+
156+
fn splat_rt<T, const N: usize>(value: T) -> Simd<T, N>
157+
where
158+
T: SimdElement,
159+
LaneCount<N>: SupportedLaneCount,
160+
{
161+
// This is preferred over `[value; N]`, since it's explicitly a splat:
162+
// https://github.com/rust-lang/rust/issues/97804
163+
struct Splat;
164+
impl<const N: usize> Swizzle<N> for Splat {
165+
const INDEX: [usize; N] = [0; N];
166+
}
167+
168+
Splat::swizzle::<T, 1>(Simd::<T, 1>::from([value]))
169+
}
170+
171+
core::intrinsics::const_eval_select((value,), splat_const, splat_rt)
155172
}
156173

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

0 commit comments

Comments
 (0)