xref: /netbsd-src/lib/libm/src/e_asinl.c (revision 345cf9fb81bd0411c53e25d62cd93bdcaa865312)
1 
2 /* FreeBSD: head/lib/msun/src/e_asin.c 176451 2008-02-22 02:30:36Z das */
3 /*
4  * ====================================================
5  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6  *
7  * Developed at SunSoft, a Sun Microsystems, Inc. business.
8  * Permission to use, copy, modify, and distribute this
9  * software is freely granted, provided that this notice
10  * is preserved.
11  * ====================================================
12  */
13 
14 #include <sys/cdefs.h>
15 /*
16  * See comments in e_asin.c.
17  * Converted to long double by David Schultz <das@FreeBSD.ORG>.
18  */
19 
20 #include "namespace.h"
21 #include <float.h>
22 #include <machine/ieee.h>
23 
24 #include "math.h"
25 #include "math_private.h"
26 
27 __weak_alias(asinl, _asinl)
28 
29 #ifdef __HAVE_LONG_DOUBLE
30 
31 #if LDBL_MANT_DIG == 64
32 #include "../ld80/invtrig.h"
33 #elif LDBL_MANT_DIG == 113
34 #include "../ld128/invtrig.h"
35 #else
36 #error "Unsupported long double format"
37 #endif
38 
39 #ifdef LDBL_IMPLICIT_NBIT
40 #define	LDBL_NBIT	0
41 #endif
42 
43 static const long double
44 one =  1.00000000000000000000e+00,
45 huge = 1.000e+300;
46 
47 long double
48 asinl(long double x)
49 {
50 	union ieee_ext_u u;
51 	long double t=0.0,w,p,q,c,r,s;
52 	int16_t expsign, expt;
53 	u.extu_ld = x;
54 	expsign = GET_EXPSIGN(&u);
55 	expt = expsign & 0x7fff;
56 	if(expt >= BIAS) {		/* |x|>= 1 */
57 		if(expt==BIAS && ((u.extu_frach&~LDBL_NBIT)|u.extu_fracl)==0)
58 		    /* asin(1)=+-pi/2 with inexact */
59 		    return x*pio2_hi+x*pio2_lo;
60 	    return (x-x)/(x-x);		/* asin(|x|>1) is NaN */
61 	} else if (expt<BIAS-1) {	/* |x|<0.5 */
62 	    if(expt<ASIN_LINEAR) {	/* if |x| is small, asinl(x)=x */
63 		if(huge+x>one) return x;/* return x with inexact if x!=0*/
64 	    }
65 	    t = x*x;
66 	    p = P(t);
67 	    q = Q(t);
68 	    w = p/q;
69 	    return x+x*w;
70 	}
71 	/* 1> |x|>= 0.5 */
72 	w = one-fabsl(x);
73 	t = w*0.5;
74 	p = P(t);
75 	q = Q(t);
76 	s = sqrtl(t);
77 	if(u.extu_frach>=THRESH) { 	/* if |x| is close to 1 */
78 	    w = p/q;
79 	    t = pio2_hi-(2.0*(s+s*w)-pio2_lo);
80 	} else {
81 	    u.extu_ld = s;
82 	    u.extu_fracl = 0;
83 	    w = u.extu_ld;
84 	    c  = (t-w*w)/(s+w);
85 	    r  = p/q;
86 	    p  = 2.0*s*r-(pio2_lo-2.0*c);
87 	    q  = pio4_hi-2.0*w;
88 	    t  = pio4_hi-(p-q);
89 	}
90 	if(expsign>0) return t; else return -t;
91 }
92 #else
93 long double
94 asinl(long double x)
95 {
96 	return asin(x);
97 }
98 #endif
99