]> git.proxmox.com Git - rustc.git/blob - vendor/compiler_builtins/libm/src/math/atan2f.rs
New upstream version 1.33.0+dfsg1
[rustc.git] / vendor / compiler_builtins / libm / src / math / atan2f.rs
1 /* origin: FreeBSD /usr/src/lib/msun/src/e_atan2f.c */
2 /*
3 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
4 */
5 /*
6 * ====================================================
7 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8 *
9 * Developed at SunPro, a Sun Microsystems, Inc. business.
10 * Permission to use, copy, modify, and distribute this
11 * software is freely granted, provided that this notice
12 * is preserved.
13 * ====================================================
14 */
15
16 use super::atanf;
17 use super::fabsf;
18
19 const PI: f32 = 3.1415927410e+00; /* 0x40490fdb */
20 const PI_LO: f32 = -8.7422776573e-08; /* 0xb3bbbd2e */
21
22 #[inline]
23 pub fn atan2f(y: f32, x: f32) -> f32 {
24 if x.is_nan() || y.is_nan() {
25 return x + y;
26 }
27 let mut ix = x.to_bits();
28 let mut iy = y.to_bits();
29
30 if ix == 0x3f800000 {
31 /* x=1.0 */
32 return atanf(y);
33 }
34 let m = ((iy >> 31) & 1) | ((ix >> 30) & 2); /* 2*sign(x)+sign(y) */
35 ix &= 0x7fffffff;
36 iy &= 0x7fffffff;
37
38 /* when y = 0 */
39 if iy == 0 {
40 return match m {
41 0 | 1 => y, /* atan(+-0,+anything)=+-0 */
42 2 => PI, /* atan(+0,-anything) = pi */
43 3 | _ => -PI, /* atan(-0,-anything) =-pi */
44 };
45 }
46 /* when x = 0 */
47 if ix == 0 {
48 return if m & 1 != 0 { -PI / 2. } else { PI / 2. };
49 }
50 /* when x is INF */
51 if ix == 0x7f800000 {
52 return if iy == 0x7f800000 {
53 match m {
54 0 => PI / 4., /* atan(+INF,+INF) */
55 1 => -PI / 4., /* atan(-INF,+INF) */
56 2 => 3. * PI / 4., /* atan(+INF,-INF)*/
57 3 | _ => -3. * PI / 4., /* atan(-INF,-INF)*/
58 }
59 } else {
60 match m {
61 0 => 0., /* atan(+...,+INF) */
62 1 => -0., /* atan(-...,+INF) */
63 2 => PI, /* atan(+...,-INF) */
64 3 | _ => -PI, /* atan(-...,-INF) */
65 }
66 };
67 }
68 /* |y/x| > 0x1p26 */
69 if (ix + (26 << 23) < iy) || (iy == 0x7f800000) {
70 return if m & 1 != 0 { -PI / 2. } else { PI / 2. };
71 }
72
73 /* z = atan(|y/x|) with correct underflow */
74 let z = if (m & 2 != 0) && (iy + (26 << 23) < ix) {
75 /*|y/x| < 0x1p-26, x < 0 */
76 0.
77 } else {
78 atanf(fabsf(y / x))
79 };
80 match m {
81 0 => z, /* atan(+,+) */
82 1 => -z, /* atan(-,+) */
83 2 => PI - (z - PI_LO), /* atan(+,-) */
84 _ => (z - PI_LO) - PI, /* case 3 */ /* atan(-,-) */
85 }
86 }