Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 6ea08d8

Browse files
Add SIMD round, trunc, fract
1 parent b4fda6e commit 6ea08d8

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

crates/core_simd/src/intrinsics.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ mod std {
8686

8787
// floor
8888
pub(crate) fn simd_floor<T>(x: T) -> T;
89+
90+
// round
91+
pub(crate) fn simd_round<T>(x: T) -> T;
92+
93+
// trunc
94+
pub(crate) fn simd_trunc<T>(x: T) -> T;
8995
}
9096
}
9197

crates/core_simd/src/round.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,39 @@ macro_rules! implement {
77
where
88
Self: crate::LanesAtMost32,
99
{
10-
/// Returns the largest integer less than or equal to each lane.
10+
/// Returns the smallest integer greater than or equal to each lane.
11+
#[must_use = "method returns a new vector and does not mutate the original value"]
12+
#[inline]
13+
pub fn ceil(self) -> Self {
14+
unsafe { crate::intrinsics::simd_ceil(self) }
15+
}
16+
17+
/// Returns the largest integer value less than or equal to each lane.
1118
#[must_use = "method returns a new vector and does not mutate the original value"]
1219
#[inline]
1320
pub fn floor(self) -> Self {
1421
unsafe { crate::intrinsics::simd_floor(self) }
1522
}
1623

17-
/// Returns the smallest integer greater than or equal to each lane.
24+
/// Rounds to the nearest integer value. Ties round toward zero.
1825
#[must_use = "method returns a new vector and does not mutate the original value"]
1926
#[inline]
20-
pub fn ceil(self) -> Self {
21-
unsafe { crate::intrinsics::simd_ceil(self) }
27+
pub fn round(self) -> Self {
28+
unsafe { crate::intrinsics::simd_round(self) }
29+
}
30+
31+
/// Returns the floating point's integer value, with its fractional part removed.
32+
#[must_use = "method returns a new vector and does not mutate the original value"]
33+
#[inline]
34+
pub fn trunc(self) -> Self {
35+
unsafe { crate::intrinsics::simd_trunc(self) }
36+
}
37+
38+
/// Returns the floating point's fractional value, with its integer part removed.
39+
#[must_use = "method returns a new vector and does not mutate the original value"]
40+
#[inline]
41+
pub fn fract(self) -> Self {
42+
self - self.trunc()
2243
}
2344
}
2445

crates/core_simd/tests/round.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,30 @@ macro_rules! float_rounding_test {
2222
&|_| true,
2323
)
2424
}
25+
26+
fn round<const LANES: usize>() {
27+
test_helpers::test_unary_elementwise(
28+
&Vector::<LANES>::round,
29+
&Scalar::round,
30+
&|_| true,
31+
)
32+
}
33+
34+
fn trunc<const LANES: usize>() {
35+
test_helpers::test_unary_elementwise(
36+
&Vector::<LANES>::trunc,
37+
&Scalar::trunc,
38+
&|_| true,
39+
)
40+
}
41+
42+
fn fract<const LANES: usize>() {
43+
test_helpers::test_unary_elementwise(
44+
&Vector::<LANES>::fract,
45+
&Scalar::fract,
46+
&|_| true,
47+
)
48+
}
2549
}
2650

2751
test_helpers::test_lanes! {

0 commit comments

Comments
 (0)