ceil.c 514 B

123456789101112131415161718192021222324
  1. #include "libm.h"
  2. double ceil(double x)
  3. {
  4. union {double f; uint64_t i;} u = {x};
  5. int e = u.i >> 52 & 0x7ff;
  6. double_t y;
  7. if (e >= 0x3ff+52 || x == 0)
  8. return x;
  9. /* y = int(x) - x, where int(x) is an integer neighbor of x */
  10. if (u.i >> 63)
  11. y = (double)(x - 0x1p52) + 0x1p52 - x;
  12. else
  13. y = (double)(x + 0x1p52) - 0x1p52 - x;
  14. /* special case because of non-nearest rounding modes */
  15. if (e <= 0x3ff-1) {
  16. FORCE_EVAL(y);
  17. return u.i >> 63 ? -0.0 : 1;
  18. }
  19. if (y < 0)
  20. return x + y + 1;
  21. return x + y;
  22. }