lrintq.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* Round argument to nearest integral value according to current rounding
  2. direction.
  3. Copyright (C) 1997, 1999, 2004, 2006 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997 and
  6. Jakub Jelinek <jj@ultra.linux.cz>, 1999.
  7. The GNU C Library is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  11. The GNU C Library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with the GNU C Library; if not, write to the Free
  17. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  18. 02111-1307 USA. */
  19. #include "quadmath-imp.h"
  20. static const __float128 two112[2] =
  21. {
  22. 5.19229685853482762853049632922009600E+33Q, /* 0x406F000000000000, 0 */
  23. -5.19229685853482762853049632922009600E+33Q /* 0xC06F000000000000, 0 */
  24. };
  25. long int
  26. lrintq (__float128 x)
  27. {
  28. int32_t j0;
  29. uint64_t i0,i1;
  30. volatile __float128 w;
  31. __float128 t;
  32. long int result;
  33. int sx;
  34. GET_FLT128_WORDS64 (i0, i1, x);
  35. j0 = ((i0 >> 48) & 0x7fff) - 0x3fff;
  36. sx = i0 >> 63;
  37. i0 &= 0x0000ffffffffffffLL;
  38. i0 |= 0x0001000000000000LL;
  39. if (j0 < 48)
  40. {
  41. w = two112[sx] + x;
  42. t = w - two112[sx];
  43. GET_FLT128_WORDS64 (i0, i1, t);
  44. j0 = ((i0 >> 48) & 0x7fff) - 0x3fff;
  45. i0 &= 0x0000ffffffffffffLL;
  46. i0 |= 0x0001000000000000LL;
  47. result = (j0 < 0 ? 0 : i0 >> (48 - j0));
  48. }
  49. else if (j0 < (int32_t) (8 * sizeof (long int)) - 1)
  50. {
  51. if (j0 >= 112)
  52. result = ((long int) i0 << (j0 - 48)) | (i1 << (j0 - 112));
  53. else
  54. {
  55. w = two112[sx] + x;
  56. t = w - two112[sx];
  57. GET_FLT128_WORDS64 (i0, i1, t);
  58. j0 = ((i0 >> 48) & 0x7fff) - 0x3fff;
  59. i0 &= 0x0000ffffffffffffLL;
  60. i0 |= 0x0001000000000000LL;
  61. if (j0 == 48)
  62. result = (long int) i0;
  63. else
  64. result = ((long int) i0 << (j0 - 48)) | (i1 >> (112 - j0));
  65. }
  66. }
  67. else
  68. {
  69. /* The number is too large. It is left implementation defined
  70. what happens. */
  71. return (long int) x;
  72. }
  73. return sx ? -result : result;
  74. }