7572767c782e8039042472a544376468b05ae1eb
[musl] / src / math / asinl.c
1 /* origin: FreeBSD /usr/src/lib/msun/src/e_asinl.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 asin.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 asinl(long double x)
21 {
22         return asin(x);
23 }
24 #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
25 #include "__invtrigl.h"
26 static const long double huge = 1.000e+300;
27 static const long double pio4_hi = 7.85398163397448309628e-01L;
28 #define ASIN_LINEAR     (BIAS - 32)     /* 2**-32 */
29 /* 0.95 */
30 #define THRESH  ((0xe666666666666666ULL>>(64-(MANH_SIZE-1)))|LDBL_NBIT)
31
32 long double asinl(long double x)
33 {
34         union IEEEl2bits u;
35         long double t=0.0,w,p,q,c,r,s;
36         int16_t expsign, expt;
37
38         u.e = x;
39         expsign = u.xbits.expsign;
40         expt = expsign & 0x7fff;
41         if (expt >= BIAS) {          /* |x|>= 1 */
42                 if (expt == BIAS &&
43                     ((u.bits.manh&~LDBL_NBIT)|u.bits.manl) == 0)
44                         /* asin(1)=+-pi/2 with inexact */
45                         return x*pio2_hi + x*pio2_lo;
46                 return (x-x)/(x-x);  /* asin(|x|>1) is NaN */
47         } else if (expt < BIAS-1) {  /* |x|<0.5 */
48                 if (expt < ASIN_LINEAR) {  /* if |x| is small, asinl(x)=x */
49                         /* return x with inexact if x!=0 */
50                         if (huge+x > 1.0)
51                                 return x;
52                 }
53                 t = x*x;
54                 p = P(t);
55                 q = Q(t);
56                 w = p/q;
57                 return x + x*w;
58         }
59         /* 1 > |x| >= 0.5 */
60         w = 1.0 - fabsl(x);
61         t = w*0.5;
62         p = P(t);
63         q = Q(t);
64         s = sqrtl(t);
65         if (u.bits.manh >= THRESH) { /* if |x| is close to 1 */
66                 w = p/q;
67                 t = pio2_hi-(2.0*(s+s*w)-pio2_lo);
68         } else {
69                 u.e = s;
70                 u.bits.manl = 0;
71                 w = u.e;
72                 c = (t-w*w)/(s+w);
73                 r = p/q;
74                 p = 2.0*s*r-(pio2_lo-2.0*c);
75                 q = pio4_hi-2.0*w;
76                 t = pio4_hi-(p-q);
77         }
78         if (expsign > 0)
79                 return t;
80         return -t;
81 }
82 #endif