xref: /freebsd/lib/msun/src/e_acosl.c (revision 17303c626fd4c19cddfd19dfdc70380d1d603cff)
1 
2 /* @(#)e_acos.c 1.3 95/01/18 */
3 /* FreeBSD: head/lib/msun/src/e_acos.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 __FBSDID("$FreeBSD$");
17 
18 /*
19  * See comments in e_acos.c.
20  * Converted to long double by David Schultz <das@FreeBSD.ORG>.
21  */
22 
23 #include <float.h>
24 
25 #include "invtrig.h"
26 #include "math.h"
27 #include "math_private.h"
28 
29 static const long double
30 one=  1.00000000000000000000e+00,
31 pi =  3.14159265358979323846264338327950280e+00L;
32 
33 long double
34 acosl(long double x)
35 {
36 	union IEEEl2bits u;
37 	long double z,p,q,r,w,s,c,df;
38 	int16_t expsign, expt;
39 	u.e = x;
40 	expsign = u.xbits.expsign;
41 	expt = expsign & 0x7fff;
42 	if(expt >= BIAS) {	/* |x| >= 1 */
43 	    if(expt==BIAS && ((u.bits.manh&~LDBL_NBIT)|u.bits.manl)==0) {
44 		if (expsign>0) return 0.0;	/* acos(1) = 0  */
45 		else return pi+2.0*pio2_lo;	/* acos(-1)= pi */
46 	    }
47 	    return (x-x)/(x-x);		/* acos(|x|>1) is NaN */
48 	}
49 	if(expt<BIAS-1) {	/* |x| < 0.5 */
50 	    if(expt<ACOS_CONST) return pio2_hi+pio2_lo;/*x tiny: acosl=pi/2*/
51 	    z = x*x;
52 	    p = P(z);
53 	    q = Q(z);
54 	    r = p/q;
55 	    return pio2_hi - (x - (pio2_lo-x*r));
56 	} else  if (expsign<0) {	/* x < -0.5 */
57 	    z = (one+x)*0.5;
58 	    p = P(z);
59 	    q = Q(z);
60 	    s = sqrtl(z);
61 	    r = p/q;
62 	    w = r*s-pio2_lo;
63 	    return pi - 2.0*(s+w);
64 	} else {			/* x > 0.5 */
65 	    z = (one-x)*0.5;
66 	    s = sqrtl(z);
67 	    u.e = s;
68 	    u.bits.manl = 0;
69 	    df = u.e;
70 	    c  = (z-df*df)/(s+df);
71 	    p = P(z);
72 	    q = Q(z);
73 	    r = p/q;
74 	    w = r*s+c;
75 	    return 2.0*(df+w);
76 	}
77 }
78