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