Skip to content

Commit

Permalink
Merge branch 'fmtlib:master' into zig-pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane committed Sep 23, 2023
2 parents 403427f + 44f3d8a commit 500418c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 46 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ License
-------

{fmt} is distributed under the MIT `license
<https://github.com/fmtlib/fmt/blob/master/LICENSE.rst>`_.
<https://github.com/fmtlib/fmt/blob/master/LICENSE>`_.

Documentation License
---------------------
Expand Down
77 changes: 32 additions & 45 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -1740,29 +1740,6 @@ FMT_CONSTEXPR inline fp operator*(fp x, fp y) {
return {multiply(x.f, y.f), x.e + y.e + 64};
}

template <typename T = void> struct basic_data {
// For checking rounding thresholds.
// The kth entry is chosen to be the smallest integer such that the
// upper 32-bits of 10^(k+1) times it is strictly bigger than 5 * 10^k.
static constexpr uint32_t fractional_part_rounding_thresholds[8] = {
2576980378U, // ceil(2^31 + 2^32/10^1)
2190433321U, // ceil(2^31 + 2^32/10^2)
2151778616U, // ceil(2^31 + 2^32/10^3)
2147913145U, // ceil(2^31 + 2^32/10^4)
2147526598U, // ceil(2^31 + 2^32/10^5)
2147487943U, // ceil(2^31 + 2^32/10^6)
2147484078U, // ceil(2^31 + 2^32/10^7)
2147483691U // ceil(2^31 + 2^32/10^8)
};
};
// This is a struct rather than an alias to avoid shadowing warnings in gcc.
struct data : basic_data<> {};

#if FMT_CPLUSPLUS < 201703L
template <typename T>
constexpr uint32_t basic_data<T>::fractional_part_rounding_thresholds[];
#endif

template <typename T, bool doublish = num_bits<T>() == num_bits<double>()>
using convert_float_result =
conditional_t<std::is_same<T, float>::value || doublish, double, T>;
Expand Down Expand Up @@ -3182,8 +3159,10 @@ FMT_CONSTEXPR20 inline void format_dragon(basic_fp<uint128_t> value,
}
if (buf[0] == overflow) {
buf[0] = '1';
if ((flags & dragon::fixed) != 0) buf.push_back('0');
else ++exp10;
if ((flags & dragon::fixed) != 0)
buf.push_back('0');
else
++exp10;
}
return;
}
Expand Down Expand Up @@ -3280,6 +3259,17 @@ FMT_CONSTEXPR20 void format_hexfloat(Float value, int precision,
format_hexfloat(static_cast<double>(value), precision, specs, buf);
}

constexpr uint32_t fractional_part_rounding_thresholds(int index) {
// For checking rounding thresholds.
// The kth entry is chosen to be the smallest integer such that the
// upper 32-bits of 10^(k+1) times it is strictly bigger than 5 * 10^k.
// It is equal to ceil(2^31 + 2^32/10^(k + 1)).
// These are stored in a string literal because we cannot have static arrays
// in constexpr functions and non-static ones are poorly optimized.
return U"\x9999999a\x828f5c29\x80418938\x80068db9\x8000a7c6\x800010c7"
U"\x800001ae\x8000002b"[index];
}

template <typename Float>
FMT_CONSTEXPR20 auto format_float(Float value, int precision, float_specs specs,
buffer<char>& buf) -> int {
Expand Down Expand Up @@ -3484,12 +3474,12 @@ FMT_CONSTEXPR20 auto format_float(Float value, int precision, float_specs specs,
// fractional part is strictly larger than 1/2.
if (precision < 9) {
uint32_t fractional_part = static_cast<uint32_t>(prod);
should_round_up = fractional_part >=
data::fractional_part_rounding_thresholds
[8 - number_of_digits_to_print] ||
((fractional_part >> 31) &
((digits & 1) | (second_third_subsegments != 0) |
has_more_segments)) != 0;
should_round_up =
fractional_part >= fractional_part_rounding_thresholds(
8 - number_of_digits_to_print) ||
((fractional_part >> 31) &
((digits & 1) | (second_third_subsegments != 0) |
has_more_segments)) != 0;
}
// Rounding at the subsegment boundary.
// In this case, the fractional part is at least 1/2 if and only if
Expand Down Expand Up @@ -3524,12 +3514,12 @@ FMT_CONSTEXPR20 auto format_float(Float value, int precision, float_specs specs,
// of 19 digits, so in this case the third segment should be
// consisting of a genuine digit from the input.
uint32_t fractional_part = static_cast<uint32_t>(prod);
should_round_up = fractional_part >=
data::fractional_part_rounding_thresholds
[8 - number_of_digits_to_print] ||
((fractional_part >> 31) &
((digits & 1) | (third_subsegment != 0) |
has_more_segments)) != 0;
should_round_up =
fractional_part >= fractional_part_rounding_thresholds(
8 - number_of_digits_to_print) ||
((fractional_part >> 31) &
((digits & 1) | (third_subsegment != 0) |
has_more_segments)) != 0;
}
// Rounding at the subsegment boundary.
else {
Expand Down Expand Up @@ -4202,14 +4192,12 @@ template <typename T> struct formatter<group_digits_view<T>> : formatter<T> {
}
};

template <typename T>
struct nested_view {
template <typename T> struct nested_view {
const formatter<T>* fmt;
const T* value;
};

template <typename T>
struct formatter<nested_view<T>> {
template <typename T> struct formatter<nested_view<T>> {
FMT_CONSTEXPR auto parse(format_parse_context& ctx) -> const char* {
return ctx.begin();
}
Expand All @@ -4219,8 +4207,7 @@ struct formatter<nested_view<T>> {
}
};

template <typename T>
struct nested_formatter {
template <typename T> struct nested_formatter {
private:
int width_;
detail::fill_t<char> fill_;
Expand All @@ -4230,8 +4217,8 @@ struct nested_formatter {
public:
FMT_CONSTEXPR auto parse(format_parse_context& ctx) -> const char* {
auto specs = detail::dynamic_format_specs<char>();
auto it = parse_format_specs(
ctx.begin(), ctx.end(), specs, ctx, detail::type::none_type);
auto it = parse_format_specs(ctx.begin(), ctx.end(), specs, ctx,
detail::type::none_type);
width_ = specs.width;
fill_ = specs.fill;
align_ = specs.align;
Expand Down

0 comments on commit 500418c

Please sign in to comment.