1a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes
2a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes/* @(#)e_asin.c 1.3 95/01/18 */
3a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes/* FreeBSD: head/lib/msun/src/e_asin.c 176451 2008-02-22 02:30:36Z das */
4a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes/*
5a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * ====================================================
6a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
7a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes *
8a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * Developed at SunSoft, a Sun Microsystems, Inc. business.
9a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * Permission to use, copy, modify, and distribute this
10a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * software is freely granted, provided that this notice
11a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * is preserved.
12a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * ====================================================
13a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes */
14a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes
15a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#include <sys/cdefs.h>
16a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes__FBSDID("$FreeBSD$");
17a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes
18a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes/*
19a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * See comments in e_asin.c.
20a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * Converted to long double by David Schultz <das@FreeBSD.ORG>.
21a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes */
22a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes
23a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#include <float.h>
24a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes
25a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#include "invtrig.h"
26a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#include "math.h"
27a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#include "math_private.h"
28a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes
29a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughesstatic const long double
30a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughesone =  1.00000000000000000000e+00,
31a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hugheshuge = 1.000e+300;
32a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes
33a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hugheslong double
34a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughesasinl(long double x)
35a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes{
36a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	union IEEEl2bits u;
37a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	long double t=0.0,w,p,q,c,r,s;
38a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	int16_t expsign, expt;
39a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	u.e = x;
40a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	expsign = u.xbits.expsign;
41a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	expt = expsign & 0x7fff;
42a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	if(expt >= BIAS) {		/* |x|>= 1 */
43a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes		if(expt==BIAS && ((u.bits.manh&~LDBL_NBIT)|u.bits.manl)==0)
44a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes		    /* asin(1)=+-pi/2 with inexact */
45a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes		    return x*pio2_hi+x*pio2_lo;
46a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	    return (x-x)/(x-x);		/* asin(|x|>1) is NaN */
47a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	} else if (expt<BIAS-1) {	/* |x|<0.5 */
48a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	    if(expt<ASIN_LINEAR) {	/* if |x| is small, asinl(x)=x */
49a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes		if(huge+x>one) return x;/* return x with inexact if x!=0*/
50a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	    }
51a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	    t = x*x;
52a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	    p = P(t);
53a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	    q = Q(t);
54a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	    w = p/q;
55a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	    return x+x*w;
56a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	}
57a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	/* 1> |x|>= 0.5 */
58a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	w = one-fabsl(x);
59a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	t = w*0.5;
60a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	p = P(t);
61a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	q = Q(t);
62a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	s = sqrtl(t);
63a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	if(u.bits.manh>=THRESH) { 	/* if |x| is close to 1 */
64a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	    w = p/q;
65a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	    t = pio2_hi-(2.0*(s+s*w)-pio2_lo);
66a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	} else {
67a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	    u.e = s;
68a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	    u.bits.manl = 0;
69a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	    w = u.e;
70a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	    c  = (t-w*w)/(s+w);
71a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	    r  = p/q;
72a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	    p  = 2.0*s*r-(pio2_lo-2.0*c);
73a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	    q  = pio4_hi-2.0*w;
74a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	    t  = pio4_hi-(p-q);
75a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	}
76a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	if(expsign>0) return t; else return -t;
77a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes}
78