mirror of
https://github.com/fastfloat/fast_float.git
synced 2025-12-06 08:46:49 +08:00
Compare commits
2 Commits
d46d5b18d8
...
f98c5e5f88
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f98c5e5f88 | ||
|
|
3685667dd2 |
@ -32,7 +32,6 @@
|
|||||||
|
|
||||||
// C++20 std::bit_cast
|
// C++20 std::bit_cast
|
||||||
#if defined(__cpp_lib_bit_cast) && __cpp_lib_bit_cast >= 201806L
|
#if defined(__cpp_lib_bit_cast) && __cpp_lib_bit_cast >= 201806L
|
||||||
#include <bit>
|
|
||||||
#define FASTFLOAT_HAS_BIT_CAST 1
|
#define FASTFLOAT_HAS_BIT_CAST 1
|
||||||
#else
|
#else
|
||||||
#define FASTFLOAT_HAS_BIT_CAST 0
|
#define FASTFLOAT_HAS_BIT_CAST 0
|
||||||
|
|||||||
@ -64,19 +64,12 @@ to_extended(T const &value) noexcept {
|
|||||||
constexpr equiv_uint mantissa_mask = binary_format<T>::mantissa_mask();
|
constexpr equiv_uint mantissa_mask = binary_format<T>::mantissa_mask();
|
||||||
constexpr equiv_uint hidden_bit_mask = binary_format<T>::hidden_bit_mask();
|
constexpr equiv_uint hidden_bit_mask = binary_format<T>::hidden_bit_mask();
|
||||||
|
|
||||||
adjusted_mantissa am;
|
|
||||||
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();
|
||||||
equiv_uint bits;
|
|
||||||
#if FASTFLOAT_HAS_BIT_CAST
|
equiv_uint const bits = bit_cast<T, equiv_uint>(value);
|
||||||
bits =
|
|
||||||
#if FASTFLOAT_HAS_BIT_CAST == 1
|
adjusted_mantissa am;
|
||||||
std::
|
|
||||||
#endif
|
|
||||||
bit_cast<equiv_uint>(value);
|
|
||||||
#else
|
|
||||||
::memcpy(&bits, &value, sizeof(T));
|
|
||||||
#endif
|
|
||||||
if ((bits & exponent_mask) == 0) {
|
if ((bits & exponent_mask) == 0) {
|
||||||
// denormal
|
// denormal
|
||||||
am.power2 = 1 - bias;
|
am.power2 = 1 - bias;
|
||||||
|
|||||||
@ -101,6 +101,23 @@ using parse_options = parse_options_t<char>;
|
|||||||
#include <bit>
|
#include <bit>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
namespace fast_float {
|
||||||
|
template <typename To, typename From>
|
||||||
|
FASTFLOAT_CONSTEXPR20 To bit_cast(const From &from) {
|
||||||
|
#if FASTFLOAT_HAS_BIT_CAST
|
||||||
|
return std::bit_cast<To>(from);
|
||||||
|
#else
|
||||||
|
// Implementation of std::bit_cast for pre-C++20.
|
||||||
|
static_assert(sizeof(To) == sizeof(From),
|
||||||
|
"bit_cast requires source and destination to be the same size");
|
||||||
|
auto to = To();
|
||||||
|
// The cast suppresses a bogus -Wclass-memaccess on GCC.
|
||||||
|
std::memcpy(static_cast<void *>(&to), &from, sizeof(to));
|
||||||
|
return to;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
} // namespace fast_float
|
||||||
|
|
||||||
#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) || \
|
||||||
defined(__MINGW64__) || defined(__s390x__) || \
|
defined(__MINGW64__) || defined(__s390x__) || \
|
||||||
@ -1029,6 +1046,7 @@ binary_format<double>::hidden_bit_mask() {
|
|||||||
return 0x0010000000000000;
|
return 0x0010000000000000;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fix for C2672: Ensure bit_cast is called with explicit template arguments
|
||||||
template <typename T>
|
template <typename T>
|
||||||
fastfloat_really_inline FASTFLOAT_CONSTEXPR20 void to_float(
|
fastfloat_really_inline FASTFLOAT_CONSTEXPR20 void to_float(
|
||||||
#ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN
|
#ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN
|
||||||
@ -1043,15 +1061,7 @@ fastfloat_really_inline FASTFLOAT_CONSTEXPR20 void to_float(
|
|||||||
word =
|
word =
|
||||||
equiv_uint(word | equiv_uint(negative) << binary_format<T>::sign_index());
|
equiv_uint(word | equiv_uint(negative) << binary_format<T>::sign_index());
|
||||||
#endif
|
#endif
|
||||||
#if FASTFLOAT_HAS_BIT_CAST
|
value = bit_cast<T, equiv_uint>(word);
|
||||||
value =
|
|
||||||
#if FASTFLOAT_HAS_BIT_CAST == 1
|
|
||||||
std::
|
|
||||||
#endif
|
|
||||||
bit_cast<T>(word);
|
|
||||||
#else
|
|
||||||
::memcpy(&value, &word, sizeof(T));
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN
|
#ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user