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