1 | use core::f32; |
2 | |
3 | use super::sqrtf; |
4 | |
5 | #[cfg_attr (all(test, assert_no_panic), no_panic::no_panic)] |
6 | pub fn hypotf(mut x: f32, mut y: f32) -> f32 { |
7 | let x1p90 = f32::from_bits(0x6c800000); // 0x1p90f === 2 ^ 90 |
8 | let x1p_90 = f32::from_bits(0x12800000); // 0x1p-90f === 2 ^ -90 |
9 | |
10 | let mut uxi = x.to_bits(); |
11 | let mut uyi = y.to_bits(); |
12 | let uti; |
13 | let mut z: f32; |
14 | |
15 | uxi &= -1i32 as u32 >> 1; |
16 | uyi &= -1i32 as u32 >> 1; |
17 | if uxi < uyi { |
18 | uti = uxi; |
19 | uxi = uyi; |
20 | uyi = uti; |
21 | } |
22 | |
23 | x = f32::from_bits(uxi); |
24 | y = f32::from_bits(uyi); |
25 | if uyi == 0xff << 23 { |
26 | return y; |
27 | } |
28 | if uxi >= 0xff << 23 || uyi == 0 || uxi - uyi >= 25 << 23 { |
29 | return x + y; |
30 | } |
31 | |
32 | z = 1.; |
33 | if uxi >= (0x7f + 60) << 23 { |
34 | z = x1p90; |
35 | x *= x1p_90; |
36 | y *= x1p_90; |
37 | } else if uyi < (0x7f - 60) << 23 { |
38 | z = x1p_90; |
39 | x *= x1p90; |
40 | y *= x1p90; |
41 | } |
42 | z * sqrtf((x as f64 * x as f64 + y as f64 * y as f64) as f32) |
43 | } |
44 | |