math: clean up inverse trigonometric functions
[musl] / src / math / acosf.c
1 /* origin: FreeBSD /usr/src/lib/msun/src/e_acosf.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 #include "libm.h"
17
18 static const float
19 pio2_hi = 1.5707962513e+00, /* 0x3fc90fda */
20 pio2_lo = 7.5497894159e-08, /* 0x33a22168 */
21 pS0 =  1.6666586697e-01,
22 pS1 = -4.2743422091e-02,
23 pS2 = -8.6563630030e-03,
24 qS1 = -7.0662963390e-01;
25
26 static float R(float z)
27 {
28         float p, q;
29         p = z*(pS0+z*(pS1+z*pS2));
30         q = 1.0f+z*qS1;
31         return p/q;
32 }
33
34 float acosf(float x)
35 {
36         float z,w,s,c,df;
37         uint32_t hx,ix;
38
39         GET_FLOAT_WORD(hx, x);
40         ix = hx & 0x7fffffff;
41         /* |x| >= 1 or nan */
42         if (ix >= 0x3f800000) {
43                 if (ix == 0x3f800000) {
44                         if (hx >> 31)
45                                 return 2*pio2_hi + 0x1p-120f;
46                         return 0;
47                 }
48                 return 0/(x-x);
49         }
50         /* |x| < 0.5 */
51         if (ix < 0x3f000000) {
52                 if (ix <= 0x32800000) /* |x| < 2**-26 */
53                         return pio2_hi + 0x1p-120f;
54                 return pio2_hi - (x - (pio2_lo-x*R(x*x)));
55         }
56         /* x < -0.5 */
57         if (hx >> 31) {
58                 z = (1+x)*0.5f;
59                 s = sqrtf(z);
60                 w = R(z)*s-pio2_lo;
61                 return 2*(pio2_hi - (s+w));
62         }
63         /* x > 0.5 */
64         z = (1-x)*0.5f;
65         s = sqrtf(z);
66         GET_FLOAT_WORD(hx,s);
67         SET_FLOAT_WORD(df,hx&0xfffff000);
68         c = (z-df*df)/(s+df);
69         w = R(z)*s+c;
70         return 2*(df+w);
71 }