Skip to content

Commit 456ac1c

Browse files
committed
perf: improve non-std round for f32/f64 with non-branching impl
1 parent ce46efe commit 456ac1c

File tree

1 file changed

+2
-10
lines changed

1 file changed

+2
-10
lines changed

dasp_sample/src/ops.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@ pub mod f32 {
2020
#[cfg(not(feature = "std"))]
2121
#[inline]
2222
pub fn round(x: f32) -> f32 {
23-
if x >= 0.0 {
24-
(x + 0.5) as i32 as f32
25-
} else {
26-
(x - 0.5) as i32 as f32
27-
}
23+
(x + 0.5_f32.copysign(x)) as i64 as f32
2824
}
2925
#[cfg(feature = "std")]
3026
#[inline]
@@ -55,11 +51,7 @@ pub mod f64 {
5551
#[cfg(not(feature = "std"))]
5652
#[inline]
5753
pub fn round(x: f64) -> f64 {
58-
if x >= 0.0 {
59-
(x + 0.5) as i64 as f64
60-
} else {
61-
(x - 0.5) as i64 as f64
62-
}
54+
(x + 0.5_f64.copysign(x)) as i64 as f64
6355
}
6456
#[cfg(feature = "std")]
6557
#[inline]

0 commit comments

Comments
 (0)