1181074Sdas
2181074Sdas/* @(#)e_asin.c 1.3 95/01/18 */
3181074Sdas/* FreeBSD: head/lib/msun/src/e_asin.c 176451 2008-02-22 02:30:36Z das */
4181074Sdas/*
5181074Sdas * ====================================================
6181074Sdas * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
7181074Sdas *
8181074Sdas * Developed at SunSoft, a Sun Microsystems, Inc. business.
9181074Sdas * Permission to use, copy, modify, and distribute this
10181074Sdas * software is freely granted, provided that this notice
11181074Sdas * is preserved.
12181074Sdas * ====================================================
13181074Sdas */
14181074Sdas
15181074Sdas#include <sys/cdefs.h>
16181074Sdas__FBSDID("$FreeBSD$");
17181074Sdas
18181074Sdas/*
19181074Sdas * See comments in e_asin.c.
20181074Sdas * Converted to long double by David Schultz <das@FreeBSD.ORG>.
21181074Sdas */
22181074Sdas
23181074Sdas#include <float.h>
24181074Sdas
25181074Sdas#include "invtrig.h"
26181074Sdas#include "math.h"
27181074Sdas#include "math_private.h"
28181074Sdas
29181074Sdasstatic const long double
30181074Sdasone =  1.00000000000000000000e+00,
31181074Sdashuge = 1.000e+300;
32181074Sdas
33181074Sdaslong double
34181074Sdasasinl(long double x)
35181074Sdas{
36181074Sdas	union IEEEl2bits u;
37181074Sdas	long double t=0.0,w,p,q,c,r,s;
38181074Sdas	int16_t expsign, expt;
39181074Sdas	u.e = x;
40181074Sdas	expsign = u.xbits.expsign;
41181074Sdas	expt = expsign & 0x7fff;
42181074Sdas	if(expt >= BIAS) {		/* |x|>= 1 */
43181074Sdas		if(expt==BIAS && ((u.bits.manh&~LDBL_NBIT)|u.bits.manl)==0)
44181074Sdas		    /* asin(1)=+-pi/2 with inexact */
45181074Sdas		    return x*pio2_hi+x*pio2_lo;
46181074Sdas	    return (x-x)/(x-x);		/* asin(|x|>1) is NaN */
47181074Sdas	} else if (expt<BIAS-1) {	/* |x|<0.5 */
48181074Sdas	    if(expt<ASIN_LINEAR) {	/* if |x| is small, asinl(x)=x */
49181074Sdas		if(huge+x>one) return x;/* return x with inexact if x!=0*/
50181258Sdas	    }
51181258Sdas	    t = x*x;
52181258Sdas	    p = P(t);
53181258Sdas	    q = Q(t);
54181258Sdas	    w = p/q;
55181258Sdas	    return x+x*w;
56181074Sdas	}
57181074Sdas	/* 1> |x|>= 0.5 */
58181074Sdas	w = one-fabsl(x);
59181074Sdas	t = w*0.5;
60181074Sdas	p = P(t);
61181074Sdas	q = Q(t);
62181074Sdas	s = sqrtl(t);
63181074Sdas	if(u.bits.manh>=THRESH) { 	/* if |x| is close to 1 */
64181074Sdas	    w = p/q;
65181074Sdas	    t = pio2_hi-(2.0*(s+s*w)-pio2_lo);
66181074Sdas	} else {
67181074Sdas	    u.e = s;
68181074Sdas	    u.bits.manl = 0;
69181074Sdas	    w = u.e;
70181074Sdas	    c  = (t-w*w)/(s+w);
71181074Sdas	    r  = p/q;
72181074Sdas	    p  = 2.0*s*r-(pio2_lo-2.0*c);
73181074Sdas	    q  = pio4_hi-2.0*w;
74181074Sdas	    t  = pio4_hi-(p-q);
75181074Sdas	}
76181074Sdas	if(expsign>0) return t; else return -t;
77181074Sdas}
78