1 | #[cfg_attr (all(test, assert_no_panic), no_panic::no_panic)] |
2 | pub fn nextafter(x: f64, y: f64) -> f64 { |
3 | if x.is_nan() || y.is_nan() { |
4 | return x + y; |
5 | } |
6 | |
7 | let mut ux_i = x.to_bits(); |
8 | let uy_i = y.to_bits(); |
9 | if ux_i == uy_i { |
10 | return y; |
11 | } |
12 | |
13 | let ax = ux_i & !1_u64 / 2; |
14 | let ay = uy_i & !1_u64 / 2; |
15 | if ax == 0 { |
16 | if ay == 0 { |
17 | return y; |
18 | } |
19 | ux_i = (uy_i & 1_u64 << 63) | 1; |
20 | } else if ax > ay || ((ux_i ^ uy_i) & 1_u64 << 63) != 0 { |
21 | ux_i -= 1; |
22 | } else { |
23 | ux_i += 1; |
24 | } |
25 | |
26 | let e = ux_i.wrapping_shr(52 & 0x7ff); |
27 | // raise overflow if ux.f is infinite and x is finite |
28 | if e == 0x7ff { |
29 | force_eval!(x + x); |
30 | } |
31 | let ux_f = f64::from_bits(ux_i); |
32 | // raise underflow if ux.f is subnormal or zero |
33 | if e == 0 { |
34 | force_eval!(x * x + ux_f * ux_f); |
35 | } |
36 | ux_f |
37 | } |
38 | |