From 254d325a3e1d8a5fe664cc66543b9ac8da5dd4ad Mon Sep 17 00:00:00 2001 From: Ed Hennis Date: Wed, 10 Dec 2025 16:06:54 -0500 Subject: [PATCH] Refactor static normalize - Use a single worker function that does all the work and explicit template instantiation. If this gives me any more trouble, I'm just going to move normalize into the header, but I was hoping to avoid that. --- include/xrpl/basics/Number.h | 20 ++++++++--------- src/libxrpl/basics/Number.cpp | 42 ++++++++++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/include/xrpl/basics/Number.h b/include/xrpl/basics/Number.h index 6f42b85dae..2135cb00a9 100644 --- a/include/xrpl/basics/Number.h +++ b/include/xrpl/basics/Number.h @@ -327,6 +327,15 @@ private: internalrep const& minMantissa, internalrep const& maxMantissa); + template + friend void + doNormalize( + bool& negative, + T& mantissa_, + int& exponent_, + MantissaRange::rep const& minMantissa, + MantissaRange::rep const& maxMantissa); + bool isnormal() const noexcept; @@ -537,17 +546,6 @@ Number::isnormal() const noexcept exponent_ <= maxExponent); } -// Because the template function definition is in the .cpp file, declare -// some of the overrides used outside of this class here. -template <> -void -Number::normalize( - bool& negative, - unsigned long& mantissa, - int& exponent, - internalrep const& minMantissa, - internalrep const& maxMantissa); - template std::pair Number::normalizeToRange(T minMantissa, T maxMantissa) const diff --git a/src/libxrpl/basics/Number.cpp b/src/libxrpl/basics/Number.cpp index 3ff1980731..199776ecef 100644 --- a/src/libxrpl/basics/Number.cpp +++ b/src/libxrpl/basics/Number.cpp @@ -245,13 +245,19 @@ Number::one() // TODO: Rename the function parameters to get rid of the "_" suffix template void -Number::normalize( +doNormalize( bool& negative, T& mantissa_, int& exponent_, - internalrep const& minMantissa, - internalrep const& maxMantissa) + MantissaRange::rep const& minMantissa, + MantissaRange::rep const& maxMantissa) { + auto constexpr minExponent = Number::minExponent; + auto constexpr maxExponent = Number::maxExponent; + auto constexpr maxRep = Number::maxRep; + + using Guard = Number::Guard; + constexpr Number zero = Number{}; if (mantissa_ == 0) { @@ -306,7 +312,7 @@ Number::normalize( } XRPL_ASSERT_PARTS( m <= maxRep, - "ripple::Number::normalize", + "ripple::doNormalize", "intermediate mantissa fits in int64"); mantissa_ = m; @@ -332,10 +338,34 @@ Number::normalize( } XRPL_ASSERT_PARTS( mantissa_ >= minMantissa && mantissa_ <= maxMantissa, - "ripple::Number::normalize", + "ripple::doNormalize", "final mantissa fits in range"); } +template <> +void +Number::normalize( + bool& negative, + uint128_t& mantissa, + int& exponent, + internalrep const& minMantissa, + internalrep const& maxMantissa) +{ + doNormalize(negative, mantissa, exponent, minMantissa, maxMantissa); +} + +template <> +void +Number::normalize( + bool& negative, + unsigned long long& mantissa, + int& exponent, + internalrep const& minMantissa, + internalrep const& maxMantissa) +{ + doNormalize(negative, mantissa, exponent, minMantissa, maxMantissa); +} + template <> void Number::normalize( @@ -345,7 +375,7 @@ Number::normalize( internalrep const& minMantissa, internalrep const& maxMantissa) { - Number::normalize(negative, mantissa, exponent, minMantissa, maxMantissa); + doNormalize(negative, mantissa, exponent, minMantissa, maxMantissa); } void