123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503 |
- #pragma once
- #include <Mathematics/ArbitraryPrecision.h>
- #include <Mathematics/QFNumber.h>
- #include <cfenv>
- namespace WwiseGTE
- {
- template <typename Rational>
- class APConversion
- {
- public:
- using QFN1 = QFNumber<Rational, 1>;
- using QFN2 = QFNumber<Rational, 2>;
-
- APConversion(int32_t precision, uint32_t maxIterations)
- :
- mZero(0),
- mOne(1),
- mThree(3),
- mFive(5),
- mPrecision(precision),
- mMaxIterations(maxIterations),
- mThreshold(std::ldexp(mOne, -mPrecision))
- {
- LogAssert(precision > 0, "Invalid precision.");
- LogAssert(maxIterations > 0, "Invalid maximum iterations.");
- }
- ~APConversion()
- {
- }
-
- void SetPrecision(int32_t precision)
- {
- LogAssert(precision > 0, "Invalid precision.");
- mPrecision = precision;
- mThreshold = std::ldexp(mOne, -mPrecision);
- }
- void SetMaxIterations(uint32_t maxIterations)
- {
- LogAssert(maxIterations > 0, "Invalid maximum iterations.");
- mMaxIterations = maxIterations;
- }
- inline int32_t GetPrecision() const
- {
- return mPrecision;
- }
- inline uint32_t GetMaxIterations() const
- {
- return mMaxIterations;
- }
-
- APConversion(APConversion const&) = delete;
- APConversion(APConversion&&) = delete;
- APConversion& operator=(APConversion const&) = delete;
- APConversion& operator=(APConversion&&) = delete;
-
-
-
-
- uint32_t EstimateSqrt(Rational const& aSqr, Rational& aMin, Rational& aMax)
- {
-
-
- Rational sSqr;
- int exponentA;
- PreprocessSqr(aSqr, sSqr, exponentA);
-
-
-
- aMax = GetMaxOfSqrt(sSqr, exponentA);
-
- aMin = aSqr / aMax;
-
-
- uint32_t iterate;
- for (iterate = 1; iterate <= mMaxIterations; ++iterate)
- {
- if (aMax - aMin < mThreshold)
- {
- break;
- }
-
-
-
- aMax = std::ldexp(aMin + aMax, -1);
- Convert(aMax, 2 * mPrecision, FE_UPWARD, aMax);
- aMin = aSqr / aMax;
- }
- return iterate;
- }
-
-
- uint32_t EstimateSqrt(Rational const& aSqr, Rational& a)
- {
-
- Rational aMin, aMax;
- uint32_t numIterates = EstimateSqrt(aSqr, aMin, aMax);
-
- a = std::ldexp(aMin + aMax, -1);
- return numIterates;
- }
- uint32_t EstimateApB(Rational const& aSqr, Rational const& bSqr,
- Rational& tMin, Rational& tMax)
- {
-
-
- Rational uSqr;
- int32_t exponentA;
- PreprocessSqr(aSqr, uSqr, exponentA);
-
-
- Rational vSqr;
- int32_t exponentB;
- PreprocessSqr(bSqr, vSqr, exponentB);
-
-
-
-
- Rational aMax = GetMaxOfSqrt(uSqr, exponentA);
- Rational bMax = GetMaxOfSqrt(vSqr, exponentB);
- tMax = aMax + bMax;
-
- Rational a2pb2 = aSqr + bSqr;
- Rational a2mb2 = aSqr - bSqr;
- Rational a2mb2Sqr = a2mb2 * a2mb2;
- Rational tMaxSqr = tMax * tMax;
- tMin = (a2pb2 * tMaxSqr - a2mb2Sqr) / (tMax * (tMaxSqr - a2pb2));
-
-
-
- uint32_t iterate;
- for (iterate = 1; iterate <= mMaxIterations; ++iterate)
- {
- if (tMax - tMin < mThreshold)
- {
- break;
- }
-
-
-
- tMax = std::ldexp(mThree * tMax + tMin, -2);
- Convert(tMax, 2 * mPrecision, FE_UPWARD, tMax);
- tMaxSqr = tMax * tMax;
- tMin = (a2pb2 * tMaxSqr - a2mb2Sqr) / (tMax * (tMaxSqr - a2pb2));
- }
- return iterate;
- }
- uint32_t EstimateAmB(Rational const& aSqr, Rational const& bSqr,
- Rational& tMin, Rational& tMax)
- {
-
- uint32_t iterate = 0;
-
- Rational a2tb2 = aSqr * bSqr;
- Rational a2pb2 = aSqr + bSqr;
- Rational a2mb2 = aSqr - bSqr;
- Rational a2mb2Sqr = a2mb2 * a2mb2;
- Rational twoa2pb2 = std::ldexp(a2pb2, 1);
-
-
- Rational uSqr;
- int32_t exponentA;
- PreprocessSqr(aSqr, uSqr, exponentA);
-
-
- Rational vSqr;
- int32_t exponentB;
- PreprocessSqr(bSqr, vSqr, exponentB);
-
-
-
- Rational signSecDer = a2mb2Sqr - mFive * a2tb2;
-
- Rational aMin, aMax, bMin, bMax, tMinSqr, tMaxSqr, tMid, tMidSqr, f;
- if (signSecDer > mZero)
- {
-
-
-
-
- aMin = GetMinOfSqrt(uSqr, exponentA);
- bMax = GetMaxOfSqrt(vSqr, exponentB);
- tMin = aMin - bMax;
-
-
-
- if (tMin < mZero)
- {
- tMin = mZero;
- }
-
-
-
- tMinSqr = tMin * tMin;
- signSecDer = mThree * tMinSqr - a2pb2;
- if (signSecDer < mZero)
- {
-
-
-
-
-
-
- aMax = GetMaxOfSqrt(uSqr, exponentA);
- bMin = GetMinOfSqrt(vSqr, exponentB);
- tMax = aMax - bMin;
- for (iterate = 1; iterate <= mMaxIterations; ++iterate)
- {
- if (tMax - tMin < mThreshold)
- {
- return iterate;
- }
- tMid = std::ldexp(tMin + tMax, -1);
- tMidSqr = tMid * tMid;
- signSecDer = mThree * tMidSqr - a2pb2;
- if (signSecDer >= mZero)
- {
- f = tMidSqr * (tMidSqr - twoa2pb2) + a2mb2Sqr;
- if (f >= mZero)
- {
- tMin = tMid;
- tMinSqr = tMidSqr;
- break;
- }
- else
- {
-
-
- tMax = tMid;
- Convert(tMax, 2 * mPrecision, FE_UPWARD, tMax);
- }
- }
- else
- {
-
-
- tMin = tMid;
- Convert(tMin, 2 * mPrecision, FE_DOWNWARD, tMin);
- }
- }
- }
-
- tMax = (a2pb2 * tMinSqr - a2mb2Sqr) / (tMin * (tMinSqr - a2pb2));
-
-
-
- for (iterate = 1; iterate <= mMaxIterations; ++iterate)
- {
- if (tMax - tMin < mThreshold)
- {
- break;
- }
-
-
-
-
- tMin = std::ldexp(mThree * tMin + tMax, -2);
- Convert(tMin, 2 * mPrecision, FE_DOWNWARD, tMin);
- tMinSqr = tMin * tMin;
- tMax = (a2pb2 * tMinSqr - a2mb2Sqr) / (tMin * (tMinSqr - a2pb2));
- }
- return iterate;
- }
- if (signSecDer < mZero)
- {
-
-
-
-
- aMax = GetMaxOfSqrt(uSqr, exponentA);
- bMin = GetMinOfSqrt(vSqr, exponentB);
- tMax = aMax - bMin;
-
-
-
- tMaxSqr = tMax * tMax;
- signSecDer = mThree * tMaxSqr - a2pb2;
- if (signSecDer > mZero)
- {
-
-
-
-
-
-
- aMin = GetMinOfSqrt(uSqr, exponentA);
- bMax = GetMaxOfSqrt(vSqr, exponentB);
- tMin = aMin - bMax;
- for (iterate = 1; iterate <= mMaxIterations; ++iterate)
- {
- if (tMax - tMin < mThreshold)
- {
- return iterate;
- }
- tMid = std::ldexp(tMin + tMax, -1);
- tMidSqr = tMid * tMid;
- signSecDer = mThree * tMidSqr - a2pb2;
- if (signSecDer <= mZero)
- {
- f = tMidSqr * (tMidSqr - twoa2pb2) + a2mb2Sqr;
- if (f <= mZero)
- {
- tMax = tMid;
- tMaxSqr = tMidSqr;
- break;
- }
- else
- {
-
-
- tMin = tMid;
- Convert(tMin, 2 * mPrecision, FE_DOWNWARD, tMin);
- }
- }
- else
- {
-
-
- tMax = tMid;
- Convert(tMax, 2 * mPrecision, FE_UPWARD, tMax);
- }
- }
- }
-
- tMin = (a2pb2 * tMaxSqr - a2mb2Sqr) / (tMax * (tMaxSqr - a2pb2));
-
-
-
- for (iterate = 1; iterate <= mMaxIterations; ++iterate)
- {
- if (tMax - tMin < mThreshold)
- {
- break;
- }
-
-
-
-
- tMax = std::ldexp(mThree * tMax + tMin, -2);
- Convert(tMax, 2 * mPrecision, FE_UPWARD, tMax);
- tMaxSqr = tMax * tMax;
- tMin = (a2pb2 * tMaxSqr - a2mb2Sqr) / (tMax * (tMaxSqr - a2pb2));
- }
- return iterate;
- }
-
-
-
-
-
- LogError("This second derivative cannot be zero at a-b.");
- }
-
-
- uint32_t Estimate(QFN1 const& q, Rational& qMin, Rational& qMax)
- {
- Rational const& x = q.x[0];
- Rational const& y = q.x[1];
- Rational const& d = q.d;
- uint32_t numIterates;
- if (d != mZero && y != mZero)
- {
- Rational aSqr = y * y * d;
- numIterates = EstimateSqrt(aSqr, qMin, qMax);
- if (y > mZero)
- {
- qMin = x + qMin;
- qMax = x + qMax;
- }
- else
- {
- Rational diff = x - qMax;
- qMax = x - qMin;
- qMin = diff;
- }
- }
- else
- {
- numIterates = 0;
- qMin = x;
- qMax = x;
- }
- return numIterates;
- }
-
-
- uint32_t Estimate(QFN1 const& q, Rational& qEstimate)
- {
-
- Rational qMin, qMax;
- uint32_t numIterates = Estimate(q, qMin, qMax);
-
- qEstimate = std::ldexp(qMin + qMax, -1);
- return numIterates;
- }
- private:
- void PreprocessSqr(Rational const& aSqr, Rational& rSqr, int& exponentA)
- {
-
- int32_t exponentASqr;
- rSqr = std::frexp(aSqr, &exponentASqr);
- if (exponentASqr & 1)
- {
-
- exponentA = (exponentASqr - 1) / 2;
- rSqr = std::ldexp(rSqr, 1);
-
- }
- else
- {
-
- exponentA = exponentASqr / 2;
-
- }
- }
- Rational GetMinOfSqrt(Rational const& rSqr, int exponent)
- {
- double lowerRSqr;
- Convert(rSqr, FE_DOWNWARD, lowerRSqr);
- int saveRoundingMode = std::fegetround();
- std::fesetround(FE_DOWNWARD);
- Rational aMin = std::sqrt(lowerRSqr);
- std::fesetround(saveRoundingMode);
- aMin = std::ldexp(aMin, exponent);
- return aMin;
- }
- Rational GetMaxOfSqrt(Rational const& rSqr, int exponent)
- {
- double upperRSqr;
- Convert(rSqr, FE_UPWARD, upperRSqr);
- int saveRoundingMode = std::fegetround();
- std::fesetround(FE_UPWARD);
- Rational aMax = std::sqrt(upperRSqr);
- std::fesetround(saveRoundingMode);
- aMax = std::ldexp(aMax, exponent);
- return aMax;
- }
- Rational const mZero, mOne, mThree, mFive;
- int32_t mPrecision;
- uint32_t mMaxIterations;
- Rational mThreshold;
- };
- }
|