| 1 | /* SPDX-License-Identifier: MIT |
| 2 | * origin: musl src/math/floor.c */ |
| 3 | |
| 4 | //! Generic `floor` algorithm. |
| 5 | //! |
| 6 | //! Note that this uses the algorithm from musl's `floorf` rather than `floor` or `floorl` 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::support::{FpResult, Status}; |
| 11 | use super::super::{Float, Int, IntTy, MinInt}; |
| 12 | |
| 13 | pub fn floor<F: Float>(x: F) -> F { |
| 14 | floor_status(x).val |
| 15 | } |
| 16 | |
| 17 | pub fn floor_status<F: Float>(x: F) -> FpResult<F> { |
| 18 | let zero = IntTy::<F>::ZERO; |
| 19 | |
| 20 | let mut ix = x.to_bits(); |
| 21 | let e = x.exp_unbiased(); |
| 22 | |
| 23 | // If the represented value has no fractional part, no truncation is needed. |
| 24 | if e >= F::SIG_BITS as i32 { |
| 25 | return FpResult::ok(x); |
| 26 | } |
| 27 | |
| 28 | let status; |
| 29 | let res = if e >= 0 { |
| 30 | // |x| >= 1.0 |
| 31 | let m = F::SIG_MASK >> e.unsigned(); |
| 32 | if ix & m == zero { |
| 33 | // Portion to be masked is already zero; no adjustment needed. |
| 34 | return FpResult::ok(x); |
| 35 | } |
| 36 | |
| 37 | // Otherwise, raise an inexact exception. |
| 38 | status = Status::INEXACT; |
| 39 | |
| 40 | if x.is_sign_negative() { |
| 41 | ix += m; |
| 42 | } |
| 43 | |
| 44 | ix &= !m; |
| 45 | F::from_bits(ix) |
| 46 | } else { |
| 47 | // |x| < 1.0, raise an inexact exception since truncation will happen. |
| 48 | if ix & F::SIG_MASK == F::Int::ZERO { |
| 49 | status = Status::OK; |
| 50 | } else { |
| 51 | status = Status::INEXACT; |
| 52 | } |
| 53 | |
| 54 | if x.is_sign_positive() { |
| 55 | // 0.0 <= x < 1.0; rounding down goes toward +0.0. |
| 56 | F::ZERO |
| 57 | } else if ix << 1 != zero { |
| 58 | // -1.0 < x < 0.0; rounding down goes toward -1.0. |
| 59 | F::NEG_ONE |
| 60 | } else { |
| 61 | // -0.0 remains unchanged |
| 62 | x |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | FpResult::new(res, status) |
| 67 | } |
| 68 | |
| 69 | #[cfg (test)] |
| 70 | mod tests { |
| 71 | use super::*; |
| 72 | use crate::support::Hexf; |
| 73 | |
| 74 | /// Test against https://en.cppreference.com/w/cpp/numeric/math/floor |
| 75 | fn spec_test<F: Float>(cases: &[(F, F, Status)]) { |
| 76 | let roundtrip = [F::ZERO, F::ONE, F::NEG_ONE, F::NEG_ZERO, F::INFINITY, F::NEG_INFINITY]; |
| 77 | |
| 78 | for x in roundtrip { |
| 79 | let FpResult { val, status } = floor_status(x); |
| 80 | assert_biteq!(val, x, "{}" , Hexf(x)); |
| 81 | assert_eq!(status, Status::OK, "{}" , Hexf(x)); |
| 82 | } |
| 83 | |
| 84 | for &(x, res, res_stat) in cases { |
| 85 | let FpResult { val, status } = floor_status(x); |
| 86 | assert_biteq!(val, res, "{}" , Hexf(x)); |
| 87 | assert_eq!(status, res_stat, "{}" , Hexf(x)); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /* Skipping f16 / f128 "sanity_check"s and spec cases due to rejected literal lexing at MSRV */ |
| 92 | |
| 93 | #[test ] |
| 94 | #[cfg (f16_enabled)] |
| 95 | fn spec_tests_f16() { |
| 96 | let cases = []; |
| 97 | spec_test::<f16>(&cases); |
| 98 | } |
| 99 | |
| 100 | #[test ] |
| 101 | fn sanity_check_f32() { |
| 102 | assert_eq!(floor(0.5f32), 0.0); |
| 103 | assert_eq!(floor(1.1f32), 1.0); |
| 104 | assert_eq!(floor(2.9f32), 2.0); |
| 105 | } |
| 106 | |
| 107 | #[test ] |
| 108 | fn spec_tests_f32() { |
| 109 | let cases = [ |
| 110 | (0.1, 0.0, Status::INEXACT), |
| 111 | (-0.1, -1.0, Status::INEXACT), |
| 112 | (0.9, 0.0, Status::INEXACT), |
| 113 | (-0.9, -1.0, Status::INEXACT), |
| 114 | (1.1, 1.0, Status::INEXACT), |
| 115 | (-1.1, -2.0, Status::INEXACT), |
| 116 | (1.9, 1.0, Status::INEXACT), |
| 117 | (-1.9, -2.0, Status::INEXACT), |
| 118 | ]; |
| 119 | spec_test::<f32>(&cases); |
| 120 | } |
| 121 | |
| 122 | #[test ] |
| 123 | fn sanity_check_f64() { |
| 124 | assert_eq!(floor(1.1f64), 1.0); |
| 125 | assert_eq!(floor(2.9f64), 2.0); |
| 126 | } |
| 127 | |
| 128 | #[test ] |
| 129 | fn spec_tests_f64() { |
| 130 | let cases = [ |
| 131 | (0.1, 0.0, Status::INEXACT), |
| 132 | (-0.1, -1.0, Status::INEXACT), |
| 133 | (0.9, 0.0, Status::INEXACT), |
| 134 | (-0.9, -1.0, Status::INEXACT), |
| 135 | (1.1, 1.0, Status::INEXACT), |
| 136 | (-1.1, -2.0, Status::INEXACT), |
| 137 | (1.9, 1.0, Status::INEXACT), |
| 138 | (-1.9, -2.0, Status::INEXACT), |
| 139 | ]; |
| 140 | spec_test::<f64>(&cases); |
| 141 | } |
| 142 | |
| 143 | #[test ] |
| 144 | #[cfg (f128_enabled)] |
| 145 | fn spec_tests_f128() { |
| 146 | let cases = []; |
| 147 | spec_test::<f128>(&cases); |
| 148 | } |
| 149 | } |
| 150 | |