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::support::{FpResult, Status}; |
11 | use super::super::{Float, Int, IntTy, MinInt}; |
12 | |
13 | #[inline ] |
14 | pub fn ceil<F: Float>(x: F) -> F { |
15 | ceil_status(x).val |
16 | } |
17 | |
18 | #[inline ] |
19 | pub fn ceil_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_positive() { |
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 (unless x == 0). |
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_negative() { |
57 | // -1.0 < x <= -0.0; rounding up goes toward -0.0. |
58 | F::NEG_ZERO |
59 | } else if ix << 1 != zero { |
60 | // 0.0 < x < 1.0; rounding up goes toward +1.0. |
61 | F::ONE |
62 | } else { |
63 | // +0.0 remains unchanged |
64 | x |
65 | } |
66 | }; |
67 | |
68 | FpResult::new(res, status) |
69 | } |
70 | |
71 | #[cfg (test)] |
72 | mod tests { |
73 | use super::*; |
74 | use crate::support::Hexf; |
75 | |
76 | /// Test against https://en.cppreference.com/w/cpp/numeric/math/ceil |
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 } = ceil_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 } = ceil_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 due to rejected literal lexing at MSRV */ |
101 | |
102 | #[test ] |
103 | #[cfg (f16_enabled)] |
104 | fn spec_tests_f16() { |
105 | let cases = [ |
106 | (0.1, 1.0, Status::INEXACT), |
107 | (-0.1, -0.0, Status::INEXACT), |
108 | (0.9, 1.0, Status::INEXACT), |
109 | (-0.9, -0.0, Status::INEXACT), |
110 | (1.1, 2.0, Status::INEXACT), |
111 | (-1.1, -1.0, Status::INEXACT), |
112 | (1.9, 2.0, Status::INEXACT), |
113 | (-1.9, -1.0, Status::INEXACT), |
114 | ]; |
115 | spec_test::<f16>(&cases); |
116 | } |
117 | |
118 | #[test ] |
119 | fn sanity_check_f32() { |
120 | assert_eq!(ceil(1.1f32), 2.0); |
121 | assert_eq!(ceil(2.9f32), 3.0); |
122 | } |
123 | |
124 | #[test ] |
125 | fn spec_tests_f32() { |
126 | let cases = [ |
127 | (0.1, 1.0, Status::INEXACT), |
128 | (-0.1, -0.0, Status::INEXACT), |
129 | (0.9, 1.0, Status::INEXACT), |
130 | (-0.9, -0.0, Status::INEXACT), |
131 | (1.1, 2.0, Status::INEXACT), |
132 | (-1.1, -1.0, Status::INEXACT), |
133 | (1.9, 2.0, Status::INEXACT), |
134 | (-1.9, -1.0, Status::INEXACT), |
135 | ]; |
136 | spec_test::<f32>(&cases); |
137 | } |
138 | |
139 | #[test ] |
140 | fn sanity_check_f64() { |
141 | assert_eq!(ceil(1.1f64), 2.0); |
142 | assert_eq!(ceil(2.9f64), 3.0); |
143 | } |
144 | |
145 | #[test ] |
146 | fn spec_tests_f64() { |
147 | let cases = [ |
148 | (0.1, 1.0, Status::INEXACT), |
149 | (-0.1, -0.0, Status::INEXACT), |
150 | (0.9, 1.0, Status::INEXACT), |
151 | (-0.9, -0.0, Status::INEXACT), |
152 | (1.1, 2.0, Status::INEXACT), |
153 | (-1.1, -1.0, Status::INEXACT), |
154 | (1.9, 2.0, Status::INEXACT), |
155 | (-1.9, -1.0, Status::INEXACT), |
156 | ]; |
157 | spec_test::<f64>(&cases); |
158 | } |
159 | |
160 | #[test ] |
161 | #[cfg (f128_enabled)] |
162 | fn spec_tests_f128() { |
163 | let cases = [ |
164 | (0.1, 1.0, Status::INEXACT), |
165 | (-0.1, -0.0, Status::INEXACT), |
166 | (0.9, 1.0, Status::INEXACT), |
167 | (-0.9, -0.0, Status::INEXACT), |
168 | (1.1, 2.0, Status::INEXACT), |
169 | (-1.1, -1.0, Status::INEXACT), |
170 | (1.9, 2.0, Status::INEXACT), |
171 | (-1.9, -1.0, Status::INEXACT), |
172 | ]; |
173 | spec_test::<f128>(&cases); |
174 | } |
175 | } |
176 | |