trunc.c 303 B

12345678910111213141516171819
  1. #include "libm.h"
  2. double trunc(double x)
  3. {
  4. union {double f; uint64_t i;} u = {x};
  5. int e = (int)(u.i >> 52 & 0x7ff) - 0x3ff + 12;
  6. uint64_t m;
  7. if (e >= 52 + 12)
  8. return x;
  9. if (e < 12)
  10. e = 1;
  11. m = -1ULL >> e;
  12. if ((u.i & m) == 0)
  13. return x;
  14. FORCE_EVAL(x + 0x1p120f);
  15. u.i &= ~m;
  16. return u.f;
  17. }