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