1
0

nextafterf.c 702 B

12345678910111213141516171819202122232425262728293031323334
  1. #include "libm.h"
  2. #define SIGN 0x80000000
  3. float nextafterf(float x, float y)
  4. {
  5. union fshape ux, uy;
  6. uint32_t ax, ay, e;
  7. if (isnan(x) || isnan(y))
  8. return x + y;
  9. ux.value = x;
  10. uy.value = y;
  11. if (ux.bits == uy.bits)
  12. return y;
  13. ax = ux.bits & ~SIGN;
  14. ay = uy.bits & ~SIGN;
  15. if (ax == 0) {
  16. if (ay == 0)
  17. return y;
  18. ux.bits = (uy.bits & SIGN) | 1;
  19. } else if (ax > ay || ((ux.bits ^ uy.bits) & SIGN))
  20. ux.bits--;
  21. else
  22. ux.bits++;
  23. e = ux.bits & 0x7f800000;
  24. /* raise overflow if ux.value is infinite and x is finite */
  25. if (e == 0x7f800000)
  26. FORCE_EVAL(x+x);
  27. /* raise underflow if ux.value is subnormal or zero */
  28. if (e == 0)
  29. FORCE_EVAL(x*x + ux.value*ux.value);
  30. return ux.value;
  31. }