mirror of
https://github.com/fastfloat/fast_float.git
synced 2025-12-06 16:56:57 +08:00
make it build
This commit is contained in:
parent
d6746f073e
commit
50e55c0327
@ -72,7 +72,7 @@ to_extended(T value) noexcept {
|
|||||||
binary_format<T>::minimum_exponent();
|
binary_format<T>::minimum_exponent();
|
||||||
equiv_uint bits;
|
equiv_uint bits;
|
||||||
#if FASTFLOAT_HAS_BIT_CAST
|
#if FASTFLOAT_HAS_BIT_CAST
|
||||||
bits = std::bit_cast<equiv_uint>(value);
|
bits = bit_cast<equiv_uint>(value);
|
||||||
#else
|
#else
|
||||||
::memcpy(&bits, &value, sizeof(T));
|
::memcpy(&bits, &value, sizeof(T));
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -212,6 +212,89 @@ fastfloat_really_inline constexpr bool is_supported_float_type() {
|
|||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
union float_union {
|
||||||
|
float f;
|
||||||
|
uint32_t bits;
|
||||||
|
};
|
||||||
|
|
||||||
|
union double_union {
|
||||||
|
double f;
|
||||||
|
uint64_t bits;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T, typename U>
|
||||||
|
constexpr T bit_cast(const U &u);
|
||||||
|
|
||||||
|
template<>
|
||||||
|
fastfloat_really_inline constexpr float bit_cast(const uint32_t &u) {
|
||||||
|
float_union fu;
|
||||||
|
fu.bits = u;
|
||||||
|
return fu.f;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
fastfloat_really_inline constexpr double bit_cast(const uint64_t &u) {
|
||||||
|
double_union fu;
|
||||||
|
fu.bits = u;
|
||||||
|
return fu.f;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
fastfloat_really_inline constexpr uint32_t bit_cast(const float &u) {
|
||||||
|
float_union fu;
|
||||||
|
fu.f = u;
|
||||||
|
return fu.bits;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
fastfloat_really_inline constexpr uint64_t bit_cast(const double &u) {
|
||||||
|
double_union fu;
|
||||||
|
fu.f = u;
|
||||||
|
return fu.bits;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef __STDCPP_FLOAT16_T__
|
||||||
|
union float16_union {
|
||||||
|
std::float16_t f;
|
||||||
|
uint16_t bits;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
fastfloat_really_inline constexpr uint16_t bit_cast(const std::float16_t &u) {
|
||||||
|
float16_union fu;
|
||||||
|
fu.f = u;
|
||||||
|
return fu.bits;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
fastfloat_really_inline constexpr std::float16_t bit_cast(const uint16_t &u) {
|
||||||
|
float16_union fu;
|
||||||
|
fu.bits = u;
|
||||||
|
return fu.f;
|
||||||
|
}
|
||||||
|
#endif // __STDCPP_FLOAT16_T__
|
||||||
|
#ifdef __STDCPP_BFLOAT16_T__
|
||||||
|
union bfloat16_union {
|
||||||
|
std::bfloat16_t f;
|
||||||
|
uint16_t bits;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
fastfloat_really_inline constexpr uint16_t bit_cast(const std::bfloat16_t &u) {
|
||||||
|
bfloat16_union fu;
|
||||||
|
fu.f = u;
|
||||||
|
return fu.bits;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
fastfloat_really_inline constexpr std::bfloat16_t bit_cast(const uint16_t &u) {
|
||||||
|
bfloat16_union fu;
|
||||||
|
fu.bits = u;
|
||||||
|
return fu.f;
|
||||||
|
}
|
||||||
|
#endif // __STDCPP_BFLOAT16_T__
|
||||||
|
|
||||||
|
|
||||||
template <typename UC>
|
template <typename UC>
|
||||||
fastfloat_really_inline constexpr bool is_supported_char_type() {
|
fastfloat_really_inline constexpr bool is_supported_char_type() {
|
||||||
return std::is_same<UC, char>::value || std::is_same<UC, wchar_t>::value ||
|
return std::is_same<UC, char>::value || std::is_same<UC, wchar_t>::value ||
|
||||||
@ -384,7 +467,7 @@ template <typename T, typename U = void> struct binary_format_lookup_tables;
|
|||||||
|
|
||||||
template <typename T> struct binary_format : binary_format_lookup_tables<T> {
|
template <typename T> struct binary_format : binary_format_lookup_tables<T> {
|
||||||
using equiv_uint =
|
using equiv_uint =
|
||||||
typename std::conditional<sizeof(T) == 4, uint32_t, uint64_t>::type;
|
typename std::conditional<sizeof(T) == 2, uint16_t, typename std::conditional<sizeof(T) == 4, uint32_t, uint64_t>::type>::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();
|
||||||
@ -566,10 +649,9 @@ inline constexpr uint64_t binary_format<double>::max_mantissa_fast_path() {
|
|||||||
|
|
||||||
// credit: Jakub Jelínek
|
// credit: Jakub Jelínek
|
||||||
#ifdef __STDCPP_FLOAT16_T__
|
#ifdef __STDCPP_FLOAT16_T__
|
||||||
|
|
||||||
template <typename U> struct binary_format_lookup_tables<std::float16_t, U> {
|
template <typename U> struct binary_format_lookup_tables<std::float16_t, U> {
|
||||||
static constexpr std::float16_t powers_of_ten[] = {};
|
static constexpr std::float16_t powers_of_ten[] = {1};// todo: fix this
|
||||||
static constexpr uint64_t max_mantissa[] = {};
|
static constexpr uint64_t max_mantissa[] = {1};// todo: fix this
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename U>
|
template <typename U>
|
||||||
@ -580,6 +662,30 @@ template <typename U>
|
|||||||
constexpr uint64_t
|
constexpr uint64_t
|
||||||
binary_format_lookup_tables<std::float16_t, U>::max_mantissa[];
|
binary_format_lookup_tables<std::float16_t, U>::max_mantissa[];
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline constexpr std::float16_t
|
||||||
|
binary_format<std::float16_t>::exact_power_of_ten(int64_t power) {
|
||||||
|
return powers_of_ten[power];
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline constexpr binary_format<std::float16_t>::equiv_uint
|
||||||
|
binary_format<std::float16_t>::exponent_mask() {
|
||||||
|
return 0x7C00;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline constexpr binary_format<std::float16_t>::equiv_uint
|
||||||
|
binary_format<std::float16_t>::mantissa_mask() {
|
||||||
|
return 0x03FF;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline constexpr binary_format<std::float16_t>::equiv_uint
|
||||||
|
binary_format<std::float16_t>::hidden_bit_mask() {
|
||||||
|
return 0x0400;
|
||||||
|
}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
inline constexpr int binary_format<std::float16_t>::max_exponent_fast_path() {
|
inline constexpr int binary_format<std::float16_t>::max_exponent_fast_path() {
|
||||||
return 0;
|
return 0;
|
||||||
@ -591,6 +697,12 @@ binary_format<std::float16_t>::max_mantissa_fast_path() {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline constexpr uint64_t
|
||||||
|
binary_format<std::float16_t>::max_mantissa_fast_path(int64_t power) {
|
||||||
|
return max_mantissa[power];
|
||||||
|
}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
inline constexpr int binary_format<std::float16_t>::min_exponent_fast_path() {
|
inline constexpr int binary_format<std::float16_t>::min_exponent_fast_path() {
|
||||||
return 0;
|
return 0;
|
||||||
@ -640,11 +752,10 @@ template <> constexpr size_t binary_format<std::float16_t>::max_digits() {
|
|||||||
|
|
||||||
// credit: Jakub Jelínek
|
// credit: Jakub Jelínek
|
||||||
#ifdef __STDCPP_BFLOAT16_T__
|
#ifdef __STDCPP_BFLOAT16_T__
|
||||||
|
|
||||||
template <typename U> struct binary_format_lookup_tables<std::bfloat16_t, U> {
|
template <typename U> struct binary_format_lookup_tables<std::bfloat16_t, U> {
|
||||||
static constexpr std::bfloat16_t powers_of_ten[] = {};
|
static constexpr std::bfloat16_t powers_of_ten[] = {1};// todo: fix this
|
||||||
|
|
||||||
static constexpr uint64_t max_mantissa[] = {};
|
static constexpr uint64_t max_mantissa[] = {1};// todo: fix this
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename U>
|
template <typename U>
|
||||||
@ -655,17 +766,47 @@ template <typename U>
|
|||||||
constexpr uint64_t
|
constexpr uint64_t
|
||||||
binary_format_lookup_tables<std::bfloat16_t, U>::max_mantissa[];
|
binary_format_lookup_tables<std::bfloat16_t, U>::max_mantissa[];
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline constexpr std::bfloat16_t
|
||||||
|
binary_format<std::bfloat16_t>::exact_power_of_ten(int64_t power) {
|
||||||
|
return (void)powers_of_ten[0], powers_of_ten[power];
|
||||||
|
}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
inline constexpr int binary_format<std::bfloat16_t>::max_exponent_fast_path() {
|
inline constexpr int binary_format<std::bfloat16_t>::max_exponent_fast_path() {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline constexpr binary_format<std::bfloat16_t>::equiv_uint
|
||||||
|
binary_format<std::bfloat16_t>::exponent_mask() {
|
||||||
|
return 0x7F80;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline constexpr binary_format<std::bfloat16_t>::equiv_uint
|
||||||
|
binary_format<std::bfloat16_t>::mantissa_mask() {
|
||||||
|
return 0x007F;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline constexpr binary_format<std::bfloat16_t>::equiv_uint
|
||||||
|
binary_format<std::bfloat16_t>::hidden_bit_mask() {
|
||||||
|
return 0x0080;
|
||||||
|
}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
inline constexpr uint64_t
|
inline constexpr uint64_t
|
||||||
binary_format<std::bfloat16_t>::max_mantissa_fast_path() {
|
binary_format<std::bfloat16_t>::max_mantissa_fast_path() {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline constexpr uint64_t
|
||||||
|
binary_format<std::bfloat16_t>::max_mantissa_fast_path(int64_t power) {
|
||||||
|
return max_mantissa[power];
|
||||||
|
}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
inline constexpr int binary_format<std::bfloat16_t>::min_exponent_fast_path() {
|
inline constexpr int binary_format<std::bfloat16_t>::min_exponent_fast_path() {
|
||||||
return 0;
|
return 0;
|
||||||
@ -818,6 +959,32 @@ to_float(bool negative, adjusted_mantissa am, T &value) {
|
|||||||
::memcpy(&value, &word, sizeof(T));
|
::memcpy(&value, &word, sizeof(T));
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
#ifdef __STDCPP_FLOAT16_T__
|
||||||
|
template<>
|
||||||
|
fastfloat_really_inline void
|
||||||
|
to_float<std::float16_t>(bool negative, adjusted_mantissa am,
|
||||||
|
std::float16_t &value)
|
||||||
|
{
|
||||||
|
constexpr int mantissa_bits
|
||||||
|
= binary_format<std::float16_t>::mantissa_explicit_bits();
|
||||||
|
value = bit_cast<std::float16_t> (uint16_t(am.mantissa
|
||||||
|
| (uint16_t(am.power2) << mantissa_bits)
|
||||||
|
| (negative ? 0x8000 : 0)));
|
||||||
|
}
|
||||||
|
#endif // __STDCPP_FLOAT16_T__
|
||||||
|
#ifdef __STDCPP_BFLOAT16_T__
|
||||||
|
template<>
|
||||||
|
fastfloat_really_inline void
|
||||||
|
to_float<std::bfloat16_t>(bool negative, adjusted_mantissa am,
|
||||||
|
std::bfloat16_t &value)
|
||||||
|
{
|
||||||
|
constexpr int mantissa_bits
|
||||||
|
= binary_format<std::bfloat16_t>::mantissa_explicit_bits();
|
||||||
|
value = bit_cast<std::bfloat16_t>(uint16_t(am.mantissa
|
||||||
|
| (uint16_t(am.power2) << mantissa_bits)
|
||||||
|
| (negative ? 0x8000 : 0)));
|
||||||
|
}
|
||||||
|
#endif // __STDCPP_BFLOAT16_T__
|
||||||
|
|
||||||
#ifdef FASTFLOAT_SKIP_WHITE_SPACE // disabled by default
|
#ifdef FASTFLOAT_SKIP_WHITE_SPACE // disabled by default
|
||||||
template <typename = void> struct space_lut {
|
template <typename = void> struct space_lut {
|
||||||
@ -938,6 +1105,21 @@ fastfloat_really_inline constexpr uint64_t min_safe_u64(int base) {
|
|||||||
return int_luts<>::min_safe_u64[base - 2];
|
return int_luts<>::min_safe_u64[base - 2];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static_assert(std::is_same<binary_format<double>::equiv_uint, uint64_t>::value,
|
||||||
|
"equiv_uint should be uint64_t for double");
|
||||||
|
static_assert(std::is_same<binary_format<float>::equiv_uint, uint32_t>::value,
|
||||||
|
"equiv_uint should be uint32_t for float");
|
||||||
|
#ifdef __STDCPP_FLOAT16_T__
|
||||||
|
static_assert(std::is_same<binary_format<std::float16_t>::equiv_uint, uint16_t>::value,
|
||||||
|
"equiv_uint should be uint16_t for std::float16_t");
|
||||||
|
#endif // __STDCPP_FLOAT16_T__
|
||||||
|
#ifdef __STDCPP_BFLOAT16_T__
|
||||||
|
static_assert(std::is_same<binary_format<std::bfloat16_t>::equiv_uint, uint16_t>::value,
|
||||||
|
"equiv_uint should be uint16_t for std::bfloat16_t");
|
||||||
|
#endif // __STDCPP_BFLOAT16_T__
|
||||||
|
|
||||||
} // namespace fast_float
|
} // namespace fast_float
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -122,6 +122,7 @@ int main() {
|
|||||||
}
|
}
|
||||||
std::cout << "parsed the number " << result << std::endl;
|
std::cout << "parsed the number " << result << std::endl;
|
||||||
#ifdef __STDCPP_FLOAT16_T__
|
#ifdef __STDCPP_FLOAT16_T__
|
||||||
|
printf("16-bit float\n");
|
||||||
// Parse as 16-bit float
|
// Parse as 16-bit float
|
||||||
std::float16_t parsed_16{};
|
std::float16_t parsed_16{};
|
||||||
input = "10000e-1452";
|
input = "10000e-1452";
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user