1 // adapted from the rintf() function from musl-1.1.16
2 
3 #include "libm.h"
4 
nearbyintf(float x)5 float nearbyintf(float x)
6 {
7 	union {float f; uint32_t i;} u = {x};
8 	int e = u.i>>23 & 0xff;
9 	int s = u.i>>31;
10 	float_t y;
11 
12 	if (e >= 0x7f+23)
13 		return x;
14 	if (s)
15 		y = x - 0x1p23f + 0x1p23f;
16 	else
17 		y = x + 0x1p23f - 0x1p23f;
18 	if (y == 0)
19 		return s ? -0.0f : 0.0f;
20 	return y;
21 }
22