remquo.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* origin: FreeBSD /usr/src/lib/msun/src/s_remquo.c */
  2. /*-
  3. * ====================================================
  4. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  5. *
  6. * Developed at SunSoft, a Sun Microsystems, Inc. business.
  7. * Permission to use, copy, modify, and distribute this
  8. * software is freely granted, provided that this notice
  9. * is preserved.
  10. * ====================================================
  11. */
  12. /*
  13. * Return the IEEE remainder and set *quo to the last n bits of the
  14. * quotient, rounded to the nearest integer. We choose n=31 because
  15. * we wind up computing all the integer bits of the quotient anyway as
  16. * a side-effect of computing the remainder by the shift and subtract
  17. * method. In practice, this is far more bits than are needed to use
  18. * remquo in reduction algorithms.
  19. */
  20. #include "libm.h"
  21. static const double Zero[] = {0.0, -0.0,};
  22. double remquo(double x, double y, int *quo)
  23. {
  24. int32_t n,hx,hy,hz,ix,iy,sx,i;
  25. uint32_t lx,ly,lz,q,sxy;
  26. EXTRACT_WORDS(hx, lx, x);
  27. EXTRACT_WORDS(hy, ly, y);
  28. sxy = (hx ^ hy) & 0x80000000;
  29. sx = hx & 0x80000000; /* sign of x */
  30. hx ^= sx; /* |x| */
  31. hy &= 0x7fffffff; /* |y| */
  32. /* purge off exception values */
  33. if ((hy|ly) == 0 || hx >= 0x7ff00000 || /* y = 0, or x not finite */
  34. (hy|((ly|-ly)>>31)) > 0x7ff00000) /* or y is NaN */
  35. return (x*y)/(x*y);
  36. if (hx <= hy) {
  37. if (hx < hy || lx < ly) { /* |x| < |y| return x or x-y */
  38. q = 0;
  39. goto fixup;
  40. }
  41. if (lx == ly) { /* |x| = |y| return x*0 */
  42. *quo = sxy ? -1 : 1;
  43. return Zero[(uint32_t)sx>>31];
  44. }
  45. }
  46. // FIXME: use ilogb?
  47. /* determine ix = ilogb(x) */
  48. if (hx < 0x00100000) { /* subnormal x */
  49. if (hx == 0) {
  50. for (ix = -1043, i=lx; i>0; i<<=1) ix--;
  51. } else {
  52. for (ix = -1022, i=hx<<11; i>0; i<<=1) ix--;
  53. }
  54. } else
  55. ix = (hx>>20) - 1023;
  56. /* determine iy = ilogb(y) */
  57. if (hy < 0x00100000) { /* subnormal y */
  58. if (hy == 0) {
  59. for (iy = -1043, i=ly; i>0; i<<=1) iy--;
  60. } else {
  61. for (iy = -1022, i=hy<<11; i>0; i<<=1) iy--;
  62. }
  63. } else
  64. iy = (hy>>20) - 1023;
  65. /* set up {hx,lx}, {hy,ly} and align y to x */
  66. if (ix >= -1022)
  67. hx = 0x00100000|(0x000fffff&hx);
  68. else { /* subnormal x, shift x to normal */
  69. n = -1022 - ix;
  70. if (n <= 31) {
  71. hx = (hx<<n)|(lx>>(32-n));
  72. lx <<= n;
  73. } else {
  74. hx = lx<<(n-32);
  75. lx = 0;
  76. }
  77. }
  78. if (iy >= -1022)
  79. hy = 0x00100000|(0x000fffff&hy);
  80. else { /* subnormal y, shift y to normal */
  81. n = -1022 - iy;
  82. if (n <= 31) {
  83. hy = (hy<<n)|(ly>>(32-n));
  84. ly <<= n;
  85. } else {
  86. hy = ly<<(n-32);
  87. ly = 0;
  88. }
  89. }
  90. /* fix point fmod */
  91. n = ix - iy;
  92. q = 0;
  93. while (n--) {
  94. hz = hx - hy;
  95. lz = lx - ly;
  96. if (lx < ly)
  97. hz--;
  98. if (hz < 0) {
  99. hx = hx + hx + (lx>>31);
  100. lx = lx + lx;
  101. } else {
  102. hx = hz + hz + (lz>>31);
  103. lx = lz + lz;
  104. q++;
  105. }
  106. q <<= 1;
  107. }
  108. hz = hx - hy;
  109. lz = lx - ly;
  110. if (lx < ly)
  111. hz--;
  112. if (hz >= 0) {
  113. hx = hz;
  114. lx = lz;
  115. q++;
  116. }
  117. /* convert back to floating value and restore the sign */
  118. if ((hx|lx) == 0) { /* return sign(x)*0 */
  119. q &= 0x7fffffff;
  120. *quo = sxy ? -q : q;
  121. return Zero[(uint32_t)sx>>31];
  122. }
  123. while (hx < 0x00100000) { /* normalize x */
  124. hx = hx + hx + (lx>>31);
  125. lx = lx + lx;
  126. iy--;
  127. }
  128. if (iy >= -1022) { /* normalize output */
  129. hx = (hx-0x00100000)|((iy+1023)<<20);
  130. } else { /* subnormal output */
  131. n = -1022 - iy;
  132. if (n <= 20) {
  133. lx = (lx>>n)|((uint32_t)hx<<(32-n));
  134. hx >>= n;
  135. } else if (n <= 31) {
  136. lx = (hx<<(32-n))|(lx>>n);
  137. hx = 0;
  138. } else {
  139. lx = hx>>(n-32);
  140. hx = 0;
  141. }
  142. }
  143. fixup:
  144. INSERT_WORDS(x, hx, lx);
  145. y = fabs(y);
  146. if (y < 0x1p-1021) {
  147. if (x + x > y || (x + x == y && (q & 1))) {
  148. q++;
  149. x -= y;
  150. }
  151. } else if (x > 0.5*y || (x == 0.5*y && (q & 1))) {
  152. q++;
  153. x -= y;
  154. }
  155. GET_HIGH_WORD(hx, x);
  156. SET_HIGH_WORD(x, hx ^ sx);
  157. q &= 0x7fffffff;
  158. *quo = sxy ? -q : q;
  159. return x;
  160. }