Add SIMD round, trunc, fract

This commit is contained in:
Jubilee Young 2021-04-25 15:03:17 -07:00
parent b4fda6ef06
commit 6ea08d8d5f
3 changed files with 55 additions and 4 deletions

View File

@ -86,6 +86,12 @@ mod std {
// floor // floor
pub(crate) fn simd_floor<T>(x: T) -> T; pub(crate) fn simd_floor<T>(x: T) -> T;
// round
pub(crate) fn simd_round<T>(x: T) -> T;
// trunc
pub(crate) fn simd_trunc<T>(x: T) -> T;
} }
} }

View File

@ -7,18 +7,39 @@ macro_rules! implement {
where where
Self: crate::LanesAtMost32, Self: crate::LanesAtMost32,
{ {
/// Returns the largest integer less than or equal to each lane. /// Returns the smallest integer greater than or equal to each lane.
#[must_use = "method returns a new vector and does not mutate the original value"]
#[inline]
pub fn ceil(self) -> Self {
unsafe { crate::intrinsics::simd_ceil(self) }
}
/// Returns the largest integer value less than or equal to each lane.
#[must_use = "method returns a new vector and does not mutate the original value"] #[must_use = "method returns a new vector and does not mutate the original value"]
#[inline] #[inline]
pub fn floor(self) -> Self { pub fn floor(self) -> Self {
unsafe { crate::intrinsics::simd_floor(self) } unsafe { crate::intrinsics::simd_floor(self) }
} }
/// Returns the smallest integer greater than or equal to each lane. /// Rounds to the nearest integer value. Ties round toward zero.
#[must_use = "method returns a new vector and does not mutate the original value"] #[must_use = "method returns a new vector and does not mutate the original value"]
#[inline] #[inline]
pub fn ceil(self) -> Self { pub fn round(self) -> Self {
unsafe { crate::intrinsics::simd_ceil(self) } unsafe { crate::intrinsics::simd_round(self) }
}
/// Returns the floating point's integer value, with its fractional part removed.
#[must_use = "method returns a new vector and does not mutate the original value"]
#[inline]
pub fn trunc(self) -> Self {
unsafe { crate::intrinsics::simd_trunc(self) }
}
/// Returns the floating point's fractional value, with its integer part removed.
#[must_use = "method returns a new vector and does not mutate the original value"]
#[inline]
pub fn fract(self) -> Self {
self - self.trunc()
} }
} }

View File

@ -22,6 +22,30 @@ macro_rules! float_rounding_test {
&|_| true, &|_| true,
) )
} }
fn round<const LANES: usize>() {
test_helpers::test_unary_elementwise(
&Vector::<LANES>::round,
&Scalar::round,
&|_| true,
)
}
fn trunc<const LANES: usize>() {
test_helpers::test_unary_elementwise(
&Vector::<LANES>::trunc,
&Scalar::trunc,
&|_| true,
)
}
fn fract<const LANES: usize>() {
test_helpers::test_unary_elementwise(
&Vector::<LANES>::fract,
&Scalar::fract,
&|_| true,
)
}
} }
test_helpers::test_lanes! { test_helpers::test_lanes! {