blob: 9047688d246a69c2698ce27399135e1db7ac2f65 [file] [log] [blame]
Szabolcs Nagy79ca8602014-10-29 00:25:50 +01001#include <float.h>
Szabolcs Nagyd1a2ead2013-09-03 03:27:02 +00002#include <math.h>
3#include <stdint.h>
Rich Felkerb69f6952012-03-13 01:17:53 -04004
Szabolcs Nagy79ca8602014-10-29 00:25:50 +01005#if FLT_EVAL_METHOD==0
6#define EPS FLT_EPSILON
7#elif FLT_EVAL_METHOD==1
8#define EPS DBL_EPSILON
9#elif FLT_EVAL_METHOD==2
10#define EPS LDBL_EPSILON
11#endif
12static const float_t toint = 1/EPS;
13
Rich Felkerb69f6952012-03-13 01:17:53 -040014float rintf(float x)
15{
Szabolcs Nagyd1a2ead2013-09-03 03:27:02 +000016 union {float f; uint32_t i;} u = {x};
17 int e = u.i>>23 & 0xff;
18 int s = u.i>>31;
19 float_t y;
Rich Felkerb69f6952012-03-13 01:17:53 -040020
Szabolcs Nagyd1a2ead2013-09-03 03:27:02 +000021 if (e >= 0x7f+23)
22 return x;
23 if (s)
Szabolcs Nagy79ca8602014-10-29 00:25:50 +010024 y = x - toint + toint;
Szabolcs Nagyd1a2ead2013-09-03 03:27:02 +000025 else
Szabolcs Nagy79ca8602014-10-29 00:25:50 +010026 y = x + toint - toint;
Szabolcs Nagyd1a2ead2013-09-03 03:27:02 +000027 if (y == 0)
28 return s ? -0.0f : 0.0f;
29 return y;
Rich Felkerb69f6952012-03-13 01:17:53 -040030}