Skip to content

Commit fb161aa

Browse files
committed
lib/libm: Add implementation of nearbyintf, from musl-1.1.16.
1 parent bacb52a commit fb161aa

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

lib/libm/nearbyintf.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// adapted from the rintf() function from musl-1.1.16
2+
3+
#include "libm.h"
4+
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+
}

0 commit comments

Comments
 (0)