Skip to content

Commit

Permalink
Add Real::{slice_as_simd,slice_as_simd_mut}.
Browse files Browse the repository at this point in the history
  • Loading branch information
n3vu0r committed Apr 29, 2024
1 parent 12ae7f3 commit 8c03fcb
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 1 deletion.
File renamed without changes.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ version = "0.2.8"
optional = true

[dependencies.target-features]
version = "0.1.5"
version = "0.1.6"
optional = true
18 changes: 18 additions & 0 deletions src/real/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,4 +524,22 @@ impl Real for f32 {
fn total_cmp(&self, other: &Self) -> Ordering {
self.total_cmp(other)
}

#[inline]
fn slice_as_simd<const N: usize>(slice: &[Self]) -> (&[Self], &[Self::Simd<N>], &[Self])
where
LaneCount<N>: SupportedLaneCount,
{
slice.as_simd()
}

#[inline]
fn slice_as_simd_mut<const N: usize>(
slice: &mut [Self],
) -> (&mut [Self], &mut [Self::Simd<N>], &mut [Self])
where
LaneCount<N>: SupportedLaneCount,
{
slice.as_simd_mut()
}
}
18 changes: 18 additions & 0 deletions src/real/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,4 +524,22 @@ impl Real for f64 {
fn total_cmp(&self, other: &Self) -> Ordering {
self.total_cmp(other)
}

#[inline]
fn slice_as_simd<const N: usize>(slice: &[Self]) -> (&[Self], &[Self::Simd<N>], &[Self])
where
LaneCount<N>: SupportedLaneCount,
{
slice.as_simd()
}

#[inline]
fn slice_as_simd_mut<const N: usize>(
slice: &mut [Self],
) -> (&mut [Self], &mut [Self::Simd<N>], &mut [Self])
where
LaneCount<N>: SupportedLaneCount,
{
slice.as_simd_mut()
}
}
47 changes: 47 additions & 0 deletions src/real/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,53 @@ where
{
Self::Simd::splat(self)
}

/// Split a slice into a prefix, a middle of aligned SIMD types, and a suffix.
///
/// You're only assured thatc`self.len() == prefix.len() + middle.len() * N + suffix.len()`.
///
/// Notably, all of the following are possible:
///
/// * `prefix.len() >= N`,
/// * `middle.is_empty()` despite `self.len() >= 3 * N`,
/// * `suffix.len() >= N`.
///
/// That said, this is a safe method, so if you're only writing safe code, then this can at most
/// cause incorrect logic, not unsoundness.
///
/// # Panics
///
/// Panic if the size of the SIMD type is different from `N` times that of the scalar.
#[must_use]
fn slice_as_simd<const N: usize>(slice: &[Self]) -> (&[Self], &[Self::Simd<N>], &[Self])
where
LaneCount<N>: SupportedLaneCount;

/// Split a mutable slice into a mutable prefix, a middle of aligned SIMD types, and a mutable
/// suffix.
///
/// You're only assured that `self.len() == prefix.len() + middle.len() * N + suffix.len()`.
///
/// Notably, all of the following are possible:
///
/// * `prefix.len() >= N`,
/// * `middle.is_empty()` despite `self.len() >= 3 * N`,
/// * `suffix.len() >= N`.
///
/// That said, this is a safe method, so if you're only writing safe code, then this can at most
/// cause incorrect logic, not unsoundness.
///
/// This is the mutable version of [`Self::slice_as_simd`].
///
/// # Panics
///
/// Panic if the size of the SIMD type is different from `N` times that of the scalar.
#[must_use]
fn slice_as_simd_mut<const N: usize>(
slice: &mut [Self],
) -> (&mut [Self], &mut [Self::Simd<N>], &mut [Self])
where
LaneCount<N>: SupportedLaneCount;
}

impl<R: Real> ApproxEq<R> for R {
Expand Down

0 comments on commit 8c03fcb

Please sign in to comment.