123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- #define QUAD_PRECISION
- #include "fp_lib.h"
- #if defined(CRT_HAS_128BIT) && defined(CRT_LDBL_128BIT)
- COMPILER_RT_ABI fp_t __divtf3(fp_t a, fp_t b) {
- const unsigned int aExponent = toRep(a) >> significandBits & maxExponent;
- const unsigned int bExponent = toRep(b) >> significandBits & maxExponent;
- const rep_t quotientSign = (toRep(a) ^ toRep(b)) & signBit;
- rep_t aSignificand = toRep(a) & significandMask;
- rep_t bSignificand = toRep(b) & significandMask;
- int scale = 0;
-
- if (aExponent-1U >= maxExponent-1U || bExponent-1U >= maxExponent-1U) {
- const rep_t aAbs = toRep(a) & absMask;
- const rep_t bAbs = toRep(b) & absMask;
-
- if (aAbs > infRep) return fromRep(toRep(a) | quietBit);
-
- if (bAbs > infRep) return fromRep(toRep(b) | quietBit);
- if (aAbs == infRep) {
-
- if (bAbs == infRep) return fromRep(qnanRep);
-
- else return fromRep(aAbs | quotientSign);
- }
-
- if (bAbs == infRep) return fromRep(quotientSign);
- if (!aAbs) {
-
- if (!bAbs) return fromRep(qnanRep);
-
- else return fromRep(quotientSign);
- }
-
- if (!bAbs) return fromRep(infRep | quotientSign);
-
-
-
- if (aAbs < implicitBit) scale += normalize(&aSignificand);
- if (bAbs < implicitBit) scale -= normalize(&bSignificand);
- }
-
-
-
- aSignificand |= implicitBit;
- bSignificand |= implicitBit;
- int quotientExponent = aExponent - bExponent + scale;
-
-
-
-
- const uint64_t q63b = bSignificand >> 49;
- uint64_t recip64 = UINT64_C(0x7504f333F9DE6484) - q63b;
-
-
-
-
-
-
-
- uint64_t correction64;
- correction64 = -((rep_t)recip64 * q63b >> 64);
- recip64 = (rep_t)recip64 * correction64 >> 63;
- correction64 = -((rep_t)recip64 * q63b >> 64);
- recip64 = (rep_t)recip64 * correction64 >> 63;
- correction64 = -((rep_t)recip64 * q63b >> 64);
- recip64 = (rep_t)recip64 * correction64 >> 63;
- correction64 = -((rep_t)recip64 * q63b >> 64);
- recip64 = (rep_t)recip64 * correction64 >> 63;
- correction64 = -((rep_t)recip64 * q63b >> 64);
- recip64 = (rep_t)recip64 * correction64 >> 63;
-
-
-
-
- recip64--;
-
-
- const uint64_t q127blo = bSignificand << 15;
- rep_t correction, reciprocal;
-
-
- rep_t r64q63, r64q127, r64cH, r64cL, dummy;
- wideMultiply((rep_t)recip64, (rep_t)q63b, &dummy, &r64q63);
- wideMultiply((rep_t)recip64, (rep_t)q127blo, &dummy, &r64q127);
- correction = -(r64q63 + (r64q127 >> 64));
- uint64_t cHi = correction >> 64;
- uint64_t cLo = correction;
- wideMultiply((rep_t)recip64, (rep_t)cHi, &dummy, &r64cH);
- wideMultiply((rep_t)recip64, (rep_t)cLo, &dummy, &r64cL);
- reciprocal = r64cH + (r64cL >> 64);
-
-
-
-
-
- reciprocal -= 2;
-
-
-
-
-
-
-
-
-
-
-
- rep_t quotient, quotientLo;
- wideMultiply(aSignificand << 2, reciprocal, "ient, "ientLo);
-
-
-
-
-
-
-
-
-
-
-
-
-
- rep_t residual;
- rep_t qb;
- if (quotient < (implicitBit << 1)) {
- wideMultiply(quotient, bSignificand, &dummy, &qb);
- residual = (aSignificand << 113) - qb;
- quotientExponent--;
- } else {
- quotient >>= 1;
- wideMultiply(quotient, bSignificand, &dummy, &qb);
- residual = (aSignificand << 112) - qb;
- }
- const int writtenExponent = quotientExponent + exponentBias;
- if (writtenExponent >= maxExponent) {
-
- return fromRep(infRep | quotientSign);
- }
- else if (writtenExponent < 1) {
-
-
- return fromRep(quotientSign);
- }
- else {
- const bool round = (residual << 1) >= bSignificand;
-
- rep_t absResult = quotient & significandMask;
-
- absResult |= (rep_t)writtenExponent << significandBits;
-
- absResult += round;
-
- const long double result = fromRep(absResult | quotientSign);
- return result;
- }
- }
- #endif
|