From 57983423c37f49bfe9d2840c564af6b60231b359 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 10 Oct 2018 09:41:01 -0700 Subject: [PATCH] Remove signbit workaround --- include/fmt/format.h | 25 +++++-------------------- test/format-impl-test.cc | 7 ++++--- 2 files changed, 9 insertions(+), 23 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index ed90f077..9fd3fa44 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -278,10 +278,8 @@ struct dummy_int { }; typedef std::numeric_limits fputil; -// Dummy implementations of system functions such as signbit and ecvt called -// if the latter are not available. -inline dummy_int signbit(...) { return dummy_int(); } -inline dummy_int _ecvt_s(...) { return dummy_int(); } +// Dummy implementations of system functions called if the latter are not +// available. inline dummy_int isinf(...) { return dummy_int(); } inline dummy_int _finite(...) { return dummy_int(); } inline dummy_int isnan(...) { return dummy_int(); } @@ -321,7 +319,7 @@ namespace std { // Standard permits specialization of std::numeric_limits. This specialization // is used to resolve ambiguity between isinf and std::isinf in glibc: // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=48891 -// and the same for isnan and signbit. +// and the same for isnan. template <> class numeric_limits : public std::numeric_limits { @@ -345,19 +343,6 @@ class numeric_limits : return isnan(x) != 0; return _isnan(static_cast(x)) != 0; } - - // Portable version of signbit. - static bool isnegative(double x) { - using namespace fmt::internal; - if (const_check(sizeof(signbit(x)) != sizeof(fmt::internal::dummy_int))) - return signbit(x) != 0; - if (x < 0) return true; - if (!isnotanumber(x)) return false; - int dec = 0, sign = 0; - char buffer[2]; // The buffer size must be >= 2 or _ecvt_s will fail. - _ecvt_s(buffer, sizeof(buffer), x, 0, &dec, &sign); - return sign != 0; - } }; } // namespace std @@ -2862,9 +2847,9 @@ void basic_writer::write_double(T value, const format_specs &spec) { internal::handle_float_type_spec(handler.type, handler); char sign = 0; - // Use isnegative instead of value < 0 because the latter is always + // Use signbit instead of value < 0 because the latter is always // false for NaN. - if (internal::fputil::isnegative(static_cast(value))) { + if (std::signbit(value)) { sign = '-'; value = -value; } else if (spec.flag(SIGN_FLAG)) { diff --git a/test/format-impl-test.cc b/test/format-impl-test.cc index 16694a4a..4679436c 100644 --- a/test/format-impl-test.cc +++ b/test/format-impl-test.cc @@ -129,7 +129,7 @@ TEST(FormatTest, ArgConverter) { TEST(FormatTest, FormatNegativeNaN) { double nan = std::numeric_limits::quiet_NaN(); - if (fmt::internal::fputil::isnegative(-nan)) + if (std::signbit(-nan)) EXPECT_EQ("-nan", fmt::format("{}", -nan)); else fmt::print("Warning: compiler doesn't handle negative NaN correctly"); @@ -138,7 +138,8 @@ TEST(FormatTest, FormatNegativeNaN) { TEST(FormatTest, StrError) { char *message = nullptr; char buffer[BUFFER_SIZE]; - EXPECT_ASSERT(fmt::safe_strerror(EDOM, message = nullptr, 0), "invalid buffer"); + EXPECT_ASSERT(fmt::safe_strerror(EDOM, message = nullptr, 0), + "invalid buffer"); EXPECT_ASSERT(fmt::safe_strerror(EDOM, message = buffer, 0), "invalid buffer"); buffer[0] = 'x'; @@ -151,7 +152,7 @@ TEST(FormatTest, StrError) { #endif int result = fmt::safe_strerror(error_code, message = buffer, BUFFER_SIZE); - EXPECT_EQ(0, result); + EXPECT_EQ(result, 0); std::size_t message_size = std::strlen(message); EXPECT_GE(BUFFER_SIZE - 1u, message_size); EXPECT_EQ(get_system_error(error_code), message);