Skip to content

Commit

Permalink
Fix compilation on platforms with exotic double (#878)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Sep 26, 2018
1 parent e4ca37c commit d7f1761
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
9 changes: 6 additions & 3 deletions include/fmt/format-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,8 @@ FMT_FUNC void grisu2_gen_digits(
}
}

FMT_FUNC void grisu2_format_positive(double value, char *buffer, size_t &size,
template <typename Double>
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);
Expand Down Expand Up @@ -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 <typename Double>
FMT_FUNC typename std::enable_if<sizeof(Double) == sizeof(uint64_t)>::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) {
Expand Down
9 changes: 7 additions & 2 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename Double>
FMT_API typename std::enable_if<sizeof(Double) == sizeof(uint64_t)>::type
grisu2_format(Double value, char *buffer, size_t &size, char type,
int precision, bool write_decimal_point);
template <typename Double>
inline typename std::enable_if<sizeof(Double) != sizeof(uint64_t)>::type
grisu2_format(Double, char *, size_t &, char, int, bool) {}

template <typename Allocator>
typename Allocator::value_type *allocate(Allocator& alloc, std::size_t n) {
Expand Down
5 changes: 5 additions & 0 deletions test/format-impl-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename T>
struct ValueExtractor: fmt::internal::function<T> {
T operator()(T value) {
Expand Down

0 comments on commit d7f1761

Please sign in to comment.