xref: /reactos/sdk/lib/crt/math/libm_sse2/atan.c (revision 84344399)
1 
2 /*******************************************************************************
3 MIT License
4 -----------
5 
6 Copyright (c) 2002-2019 Advanced Micro Devices, Inc.
7 
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this Software and associated documentaon files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14 
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 THE SOFTWARE.
25 *******************************************************************************/
26 
27 #include "libm.h"
28 #include "libm_util.h"
29 
30 #define USE_VAL_WITH_FLAGS
31 #define USE_NAN_WITH_FLAGS
32 #define USE_HANDLE_ERROR
33 #include "libm_inlines.h"
34 #undef USE_VAL_WITH_FLAGS
35 #undef USE_NAN_WITH_FLAGS
36 #undef USE_HANDLE_ERROR
37 
38 #include "libm_errno.h"
39 
40 #ifdef _MSC_VER
41 #pragma function(atan)
42 #endif
43 
44 double FN_PROTOTYPE(atan)(double x)
45 {
46 
47   /* Some constants and split constants. */
48 
49   static double piby2 = 1.5707963267948966e+00; /* 0x3ff921fb54442d18 */
50   double chi, clo, v, s, q, z;
51 
52   /* Find properties of argument x. */
53 
54   unsigned long long ux, aux, xneg;
55   GET_BITS_DP64(x, ux);
56   aux = ux & ~SIGNBIT_DP64;
57   xneg = (ux != aux);
58 
59   if (xneg) v = -x;
60   else v = x;
61 
62   /* Argument reduction to range [-7/16,7/16] */
63 
64   if (aux > 0x4003800000000000) /* v > 39./16. */
65     {
66 
67       if (aux > PINFBITPATT_DP64)
68         {
69           /* x is NaN */
70           return _handle_error("atan", OP_ATAN, ux|0x0008000000000000, _DOMAIN, 0,
71                               EDOM, x, 0.0, 1);
72         }
73       else if (v > 0x4370000000000000)
74 	{ /* abs(x) > 2^56 => arctan(1/x) is
75 	     insignificant compared to piby2 */
76 	  if (xneg)
77             return val_with_flags(-piby2, AMD_F_INEXACT);
78 	  else
79             return val_with_flags(piby2, AMD_F_INEXACT);
80 	}
81 
82       x = -1.0/v;
83       /* (chi + clo) = arctan(infinity) */
84       chi = 1.57079632679489655800e+00; /* 0x3ff921fb54442d18 */
85       clo = 6.12323399573676480327e-17; /* 0x3c91a62633145c06 */
86     }
87   else if (aux > 0x3ff3000000000000) /* 39./16. > v > 19./16. */
88     {
89       x = (v-1.5)/(1.0+1.5*v);
90       /* (chi + clo) = arctan(1.5) */
91       chi = 9.82793723247329054082e-01; /* 0x3fef730bd281f69b */
92       clo = 1.39033110312309953701e-17; /* 0x3c7007887af0cbbc */
93     }
94   else if (aux > 0x3fe6000000000000) /* 19./16. > v > 11./16. */
95     {
96       x = (v-1.0)/(1.0+v);
97       /* (chi + clo) = arctan(1.) */
98       chi = 7.85398163397448278999e-01; /* 0x3fe921fb54442d18 */
99       clo = 3.06161699786838240164e-17; /* 0x3c81a62633145c06 */
100     }
101   else if (aux > 0x3fdc000000000000) /* 11./16. > v > 7./16. */
102     {
103       x = (2.0*v-1.0)/(2.0+v);
104       /* (chi + clo) = arctan(0.5) */
105       chi = 4.63647609000806093515e-01; /* 0x3fddac670561bb4f */
106       clo = 2.26987774529616809294e-17; /* 0x3c7a2b7f222f65e0 */
107     }
108   else  /* v < 7./16. */
109     {
110       x = v;
111       chi = 0.0;
112       clo = 0.0;
113     }
114 
115   /* Core approximation: Remez(4,4) on [-7/16,7/16] */
116 
117   s = x*x;
118   q = x*s*
119        (0.268297920532545909e0 +
120 	(0.447677206805497472e0 +
121 	 (0.220638780716667420e0 +
122 	  (0.304455919504853031e-1 +
123 	    0.142316903342317766e-3*s)*s)*s)*s)/
124        (0.804893761597637733e0 +
125 	(0.182596787737507063e1 +
126 	 (0.141254259931958921e1 +
127 	  (0.424602594203847109e0 +
128 	    0.389525873944742195e-1*s)*s)*s)*s);
129 
130   z = chi - ((q - clo) - x);
131 
132   if (xneg) z = -z;
133   return z;
134 }
135