1 | /* |
2 | * origin: FreeBSD /usr/src/lib/msun/src/s_atanf.c |
3 | * |
4 | * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. |
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 | /// The arctangent function. |
16 | pub fn atan(mut x: f32) -> f32 { |
17 | const ATAN_HI: [f32; 4] = [ |
18 | 4.6364760399e-01, /* atan(0.5)hi 0x3eed6338 */ |
19 | 7.8539812565e-01, /* atan(1.0)hi 0x3f490fda */ |
20 | 9.8279368877e-01, /* atan(1.5)hi 0x3f7b985e */ |
21 | 1.5707962513e+00, /* atan(inf)hi 0x3fc90fda */ |
22 | ]; |
23 | |
24 | const ATAN_LO: [f32; 4] = [ |
25 | 5.0121582440e-09, /* atan(0.5)lo 0x31ac3769 */ |
26 | 3.7748947079e-08, /* atan(1.0)lo 0x33222168 */ |
27 | 3.4473217170e-08, /* atan(1.5)lo 0x33140fb4 */ |
28 | 7.5497894159e-08, /* atan(inf)lo 0x33a22168 */ |
29 | ]; |
30 | |
31 | const A_T: [f32; 5] = |
32 | [3.3333328366e-01, -1.9999158382e-01, 1.4253635705e-01, -1.0648017377e-01, 6.1687607318e-02]; |
33 | let x1p_120 = f32::from_bits(0x03800000); // 0x1p-120 === 2 ^ (-120) |
34 | |
35 | let z: f32; |
36 | |
37 | let mut ix = x.to_bits(); |
38 | let sign = (ix >> 31) != 0; |
39 | ix &= 0x7fffffff; |
40 | |
41 | if ix >= 0x4c800000 { |
42 | /* if |x| >= 2**26 */ |
43 | if x.is_nan() { |
44 | return x; |
45 | } |
46 | z = ATAN_HI[3] + x1p_120; |
47 | return if sign { |
48 | -z |
49 | } else { |
50 | z |
51 | }; |
52 | } |
53 | let id = if ix < 0x3ee00000 { |
54 | /* |x| < 0.4375 */ |
55 | if ix < 0x39800000 { |
56 | return x; |
57 | } |
58 | -1 |
59 | } else { |
60 | x = super::abs(x); |
61 | if ix < 0x3f980000 { |
62 | /* |x| < 1.1875 */ |
63 | if ix < 0x3f300000 { |
64 | /* 7/16 <= |x| < 11/16 */ |
65 | x = (2. * x - 1.) / (2. + x); |
66 | 0 |
67 | } else { |
68 | /* 11/16 <= |x| < 19/16 */ |
69 | x = (x - 1.) / (x + 1.); |
70 | 1 |
71 | } |
72 | } else if ix < 0x401c0000 { |
73 | /* |x| < 2.4375 */ |
74 | x = (x - 1.5) / (1. + 1.5 * x); |
75 | 2 |
76 | } else { |
77 | /* 2.4375 <= |x| < 2**26 */ |
78 | x = -1. / x; |
79 | 3 |
80 | } |
81 | }; |
82 | /* end of argument reduction */ |
83 | z = x * x; |
84 | let w = z * z; |
85 | /* break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly */ |
86 | let s1 = z * (A_T[0] + w * (A_T[2] + w * A_T[4])); |
87 | let s2 = w * (A_T[1] + w * A_T[3]); |
88 | if id < 0 { |
89 | return x - x * (s1 + s2); |
90 | } |
91 | let id = id as usize; |
92 | let z = ATAN_HI[id] - ((x * (s1 + s2) - ATAN_LO[id]) - x); |
93 | if sign { |
94 | -z |
95 | } else { |
96 | z |
97 | } |
98 | } |
99 | |