mirror of
https://github.com/fastfloat/fast_float.git
synced 2025-12-07 01:06:48 +08:00
Merge pull request #118 from pitrou/issue-117
Fix #117: compilation warning with gcc 6.3.0
This commit is contained in:
commit
62a8dba55a
@ -44,13 +44,14 @@ fastfloat_really_inline int32_t scientific_exponent(parsed_number_string& num) n
|
|||||||
// this converts a native floating-point number to an extended-precision float.
|
// this converts a native floating-point number to an extended-precision float.
|
||||||
template <typename T>
|
template <typename T>
|
||||||
fastfloat_really_inline adjusted_mantissa to_extended(T value) noexcept {
|
fastfloat_really_inline adjusted_mantissa to_extended(T value) noexcept {
|
||||||
|
using equiv_uint = typename binary_format<T>::equiv_uint;
|
||||||
|
constexpr equiv_uint exponent_mask = binary_format<T>::exponent_mask();
|
||||||
|
constexpr equiv_uint mantissa_mask = binary_format<T>::mantissa_mask();
|
||||||
|
constexpr equiv_uint hidden_bit_mask = binary_format<T>::hidden_bit_mask();
|
||||||
|
|
||||||
adjusted_mantissa am;
|
adjusted_mantissa am;
|
||||||
int32_t bias = binary_format<T>::mantissa_explicit_bits() - binary_format<T>::minimum_exponent();
|
int32_t bias = binary_format<T>::mantissa_explicit_bits() - binary_format<T>::minimum_exponent();
|
||||||
if (std::is_same<T, float>::value) {
|
equiv_uint bits;
|
||||||
constexpr uint32_t exponent_mask = 0x7F800000;
|
|
||||||
constexpr uint32_t mantissa_mask = 0x007FFFFF;
|
|
||||||
constexpr uint64_t hidden_bit_mask = 0x00800000;
|
|
||||||
uint32_t bits;
|
|
||||||
::memcpy(&bits, &value, sizeof(T));
|
::memcpy(&bits, &value, sizeof(T));
|
||||||
if ((bits & exponent_mask) == 0) {
|
if ((bits & exponent_mask) == 0) {
|
||||||
// denormal
|
// denormal
|
||||||
@ -62,23 +63,6 @@ fastfloat_really_inline adjusted_mantissa to_extended(T value) noexcept {
|
|||||||
am.power2 -= bias;
|
am.power2 -= bias;
|
||||||
am.mantissa = (bits & mantissa_mask) | hidden_bit_mask;
|
am.mantissa = (bits & mantissa_mask) | hidden_bit_mask;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
constexpr uint64_t exponent_mask = 0x7FF0000000000000;
|
|
||||||
constexpr uint64_t mantissa_mask = 0x000FFFFFFFFFFFFF;
|
|
||||||
constexpr uint64_t hidden_bit_mask = 0x0010000000000000;
|
|
||||||
uint64_t bits;
|
|
||||||
::memcpy(&bits, &value, sizeof(T));
|
|
||||||
if ((bits & exponent_mask) == 0) {
|
|
||||||
// denormal
|
|
||||||
am.power2 = 1 - bias;
|
|
||||||
am.mantissa = bits & mantissa_mask;
|
|
||||||
} else {
|
|
||||||
// normal
|
|
||||||
am.power2 = int32_t((bits & exponent_mask) >> binary_format<T>::mantissa_explicit_bits());
|
|
||||||
am.power2 -= bias;
|
|
||||||
am.mantissa = (bits & mantissa_mask) | hidden_bit_mask;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return am;
|
return am;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,6 +5,7 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
#if (defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) \
|
#if (defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) \
|
||||||
|| defined(__amd64) || defined(__aarch64__) || defined(_M_ARM64) \
|
|| defined(__amd64) || defined(__aarch64__) || defined(_M_ARM64) \
|
||||||
@ -219,6 +220,8 @@ constexpr static float powers_of_ten_float[] = {1e0, 1e1, 1e2, 1e3, 1e4, 1e5,
|
|||||||
1e6, 1e7, 1e8, 1e9, 1e10};
|
1e6, 1e7, 1e8, 1e9, 1e10};
|
||||||
|
|
||||||
template <typename T> struct binary_format {
|
template <typename T> struct binary_format {
|
||||||
|
using equiv_uint = typename std::conditional<sizeof(T) == 4, uint32_t, uint64_t>::type;
|
||||||
|
|
||||||
static inline constexpr int mantissa_explicit_bits();
|
static inline constexpr int mantissa_explicit_bits();
|
||||||
static inline constexpr int minimum_exponent();
|
static inline constexpr int minimum_exponent();
|
||||||
static inline constexpr int infinite_power();
|
static inline constexpr int infinite_power();
|
||||||
@ -232,6 +235,9 @@ template <typename T> struct binary_format {
|
|||||||
static inline constexpr int smallest_power_of_ten();
|
static inline constexpr int smallest_power_of_ten();
|
||||||
static inline constexpr T exact_power_of_ten(int64_t power);
|
static inline constexpr T exact_power_of_ten(int64_t power);
|
||||||
static inline constexpr size_t max_digits();
|
static inline constexpr size_t max_digits();
|
||||||
|
static inline constexpr equiv_uint exponent_mask();
|
||||||
|
static inline constexpr equiv_uint mantissa_mask();
|
||||||
|
static inline constexpr equiv_uint hidden_bit_mask();
|
||||||
};
|
};
|
||||||
|
|
||||||
template <> inline constexpr int binary_format<double>::mantissa_explicit_bits() {
|
template <> inline constexpr int binary_format<double>::mantissa_explicit_bits() {
|
||||||
@ -339,6 +345,33 @@ template <> inline constexpr size_t binary_format<float>::max_digits() {
|
|||||||
return 114;
|
return 114;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <> inline constexpr binary_format<float>::equiv_uint
|
||||||
|
binary_format<float>::exponent_mask() {
|
||||||
|
return 0x7F800000;
|
||||||
|
}
|
||||||
|
template <> inline constexpr binary_format<double>::equiv_uint
|
||||||
|
binary_format<double>::exponent_mask() {
|
||||||
|
return 0x7FF0000000000000;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <> inline constexpr binary_format<float>::equiv_uint
|
||||||
|
binary_format<float>::mantissa_mask() {
|
||||||
|
return 0x007FFFFF;
|
||||||
|
}
|
||||||
|
template <> inline constexpr binary_format<double>::equiv_uint
|
||||||
|
binary_format<double>::mantissa_mask() {
|
||||||
|
return 0x000FFFFFFFFFFFFF;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <> inline constexpr binary_format<float>::equiv_uint
|
||||||
|
binary_format<float>::hidden_bit_mask() {
|
||||||
|
return 0x00800000;
|
||||||
|
}
|
||||||
|
template <> inline constexpr binary_format<double>::equiv_uint
|
||||||
|
binary_format<double>::hidden_bit_mask() {
|
||||||
|
return 0x0010000000000000;
|
||||||
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
fastfloat_really_inline void to_float(bool negative, adjusted_mantissa am, T &value) {
|
fastfloat_really_inline void to_float(bool negative, adjusted_mantissa am, T &value) {
|
||||||
uint64_t word = am.mantissa;
|
uint64_t word = am.mantissa;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user