Skip to content

Commit 3685667

Browse files
committed
* FASTFLOAT_HAS_BIT_CAST cleanup.
1 parent 843aa3f commit 3685667

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

include/fast_float/constexpr_feature_detect.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232

3333
// C++20 std::bit_cast
3434
#if defined(__cpp_lib_bit_cast) && __cpp_lib_bit_cast >= 201806L
35-
#include <bit>
3635
#define FASTFLOAT_HAS_BIT_CAST 1
3736
#else
3837
#define FASTFLOAT_HAS_BIT_CAST 0

include/fast_float/digit_comparison.h

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,12 @@ to_extended(T const &value) noexcept {
6464
constexpr equiv_uint mantissa_mask = binary_format<T>::mantissa_mask();
6565
constexpr equiv_uint hidden_bit_mask = binary_format<T>::hidden_bit_mask();
6666

67-
adjusted_mantissa am;
6867
constexpr am_pow_t bias = binary_format<T>::mantissa_explicit_bits() -
6968
binary_format<T>::minimum_exponent();
70-
equiv_uint bits;
71-
#if FASTFLOAT_HAS_BIT_CAST
72-
bits =
73-
#if FASTFLOAT_HAS_BIT_CAST == 1
74-
std::
75-
#endif
76-
bit_cast<equiv_uint>(value);
77-
#else
78-
::memcpy(&bits, &value, sizeof(T));
79-
#endif
69+
70+
equiv_uint const bits = bit_cast<T, equiv_uint>(value);
71+
72+
adjusted_mantissa am;
8073
if ((bits & exponent_mask) == 0) {
8174
// denormal
8275
am.power2 = 1 - bias;

include/fast_float/float_common.h

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,23 @@ using parse_options = parse_options_t<char>;
101101
#include <bit>
102102
#endif
103103

104+
namespace fast_float {
105+
template <typename To, typename From>
106+
FASTFLOAT_CONSTEXPR20 To bit_cast(const From &from) {
107+
#if FASTFLOAT_HAS_BIT_CAST
108+
return std::bit_cast<To>(from);
109+
#else
110+
// Implementation of std::bit_cast for pre-C++20.
111+
static_assert(sizeof(To) == sizeof(From),
112+
"bit_cast requires source and destination to be the same size");
113+
auto to = To();
114+
// The cast suppresses a bogus -Wclass-memaccess on GCC.
115+
std::memcpy(static_cast<void *>(&to), &from, sizeof(to));
116+
return to;
117+
#endif
118+
}
119+
} // namespace fast_float
120+
104121
#if (defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
105122
defined(__amd64) || defined(__aarch64__) || defined(_M_ARM64) || \
106123
defined(__MINGW64__) || defined(__s390x__) || \
@@ -1029,6 +1046,7 @@ binary_format<double>::hidden_bit_mask() {
10291046
return 0x0010000000000000;
10301047
}
10311048

1049+
// Fix for C2672: Ensure bit_cast is called with explicit template arguments
10321050
template <typename T>
10331051
fastfloat_really_inline FASTFLOAT_CONSTEXPR20 void to_float(
10341052
#ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN
@@ -1043,15 +1061,7 @@ fastfloat_really_inline FASTFLOAT_CONSTEXPR20 void to_float(
10431061
word =
10441062
equiv_uint(word | equiv_uint(negative) << binary_format<T>::sign_index());
10451063
#endif
1046-
#if FASTFLOAT_HAS_BIT_CAST
1047-
value =
1048-
#if FASTFLOAT_HAS_BIT_CAST == 1
1049-
std::
1050-
#endif
1051-
bit_cast<T>(word);
1052-
#else
1053-
::memcpy(&value, &word, sizeof(T));
1054-
#endif
1064+
value = bit_cast<T, equiv_uint>(word);
10551065
}
10561066

10571067
#ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN

0 commit comments

Comments
 (0)