From d7f17613f5d7f0411c263a563bca8295bd062575 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 26 Sep 2018 08:45:34 -0700 Subject: [PATCH] Fix compilation on platforms with exotic double (#878) --- include/fmt/format-inl.h | 9 ++++++--- include/fmt/format.h | 9 +++++++-- test/format-impl-test.cc | 5 +++++ 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index 24403cecc10b..d47334b19380 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -513,7 +513,8 @@ FMT_FUNC void grisu2_gen_digits( } } -FMT_FUNC void grisu2_format_positive(double value, char *buffer, size_t &size, +template +FMT_FUNC void grisu2_format_positive(Double value, char *buffer, size_t &size, int &dec_exp) { FMT_ASSERT(value > 0, "value is nonpositive"); fp fp_value(value); @@ -640,8 +641,10 @@ FMT_FUNC void grisu2_prettify(char *buffer, size_t &size, int exp, // Formats a nonnegative value using Grisu2 algorithm. Grisu2 doesn't give any // guarantees on the shortness of the result. -FMT_FUNC void grisu2_format(double value, char *buffer, size_t &size, char type, - int precision, bool write_decimal_point) { +template +FMT_FUNC typename std::enable_if::type + grisu2_format(Double value, char *buffer, size_t &size, char type, + int precision, bool write_decimal_point) { FMT_ASSERT(value >= 0, "value is negative"); int dec_exp = 0; // K in Grisu. if (value > 0) { diff --git a/include/fmt/format.h b/include/fmt/format.h index 4ba7a4dada43..a0d2ad76ddd8 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -293,8 +293,13 @@ inline bool use_grisu() { // Formats value using Grisu2 algorithm: // https://www.cs.tufts.edu/~nr/cs257/archive/florian-loitsch/printf.pdf -FMT_API void grisu2_format(double value, char *buffer, size_t &size, char type, - int precision, bool write_decimal_point); +template +FMT_API typename std::enable_if::type + grisu2_format(Double value, char *buffer, size_t &size, char type, + int precision, bool write_decimal_point); +template +inline typename std::enable_if::type + grisu2_format(Double, char *, size_t &, char, int, bool) {} template typename Allocator::value_type *allocate(Allocator& alloc, std::size_t n) { diff --git a/test/format-impl-test.cc b/test/format-impl-test.cc index 08092516e4bc..16694a4ac35c 100644 --- a/test/format-impl-test.cc +++ b/test/format-impl-test.cc @@ -102,6 +102,11 @@ TEST(FPTest, GetCachedPower) { } } +TEST(FPTest, Grisu2FormatCompilesWithNonIEEEDouble) { + size_t size = 0; + fmt::internal::grisu2_format(4.2f, FMT_NULL, size, 0, 0, false); +} + template struct ValueExtractor: fmt::internal::function { T operator()(T value) {