nearbyintf.c 353 B

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