Skip to content

Commit

Permalink
Add SIMD round, trunc, fract
Browse files Browse the repository at this point in the history
  • Loading branch information
workingjubilee committed Apr 26, 2021
1 parent b4fda6e commit 6ea08d8
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
6 changes: 6 additions & 0 deletions crates/core_simd/src/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ mod std {

// floor
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;
}
}

Expand Down
29 changes: 25 additions & 4 deletions crates/core_simd/src/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down
24 changes: 24 additions & 0 deletions crates/core_simd/tests/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,30 @@ macro_rules! float_rounding_test {
&|_| 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! {
Expand Down

0 comments on commit 6ea08d8

Please sign in to comment.