1#![allow(unreachable_code)]
2use core::f64;
3
4const TOINT: f64 = 1. / f64::EPSILON;
5
6#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
10pub fn ceil(x: f64) -> f64 {
11 llvm_intrinsically_optimized! {
15 #[cfg(target_arch = "wasm32")] {
16 return unsafe { ::core::intrinsics::ceilf64(x) }
17 }
18 }
19 #[cfg(all(target_arch = "x86", not(target_feature = "sse2")))]
20 {
21 use super::fabs;
26 if fabs(x).to_bits() < 4503599627370496.0_f64.to_bits() {
27 let truncated = x as i64 as f64;
28 if truncated < x {
29 return truncated + 1.0;
30 } else {
31 return truncated;
32 }
33 } else {
34 return x;
35 }
36 }
37 let u: u64 = x.to_bits();
38 let e: i64 = (u >> 52 & 0x7ff) as i64;
39 let y: f64;
40
41 if e >= 0x3ff + 52 || x == 0. {
42 return x;
43 }
44 y = if (u >> 63) != 0 { x - TOINT + TOINT - x } else { x + TOINT - TOINT - x };
46 if e < 0x3ff {
48 force_eval!(y);
49 return if (u >> 63) != 0 { -0. } else { 1. };
50 }
51 if y < 0. { x + y + 1. } else { x + y }
52}
53
54#[cfg(test)]
55mod tests {
56 use core::f64::*;
57
58 use super::*;
59
60 #[test]
61 fn sanity_check() {
62 assert_eq!(ceil(1.1), 2.0);
63 assert_eq!(ceil(2.9), 3.0);
64 }
65
66 #[test]
68 fn spec_tests() {
69 assert!(ceil(NAN).is_nan());
71 for f in [0.0, -0.0, INFINITY, NEG_INFINITY].iter().copied() {
72 assert_eq!(ceil(f), f);
73 }
74 }
75}