diff --git a/crates/core_simd/src/intrinsics.rs b/crates/core_simd/src/intrinsics.rs index e8bcca22f684..665dc1a51d74 100644 --- a/crates/core_simd/src/intrinsics.rs +++ b/crates/core_simd/src/intrinsics.rs @@ -86,6 +86,12 @@ mod std { // floor pub(crate) fn simd_floor(x: T) -> T; + + // round + pub(crate) fn simd_round(x: T) -> T; + + // trunc + pub(crate) fn simd_trunc(x: T) -> T; } } diff --git a/crates/core_simd/src/round.rs b/crates/core_simd/src/round.rs index 1855c1480d26..281851c68ace 100644 --- a/crates/core_simd/src/round.rs +++ b/crates/core_simd/src/round.rs @@ -7,18 +7,39 @@ macro_rules! implement { where 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"] #[inline] pub fn floor(self) -> 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"] #[inline] - pub fn ceil(self) -> Self { - unsafe { crate::intrinsics::simd_ceil(self) } + pub fn round(self) -> 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() } } diff --git a/crates/core_simd/tests/round.rs b/crates/core_simd/tests/round.rs index dc9c8ad4ad25..85853c0e8778 100644 --- a/crates/core_simd/tests/round.rs +++ b/crates/core_simd/tests/round.rs @@ -22,6 +22,30 @@ macro_rules! float_rounding_test { &|_| true, ) } + + fn round() { + test_helpers::test_unary_elementwise( + &Vector::::round, + &Scalar::round, + &|_| true, + ) + } + + fn trunc() { + test_helpers::test_unary_elementwise( + &Vector::::trunc, + &Scalar::trunc, + &|_| true, + ) + } + + fn fract() { + test_helpers::test_unary_elementwise( + &Vector::::fract, + &Scalar::fract, + &|_| true, + ) + } } test_helpers::test_lanes! {