mirror of
https://github.com/fastfloat/fast_float.git
synced 2025-12-06 16:56:57 +08:00
# cleanup and speedup digit_comp.
This commit is contained in:
parent
0e123c5e82
commit
487799ede2
@ -345,18 +345,18 @@ parse_mantissa(bigint &result, const parsed_number_string_t<UC> &num) noexcept {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa positive_digit_comp(
|
fastfloat_really_inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa
|
||||||
bigint &bigmant, am_pow_t const exponent) noexcept {
|
positive_digit_comp(
|
||||||
|
bigint &bigmant, adjusted_mantissa& am, am_pow_t const exponent) noexcept {
|
||||||
FASTFLOAT_ASSERT(bigmant.pow10(exponent));
|
FASTFLOAT_ASSERT(bigmant.pow10(exponent));
|
||||||
adjusted_mantissa answer;
|
|
||||||
bool truncated;
|
bool truncated;
|
||||||
answer.mantissa = bigmant.hi64(truncated);
|
am.mantissa = bigmant.hi64(truncated);
|
||||||
constexpr am_pow_t bias = binary_format<T>::mantissa_explicit_bits() -
|
constexpr am_pow_t bias = binary_format<T>::mantissa_explicit_bits() -
|
||||||
binary_format<T>::minimum_exponent();
|
binary_format<T>::minimum_exponent();
|
||||||
answer.power2 =
|
am.power2 =
|
||||||
static_cast<fast_float::am_pow_t>(bigmant.bit_length() - 64 + bias);
|
static_cast<fast_float::am_pow_t>(bigmant.bit_length() - 64 + bias);
|
||||||
|
|
||||||
round<T>(answer, [truncated](adjusted_mantissa &a, am_pow_t shift) {
|
round<T>(am, [truncated](adjusted_mantissa &a, am_pow_t shift) {
|
||||||
round_nearest_tie_even(
|
round_nearest_tie_even(
|
||||||
a, shift,
|
a, shift,
|
||||||
[truncated](bool is_odd, bool is_halfway, bool is_above) -> bool {
|
[truncated](bool is_odd, bool is_halfway, bool is_above) -> bool {
|
||||||
@ -365,7 +365,7 @@ inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa positive_digit_comp(
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
return answer;
|
return am;
|
||||||
}
|
}
|
||||||
|
|
||||||
// the scaling here is quite simple: we have, for the real digits `m * 10^e`,
|
// the scaling here is quite simple: we have, for the real digits `m * 10^e`,
|
||||||
@ -374,8 +374,9 @@ inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa positive_digit_comp(
|
|||||||
// we then need to scale by `2^(f- e)`, and then the two significant digits
|
// we then need to scale by `2^(f- e)`, and then the two significant digits
|
||||||
// are of the same magnitude.
|
// are of the same magnitude.
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa negative_digit_comp(
|
fastfloat_really_inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa
|
||||||
bigint &bigmant, adjusted_mantissa am, am_pow_t const exponent) noexcept {
|
negative_digit_comp(
|
||||||
|
bigint &bigmant, adjusted_mantissa& am, am_pow_t const exponent) noexcept {
|
||||||
bigint &real_digits = bigmant;
|
bigint &real_digits = bigmant;
|
||||||
am_pow_t const &real_exp = exponent;
|
am_pow_t const &real_exp = exponent;
|
||||||
|
|
||||||
@ -411,7 +412,6 @@ inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa negative_digit_comp(
|
|||||||
|
|
||||||
// compare digits, and use it to direct rounding
|
// compare digits, and use it to direct rounding
|
||||||
int ord = real_digits.compare(theor_digits);
|
int ord = real_digits.compare(theor_digits);
|
||||||
adjusted_mantissa answer = am;
|
|
||||||
round<T>(am, [ord](adjusted_mantissa &a, am_pow_t shift) {
|
round<T>(am, [ord](adjusted_mantissa &a, am_pow_t shift) {
|
||||||
round_nearest_tie_even(
|
round_nearest_tie_even(
|
||||||
a, shift, [ord](bool is_odd, bool _, bool __) -> bool {
|
a, shift, [ord](bool is_odd, bool _, bool __) -> bool {
|
||||||
@ -427,7 +427,7 @@ inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa negative_digit_comp(
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
return answer;
|
return am;
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse the significant digits as a big integer to unambiguously round
|
// parse the significant digits as a big integer to unambiguously round
|
||||||
@ -445,7 +445,7 @@ inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa negative_digit_comp(
|
|||||||
// of both, and use that to direct rounding.
|
// of both, and use that to direct rounding.
|
||||||
template <typename T, typename UC>
|
template <typename T, typename UC>
|
||||||
inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa digit_comp(
|
inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa digit_comp(
|
||||||
parsed_number_string_t<UC> const &num, adjusted_mantissa am) noexcept {
|
parsed_number_string_t<UC> const &num, adjusted_mantissa& am) noexcept {
|
||||||
// remove the invalid exponent bias
|
// remove the invalid exponent bias
|
||||||
am.power2 -= invalid_am_bias;
|
am.power2 -= invalid_am_bias;
|
||||||
|
|
||||||
@ -456,7 +456,7 @@ inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa digit_comp(
|
|||||||
am_pow_t const exponent =
|
am_pow_t const exponent =
|
||||||
static_cast<am_pow_t>(sci_exp + 1 - static_cast<am_pow_t>(digits));
|
static_cast<am_pow_t>(sci_exp + 1 - static_cast<am_pow_t>(digits));
|
||||||
if (exponent >= 0) {
|
if (exponent >= 0) {
|
||||||
return positive_digit_comp<T>(bigmant, exponent);
|
return positive_digit_comp<T>(bigmant, am, exponent);
|
||||||
} else {
|
} else {
|
||||||
return negative_digit_comp<T>(bigmant, am, exponent);
|
return negative_digit_comp<T>(bigmant, am, exponent);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user