|
| 1 | +/* SPDX-License-Identifier: MIT */ |
| 2 | +/* origin: musl src/math/ceilf.c */ |
| 3 | + |
| 4 | +//! Generic `ceil` algorithm. |
| 5 | +//! |
| 6 | +//! Note that this uses the algorithm from musl's `ceilf` rather than `ceil` or `ceill` because |
| 7 | +//! performance seems to be better (based on icount) and it does not seem to experience rounding |
| 8 | +//! errors on i386. |
| 9 | +
|
| 10 | +use super::super::{Float, Int, IntTy, MinInt}; |
| 11 | + |
| 12 | +pub fn ceil<F: Float>(x: F) -> F { |
| 13 | + let zero = IntTy::<F>::ZERO; |
| 14 | + |
| 15 | + let mut ix = x.to_bits(); |
| 16 | + let e = x.exp_unbiased(); |
| 17 | + |
| 18 | + // If the represented value has no fractional part, no truncation is needed. |
| 19 | + if e >= F::SIG_BITS as i32 { |
| 20 | + return x; |
| 21 | + } |
| 22 | + |
| 23 | + if e >= 0 { |
| 24 | + // |x| >= 1.0 |
| 25 | + |
| 26 | + let m = F::SIG_MASK >> e.unsigned(); |
| 27 | + if (ix & m) == zero { |
| 28 | + // Portion to be masked is already zero; no adjustment needed. |
| 29 | + return x; |
| 30 | + } |
| 31 | + |
| 32 | + // Otherwise, raise an inexact exception. |
| 33 | + force_eval!(x + F::MAX); |
| 34 | + if x.is_sign_positive() { |
| 35 | + ix += m; |
| 36 | + } |
| 37 | + ix &= !m; |
| 38 | + } else { |
| 39 | + // |x| < 1.0, raise an inexact exception since truncation will happen (unless x == 0). |
| 40 | + force_eval!(x + F::MAX); |
| 41 | + |
| 42 | + if x.is_sign_negative() { |
| 43 | + // -1.0 < x <= -0.0; rounding up goes toward -0.0. |
| 44 | + return F::NEG_ZERO; |
| 45 | + } else if ix << 1 != zero { |
| 46 | + // 0.0 < x < 1.0; rounding up goes toward +1.0. |
| 47 | + return F::ONE; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + F::from_bits(ix) |
| 52 | +} |
| 53 | + |
| 54 | +#[cfg(test)] |
| 55 | +mod tests { |
| 56 | + use super::*; |
| 57 | + |
| 58 | + /// Test against https://en.cppreference.com/w/cpp/numeric/math/ceil |
| 59 | + fn spec_test<F: Float>() { |
| 60 | + // Not Asserted: that the current rounding mode has no effect. |
| 61 | + for f in [F::ZERO, F::NEG_ZERO, F::INFINITY, F::NEG_INFINITY].iter().copied() { |
| 62 | + assert_biteq!(ceil(f), f); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /* Skipping f16 / f128 "sanity_check"s due to rejected literal lexing at MSRV */ |
| 67 | + |
| 68 | + #[test] |
| 69 | + #[cfg(f16_enabled)] |
| 70 | + fn spec_tests_f16() { |
| 71 | + spec_test::<f16>(); |
| 72 | + } |
| 73 | + |
| 74 | + #[test] |
| 75 | + fn sanity_check_f32() { |
| 76 | + assert_eq!(ceil(1.1f32), 2.0); |
| 77 | + assert_eq!(ceil(2.9f32), 3.0); |
| 78 | + } |
| 79 | + |
| 80 | + #[test] |
| 81 | + fn spec_tests_f32() { |
| 82 | + spec_test::<f32>(); |
| 83 | + } |
| 84 | + |
| 85 | + #[test] |
| 86 | + fn sanity_check_f64() { |
| 87 | + assert_eq!(ceil(1.1f64), 2.0); |
| 88 | + assert_eq!(ceil(2.9f64), 3.0); |
| 89 | + } |
| 90 | + |
| 91 | + #[test] |
| 92 | + fn spec_tests_f64() { |
| 93 | + spec_test::<f64>(); |
| 94 | + } |
| 95 | + |
| 96 | + #[test] |
| 97 | + #[cfg(f128_enabled)] |
| 98 | + fn spec_tests_f128() { |
| 99 | + spec_test::<f128>(); |
| 100 | + } |
| 101 | +} |
0 commit comments