From 6d2fb68f5c928f75642762b235875cfbbd807687 Mon Sep 17 00:00:00 2001 From: Lenard Szolnoki Date: Wed, 1 Mar 2023 23:25:20 +0000 Subject: [PATCH] Simplify to_float * Use right-sized uint type for bit fiddling ** This removes the need to special casing on endianness * Replace ternary with just shifting the sign at the right place ** This seems to improve codegen (less instructions, no cmov) --- include/fast_float/float_common.h | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/include/fast_float/float_common.h b/include/fast_float/float_common.h index 65302e0..1e927dc 100644 --- a/include/fast_float/float_common.h +++ b/include/fast_float/float_common.h @@ -443,20 +443,11 @@ template <> inline constexpr binary_format::equiv_uint template fastfloat_really_inline void to_float(bool negative, adjusted_mantissa am, T &value) { - uint64_t word = am.mantissa; - word |= uint64_t(am.power2) << binary_format::mantissa_explicit_bits(); - word = negative - ? word | (uint64_t(1) << binary_format::sign_index()) : word; -#if FASTFLOAT_IS_BIG_ENDIAN == 1 - if (std::is_same::value) { - ::memcpy(&value, (char *)&word + 4, sizeof(T)); // extract value at offset 4-7 if float on big-endian - } else { - ::memcpy(&value, &word, sizeof(T)); - } -#else - // For little-endian systems: - ::memcpy(&value, &word, sizeof(T)); -#endif + using uint = typename binary_format::equiv_uint; + uint word = (uint)am.mantissa; + word |= uint(am.power2) << binary_format::mantissa_explicit_bits(); + word |= uint(negative) << binary_format::sign_index(); + ::memcpy(&value, &word, sizeof(T)); } #if FASTFLOAT_SKIP_WHITE_SPACE // disabled by default