From fa42b7bed3a4e235affd9046bd70dd5e91a9b8d6 Mon Sep 17 00:00:00 2001 From: Nicolas 'Pixel' Noble Date: Wed, 8 Jul 2026 13:49:47 -0700 Subject: [PATCH] psyqo: make fixed-point iDiv/dDiv constexpr Move the FixedPointInternals iDiv/dDiv division helpers out of fixed-point.cpp into the header as constexpr, so fixed-point division can be evaluated at compile time. The out-of-line definitions are removed; the bodies are unchanged. printInt stays in the .cpp. Co-authored-by: Elias Daler Signed-off-by: Nicolas 'Pixel' Noble --- src/mips/psyqo/fixed-point.hh | 45 ++++++++++++++++++++++++++++-- src/mips/psyqo/src/fixed-point.cpp | 42 ---------------------------- 2 files changed, 43 insertions(+), 44 deletions(-) diff --git a/src/mips/psyqo/fixed-point.hh b/src/mips/psyqo/fixed-point.hh index a17c6ef8a..dfccf8052 100644 --- a/src/mips/psyqo/fixed-point.hh +++ b/src/mips/psyqo/fixed-point.hh @@ -37,10 +37,51 @@ namespace psyqo { namespace FixedPointInternals { -uint32_t iDiv(uint64_t rem, uint32_t base, unsigned scale); -int32_t dDiv(int32_t a, int32_t b, unsigned scale); void printInt(uint32_t value, const eastl::function&, unsigned scale); +constexpr uint32_t iDiv(uint64_t rem, uint32_t base, unsigned scale) { + rem *= scale; + uint64_t b = base; + uint64_t res, d = 1; + uint32_t high = rem >> 32; + + res = 0; + if (high >= base) { + high /= base; + res = static_cast(high) << 32; + rem -= static_cast(high * base) << 32; + } + + while (static_cast(b) > 0 && b < rem) { + b = b + b; + d = d + d; + } + + do { + if (rem >= b) { + rem -= b; + res += d; + } + b >>= 1; + d >>= 1; + } while (d); + + return res; +} + +constexpr int32_t dDiv(int32_t a, int32_t b, unsigned scale) { + int s = 1; + if (a < 0) { + a = -a; + s = -1; + } + if (b < 0) { + b = -b; + s = -s; + } + return iDiv(a, b, scale) * s; +} + } // namespace FixedPointInternals /** diff --git a/src/mips/psyqo/src/fixed-point.cpp b/src/mips/psyqo/src/fixed-point.cpp index 092d726e4..8d52eff78 100644 --- a/src/mips/psyqo/src/fixed-point.cpp +++ b/src/mips/psyqo/src/fixed-point.cpp @@ -26,48 +26,6 @@ SOFTWARE. #include "psyqo/fixed-point.hh" -uint32_t psyqo::FixedPointInternals::iDiv(uint64_t rem, uint32_t base, unsigned scale) { - rem *= scale; - uint64_t b = base; - uint64_t res, d = 1; - uint32_t high = rem >> 32; - - res = 0; - if (high >= base) { - high /= base; - res = static_cast(high) << 32; - rem -= static_cast(high * base) << 32; - } - - while (static_cast(b) > 0 && b < rem) { - b = b + b; - d = d + d; - } - - do { - if (rem >= b) { - rem -= b; - res += d; - } - b >>= 1; - d >>= 1; - } while (d); - - return res; -} - -int32_t psyqo::FixedPointInternals::dDiv(int32_t a, int32_t b, unsigned scale) { - int s = 1; - if (a < 0) { - a = -a; - s = -1; - } - if (b < 0) { - b = -b; - s = -s; - } - return iDiv(a, b, scale) * s; -} void psyqo::FixedPointInternals::printInt(uint32_t value, const eastl::function& charPrinter, unsigned scale) {