roundf.c 762 B

123456789101112131415161718192021222324252627282930313233
  1. /*****************************************************************************/
  2. /*****************************************************************************/
  3. // roundf from musl-0.9.15
  4. /*****************************************************************************/
  5. /*****************************************************************************/
  6. #include "libm.h"
  7. float roundf(float x)
  8. {
  9. union {float f; uint32_t i;} u = {x};
  10. int e = u.i >> 23 & 0xff;
  11. float_t y;
  12. if (e >= 0x7f+23)
  13. return x;
  14. if (u.i >> 31)
  15. x = -x;
  16. if (e < 0x7f-1) {
  17. FORCE_EVAL(x + 0x1p23f);
  18. return 0*u.f;
  19. }
  20. y = (float)(x + 0x1p23f) - 0x1p23f - x;
  21. if (y > 0.5f)
  22. y = y + x - 1;
  23. else if (y <= -0.5f)
  24. y = y + x + 1;
  25. else
  26. y = y + x;
  27. if (u.i >> 31)
  28. y = -y;
  29. return y;
  30. }