Szabolcs Nagy | 79ca860 | 2014-10-29 00:25:50 +0100 | [diff] [blame] | 1 | #include <float.h> |
Szabolcs Nagy | d1a2ead | 2013-09-03 03:27:02 +0000 | [diff] [blame] | 2 | #include <math.h> |
| 3 | #include <stdint.h> |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 4 | |
Szabolcs Nagy | 79ca860 | 2014-10-29 00:25:50 +0100 | [diff] [blame] | 5 | #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 |
| 12 | static const float_t toint = 1/EPS; |
| 13 | |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 14 | float rintf(float x) |
| 15 | { |
Szabolcs Nagy | d1a2ead | 2013-09-03 03:27:02 +0000 | [diff] [blame] | 16 | 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 Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 20 | |
Szabolcs Nagy | d1a2ead | 2013-09-03 03:27:02 +0000 | [diff] [blame] | 21 | if (e >= 0x7f+23) |
| 22 | return x; |
| 23 | if (s) |
Szabolcs Nagy | 79ca860 | 2014-10-29 00:25:50 +0100 | [diff] [blame] | 24 | y = x - toint + toint; |
Szabolcs Nagy | d1a2ead | 2013-09-03 03:27:02 +0000 | [diff] [blame] | 25 | else |
Szabolcs Nagy | 79ca860 | 2014-10-29 00:25:50 +0100 | [diff] [blame] | 26 | y = x + toint - toint; |
Szabolcs Nagy | d1a2ead | 2013-09-03 03:27:02 +0000 | [diff] [blame] | 27 | if (y == 0) |
| 28 | return s ? -0.0f : 0.0f; |
| 29 | return y; |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 30 | } |