xref: /haiku/src/system/libroot/posix/musl/math/acosl.c (revision f504f61099b010fbfa94b1cc63d2e9072c7f7185)
1*f504f610SAugustin Cavalier /* origin: FreeBSD /usr/src/lib/msun/src/e_acosl.c */
2*f504f610SAugustin Cavalier /*
3*f504f610SAugustin Cavalier  * ====================================================
4*f504f610SAugustin Cavalier  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5*f504f610SAugustin Cavalier  *
6*f504f610SAugustin Cavalier  * Developed at SunSoft, a Sun Microsystems, Inc. business.
7*f504f610SAugustin Cavalier  * Permission to use, copy, modify, and distribute this
8*f504f610SAugustin Cavalier  * software is freely granted, provided that this notice
9*f504f610SAugustin Cavalier  * is preserved.
10*f504f610SAugustin Cavalier  * ====================================================
11*f504f610SAugustin Cavalier  */
12*f504f610SAugustin Cavalier /*
13*f504f610SAugustin Cavalier  * See comments in acos.c.
14*f504f610SAugustin Cavalier  * Converted to long double by David Schultz <das@FreeBSD.ORG>.
15*f504f610SAugustin Cavalier  */
16*f504f610SAugustin Cavalier 
17*f504f610SAugustin Cavalier #include "libm.h"
18*f504f610SAugustin Cavalier 
19*f504f610SAugustin Cavalier #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
acosl(long double x)20*f504f610SAugustin Cavalier long double acosl(long double x)
21*f504f610SAugustin Cavalier {
22*f504f610SAugustin Cavalier 	return acos(x);
23*f504f610SAugustin Cavalier }
24*f504f610SAugustin Cavalier #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
25*f504f610SAugustin Cavalier #include "__invtrigl.h"
26*f504f610SAugustin Cavalier #if LDBL_MANT_DIG == 64
27*f504f610SAugustin Cavalier #define CLEARBOTTOM(u) (u.i.m &= -1ULL << 32)
28*f504f610SAugustin Cavalier #elif LDBL_MANT_DIG == 113
29*f504f610SAugustin Cavalier #define CLEARBOTTOM(u) (u.i.lo = 0)
30*f504f610SAugustin Cavalier #endif
31*f504f610SAugustin Cavalier 
acosl(long double x)32*f504f610SAugustin Cavalier long double acosl(long double x)
33*f504f610SAugustin Cavalier {
34*f504f610SAugustin Cavalier 	union ldshape u = {x};
35*f504f610SAugustin Cavalier 	long double z, s, c, f;
36*f504f610SAugustin Cavalier 	uint16_t e = u.i.se & 0x7fff;
37*f504f610SAugustin Cavalier 
38*f504f610SAugustin Cavalier 	/* |x| >= 1 or nan */
39*f504f610SAugustin Cavalier 	if (e >= 0x3fff) {
40*f504f610SAugustin Cavalier 		if (x == 1)
41*f504f610SAugustin Cavalier 			return 0;
42*f504f610SAugustin Cavalier 		if (x == -1)
43*f504f610SAugustin Cavalier 			return 2*pio2_hi + 0x1p-120f;
44*f504f610SAugustin Cavalier 		return 0/(x-x);
45*f504f610SAugustin Cavalier 	}
46*f504f610SAugustin Cavalier 	/* |x| < 0.5 */
47*f504f610SAugustin Cavalier 	if (e < 0x3fff - 1) {
48*f504f610SAugustin Cavalier 		if (e < 0x3fff - LDBL_MANT_DIG - 1)
49*f504f610SAugustin Cavalier 			return pio2_hi + 0x1p-120f;
50*f504f610SAugustin Cavalier 		return pio2_hi - (__invtrigl_R(x*x)*x - pio2_lo + x);
51*f504f610SAugustin Cavalier 	}
52*f504f610SAugustin Cavalier 	/* x < -0.5 */
53*f504f610SAugustin Cavalier 	if (u.i.se >> 15) {
54*f504f610SAugustin Cavalier 		z = (1 + x)*0.5;
55*f504f610SAugustin Cavalier 		s = sqrtl(z);
56*f504f610SAugustin Cavalier 		return 2*(pio2_hi - (__invtrigl_R(z)*s - pio2_lo + s));
57*f504f610SAugustin Cavalier 	}
58*f504f610SAugustin Cavalier 	/* x > 0.5 */
59*f504f610SAugustin Cavalier 	z = (1 - x)*0.5;
60*f504f610SAugustin Cavalier 	s = sqrtl(z);
61*f504f610SAugustin Cavalier 	u.f = s;
62*f504f610SAugustin Cavalier 	CLEARBOTTOM(u);
63*f504f610SAugustin Cavalier 	f = u.f;
64*f504f610SAugustin Cavalier 	c = (z - f*f)/(s + f);
65*f504f610SAugustin Cavalier 	return 2*(__invtrigl_R(z)*s + c + f);
66*f504f610SAugustin Cavalier }
67*f504f610SAugustin Cavalier #endif
68