math: clean up inverse trigonometric functions
[musl] / src / math / acosl.c
1 /* origin: FreeBSD /usr/src/lib/msun/src/e_acosl.c */
2 /*
3  * ====================================================
4  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5  *
6  * Developed at SunSoft, a Sun Microsystems, Inc. business.
7  * Permission to use, copy, modify, and distribute this
8  * software is freely granted, provided that this notice
9  * is preserved.
10  * ====================================================
11  */
12 /*
13  * See comments in acos.c.
14  * Converted to long double by David Schultz <das@FreeBSD.ORG>.
15  */
16
17 #include "libm.h"
18
19 #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
20 long double acosl(long double x)
21 {
22         return acos(x);
23 }
24 #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
25 #include "__invtrigl.h"
26
27 long double acosl(long double x)
28 {
29         union IEEEl2bits u;
30         long double z, w, s, c, df;
31         int16_t expsign, expt;
32         u.e = x;
33         expsign = u.xbits.expsign;
34         expt = expsign & 0x7fff;
35         /* |x| >= 1 or nan */
36         if (expt >= 0x3fff) {
37                 if (expt == 0x3fff &&
38                         ((u.bits.manh & ~LDBL_NBIT) | u.bits.manl) == 0) {
39                         if (expsign > 0)
40                                 return 0;  /* acos(1) = 0 */
41                         return 2*pio2_hi + 0x1p-1000;  /* acos(-1)= pi */
42                 }
43                 return 0/(x-x);  /* acos(|x|>1) is NaN */
44         }
45         /* |x| < 0.5 */
46         if (expt < 0x3fff - 1) {
47                 if (expt < 0x3fff - 65)
48                         return pio2_hi + 0x1p-1000;  /* x < 0x1p-65: acosl(x)=pi/2 */
49                 return pio2_hi - (x - (pio2_lo - x * __invtrigl_R(x*x)));
50         }
51         /* x < -0.5 */
52         if (expsign < 0) {
53                 z = (1.0 + x) * 0.5;
54                 s = sqrtl(z);
55                 w = __invtrigl_R(z) * s - pio2_lo;
56                 return 2*(pio2_hi - (s + w));
57         }
58         /* x > 0.5 */
59         z = (1.0 - x) * 0.5;
60         s = sqrtl(z);
61         u.e = s;
62         u.bits.manl = 0;
63         df = u.e;
64         c = (z - df * df) / (s + df);
65         w = __invtrigl_R(z) * s + c;
66         return 2*(df + w);
67 }
68 #endif