xref: /haiku/src/system/libroot/posix/musl/math/rintf.c (revision f504f61099b010fbfa94b1cc63d2e9072c7f7185)
1*f504f610SAugustin Cavalier #include <float.h>
2*f504f610SAugustin Cavalier #include <math.h>
3*f504f610SAugustin Cavalier #include <stdint.h>
4*f504f610SAugustin Cavalier 
5*f504f610SAugustin Cavalier #if FLT_EVAL_METHOD==0
6*f504f610SAugustin Cavalier #define EPS FLT_EPSILON
7*f504f610SAugustin Cavalier #elif FLT_EVAL_METHOD==1
8*f504f610SAugustin Cavalier #define EPS DBL_EPSILON
9*f504f610SAugustin Cavalier #elif FLT_EVAL_METHOD==2
10*f504f610SAugustin Cavalier #define EPS LDBL_EPSILON
11*f504f610SAugustin Cavalier #endif
12*f504f610SAugustin Cavalier static const float_t toint = 1/EPS;
13*f504f610SAugustin Cavalier 
rintf(float x)14*f504f610SAugustin Cavalier float rintf(float x)
15*f504f610SAugustin Cavalier {
16*f504f610SAugustin Cavalier 	union {float f; uint32_t i;} u = {x};
17*f504f610SAugustin Cavalier 	int e = u.i>>23 & 0xff;
18*f504f610SAugustin Cavalier 	int s = u.i>>31;
19*f504f610SAugustin Cavalier 	float_t y;
20*f504f610SAugustin Cavalier 
21*f504f610SAugustin Cavalier 	if (e >= 0x7f+23)
22*f504f610SAugustin Cavalier 		return x;
23*f504f610SAugustin Cavalier 	if (s)
24*f504f610SAugustin Cavalier 		y = x - toint + toint;
25*f504f610SAugustin Cavalier 	else
26*f504f610SAugustin Cavalier 		y = x + toint - toint;
27*f504f610SAugustin Cavalier 	if (y == 0)
28*f504f610SAugustin Cavalier 		return s ? -0.0f : 0.0f;
29*f504f610SAugustin Cavalier 	return y;
30*f504f610SAugustin Cavalier }
31