Skip to content

Commit

Permalink
Merge pull request #417 from rust-lang/const-splat
Browse files Browse the repository at this point in the history
Make splat const fn
  • Loading branch information
calebzulawski authored Apr 30, 2024
2 parents 3125888 + e72b450 commit c886e69
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
1 change: 1 addition & 0 deletions crates/core_simd/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![no_std]
#![feature(
const_eval_select,
const_intrinsic_copy,
const_refs_to_cell,
const_maybe_uninit_as_mut_ptr,
Expand Down
31 changes: 24 additions & 7 deletions crates/core_simd/src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,31 @@ where
/// assert_eq!(v.as_array(), &[8, 8, 8, 8]);
/// ```
#[inline]
pub fn splat(value: T) -> Self {
// This is preferred over `[value; N]`, since it's explicitly a splat:
// https://github.com/rust-lang/rust/issues/97804
struct Splat;
impl<const N: usize> Swizzle<N> for Splat {
const INDEX: [usize; N] = [0; N];
pub const fn splat(value: T) -> Self {
const fn splat_const<T, const N: usize>(value: T) -> Simd<T, N>
where
T: SimdElement,
LaneCount<N>: SupportedLaneCount,
{
Simd::from_array([value; N])
}
Splat::swizzle::<T, 1>(Simd::<T, 1>::from([value]))

fn splat_rt<T, const N: usize>(value: T) -> Simd<T, N>
where
T: SimdElement,
LaneCount<N>: SupportedLaneCount,
{
// This is preferred over `[value; N]`, since it's explicitly a splat:
// https://github.com/rust-lang/rust/issues/97804
struct Splat;
impl<const N: usize> Swizzle<N> for Splat {
const INDEX: [usize; N] = [0; N];
}

Splat::swizzle::<T, 1>(Simd::<T, 1>::from([value]))
}

core::intrinsics::const_eval_select((value,), splat_const, splat_rt)
}

/// Returns an array reference containing the entire SIMD vector.
Expand Down

0 comments on commit c886e69

Please sign in to comment.