Skip to content

Commit 0d0e96d

Browse files
committed
Add round_ties_even to StdFloat trait
Adds `round_ties_even` using `simd_round_ties_even` intrinsic, matching the scalar `f32::round_ties_even` / `f64::round_ties_even` API. Closes #390
1 parent b8bef67 commit 0d0e96d

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

crates/core_simd/tests/round.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ macro_rules! float_rounding_test {
4242
)
4343
}
4444

45+
fn round_ties_even<const LANES: usize>() {
46+
test_helpers::test_unary_elementwise(
47+
&Vector::<LANES>::round_ties_even,
48+
&Scalar::round_ties_even,
49+
&|_| true,
50+
)
51+
}
52+
4553
fn fract<const LANES: usize>() {
4654
test_helpers::test_unary_elementwise_flush_subnormals(
4755
&Vector::<LANES>::fract,

crates/std_float/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ pub trait StdFloat: Sealed + Sized {
156156
unsafe { intrinsics::simd_trunc(self) }
157157
}
158158

159+
/// Rounds each element to the nearest integer-valued float.
160+
/// Ties are resolved by rounding to the number with an even least significant digit.
161+
#[must_use = "method returns a new vector and does not mutate the original value"]
162+
#[inline]
163+
fn round_ties_even(self) -> Self {
164+
unsafe { intrinsics::simd_round_ties_even(self) }
165+
}
166+
159167
/// Returns the floating point's fractional value, with its integer part removed.
160168
#[must_use = "method returns a new vector and does not mutate the original value"]
161169
fn fract(self) -> Self;

crates/std_float/tests/float.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ macro_rules! impl_tests {
7171
mod $scalar {
7272
use std_float::StdFloat;
7373

74-
unary_test! { $scalar, sqrt, ceil, floor, round, trunc }
74+
unary_test! { $scalar, sqrt, ceil, floor, round, trunc, round_ties_even }
7575
ternary_test! { $scalar, mul_add }
7676

7777
// https://github.com/rust-lang/miri/issues/3555

0 commit comments

Comments
 (0)