Skip to content

Commit

Permalink
Improve __float128 support
Browse files Browse the repository at this point in the history
vitaut committed Mar 19, 2022

Verified

This commit was signed with the committer’s verified signature.
snyk-bot Snyk bot
1 parent 71778e8 commit 3f9b743
Showing 2 changed files with 13 additions and 6 deletions.
8 changes: 5 additions & 3 deletions include/fmt/format-inl.h
Original file line number Diff line number Diff line change
@@ -217,8 +217,10 @@ template <typename F> struct basic_fp {
template <typename Float> FMT_CONSTEXPR basic_fp(Float n) { assign(n); }

template <typename Float>
using is_supported = bool_constant<std::numeric_limits<Float>::is_iec559 &&
std::numeric_limits<Float>::digits <= 113>;
using is_supported =
bool_constant<(std::numeric_limits<Float>::is_iec559 &&
std::numeric_limits<Float>::digits <= 113) ||
is_float128<Float>::value>;

// Assigns d to this and return true iff predecessor is closer than successor.
template <typename Float, FMT_ENABLE_IF(is_supported<Float>::value)>
@@ -229,7 +231,7 @@ template <typename F> struct basic_fp {
<< detail::num_significand_bits<Float>();
const carrier_uint significand_mask = implicit_bit - 1;
auto u = bit_cast<carrier_uint>(n);
f = static_cast<uint64_t>(u & significand_mask);
f = static_cast<F>(u & significand_mask);
int biased_e = static_cast<int>((u & exponent_mask<Float>()) >>
detail::num_significand_bits<Float>());
// The predecessor is closer if n is a normalized power of 2 (f == 0) other
11 changes: 8 additions & 3 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
@@ -1286,14 +1286,19 @@ template <typename T> struct decimal_fp {
template <typename T> FMT_API auto to_decimal(T x) noexcept -> decimal_fp<T>;
} // namespace dragonbox

// Returns true iff Float has the implicit bit which is not stored.
template <typename Float> constexpr bool has_implicit_bit() {
// An 80-bit FP number has a 64-bit significand an no implicit bit.
return std::numeric_limits<Float>::digits != 64;
}

// Returns the number of significand bits in Float excluding the implicit bit.
// Returns the number of significand bits stored in Float. The implicit bit is
// not counted since it is not stored.
template <typename Float> constexpr int num_significand_bits() {
return std::numeric_limits<Float>::digits -
(has_implicit_bit<Float>() ? 1 : 0);
// std::numeric_limits may not support __float128.
return is_float128<Float>() ? 112
: (std::numeric_limits<Float>::digits -
(has_implicit_bit<Float>() ? 1 : 0));
}

template <typename Float>

0 comments on commit 3f9b743

Please sign in to comment.