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.
This commit is contained in:
Ed Hennis
2025-12-10 16:06:54 -05:00
parent 5400381cac
commit 254d325a3e
2 changed files with 45 additions and 17 deletions

View File

@@ -327,6 +327,15 @@ private:
internalrep const& minMantissa,
internalrep const& maxMantissa);
template <class T>
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<unsigned long>(
bool& negative,
unsigned long& mantissa,
int& exponent,
internalrep const& minMantissa,
internalrep const& maxMantissa);
template <class T>
std::pair<T, int>
Number::normalizeToRange(T minMantissa, T maxMantissa) const

View File

@@ -245,13 +245,19 @@ Number::one()
// TODO: Rename the function parameters to get rid of the "_" suffix
template <class T>
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<uint128_t>(
bool& negative,
uint128_t& mantissa,
int& exponent,
internalrep const& minMantissa,
internalrep const& maxMantissa)
{
doNormalize(negative, mantissa, exponent, minMantissa, maxMantissa);
}
template <>
void
Number::normalize<unsigned long long>(
bool& negative,
unsigned long long& mantissa,
int& exponent,
internalrep const& minMantissa,
internalrep const& maxMantissa)
{
doNormalize(negative, mantissa, exponent, minMantissa, maxMantissa);
}
template <>
void
Number::normalize<unsigned long>(
@@ -345,7 +375,7 @@ Number::normalize<unsigned long>(
internalrep const& minMantissa,
internalrep const& maxMantissa)
{
Number::normalize(negative, mantissa, exponent, minMantissa, maxMantissa);
doNormalize(negative, mantissa, exponent, minMantissa, maxMantissa);
}
void