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 9963172 commit 237ac10
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/snitch_append.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,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 @@ -31,6 +38,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
4 changes: 2 additions & 2 deletions tests/runtime_tests/string_utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,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 @@ -514,7 +514,7 @@ TEST_CASE("append doubles", "[utility]") {
// 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}});
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 237ac10

Please sign in to comment.