s_trunc.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* Truncate argument to nearest integral value not larger than the argument.
  2. Copyright (C) 1997, 1998 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <math.h>
  17. #include "math_private.h"
  18. double
  19. trunc (double x)
  20. {
  21. int32_t i0, _j0;
  22. u_int32_t i1;
  23. int sx;
  24. EXTRACT_WORDS (i0, i1, x);
  25. sx = i0 & 0x80000000;
  26. _j0 = ((i0 >> 20) & 0x7ff) - 0x3ff;
  27. if (_j0 < 20)
  28. {
  29. if (_j0 < 0)
  30. /* The magnitude of the number is < 1 so the result is +-0. */
  31. INSERT_WORDS (x, sx, 0);
  32. else
  33. INSERT_WORDS (x, sx | (i0 & ~(0x000fffff >> _j0)), 0);
  34. }
  35. else if (_j0 > 51)
  36. {
  37. if (_j0 == 0x400)
  38. /* x is inf or NaN. */
  39. return x + x;
  40. }
  41. else
  42. {
  43. INSERT_WORDS (x, i0, i1 & ~(0xffffffffu >> (_j0 - 20)));
  44. }
  45. return x;
  46. }
  47. libm_hidden_def(trunc)