divtf3.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //===-- lib/divtf3.c - Quad-precision division --------------------*- C -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements quad-precision soft-float division
  11. // with the IEEE-754 default rounding (to nearest, ties to even).
  12. //
  13. // For simplicity, this implementation currently flushes denormals to zero.
  14. // It should be a fairly straightforward exercise to implement gradual
  15. // underflow with correct rounding.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #define QUAD_PRECISION
  19. #include "fp_lib.h"
  20. #if defined(CRT_HAS_128BIT) && defined(CRT_LDBL_128BIT)
  21. COMPILER_RT_ABI fp_t __divtf3(fp_t a, fp_t b) {
  22. const unsigned int aExponent = toRep(a) >> significandBits & maxExponent;
  23. const unsigned int bExponent = toRep(b) >> significandBits & maxExponent;
  24. const rep_t quotientSign = (toRep(a) ^ toRep(b)) & signBit;
  25. rep_t aSignificand = toRep(a) & significandMask;
  26. rep_t bSignificand = toRep(b) & significandMask;
  27. int scale = 0;
  28. // Detect if a or b is zero, denormal, infinity, or NaN.
  29. if (aExponent-1U >= maxExponent-1U || bExponent-1U >= maxExponent-1U) {
  30. const rep_t aAbs = toRep(a) & absMask;
  31. const rep_t bAbs = toRep(b) & absMask;
  32. // NaN / anything = qNaN
  33. if (aAbs > infRep) return fromRep(toRep(a) | quietBit);
  34. // anything / NaN = qNaN
  35. if (bAbs > infRep) return fromRep(toRep(b) | quietBit);
  36. if (aAbs == infRep) {
  37. // infinity / infinity = NaN
  38. if (bAbs == infRep) return fromRep(qnanRep);
  39. // infinity / anything else = +/- infinity
  40. else return fromRep(aAbs | quotientSign);
  41. }
  42. // anything else / infinity = +/- 0
  43. if (bAbs == infRep) return fromRep(quotientSign);
  44. if (!aAbs) {
  45. // zero / zero = NaN
  46. if (!bAbs) return fromRep(qnanRep);
  47. // zero / anything else = +/- zero
  48. else return fromRep(quotientSign);
  49. }
  50. // anything else / zero = +/- infinity
  51. if (!bAbs) return fromRep(infRep | quotientSign);
  52. // one or both of a or b is denormal, the other (if applicable) is a
  53. // normal number. Renormalize one or both of a and b, and set scale to
  54. // include the necessary exponent adjustment.
  55. if (aAbs < implicitBit) scale += normalize(&aSignificand);
  56. if (bAbs < implicitBit) scale -= normalize(&bSignificand);
  57. }
  58. // Or in the implicit significand bit. (If we fell through from the
  59. // denormal path it was already set by normalize( ), but setting it twice
  60. // won't hurt anything.)
  61. aSignificand |= implicitBit;
  62. bSignificand |= implicitBit;
  63. int quotientExponent = aExponent - bExponent + scale;
  64. // Align the significand of b as a Q63 fixed-point number in the range
  65. // [1, 2.0) and get a Q64 approximate reciprocal using a small minimax
  66. // polynomial approximation: reciprocal = 3/4 + 1/sqrt(2) - b/2. This
  67. // is accurate to about 3.5 binary digits.
  68. const uint64_t q63b = bSignificand >> 49;
  69. uint64_t recip64 = UINT64_C(0x7504f333F9DE6484) - q63b;
  70. // 0x7504f333F9DE6484 / 2^64 + 1 = 3/4 + 1/sqrt(2)
  71. // Now refine the reciprocal estimate using a Newton-Raphson iteration:
  72. //
  73. // x1 = x0 * (2 - x0 * b)
  74. //
  75. // This doubles the number of correct binary digits in the approximation
  76. // with each iteration.
  77. uint64_t correction64;
  78. correction64 = -((rep_t)recip64 * q63b >> 64);
  79. recip64 = (rep_t)recip64 * correction64 >> 63;
  80. correction64 = -((rep_t)recip64 * q63b >> 64);
  81. recip64 = (rep_t)recip64 * correction64 >> 63;
  82. correction64 = -((rep_t)recip64 * q63b >> 64);
  83. recip64 = (rep_t)recip64 * correction64 >> 63;
  84. correction64 = -((rep_t)recip64 * q63b >> 64);
  85. recip64 = (rep_t)recip64 * correction64 >> 63;
  86. correction64 = -((rep_t)recip64 * q63b >> 64);
  87. recip64 = (rep_t)recip64 * correction64 >> 63;
  88. // recip64 might have overflowed to exactly zero in the preceeding
  89. // computation if the high word of b is exactly 1.0. This would sabotage
  90. // the full-width final stage of the computation that follows, so we adjust
  91. // recip64 downward by one bit.
  92. recip64--;
  93. // We need to perform one more iteration to get us to 112 binary digits;
  94. // The last iteration needs to happen with extra precision.
  95. const uint64_t q127blo = bSignificand << 15;
  96. rep_t correction, reciprocal;
  97. // NOTE: This operation is equivalent to __multi3, which is not implemented
  98. // in some architechure
  99. rep_t r64q63, r64q127, r64cH, r64cL, dummy;
  100. wideMultiply((rep_t)recip64, (rep_t)q63b, &dummy, &r64q63);
  101. wideMultiply((rep_t)recip64, (rep_t)q127blo, &dummy, &r64q127);
  102. correction = -(r64q63 + (r64q127 >> 64));
  103. uint64_t cHi = correction >> 64;
  104. uint64_t cLo = correction;
  105. wideMultiply((rep_t)recip64, (rep_t)cHi, &dummy, &r64cH);
  106. wideMultiply((rep_t)recip64, (rep_t)cLo, &dummy, &r64cL);
  107. reciprocal = r64cH + (r64cL >> 64);
  108. // We already adjusted the 64-bit estimate, now we need to adjust the final
  109. // 128-bit reciprocal estimate downward to ensure that it is strictly smaller
  110. // than the infinitely precise exact reciprocal. Because the computation
  111. // of the Newton-Raphson step is truncating at every step, this adjustment
  112. // is small; most of the work is already done.
  113. reciprocal -= 2;
  114. // The numerical reciprocal is accurate to within 2^-112, lies in the
  115. // interval [0.5, 1.0), and is strictly smaller than the true reciprocal
  116. // of b. Multiplying a by this reciprocal thus gives a numerical q = a/b
  117. // in Q127 with the following properties:
  118. //
  119. // 1. q < a/b
  120. // 2. q is in the interval [0.5, 2.0)
  121. // 3. the error in q is bounded away from 2^-113 (actually, we have a
  122. // couple of bits to spare, but this is all we need).
  123. // We need a 128 x 128 multiply high to compute q, which isn't a basic
  124. // operation in C, so we need to be a little bit fussy.
  125. rep_t quotient, quotientLo;
  126. wideMultiply(aSignificand << 2, reciprocal, &quotient, &quotientLo);
  127. // Two cases: quotient is in [0.5, 1.0) or quotient is in [1.0, 2.0).
  128. // In either case, we are going to compute a residual of the form
  129. //
  130. // r = a - q*b
  131. //
  132. // We know from the construction of q that r satisfies:
  133. //
  134. // 0 <= r < ulp(q)*b
  135. //
  136. // if r is greater than 1/2 ulp(q)*b, then q rounds up. Otherwise, we
  137. // already have the correct result. The exact halfway case cannot occur.
  138. // We also take this time to right shift quotient if it falls in the [1,2)
  139. // range and adjust the exponent accordingly.
  140. rep_t residual;
  141. rep_t qb;
  142. if (quotient < (implicitBit << 1)) {
  143. wideMultiply(quotient, bSignificand, &dummy, &qb);
  144. residual = (aSignificand << 113) - qb;
  145. quotientExponent--;
  146. } else {
  147. quotient >>= 1;
  148. wideMultiply(quotient, bSignificand, &dummy, &qb);
  149. residual = (aSignificand << 112) - qb;
  150. }
  151. const int writtenExponent = quotientExponent + exponentBias;
  152. if (writtenExponent >= maxExponent) {
  153. // If we have overflowed the exponent, return infinity.
  154. return fromRep(infRep | quotientSign);
  155. }
  156. else if (writtenExponent < 1) {
  157. // Flush denormals to zero. In the future, it would be nice to add
  158. // code to round them correctly.
  159. return fromRep(quotientSign);
  160. }
  161. else {
  162. const bool round = (residual << 1) >= bSignificand;
  163. // Clear the implicit bit
  164. rep_t absResult = quotient & significandMask;
  165. // Insert the exponent
  166. absResult |= (rep_t)writtenExponent << significandBits;
  167. // Round
  168. absResult += round;
  169. // Insert the sign and return
  170. const long double result = fromRep(absResult | quotientSign);
  171. return result;
  172. }
  173. }
  174. #endif