Skip to content

Commit

Permalink
Add partial support for extended precision FP
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Feb 15, 2022
1 parent 0a24a07 commit 2b6f7fc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
33 changes: 18 additions & 15 deletions include/fmt/format-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,9 @@ struct fp {
template <typename Float> explicit FMT_CONSTEXPR fp(Float n) { assign(n); }

template <typename Float>
using is_supported = bool_constant<sizeof(Float) == sizeof(uint64_t) ||
sizeof(Float) == sizeof(uint32_t)>;
using is_supported = bool_constant<sizeof(Float) == sizeof(uint32_t) ||
sizeof(Float) == 2 * sizeof(uint32_t) ||
std::numeric_limits<Float>::digits == 64>;

// Assigns d to this and return true iff predecessor is closer than successor.
template <typename Float, FMT_ENABLE_IF(is_supported<Float>::value)>
Expand All @@ -255,13 +256,13 @@ struct fp {
const int num_float_significand_bits =
detail::num_significand_bits<Float>();
const uint64_t implicit_bit = 1ULL << num_float_significand_bits;
const uint64_t significand_mask = implicit_bit - 1;
constexpr bool is_double = sizeof(Float) == sizeof(uint64_t);
auto u = bit_cast<conditional_t<is_double, uint64_t, uint32_t>>(n);
using carrier_uint = typename dragonbox::float_info<Float>::carrier_uint;
const carrier_uint significand_mask = implicit_bit - 1;
auto u = bit_cast<carrier_uint>(n);
f = u & significand_mask;
const uint64_t exponent_mask = (~0ULL >> 1) & ~significand_mask;
int biased_e =
static_cast<int>((u & exponent_mask) >> num_float_significand_bits);
static_cast<int>((u & exponent_mask<Float>()) >>
dragonbox::float_info<Float>::significand_bits);
// The predecessor is closer if n is a normalized power of 2 (f == 0) other
// than the smallest normalized number (biased_e > 1).
bool is_predecessor_closer = f == 0 && biased_e > 1;
Expand Down Expand Up @@ -2120,10 +2121,9 @@ FMT_CONSTEXPR20 inline void format_dragon(fp value, bool is_predecessor_closer,
// is closer) to make lower and upper integers. This eliminates multiplication
// by 2 during later computations.
int shift = is_predecessor_closer ? 2 : 1;
uint64_t significand = value.f << shift;
if (value.e >= 0) {
numerator.assign(significand);
numerator <<= value.e;
numerator.assign(value.f);
numerator <<= value.e + shift;
lower.assign(1);
lower <<= value.e;
if (shift != 1) {
Expand All @@ -2141,11 +2141,13 @@ FMT_CONSTEXPR20 inline void format_dragon(fp value, bool is_predecessor_closer,
upper_store <<= 1;
upper = &upper_store;
}
numerator *= significand;
numerator *= value.f;
numerator <<= shift;
denominator.assign(1);
denominator <<= shift - value.e;
} else {
numerator.assign(significand);
numerator.assign(value.f);
numerator <<= shift;
denominator.assign_pow10(exp10);
denominator <<= shift - value.e;
lower.assign(1);
Expand Down Expand Up @@ -2261,7 +2263,7 @@ FMT_HEADER_ONLY_CONSTEXPR20 int format_float(Float value, int precision,
// https://www.cs.tufts.edu/~nr/cs257/archive/florian-loitsch/printf.pdf.
const int min_exp = -60; // alpha in Grisu.
int cached_exp10 = 0; // K in Grisu.
fp normalized = normalize(fp(value));
fp normalized = normalize(fp(convert_float(value)));
const auto cached_pow = get_cached_power(
min_exp - (normalized.e + fp::num_significand_bits), cached_exp10);
normalized = normalized * cached_pow;
Expand All @@ -2278,8 +2280,9 @@ FMT_HEADER_ONLY_CONSTEXPR20 int format_float(Float value, int precision,
}
if (use_dragon) {
auto f = fp();
bool is_predecessor_closer =
specs.binary32 ? f.assign(static_cast<float>(value)) : f.assign(value);
bool is_predecessor_closer = specs.binary32
? f.assign(static_cast<float>(value))
: f.assign(convert_float(value));
// Limit precision to the maximum possible number of significant digits in
// an IEEE754 double because we don't need to generate zeros.
const int max_double_digits = 767;
Expand Down
28 changes: 20 additions & 8 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -1198,7 +1198,7 @@ class utf8_to_utf16 {
namespace dragonbox {

// Type-specific information that Dragonbox uses.
template <class T> struct float_info;
template <typename T, typename Enable = void> struct float_info;

template <> struct float_info<float> {
using carrier_uint = uint32_t;
Expand Down Expand Up @@ -1246,6 +1246,15 @@ template <> struct float_info<double> {
static const int max_trailing_zeros = 16;
};

// 80-bit extended precision long double.
template <typename T>
struct float_info<T, enable_if_t<std::is_same<T, long double>::value &&
std::numeric_limits<T>::digits == 64>> {
using carrier_uint = detail::uint128_t;
static const int significand_bits = 64;
static const int exponent_bits = 15;
};

template <typename T> struct decimal_fp {
using significand_type = typename float_info<T>::carrier_uint;
significand_type significand;
Expand Down Expand Up @@ -1295,11 +1304,14 @@ template <typename T>
auto snprintf_float(T value, int precision, float_specs specs,
buffer<char>& buf) -> int;

template <typename T> constexpr auto promote_float(T value) -> T {
return value;
}
constexpr auto promote_float(float value) -> double {
return static_cast<double>(value);
template <typename T>
using convert_float_result =
conditional_t<std::is_same<T, float>::value || sizeof(T) == sizeof(double),
double, T>;

template <typename T>
constexpr auto convert_float(T value) -> convert_float_result<T> {
return static_cast<convert_float_result<T>>(value);
}

template <typename OutputIt, typename Char>
Expand Down Expand Up @@ -2207,7 +2219,7 @@ FMT_CONSTEXPR20 auto write(OutputIt out, T value,
memory_buffer buffer;
if (fspecs.format == float_format::hex) {
if (fspecs.sign) buffer.push_back(detail::sign<char>(fspecs.sign));
snprintf_float(promote_float(value), specs.precision, fspecs, buffer);
snprintf_float(convert_float(value), specs.precision, fspecs, buffer);
return write_bytes<align::right>(out, {buffer.data(), buffer.size()},
specs);
}
Expand All @@ -2222,7 +2234,7 @@ FMT_CONSTEXPR20 auto write(OutputIt out, T value,
}
if (const_check(std::is_same<T, float>())) fspecs.binary32 = true;
if (!is_fast_float<T>()) fspecs.fallback = true;
int exp = format_float(promote_float(value), precision, fspecs, buffer);
int exp = format_float(convert_float(value), precision, fspecs, buffer);
fspecs.precision = precision;
auto fp = big_decimal_fp{buffer.data(), static_cast<int>(buffer.size()), exp};
return write_float(out, fp, specs, fspecs, loc);
Expand Down

0 comments on commit 2b6f7fc

Please sign in to comment.