1#include <float.h> 2#include <math.h> 3#include <stdint.h> 4 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 12static const float_t toint = 1 / EPS; 13 14float rintf(float x) { 15 union { 16 float f; 17 uint32_t i; 18 } u = {x}; 19 int e = u.i >> 23 & 0xff; 20 int s = u.i >> 31; 21 float_t y; 22 23 if (e >= 0x7f + 23) 24 return x; 25 if (s) 26 y = x - toint + toint; 27 else 28 y = x + toint - toint; 29 if (y == 0) 30 return s ? -0.0f : 0.0f; 31 return y; 32} 33