1*2f2c0062Sguenther /* $OpenBSD: e_asinl.c,v 1.2 2016/09/12 19:47:02 guenther Exp $ */
2390c8400Smartynas /* @(#)e_asin.c 1.3 95/01/18 */
3390c8400Smartynas /* FreeBSD: head/lib/msun/src/e_asin.c 176451 2008-02-22 02:30:36Z das */
4390c8400Smartynas /*
5390c8400Smartynas * ====================================================
6390c8400Smartynas * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
7390c8400Smartynas *
8390c8400Smartynas * Developed at SunSoft, a Sun Microsystems, Inc. business.
9390c8400Smartynas * Permission to use, copy, modify, and distribute this
10390c8400Smartynas * software is freely granted, provided that this notice
11390c8400Smartynas * is preserved.
12390c8400Smartynas * ====================================================
13390c8400Smartynas */
14390c8400Smartynas
15390c8400Smartynas /*
16390c8400Smartynas * See comments in e_asin.c.
17390c8400Smartynas * Converted to long double by David Schultz <das@FreeBSD.ORG>.
18390c8400Smartynas * Adapted for OpenBSD by Martynas Venckus <martynas@openbsd.org>.
19390c8400Smartynas */
20390c8400Smartynas
21390c8400Smartynas #include <float.h>
22390c8400Smartynas #include <math.h>
23390c8400Smartynas
24390c8400Smartynas #include "invtrig.h"
25390c8400Smartynas #include "math_private.h"
26390c8400Smartynas
27390c8400Smartynas #ifdef EXT_IMPLICIT_NBIT
28390c8400Smartynas #define LDBL_NBIT 0
29390c8400Smartynas #else /* EXT_IMPLICIT_NBIT */
30390c8400Smartynas #define LDBL_NBIT 0x80000000
31390c8400Smartynas #endif /* EXT_IMPLICIT_NBIT */
32390c8400Smartynas
33390c8400Smartynas static const long double
34390c8400Smartynas one = 1.00000000000000000000e+00,
35390c8400Smartynas huge = 1.000e+300;
36390c8400Smartynas
37390c8400Smartynas long double
asinl(long double x)38390c8400Smartynas asinl(long double x)
39390c8400Smartynas {
40390c8400Smartynas union {
41390c8400Smartynas long double e;
42390c8400Smartynas struct ieee_ext bits;
43390c8400Smartynas } u;
44390c8400Smartynas long double t=0.0,w,p,q,c,r,s;
45390c8400Smartynas int16_t expsign, expt;
46390c8400Smartynas u.e = x;
47390c8400Smartynas expsign = (u.bits.ext_sign << 15) | u.bits.ext_exp;
48390c8400Smartynas expt = expsign & 0x7fff;
49390c8400Smartynas if(expt >= BIAS) { /* |x|>= 1 */
50390c8400Smartynas if(expt==BIAS && ((u.bits.ext_frach&~LDBL_NBIT)
51390c8400Smartynas #ifdef EXT_FRACHMBITS
52390c8400Smartynas | u.bits.ext_frachm
53390c8400Smartynas #endif /* EXT_FRACHMBITS */
54390c8400Smartynas #ifdef EXT_FRACLMBITS
55390c8400Smartynas | u.bits.ext_fraclm
56390c8400Smartynas #endif /* EXT_FRACLMBITS */
57390c8400Smartynas | u.bits.ext_fracl)==0)
58390c8400Smartynas /* asin(1)=+-pi/2 with inexact */
59390c8400Smartynas return x*pio2_hi+x*pio2_lo;
60390c8400Smartynas return (x-x)/(x-x); /* asin(|x|>1) is NaN */
61390c8400Smartynas } else if (expt<BIAS-1) { /* |x|<0.5 */
62390c8400Smartynas if(expt<ASIN_LINEAR) { /* if |x| is small, asinl(x)=x */
63390c8400Smartynas if(huge+x>one) return x;/* return x with inexact if x!=0*/
64390c8400Smartynas }
65390c8400Smartynas t = x*x;
66390c8400Smartynas p = P(t);
67390c8400Smartynas q = Q(t);
68390c8400Smartynas w = p/q;
69390c8400Smartynas return x+x*w;
70390c8400Smartynas }
71390c8400Smartynas /* 1> |x|>= 0.5 */
72390c8400Smartynas w = one-fabsl(x);
73390c8400Smartynas t = w*0.5;
74390c8400Smartynas p = P(t);
75390c8400Smartynas q = Q(t);
76390c8400Smartynas s = sqrtl(t);
77390c8400Smartynas #ifdef EXT_FRACHMBITS
78390c8400Smartynas if((((uint64_t)u.bits.ext_frach << EXT_FRACHMBITS)
79390c8400Smartynas | u.bits.ext_frachm) >= THRESH) {
80390c8400Smartynas /* if |x| is close to 1 */
81390c8400Smartynas #else /* EXT_FRACHMBITS */
82390c8400Smartynas if(u.bits.ext_frach>=THRESH) { /* if |x| is close to 1 */
83390c8400Smartynas #endif /* EXT_FRACHMBITS */
84390c8400Smartynas w = p/q;
85390c8400Smartynas t = pio2_hi-(2.0*(s+s*w)-pio2_lo);
86390c8400Smartynas } else {
87390c8400Smartynas u.e = s;
88390c8400Smartynas u.bits.ext_fracl = 0;
89390c8400Smartynas #ifdef EXT_FRACLMBITS
90390c8400Smartynas u.bits.ext_fraclm = 0;
91390c8400Smartynas #endif /* EXT_FRACLMBITS */
92390c8400Smartynas w = u.e;
93390c8400Smartynas c = (t-w*w)/(s+w);
94390c8400Smartynas r = p/q;
95390c8400Smartynas p = 2.0*s*r-(pio2_lo-2.0*c);
96390c8400Smartynas q = pio4_hi-2.0*w;
97390c8400Smartynas t = pio4_hi-(p-q);
98390c8400Smartynas }
99390c8400Smartynas if(expsign>0) return t; else return -t;
100390c8400Smartynas }
101*2f2c0062Sguenther DEF_STD(asinl);
102