xref: /openbsd-src/lib/libm/src/e_acosl.c (revision 390c8400525fc0ecc9e2c49c2246076b1b49fe19)
1*390c8400Smartynas /*	$OpenBSD: e_acosl.c,v 1.1 2008/12/09 20:00:35 martynas Exp $	*/
2*390c8400Smartynas /* @(#)e_acos.c 1.3 95/01/18 */
3*390c8400Smartynas /* FreeBSD: head/lib/msun/src/e_acos.c 176451 2008-02-22 02:30:36Z das */
4*390c8400Smartynas /*
5*390c8400Smartynas  * ====================================================
6*390c8400Smartynas  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
7*390c8400Smartynas  *
8*390c8400Smartynas  * Developed at SunSoft, a Sun Microsystems, Inc. business.
9*390c8400Smartynas  * Permission to use, copy, modify, and distribute this
10*390c8400Smartynas  * software is freely granted, provided that this notice
11*390c8400Smartynas  * is preserved.
12*390c8400Smartynas  * ====================================================
13*390c8400Smartynas  */
14*390c8400Smartynas 
15*390c8400Smartynas /*
16*390c8400Smartynas  * See comments in e_acos.c.
17*390c8400Smartynas  * Converted to long double by David Schultz <das@FreeBSD.ORG>.
18*390c8400Smartynas  * Adapted for OpenBSD by Martynas Venckus <martynas@openbsd.org>.
19*390c8400Smartynas  */
20*390c8400Smartynas 
21*390c8400Smartynas #include <float.h>
22*390c8400Smartynas #include <math.h>
23*390c8400Smartynas 
24*390c8400Smartynas #include "invtrig.h"
25*390c8400Smartynas #include "math_private.h"
26*390c8400Smartynas 
27*390c8400Smartynas #ifdef EXT_IMPLICIT_NBIT
28*390c8400Smartynas #define	LDBL_NBIT	0
29*390c8400Smartynas #else /* EXT_IMPLICIT_NBIT */
30*390c8400Smartynas #define	LDBL_NBIT	0x80000000
31*390c8400Smartynas #endif /* EXT_IMPLICIT_NBIT */
32*390c8400Smartynas 
33*390c8400Smartynas static const long double
34*390c8400Smartynas one=  1.00000000000000000000e+00;
35*390c8400Smartynas 
36*390c8400Smartynas #if defined(__amd64__) || defined(__i386__)
37*390c8400Smartynas /* XXX Work around the fact that gcc truncates long double constants on i386 */
38*390c8400Smartynas static volatile double
39*390c8400Smartynas pi1 =  3.14159265358979311600e+00,	/*  0x1.921fb54442d18p+1  */
40*390c8400Smartynas pi2 =  1.22514845490862001043e-16;	/*  0x1.1a80000000000p-53 */
41*390c8400Smartynas #define	pi	((long double)pi1 + pi2)
42*390c8400Smartynas #else
43*390c8400Smartynas static const long double
44*390c8400Smartynas pi =  3.14159265358979323846264338327950280e+00L;
45*390c8400Smartynas #endif
46*390c8400Smartynas 
47*390c8400Smartynas long double
acosl(long double x)48*390c8400Smartynas acosl(long double x)
49*390c8400Smartynas {
50*390c8400Smartynas 	union {
51*390c8400Smartynas 		long double e;
52*390c8400Smartynas 		struct ieee_ext bits;
53*390c8400Smartynas 	} u;
54*390c8400Smartynas 	long double z,p,q,r,w,s,c,df;
55*390c8400Smartynas 	int16_t expsign, expt;
56*390c8400Smartynas 	u.e = x;
57*390c8400Smartynas 	expsign = (u.bits.ext_sign << 15) | u.bits.ext_exp;
58*390c8400Smartynas 	expt = expsign & 0x7fff;
59*390c8400Smartynas 	if(expt >= BIAS) {	/* |x| >= 1 */
60*390c8400Smartynas 	    if(expt==BIAS && ((u.bits.ext_frach&~LDBL_NBIT)
61*390c8400Smartynas #ifdef EXT_FRACHMBITS
62*390c8400Smartynas 		| u.bits.ext_frachm
63*390c8400Smartynas #endif /* EXT_FRACHMBITS */
64*390c8400Smartynas #ifdef EXT_FRACLMBITS
65*390c8400Smartynas 		| u.bits.ext_fraclm
66*390c8400Smartynas #endif /* EXT_FRACLMBITS */
67*390c8400Smartynas 		| u.bits.ext_fracl)==0) {
68*390c8400Smartynas 		if (expsign>0) return 0.0;	/* acos(1) = 0  */
69*390c8400Smartynas 		else return pi+2.0*pio2_lo;	/* acos(-1)= pi */
70*390c8400Smartynas 	    }
71*390c8400Smartynas 	    return (x-x)/(x-x);		/* acos(|x|>1) is NaN */
72*390c8400Smartynas 	}
73*390c8400Smartynas 	if(expt<BIAS-1) {	/* |x| < 0.5 */
74*390c8400Smartynas 	    if(expt<ACOS_CONST) return pio2_hi+pio2_lo;/*x tiny: acosl=pi/2*/
75*390c8400Smartynas 	    z = x*x;
76*390c8400Smartynas 	    p = P(z);
77*390c8400Smartynas 	    q = Q(z);
78*390c8400Smartynas 	    r = p/q;
79*390c8400Smartynas 	    return pio2_hi - (x - (pio2_lo-x*r));
80*390c8400Smartynas 	} else  if (expsign<0) {	/* x < -0.5 */
81*390c8400Smartynas 	    z = (one+x)*0.5;
82*390c8400Smartynas 	    p = P(z);
83*390c8400Smartynas 	    q = Q(z);
84*390c8400Smartynas 	    s = sqrtl(z);
85*390c8400Smartynas 	    r = p/q;
86*390c8400Smartynas 	    w = r*s-pio2_lo;
87*390c8400Smartynas 	    return pi - 2.0*(s+w);
88*390c8400Smartynas 	} else {			/* x > 0.5 */
89*390c8400Smartynas 	    z = (one-x)*0.5;
90*390c8400Smartynas 	    s = sqrtl(z);
91*390c8400Smartynas 	    u.e = s;
92*390c8400Smartynas 	    u.bits.ext_fracl = 0;
93*390c8400Smartynas #ifdef EXT_FRACLMBITS
94*390c8400Smartynas 	    u.bits.ext_fraclm = 0;
95*390c8400Smartynas #endif /* EXT_FRACLMBITS */
96*390c8400Smartynas 	    df = u.e;
97*390c8400Smartynas 	    c  = (z-df*df)/(s+df);
98*390c8400Smartynas 	    p = P(z);
99*390c8400Smartynas 	    q = Q(z);
100*390c8400Smartynas 	    r = p/q;
101*390c8400Smartynas 	    w = r*s+c;
102*390c8400Smartynas 	    return 2.0*(df+w);
103*390c8400Smartynas 	}
104*390c8400Smartynas }
105