1
0

truncl.c 668 B

12345678910111213141516171819202122232425262728293031323334
  1. #include "libm.h"
  2. #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
  3. long double truncl(long double x)
  4. {
  5. return trunc(x);
  6. }
  7. #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
  8. static const long double toint = 1/LDBL_EPSILON;
  9. long double truncl(long double x)
  10. {
  11. union ldshape u = {x};
  12. int e = u.i.se & 0x7fff;
  13. int s = u.i.se >> 15;
  14. long double y;
  15. if (e >= 0x3fff+LDBL_MANT_DIG-1)
  16. return x;
  17. if (e <= 0x3fff-1) {
  18. FORCE_EVAL(x + 0x1p120f);
  19. return x*0;
  20. }
  21. /* y = int(|x|) - |x|, where int(|x|) is an integer neighbor of |x| */
  22. if (s)
  23. x = -x;
  24. y = x + toint - toint - x;
  25. if (y > 0)
  26. y -= 1;
  27. x += y;
  28. return s ? -x : x;
  29. }
  30. #endif