remquol.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* origin: FreeBSD /usr/src/lib/msun/src/s_remquol.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. #include "libm.h"
  13. #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
  14. long double remquol(long double x, long double y, int *quo)
  15. {
  16. return remquo(x, y, quo);
  17. }
  18. #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
  19. #define BIAS (LDBL_MAX_EXP - 1)
  20. #if LDBL_MANL_SIZE > 32
  21. typedef uint64_t manl_t;
  22. #else
  23. typedef uint32_t manl_t;
  24. #endif
  25. #if LDBL_MANH_SIZE > 32
  26. typedef uint64_t manh_t;
  27. #else
  28. typedef uint32_t manh_t;
  29. #endif
  30. /*
  31. * These macros add and remove an explicit integer bit in front of the
  32. * fractional mantissa, if the architecture doesn't have such a bit by
  33. * default already.
  34. */
  35. #ifdef LDBL_IMPLICIT_NBIT
  36. #define SET_NBIT(hx) ((hx) | (1ULL << LDBL_MANH_SIZE))
  37. #define HFRAC_BITS LDBL_MANH_SIZE
  38. #else
  39. #define SET_NBIT(hx) (hx)
  40. #define HFRAC_BITS (LDBL_MANH_SIZE - 1)
  41. #endif
  42. #define MANL_SHIFT (LDBL_MANL_SIZE - 1)
  43. static const long double Zero[] = {0.0, -0.0};
  44. /*
  45. * Return the IEEE remainder and set *quo to the last n bits of the
  46. * quotient, rounded to the nearest integer. We choose n=31 because
  47. * we wind up computing all the integer bits of the quotient anyway as
  48. * a side-effect of computing the remainder by the shift and subtract
  49. * method. In practice, this is far more bits than are needed to use
  50. * remquo in reduction algorithms.
  51. *
  52. * Assumptions:
  53. * - The low part of the mantissa fits in a manl_t exactly.
  54. * - The high part of the mantissa fits in an int64_t with enough room
  55. * for an explicit integer bit in front of the fractional bits.
  56. */
  57. long double remquol(long double x, long double y, int *quo)
  58. {
  59. union IEEEl2bits ux, uy;
  60. int64_t hx,hz; /* We need a carry bit even if LDBL_MANH_SIZE is 32. */
  61. manh_t hy;
  62. manl_t lx,ly,lz;
  63. int ix,iy,n,q,sx,sxy;
  64. ux.e = x;
  65. uy.e = y;
  66. sx = ux.bits.sign;
  67. sxy = sx ^ uy.bits.sign;
  68. ux.bits.sign = 0; /* |x| */
  69. uy.bits.sign = 0; /* |y| */
  70. x = ux.e;
  71. /* purge off exception values */
  72. if ((uy.bits.exp|uy.bits.manh|uy.bits.manl)==0 || /* y=0 */
  73. (ux.bits.exp == BIAS + LDBL_MAX_EXP) || /* or x not finite */
  74. (uy.bits.exp == BIAS + LDBL_MAX_EXP &&
  75. ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl)!=0)) /* or y is NaN */
  76. return (x*y)/(x*y);
  77. if (ux.bits.exp <= uy.bits.exp) {
  78. if ((ux.bits.exp < uy.bits.exp) ||
  79. (ux.bits.manh <= uy.bits.manh &&
  80. (ux.bits.manh < uy.bits.manh ||
  81. ux.bits.manl < uy.bits.manl))) {
  82. q = 0;
  83. goto fixup; /* |x|<|y| return x or x-y */
  84. }
  85. if (ux.bits.manh == uy.bits.manh && ux.bits.manl == uy.bits.manl) {
  86. *quo = sxy ? -1 : 1;
  87. return Zero[sx]; /* |x|=|y| return x*0*/
  88. }
  89. }
  90. /* determine ix = ilogb(x) */
  91. if (ux.bits.exp == 0) { /* subnormal x */
  92. ux.e *= 0x1.0p512;
  93. ix = ux.bits.exp - (BIAS + 512);
  94. } else {
  95. ix = ux.bits.exp - BIAS;
  96. }
  97. /* determine iy = ilogb(y) */
  98. if (uy.bits.exp == 0) { /* subnormal y */
  99. uy.e *= 0x1.0p512;
  100. iy = uy.bits.exp - (BIAS + 512);
  101. } else {
  102. iy = uy.bits.exp - BIAS;
  103. }
  104. /* set up {hx,lx}, {hy,ly} and align y to x */
  105. hx = SET_NBIT(ux.bits.manh);
  106. hy = SET_NBIT(uy.bits.manh);
  107. lx = ux.bits.manl;
  108. ly = uy.bits.manl;
  109. /* fix point fmod */
  110. n = ix - iy;
  111. q = 0;
  112. while (n--) {
  113. hz = hx - hy;
  114. lz = lx - ly;
  115. if (lx < ly)
  116. hz -= 1;
  117. if (hz < 0) {
  118. hx = hx + hx + (lx>>MANL_SHIFT);
  119. lx = lx + lx;
  120. } else {
  121. hx = hz + hz + (lz>>MANL_SHIFT);
  122. lx = lz + lz;
  123. q++;
  124. }
  125. q <<= 1;
  126. }
  127. hz = hx - hy;
  128. lz = lx - ly;
  129. if (lx < ly)
  130. hz -= 1;
  131. if (hz >= 0) {
  132. hx = hz;
  133. lx = lz;
  134. q++;
  135. }
  136. /* convert back to floating value and restore the sign */
  137. if ((hx|lx) == 0) { /* return sign(x)*0 */
  138. q &= 0x7fffffff;
  139. *quo = sxy ? -q : q;
  140. return Zero[sx];
  141. }
  142. while (hx < (1ULL<<HFRAC_BITS)) { /* normalize x */
  143. hx = hx + hx + (lx>>MANL_SHIFT);
  144. lx = lx + lx;
  145. iy -= 1;
  146. }
  147. ux.bits.manh = hx; /* The integer bit is truncated here if needed. */
  148. ux.bits.manl = lx;
  149. if (iy < LDBL_MIN_EXP) {
  150. ux.bits.exp = iy + (BIAS + 512);
  151. ux.e *= 0x1p-512;
  152. } else {
  153. ux.bits.exp = iy + BIAS;
  154. }
  155. ux.bits.sign = 0;
  156. x = ux.e;
  157. fixup:
  158. y = fabsl(y);
  159. if (y < LDBL_MIN * 2) {
  160. if (x + x > y || (x + x == y && (q & 1))) {
  161. q++;
  162. x-=y;
  163. }
  164. } else if (x > 0.5*y || (x == 0.5*y && (q & 1))) {
  165. q++;
  166. x-=y;
  167. }
  168. ux.e = x;
  169. ux.bits.sign ^= sx;
  170. x = ux.e;
  171. q &= 0x7fffffff;
  172. *quo = sxy ? -q : q;
  173. return x;
  174. }
  175. #endif