Skip to content

Commit

Permalink
Fall back to constexpr append for floating point
Browse files Browse the repository at this point in the history
Use macros to check if the standard library version supports std::to_char
for floating point, otherwise fall back to the constexpr append.

We do need to specifically check the library version, NOT the compiler
version. At least on compilers that will let you use a different library
like GCC and Clang. clang-cl is a possibility, but it will pretend to be
MSVC. This is why we check for libc++ before checking _MSC_VER.
  • Loading branch information
CrustyAuklet committed Apr 25, 2024
1 parent 151c9d0 commit 36090de
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/snitch_append.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ namespace snitch::impl {
namespace {
using snitch::small_string_span;

// libstdc++11 or greater
// libc++14 or greater
// MSVC 19.24 or greater (AKA 2019 16.4)
// Apple clang has no support to date
#if (defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE > 11) || \
(defined(_LIBCPP_VERSION) && _LIBCPP_VERSION > 14000) || \
(defined(_MSC_VER) && _MSC_VER > 1924)
template<std::floating_point T>
bool append_to_chars(small_string_span ss, T value) noexcept {
constexpr auto fmt = std::chars_format::scientific;
Expand All @@ -29,6 +36,12 @@ bool append_to_chars(small_string_span ss, T value) noexcept {
ss.grow(end - ss.end());
return true;
}
#else
template<std::floating_point T>
bool append_to_chars(small_string_span ss, T value) noexcept {
return append_constexpr(ss, value);
}
#endif

template<std::integral T>
bool append_to_chars(small_string_span ss, T value, int base = 10) noexcept {
Expand Down
5 changes: 2 additions & 3 deletions tests/runtime_tests/string_utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ TEST_CASE("append floats", "[utility]") {
#else
// Without std::bit_cast (or C++23), we are unable to tell the difference between -0.0f and
// +0.0f in constexpr expressions. Therefore -0.0f in constexpr gets displayed as +0.0f.
CONSTEXPR_CHECK(a(-0.0f) == aed{{"0.000000e+00"sv, true}, {"-0.000000e+00"sv, true}});
CONSTEXPR_CHECK(a(-0.0f) == ae{"0.000000e+00"sv, true});
#endif
CONSTEXPR_CHECK(a(1.0f) == ae{"1.000000e+00"sv, true});
CONSTEXPR_CHECK(a(1.5f) == ae{"1.500000e+00"sv, true});
Expand Down Expand Up @@ -471,8 +471,7 @@ TEST_CASE("append doubles", "[utility]") {
#else
// Without std::bit_cast (or C++23), we are unable to tell the difference between -0.0f and
// +0.0f in constexpr expressions. Therefore -0.0f in constexpr gets displayed as +0.0f.
CONSTEXPR_CHECK(
a(-0.0) == aed{{"0.000000000000000e+00"sv, true}, {"-0.000000000000000e+00"sv, true}});
CONSTEXPR_CHECK(a(-0.0) == ae{"0.000000000000000e+00"sv, true});
#endif
CONSTEXPR_CHECK(a(1.0) == ae{"1.000000000000000e+00"sv, true});
CONSTEXPR_CHECK(a(1.5) == ae{"1.500000000000000e+00"sv, true});
Expand Down

0 comments on commit 36090de

Please sign in to comment.