5 /// Finds the nearest integer greater than or equal to `x`.
6 #[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
7 pub fn ceilf(x
: f32) -> f32 {
8 // On wasm32 we know that LLVM's intrinsic will compile to an optimized
9 // `f32.ceil` native instruction, so we can leverage this for both code size
11 llvm_intrinsically_optimized
! {
12 #[cfg(target_arch = "wasm32")] {
13 return unsafe { ::core::intrinsics::ceilf32(x) }
16 let mut ui
= x
.to_bits();
17 let e
= (((ui
>> 23) & 0xff).wrapping_sub(0x7f)) as i32;
23 let m
= 0x007fffff >> e
;
27 force_eval
!(x
+ f32::from_bits(0x7b800000));
33 force_eval
!(x
+ f32::from_bits(0x7b800000));
36 } else if ui
<< 1 != 0 {
50 assert_eq
!(ceilf(1.1), 2.0);
51 assert_eq
!(ceilf(2.9), 3.0);
54 /// The spec: https://en.cppreference.com/w/cpp/numeric/math/ceil
57 // Not Asserted: that the current rounding mode has no effect.
58 assert
!(ceilf(NAN
).is_nan());
59 for f
in [0.0, -0.0, INFINITY
, NEG_INFINITY
].iter().copied() {
60 assert_eq
!(ceilf(f
), f
);