Keep only one implementation of make_float()

This commit is contained in:
Benoit Blanchon
2023-02-22 14:35:05 +01:00
parent 886254c41e
commit 319ececf26
2 changed files with 18 additions and 31 deletions

View File

@ -29,21 +29,6 @@ struct FloatTraits<T, 8 /*64bits*/> {
typedef int16_t exponent_type;
static const exponent_type exponent_max = 308;
template <typename TExponent>
static T make_float(T m, TExponent e) {
auto powersOfTen =
e > 0 ? positiveBinaryPowersOfTen() : negativeBinaryPowersOfTen();
if (e <= 0)
e = TExponent(-e);
for (uint8_t index = 0; e != 0; index++) {
if (e & 1)
m *= powersOfTen[index];
e >>= 1;
}
return m;
}
static pgm_ptr<T> positiveBinaryPowersOfTen() {
ARDUINOJSON_DEFINE_PROGMEM_ARRAY( //
uint64_t, factors,
@ -145,21 +130,6 @@ struct FloatTraits<T, 4 /*32bits*/> {
typedef int8_t exponent_type;
static const exponent_type exponent_max = 38;
template <typename TExponent>
static T make_float(T m, TExponent e) {
auto powersOfTen =
e > 0 ? positiveBinaryPowersOfTen() : negativeBinaryPowersOfTen();
if (e <= 0)
e = TExponent(-e);
for (uint8_t index = 0; e != 0; index++) {
if (e & 1)
m *= powersOfTen[index];
e >>= 1;
}
return m;
}
static pgm_ptr<T> positiveBinaryPowersOfTen() {
ARDUINOJSON_DEFINE_PROGMEM_ARRAY(uint32_t, factors,
{
@ -252,4 +222,21 @@ struct FloatTraits<T, 4 /*32bits*/> {
}
};
template <typename TFloat, typename TExponent>
inline TFloat make_float(TFloat m, TExponent e) {
using traits = FloatTraits<TFloat>;
auto powersOfTen = e > 0 ? traits::positiveBinaryPowersOfTen()
: traits::negativeBinaryPowersOfTen();
if (e <= 0)
e = TExponent(-e);
for (uint8_t index = 0; e != 0; index++) {
if (e & 1)
m *= powersOfTen[index];
e >>= 1;
}
return m;
}
ARDUINOJSON_END_PRIVATE_NAMESPACE

View File

@ -137,7 +137,7 @@ inline bool parseNumber(const char* s, VariantData& result) {
return false;
JsonFloat final_result =
traits::make_float(static_cast<JsonFloat>(mantissa), exponent);
make_float(static_cast<JsonFloat>(mantissa), exponent);
result.setFloat(is_negative ? -final_result : final_result);
return true;