cc5653363d46dcf1859332e287f90c150d0a3d34
[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 static const long double
28 pi = 3.14159265358979323846264338327950280e+00L;
29
30 long double acosl(long double x)
31 {
32         union IEEEl2bits u;
33         long double z, p, q, r, w, s, c, df;
34         int16_t expsign, expt;
35         u.e = x;
36         expsign = u.xbits.expsign;
37         expt = expsign & 0x7fff;
38         if (expt >= BIAS) {        /* |x| >= 1 */
39                 if (expt == BIAS &&
40                         ((u.bits.manh & ~LDBL_NBIT) | u.bits.manl) == 0) {
41                         if (expsign > 0)
42                                 return 0.0;  /* acos(1) = 0 */
43                         else
44                                 return pi + 2.0 * pio2_lo;  /* acos(-1)= pi */
45                 }
46                 return (x - x) / (x - x);  /* acos(|x|>1) is NaN */
47         }
48         if (expt < BIAS - 1) {     /* |x| < 0.5 */
49                 if (expt < ACOS_CONST)
50                         return pio2_hi + pio2_lo;  /* x tiny: acosl=pi/2 */
51                 z = x * x;
52                 p = P(z);
53                 q = Q(z);
54                 r = p / q;
55                 return pio2_hi - (x - (pio2_lo - x * r));
56         } else if (expsign < 0) {  /* x < -0.5 */
57                 z = (1.0 + x) * 0.5;
58                 p = P(z);
59                 q = Q(z);
60                 s = sqrtl(z);
61                 r = p / q;
62                 w = r * s - pio2_lo;
63                 return pi - 2.0 * (s + w);
64         } else {                   /* x > 0.5 */
65                 z = (1.0 - x) * 0.5;
66                 s = sqrtl(z);
67                 u.e = s;
68                 u.bits.manl = 0;
69                 df = u.e;
70                 c = (z - df * df) / (s + df);
71                 p = P(z);
72                 q = Q(z);
73                 r = p / q;
74                 w = r * s + c;
75                 return 2.0 * (df + w);
76         }
77 }
78 #endif