Skip to content

Commit 033a1a7

Browse files
committed
libm: Add an optimization for floor
ceil seems to optimize better, but this gets closer to the pre-fix codegen.
1 parent 9d177e5 commit 033a1a7

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed

libm/src/math/generic/floor.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ pub fn floor_status<F: Float>(x: F) -> FpResult<F> {
2626
return FpResult::ok(x);
2727
}
2828

29-
let status;
3029
let res = if e >= 0 {
3130
// |x| >= 1.0
3231
let m = F::SIG_MASK >> e.unsigned();
@@ -35,36 +34,29 @@ pub fn floor_status<F: Float>(x: F) -> FpResult<F> {
3534
return FpResult::ok(x);
3635
}
3736

38-
// Otherwise, raise an inexact exception.
39-
status = Status::INEXACT;
40-
4137
if x.is_sign_negative() {
4238
ix += m;
4339
}
4440

4541
ix &= !m;
4642
F::from_bits(ix)
4743
} else {
48-
// |x| < 1.0, raise an inexact exception since truncation will happen.
49-
if ix & !F::SIGN_MASK == F::Int::ZERO {
50-
status = Status::OK;
51-
} else {
52-
status = Status::INEXACT;
44+
// |x| < 1.0, zero or inexact with truncation
45+
46+
if (ix & !F::SIGN_MASK) == F::Int::ZERO {
47+
return FpResult::ok(x);
5348
}
5449

5550
if x.is_sign_positive() {
5651
// 0.0 <= x < 1.0; rounding down goes toward +0.0.
5752
F::ZERO
58-
} else if ix << 1 != zero {
53+
} else {
5954
// -1.0 < x < 0.0; rounding down goes toward -1.0.
6055
F::NEG_ONE
61-
} else {
62-
// -0.0 remains unchanged
63-
x
6456
}
6557
};
6658

67-
FpResult::new(res, status)
59+
FpResult::new(res, Status::INEXACT)
6860
}
6961

7062
#[cfg(test)]

0 commit comments

Comments
 (0)