/rust/registry/src/index.crates.io-6f17d22bba15001f/libm-0.2.11/src/math/ceilf.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use core::f32; |
2 | | |
3 | | /// Ceil (f32) |
4 | | /// |
5 | | /// Finds the nearest integer greater than or equal to `x`. |
6 | | #[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)] |
7 | 0 | pub fn ceilf(x: f32) -> f32 { |
8 | 0 | // On wasm32 we know that LLVM's intrinsic will compile to an optimized |
9 | 0 | // `f32.ceil` native instruction, so we can leverage this for both code size |
10 | 0 | // and speed. |
11 | 0 | llvm_intrinsically_optimized! { |
12 | 0 | #[cfg(target_arch = "wasm32")] { |
13 | 0 | return unsafe { ::core::intrinsics::ceilf32(x) } |
14 | 0 | } |
15 | 0 | } |
16 | 0 | let mut ui = x.to_bits(); |
17 | 0 | let e = (((ui >> 23) & 0xff).wrapping_sub(0x7f)) as i32; |
18 | 0 |
|
19 | 0 | if e >= 23 { |
20 | 0 | return x; |
21 | 0 | } |
22 | 0 | if e >= 0 { |
23 | 0 | let m = 0x007fffff >> e; |
24 | 0 | if (ui & m) == 0 { |
25 | 0 | return x; |
26 | 0 | } |
27 | 0 | force_eval!(x + f32::from_bits(0x7b800000)); |
28 | 0 | if ui >> 31 == 0 { |
29 | 0 | ui += m; |
30 | 0 | } |
31 | 0 | ui &= !m; |
32 | | } else { |
33 | 0 | force_eval!(x + f32::from_bits(0x7b800000)); |
34 | 0 | if ui >> 31 != 0 { |
35 | 0 | return -0.0; |
36 | 0 | } else if ui << 1 != 0 { |
37 | 0 | return 1.0; |
38 | 0 | } |
39 | | } |
40 | 0 | f32::from_bits(ui) |
41 | 0 | } |
42 | | |
43 | | // PowerPC tests are failing on LLVM 13: https://github.com/rust-lang/rust/issues/88520 |
44 | | #[cfg(not(target_arch = "powerpc64"))] |
45 | | #[cfg(test)] |
46 | | mod tests { |
47 | | use core::f32::*; |
48 | | |
49 | | use super::*; |
50 | | |
51 | | #[test] |
52 | | fn sanity_check() { |
53 | | assert_eq!(ceilf(1.1), 2.0); |
54 | | assert_eq!(ceilf(2.9), 3.0); |
55 | | } |
56 | | |
57 | | /// The spec: https://en.cppreference.com/w/cpp/numeric/math/ceil |
58 | | #[test] |
59 | | fn spec_tests() { |
60 | | // Not Asserted: that the current rounding mode has no effect. |
61 | | assert!(ceilf(NAN).is_nan()); |
62 | | for f in [0.0, -0.0, INFINITY, NEG_INFINITY].iter().copied() { |
63 | | assert_eq!(ceilf(f), f); |
64 | | } |
65 | | } |
66 | | } |