xref: /haiku/src/system/libroot/posix/musl/math/atanf.c (revision f504f61099b010fbfa94b1cc63d2e9072c7f7185)
1*f504f610SAugustin Cavalier /* origin: FreeBSD /usr/src/lib/msun/src/s_atanf.c */
2*f504f610SAugustin Cavalier /*
3*f504f610SAugustin Cavalier  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
4*f504f610SAugustin Cavalier  */
5*f504f610SAugustin Cavalier /*
6*f504f610SAugustin Cavalier  * ====================================================
7*f504f610SAugustin Cavalier  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8*f504f610SAugustin Cavalier  *
9*f504f610SAugustin Cavalier  * Developed at SunPro, a Sun Microsystems, Inc. business.
10*f504f610SAugustin Cavalier  * Permission to use, copy, modify, and distribute this
11*f504f610SAugustin Cavalier  * software is freely granted, provided that this notice
12*f504f610SAugustin Cavalier  * is preserved.
13*f504f610SAugustin Cavalier  * ====================================================
14*f504f610SAugustin Cavalier  */
15*f504f610SAugustin Cavalier 
16*f504f610SAugustin Cavalier 
17*f504f610SAugustin Cavalier #include "libm.h"
18*f504f610SAugustin Cavalier 
19*f504f610SAugustin Cavalier static const float atanhi[] = {
20*f504f610SAugustin Cavalier   4.6364760399e-01, /* atan(0.5)hi 0x3eed6338 */
21*f504f610SAugustin Cavalier   7.8539812565e-01, /* atan(1.0)hi 0x3f490fda */
22*f504f610SAugustin Cavalier   9.8279368877e-01, /* atan(1.5)hi 0x3f7b985e */
23*f504f610SAugustin Cavalier   1.5707962513e+00, /* atan(inf)hi 0x3fc90fda */
24*f504f610SAugustin Cavalier };
25*f504f610SAugustin Cavalier 
26*f504f610SAugustin Cavalier static const float atanlo[] = {
27*f504f610SAugustin Cavalier   5.0121582440e-09, /* atan(0.5)lo 0x31ac3769 */
28*f504f610SAugustin Cavalier   3.7748947079e-08, /* atan(1.0)lo 0x33222168 */
29*f504f610SAugustin Cavalier   3.4473217170e-08, /* atan(1.5)lo 0x33140fb4 */
30*f504f610SAugustin Cavalier   7.5497894159e-08, /* atan(inf)lo 0x33a22168 */
31*f504f610SAugustin Cavalier };
32*f504f610SAugustin Cavalier 
33*f504f610SAugustin Cavalier static const float aT[] = {
34*f504f610SAugustin Cavalier   3.3333328366e-01,
35*f504f610SAugustin Cavalier  -1.9999158382e-01,
36*f504f610SAugustin Cavalier   1.4253635705e-01,
37*f504f610SAugustin Cavalier  -1.0648017377e-01,
38*f504f610SAugustin Cavalier   6.1687607318e-02,
39*f504f610SAugustin Cavalier };
40*f504f610SAugustin Cavalier 
atanf(float x)41*f504f610SAugustin Cavalier float atanf(float x)
42*f504f610SAugustin Cavalier {
43*f504f610SAugustin Cavalier 	float_t w,s1,s2,z;
44*f504f610SAugustin Cavalier 	uint32_t ix,sign;
45*f504f610SAugustin Cavalier 	int id;
46*f504f610SAugustin Cavalier 
47*f504f610SAugustin Cavalier 	GET_FLOAT_WORD(ix, x);
48*f504f610SAugustin Cavalier 	sign = ix>>31;
49*f504f610SAugustin Cavalier 	ix &= 0x7fffffff;
50*f504f610SAugustin Cavalier 	if (ix >= 0x4c800000) {  /* if |x| >= 2**26 */
51*f504f610SAugustin Cavalier 		if (isnan(x))
52*f504f610SAugustin Cavalier 			return x;
53*f504f610SAugustin Cavalier 		z = atanhi[3] + 0x1p-120f;
54*f504f610SAugustin Cavalier 		return sign ? -z : z;
55*f504f610SAugustin Cavalier 	}
56*f504f610SAugustin Cavalier 	if (ix < 0x3ee00000) {   /* |x| < 0.4375 */
57*f504f610SAugustin Cavalier 		if (ix < 0x39800000) {  /* |x| < 2**-12 */
58*f504f610SAugustin Cavalier 			if (ix < 0x00800000)
59*f504f610SAugustin Cavalier 				/* raise underflow for subnormal x */
60*f504f610SAugustin Cavalier 				FORCE_EVAL(x*x);
61*f504f610SAugustin Cavalier 			return x;
62*f504f610SAugustin Cavalier 		}
63*f504f610SAugustin Cavalier 		id = -1;
64*f504f610SAugustin Cavalier 	} else {
65*f504f610SAugustin Cavalier 		x = fabsf(x);
66*f504f610SAugustin Cavalier 		if (ix < 0x3f980000) {  /* |x| < 1.1875 */
67*f504f610SAugustin Cavalier 			if (ix < 0x3f300000) {  /*  7/16 <= |x| < 11/16 */
68*f504f610SAugustin Cavalier 				id = 0;
69*f504f610SAugustin Cavalier 				x = (2.0f*x - 1.0f)/(2.0f + x);
70*f504f610SAugustin Cavalier 			} else {                /* 11/16 <= |x| < 19/16 */
71*f504f610SAugustin Cavalier 				id = 1;
72*f504f610SAugustin Cavalier 				x = (x - 1.0f)/(x + 1.0f);
73*f504f610SAugustin Cavalier 			}
74*f504f610SAugustin Cavalier 		} else {
75*f504f610SAugustin Cavalier 			if (ix < 0x401c0000) {  /* |x| < 2.4375 */
76*f504f610SAugustin Cavalier 				id = 2;
77*f504f610SAugustin Cavalier 				x = (x - 1.5f)/(1.0f + 1.5f*x);
78*f504f610SAugustin Cavalier 			} else {                /* 2.4375 <= |x| < 2**26 */
79*f504f610SAugustin Cavalier 				id = 3;
80*f504f610SAugustin Cavalier 				x = -1.0f/x;
81*f504f610SAugustin Cavalier 			}
82*f504f610SAugustin Cavalier 		}
83*f504f610SAugustin Cavalier 	}
84*f504f610SAugustin Cavalier 	/* end of argument reduction */
85*f504f610SAugustin Cavalier 	z = x*x;
86*f504f610SAugustin Cavalier 	w = z*z;
87*f504f610SAugustin Cavalier 	/* break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly */
88*f504f610SAugustin Cavalier 	s1 = z*(aT[0]+w*(aT[2]+w*aT[4]));
89*f504f610SAugustin Cavalier 	s2 = w*(aT[1]+w*aT[3]);
90*f504f610SAugustin Cavalier 	if (id < 0)
91*f504f610SAugustin Cavalier 		return x - x*(s1+s2);
92*f504f610SAugustin Cavalier 	z = atanhi[id] - ((x*(s1+s2) - atanlo[id]) - x);
93*f504f610SAugustin Cavalier 	return sign ? -z : z;
94*f504f610SAugustin Cavalier }
95