TODO update
[libm] / 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 static const long double
28 one = 1.00000000000000000000e+00;
29
30 // FIXME
31 //#ifdef __i386__
32 /* XXX Work around the fact that gcc truncates long double constants on i386 */
33 static volatile double
34 pi1 = 3.14159265358979311600e+00, /* 0x1.921fb54442d18p+1 */
35 pi2 = 1.22514845490862001043e-16; /* 0x1.1a80000000000p-53 */
36 #define pi ((long double)pi1 + pi2)
37 //#else
38 #if 0
39 static const long double
40 pi = 3.14159265358979323846264338327950280e+00L;
41 #endif
42
43 long double acosl(long double x)
44 {
45         union IEEEl2bits u;
46         long double z, p, q, r, w, s, c, df;
47         int16_t expsign, expt;
48         u.e = x;
49         expsign = u.xbits.expsign;
50         expt = expsign & 0x7fff;
51         if (expt >= BIAS) {        /* |x| >= 1 */
52                 if (expt == BIAS &&
53                         ((u.bits.manh & ~LDBL_NBIT) | u.bits.manl) == 0) {
54                         if (expsign > 0)
55                                 return 0.0;  /* acos(1) = 0 */
56                         else
57                                 return pi + 2.0 * pio2_lo;  /* acos(-1)= pi */
58                 }
59                 return (x - x) / (x - x);  /* acos(|x|>1) is NaN */
60         }
61         if (expt < BIAS - 1) {     /* |x| < 0.5 */
62                 if (expt < ACOS_CONST)
63                         return pio2_hi + pio2_lo;  /* x tiny: acosl=pi/2 */
64                 z = x * x;
65                 p = P(z);
66                 q = Q(z);
67                 r = p / q;
68                 return pio2_hi - (x - (pio2_lo - x * r));
69         } else if (expsign < 0) {  /* x < -0.5 */
70                 z = (one + x) * 0.5;
71                 p = P(z);
72                 q = Q(z);
73                 s = sqrtl(z);
74                 r = p / q;
75                 w = r * s - pio2_lo;
76                 return pi - 2.0 * (s + w);
77         } else {                   /* x > 0.5 */
78                 z = (one - x) * 0.5;
79                 s = sqrtl(z);
80                 u.e = s;
81                 u.bits.manl = 0;
82                 df = u.e;
83                 c = (z - df * df) / (s + df);
84                 p = P(z);
85                 q = Q(z);
86                 r = p / q;
87                 w = r * s + c;
88                 return 2.0 * (df + w);
89         }
90 }
91 #endif