Skip to content

Commit 96c0059

Browse files
committed
Revised implementation of float32_t and float64_t support as template specializations, added test
1 parent ae99db4 commit 96c0059

3 files changed

Lines changed: 86 additions & 1099 deletions

File tree

include/fast_float/parse_number.h

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
#include <cstring>
1111
#include <limits>
1212
#include <system_error>
13-
#if __cplusplus >= 202300L || _MSVC_LANG >= 202300L
14-
#include <stdfloat>
13+
#ifdef __has_include
14+
#if __has_include(<stdfloat>)
15+
#include <stdfloat>
16+
#endif
1517
#endif
1618
namespace fast_float {
1719

@@ -135,33 +137,59 @@ fastfloat_really_inline bool rounds_to_nearest() noexcept {
135137

136138
} // namespace detail
137139

138-
template<typename T, typename UC>
139-
FASTFLOAT_CONSTEXPR20
140-
from_chars_result_t<UC> from_chars(UC const * first, UC const * last,
141-
T &value, chars_format fmt /*= chars_format::general*/) noexcept {
142-
#if __cplusplus >= 202300L || _MSVC_LANG >= 202300L
143-
#ifdef __STDCPP_FLOAT32_T__
144-
// if std::float32_t is defined, and we are in C++23 mode; macro set for float32;
145-
// set value to float due to equivalence between float and float32_t
146-
if(std::is_same<T, std::float32_t>::value){
147-
float value32;
148-
from_chars_result_t<UC> ret = from_chars_advanced(first, last, value32, parse_options_t<UC>{fmt});
149-
value = value32;
140+
template <typename T>
141+
struct from_chars_caller
142+
{
143+
template <typename UC>
144+
FASTFLOAT_CONSTEXPR20
145+
static from_chars_result_t<UC> call(UC const * first, UC const * last,
146+
T &value, parse_options_t<UC> options) noexcept {
147+
return from_chars_advanced(first, last, value, options);
148+
}
149+
};
150+
151+
#if __STDCPP_FLOAT32_T__ == 1
152+
template <>
153+
struct from_chars_caller<std::float32_t>
154+
{
155+
template <typename UC>
156+
FASTFLOAT_CONSTEXPR20
157+
static from_chars_result_t<UC> call(UC const * first, UC const * last,
158+
std::float32_t &value, parse_options_t<UC> options) noexcept{
159+
// if std::float32_t is defined, and we are in C++23 mode; macro set for float32;
160+
// set value to float due to equivalence between float and float32_t
161+
float val;
162+
auto ret = from_chars_advanced(first, last, val, options);
163+
value = val;
150164
return ret;
151165
}
152-
#endif
153-
#ifdef __STDCPP_FLOAT64_T__
154-
// if std::float64_t is defined, and we are in C++23 mode; macro set for float64;
155-
// set value as double due to equivalence between double and float64_t
156-
if(std::is_same<T, std::float64_t>::value){
157-
double value64;
158-
from_chars_result_t<UC> ret = from_chars_advanced(first, last, value64, parse_options_t<UC>{fmt});
159-
value = value64;
166+
};
167+
#endif
168+
169+
#if __STDCPP_FLOAT64_T__ == 1
170+
template <>
171+
struct from_chars_caller<std::float64_t>
172+
{
173+
template <typename UC>
174+
FASTFLOAT_CONSTEXPR20
175+
static from_chars_result_t<UC> call(UC const * first, UC const * last,
176+
std::float64_t &value, parse_options_t<UC> options) noexcept{
177+
// if std::float64_t is defined, and we are in C++23 mode; macro set for float64;
178+
// set value as double due to equivalence between double and float64_t
179+
double val;
180+
auto ret = from_chars_advanced(first, last, val, options);
181+
value = val;
160182
return ret;
161183
}
162-
#endif
184+
};
163185
#endif
164-
return from_chars_advanced(first, last, value, parse_options_t<UC>{fmt});
186+
187+
188+
template<typename T, typename UC>
189+
FASTFLOAT_CONSTEXPR20
190+
from_chars_result_t<UC> from_chars(UC const * first, UC const * last,
191+
T &value, chars_format fmt /*= chars_format::general*/) noexcept {
192+
return from_chars_caller<T>::call(first, last, value, parse_options_t<UC>(fmt));
165193
}
166194

167195
template<typename T, typename UC>

tests/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,13 @@ else()
6868
target_compile_features(basictest PRIVATE cxx_std_17)
6969
endif()
7070

71-
7271
fast_float_add_cpp_test(long_test)
7372
fast_float_add_cpp_test(powersoffive_hardround)
7473
fast_float_add_cpp_test(string_test)
7574

7675
fast_float_add_cpp_test(json_fmt)
7776
fast_float_add_cpp_test(fortran)
7877

79-
8078
option(FASTFLOAT_FIXEDWIDTH_TESTS "Require fixed width test for C++23 (build will fail if the compiler won't support it)" OFF)
8179
if (FASTFLOAT_FIXEDWIDTH_TESTS)
8280
fast_float_add_cpp_test(fixedwidthtest)
@@ -97,7 +95,5 @@ if (FASTFLOAT_EXHAUSTIVE)
9795
fast_float_add_cpp_test(random64)
9896
endif(FASTFLOAT_EXHAUSTIVE)
9997

100-
101-
10298
add_subdirectory(build_tests)
10399
add_subdirectory(bloat_analysis)

0 commit comments

Comments
 (0)