xref: /haiku/src/system/libroot/posix/musl/math/roundl.c (revision f504f61099b010fbfa94b1cc63d2e9072c7f7185)
1*f504f610SAugustin Cavalier #include "libm.h"
2*f504f610SAugustin Cavalier 
3*f504f610SAugustin Cavalier #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
roundl(long double x)4*f504f610SAugustin Cavalier long double roundl(long double x)
5*f504f610SAugustin Cavalier {
6*f504f610SAugustin Cavalier 	return round(x);
7*f504f610SAugustin Cavalier }
8*f504f610SAugustin Cavalier #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
9*f504f610SAugustin Cavalier 
10*f504f610SAugustin Cavalier static const long double toint = 1/LDBL_EPSILON;
11*f504f610SAugustin Cavalier 
roundl(long double x)12*f504f610SAugustin Cavalier long double roundl(long double x)
13*f504f610SAugustin Cavalier {
14*f504f610SAugustin Cavalier 	union ldshape u = {x};
15*f504f610SAugustin Cavalier 	int e = u.i.se & 0x7fff;
16*f504f610SAugustin Cavalier 	long double y;
17*f504f610SAugustin Cavalier 
18*f504f610SAugustin Cavalier 	if (e >= 0x3fff+LDBL_MANT_DIG-1)
19*f504f610SAugustin Cavalier 		return x;
20*f504f610SAugustin Cavalier 	if (u.i.se >> 15)
21*f504f610SAugustin Cavalier 		x = -x;
22*f504f610SAugustin Cavalier 	if (e < 0x3fff-1) {
23*f504f610SAugustin Cavalier 		FORCE_EVAL(x + toint);
24*f504f610SAugustin Cavalier 		return 0*u.f;
25*f504f610SAugustin Cavalier 	}
26*f504f610SAugustin Cavalier 	y = x + toint - toint - x;
27*f504f610SAugustin Cavalier 	if (y > 0.5)
28*f504f610SAugustin Cavalier 		y = y + x - 1;
29*f504f610SAugustin Cavalier 	else if (y <= -0.5)
30*f504f610SAugustin Cavalier 		y = y + x + 1;
31*f504f610SAugustin Cavalier 	else
32*f504f610SAugustin Cavalier 		y = y + x;
33*f504f610SAugustin Cavalier 	if (u.i.se >> 15)
34*f504f610SAugustin Cavalier 		y = -y;
35*f504f610SAugustin Cavalier 	return y;
36*f504f610SAugustin Cavalier }
37*f504f610SAugustin Cavalier #endif
38