• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "absl/strings/internal/str_format/float_conversion.h"
16 
17 #include <string.h>
18 
19 #include <algorithm>
20 #include <cassert>
21 #include <cmath>
22 #include <limits>
23 #include <string>
24 
25 #include "absl/base/attributes.h"
26 #include "absl/base/config.h"
27 #include "absl/base/optimization.h"
28 #include "absl/functional/function_ref.h"
29 #include "absl/meta/type_traits.h"
30 #include "absl/numeric/bits.h"
31 #include "absl/numeric/int128.h"
32 #include "absl/numeric/internal/representation.h"
33 #include "absl/strings/numbers.h"
34 #include "absl/types/optional.h"
35 #include "absl/types/span.h"
36 
37 namespace absl {
38 ABSL_NAMESPACE_BEGIN
39 namespace str_format_internal {
40 
41 namespace {
42 
43 using ::absl::numeric_internal::IsDoubleDouble;
44 
45 // The code below wants to avoid heap allocations.
46 // To do so it needs to allocate memory on the stack.
47 // `StackArray` will allocate memory on the stack in the form of a uint32_t
48 // array and call the provided callback with said memory.
49 // It will allocate memory in increments of 512 bytes. We could allocate the
50 // largest needed unconditionally, but that is more than we need in most of
51 // cases. This way we use less stack in the common cases.
52 class StackArray {
53   using Func = absl::FunctionRef<void(absl::Span<uint32_t>)>;
54   static constexpr size_t kStep = 512 / sizeof(uint32_t);
55   // 5 steps is 2560 bytes, which is enough to hold a long double with the
56   // largest/smallest exponents.
57   // The operations below will static_assert their particular maximum.
58   static constexpr size_t kNumSteps = 5;
59 
60   // We do not want this function to be inlined.
61   // Otherwise the caller will allocate the stack space unnecessarily for all
62   // the variants even though it only calls one.
63   template <size_t steps>
RunWithCapacityImpl(Func f)64   ABSL_ATTRIBUTE_NOINLINE static void RunWithCapacityImpl(Func f) {
65     uint32_t values[steps * kStep]{};
66     f(absl::MakeSpan(values));
67   }
68 
69  public:
70   static constexpr size_t kMaxCapacity = kStep * kNumSteps;
71 
RunWithCapacity(size_t capacity,Func f)72   static void RunWithCapacity(size_t capacity, Func f) {
73     assert(capacity <= kMaxCapacity);
74     const size_t step = (capacity + kStep - 1) / kStep;
75     assert(step <= kNumSteps);
76     switch (step) {
77       case 1:
78         return RunWithCapacityImpl<1>(f);
79       case 2:
80         return RunWithCapacityImpl<2>(f);
81       case 3:
82         return RunWithCapacityImpl<3>(f);
83       case 4:
84         return RunWithCapacityImpl<4>(f);
85       case 5:
86         return RunWithCapacityImpl<5>(f);
87     }
88 
89     assert(false && "Invalid capacity");
90   }
91 };
92 
93 // Calculates `10 * (*v) + carry` and stores the result in `*v` and returns
94 // the carry.
95 template <typename Int>
MultiplyBy10WithCarry(Int * v,Int carry)96 inline Int MultiplyBy10WithCarry(Int *v, Int carry) {
97   using BiggerInt = absl::conditional_t<sizeof(Int) == 4, uint64_t, uint128>;
98   BiggerInt tmp = 10 * static_cast<BiggerInt>(*v) + carry;
99   *v = static_cast<Int>(tmp);
100   return static_cast<Int>(tmp >> (sizeof(Int) * 8));
101 }
102 
103 // Calculates `(2^64 * carry + *v) / 10`.
104 // Stores the quotient in `*v` and returns the remainder.
105 // Requires: `0 <= carry <= 9`
DivideBy10WithCarry(uint64_t * v,uint64_t carry)106 inline uint64_t DivideBy10WithCarry(uint64_t *v, uint64_t carry) {
107   constexpr uint64_t divisor = 10;
108   // 2^64 / divisor = chunk_quotient + chunk_remainder / divisor
109   constexpr uint64_t chunk_quotient = (uint64_t{1} << 63) / (divisor / 2);
110   constexpr uint64_t chunk_remainder = uint64_t{} - chunk_quotient * divisor;
111 
112   const uint64_t mod = *v % divisor;
113   const uint64_t next_carry = chunk_remainder * carry + mod;
114   *v = *v / divisor + carry * chunk_quotient + next_carry / divisor;
115   return next_carry % divisor;
116 }
117 
118 using MaxFloatType =
119     typename std::conditional<IsDoubleDouble(), double, long double>::type;
120 
121 // Generates the decimal representation for an integer of the form `v * 2^exp`,
122 // where `v` and `exp` are both positive integers.
123 // It generates the digits from the left (ie the most significant digit first)
124 // to allow for direct printing into the sink.
125 //
126 // Requires `0 <= exp` and `exp <= numeric_limits<MaxFloatType>::max_exponent`.
127 class BinaryToDecimal {
ChunksNeeded(int exp)128   static constexpr int ChunksNeeded(int exp) {
129     // We will left shift a uint128 by `exp` bits, so we need `128+exp` total
130     // bits. Round up to 32.
131     // See constructor for details about adding `10%` to the value.
132     return (128 + exp + 31) / 32 * 11 / 10;
133   }
134 
135  public:
136   // Run the conversion for `v * 2^exp` and call `f(binary_to_decimal)`.
137   // This function will allocate enough stack space to perform the conversion.
RunConversion(uint128 v,int exp,absl::FunctionRef<void (BinaryToDecimal)> f)138   static void RunConversion(uint128 v, int exp,
139                             absl::FunctionRef<void(BinaryToDecimal)> f) {
140     assert(exp > 0);
141     assert(exp <= std::numeric_limits<MaxFloatType>::max_exponent);
142     static_assert(
143         static_cast<int>(StackArray::kMaxCapacity) >=
144             ChunksNeeded(std::numeric_limits<MaxFloatType>::max_exponent),
145         "");
146 
147     StackArray::RunWithCapacity(
148         ChunksNeeded(exp),
149         [=](absl::Span<uint32_t> input) { f(BinaryToDecimal(input, v, exp)); });
150   }
151 
TotalDigits() const152   int TotalDigits() const {
153     return static_cast<int>((decimal_end_ - decimal_start_) * kDigitsPerChunk +
154                             CurrentDigits().size());
155   }
156 
157   // See the current block of digits.
CurrentDigits() const158   absl::string_view CurrentDigits() const {
159     return absl::string_view(digits_ + kDigitsPerChunk - size_, size_);
160   }
161 
162   // Advance the current view of digits.
163   // Returns `false` when no more digits are available.
AdvanceDigits()164   bool AdvanceDigits() {
165     if (decimal_start_ >= decimal_end_) return false;
166 
167     uint32_t w = data_[decimal_start_++];
168     for (size_ = 0; size_ < kDigitsPerChunk; w /= 10) {
169       digits_[kDigitsPerChunk - ++size_] = w % 10 + '0';
170     }
171     return true;
172   }
173 
174  private:
BinaryToDecimal(absl::Span<uint32_t> data,uint128 v,int exp)175   BinaryToDecimal(absl::Span<uint32_t> data, uint128 v, int exp) : data_(data) {
176     // We need to print the digits directly into the sink object without
177     // buffering them all first. To do this we need two things:
178     // - to know the total number of digits to do padding when necessary
179     // - to generate the decimal digits from the left.
180     //
181     // In order to do this, we do a two pass conversion.
182     // On the first pass we convert the binary representation of the value into
183     // a decimal representation in which each uint32_t chunk holds up to 9
184     // decimal digits.  In the second pass we take each decimal-holding-uint32_t
185     // value and generate the ascii decimal digits into `digits_`.
186     //
187     // The binary and decimal representations actually share the same memory
188     // region. As we go converting the chunks from binary to decimal we free
189     // them up and reuse them for the decimal representation. One caveat is that
190     // the decimal representation is around 7% less efficient in space than the
191     // binary one. We allocate an extra 10% memory to account for this. See
192     // ChunksNeeded for this calculation.
193     int chunk_index = exp / 32;
194     decimal_start_ = decimal_end_ = ChunksNeeded(exp);
195     const int offset = exp % 32;
196     // Left shift v by exp bits.
197     data_[chunk_index] = static_cast<uint32_t>(v << offset);
198     for (v >>= (32 - offset); v; v >>= 32)
199       data_[++chunk_index] = static_cast<uint32_t>(v);
200 
201     while (chunk_index >= 0) {
202       // While we have more than one chunk available, go in steps of 1e9.
203       // `data_[chunk_index]` holds the highest non-zero binary chunk, so keep
204       // the variable updated.
205       uint32_t carry = 0;
206       for (int i = chunk_index; i >= 0; --i) {
207         uint64_t tmp = uint64_t{data_[i]} + (uint64_t{carry} << 32);
208         data_[i] = static_cast<uint32_t>(tmp / uint64_t{1000000000});
209         carry = static_cast<uint32_t>(tmp % uint64_t{1000000000});
210       }
211 
212       // If the highest chunk is now empty, remove it from view.
213       if (data_[chunk_index] == 0) --chunk_index;
214 
215       --decimal_start_;
216       assert(decimal_start_ != chunk_index);
217       data_[decimal_start_] = carry;
218     }
219 
220     // Fill the first set of digits. The first chunk might not be complete, so
221     // handle differently.
222     for (uint32_t first = data_[decimal_start_++]; first != 0; first /= 10) {
223       digits_[kDigitsPerChunk - ++size_] = first % 10 + '0';
224     }
225   }
226 
227  private:
228   static constexpr int kDigitsPerChunk = 9;
229 
230   int decimal_start_;
231   int decimal_end_;
232 
233   char digits_[kDigitsPerChunk];
234   int size_ = 0;
235 
236   absl::Span<uint32_t> data_;
237 };
238 
239 // Converts a value of the form `x * 2^-exp` into a sequence of decimal digits.
240 // Requires `-exp < 0` and
241 // `-exp >= limits<MaxFloatType>::min_exponent - limits<MaxFloatType>::digits`.
242 class FractionalDigitGenerator {
243  public:
244   // Run the conversion for `v * 2^exp` and call `f(generator)`.
245   // This function will allocate enough stack space to perform the conversion.
RunConversion(uint128 v,int exp,absl::FunctionRef<void (FractionalDigitGenerator)> f)246   static void RunConversion(
247       uint128 v, int exp, absl::FunctionRef<void(FractionalDigitGenerator)> f) {
248     using Limits = std::numeric_limits<MaxFloatType>;
249     assert(-exp < 0);
250     assert(-exp >= Limits::min_exponent - 128);
251     static_assert(StackArray::kMaxCapacity >=
252                       (Limits::digits + 128 - Limits::min_exponent + 31) / 32,
253                   "");
254     StackArray::RunWithCapacity((Limits::digits + exp + 31) / 32,
255                                 [=](absl::Span<uint32_t> input) {
256                                   f(FractionalDigitGenerator(input, v, exp));
257                                 });
258   }
259 
260   // Returns true if there are any more non-zero digits left.
HasMoreDigits() const261   bool HasMoreDigits() const { return next_digit_ != 0 || chunk_index_ >= 0; }
262 
263   // Returns true if the remainder digits are greater than 5000...
IsGreaterThanHalf() const264   bool IsGreaterThanHalf() const {
265     return next_digit_ > 5 || (next_digit_ == 5 && chunk_index_ >= 0);
266   }
267   // Returns true if the remainder digits are exactly 5000...
IsExactlyHalf() const268   bool IsExactlyHalf() const { return next_digit_ == 5 && chunk_index_ < 0; }
269 
270   struct Digits {
271     int digit_before_nine;
272     int num_nines;
273   };
274 
275   // Get the next set of digits.
276   // They are composed by a non-9 digit followed by a runs of zero or more 9s.
GetDigits()277   Digits GetDigits() {
278     Digits digits{next_digit_, 0};
279 
280     next_digit_ = GetOneDigit();
281     while (next_digit_ == 9) {
282       ++digits.num_nines;
283       next_digit_ = GetOneDigit();
284     }
285 
286     return digits;
287   }
288 
289  private:
290   // Return the next digit.
GetOneDigit()291   int GetOneDigit() {
292     if (chunk_index_ < 0) return 0;
293 
294     uint32_t carry = 0;
295     for (int i = chunk_index_; i >= 0; --i) {
296       carry = MultiplyBy10WithCarry(&data_[i], carry);
297     }
298     // If the lowest chunk is now empty, remove it from view.
299     if (data_[chunk_index_] == 0) --chunk_index_;
300     return carry;
301   }
302 
FractionalDigitGenerator(absl::Span<uint32_t> data,uint128 v,int exp)303   FractionalDigitGenerator(absl::Span<uint32_t> data, uint128 v, int exp)
304       : chunk_index_(exp / 32), data_(data) {
305     const int offset = exp % 32;
306     // Right shift `v` by `exp` bits.
307     data_[chunk_index_] = static_cast<uint32_t>(v << (32 - offset));
308     v >>= offset;
309     // Make sure we don't overflow the data. We already calculated that
310     // non-zero bits fit, so we might not have space for leading zero bits.
311     for (int pos = chunk_index_; v; v >>= 32)
312       data_[--pos] = static_cast<uint32_t>(v);
313 
314     // Fill next_digit_, as GetDigits expects it to be populated always.
315     next_digit_ = GetOneDigit();
316   }
317 
318   int next_digit_;
319   int chunk_index_;
320   absl::Span<uint32_t> data_;
321 };
322 
323 // Count the number of leading zero bits.
LeadingZeros(uint64_t v)324 int LeadingZeros(uint64_t v) { return countl_zero(v); }
LeadingZeros(uint128 v)325 int LeadingZeros(uint128 v) {
326   auto high = static_cast<uint64_t>(v >> 64);
327   auto low = static_cast<uint64_t>(v);
328   return high != 0 ? countl_zero(high) : 64 + countl_zero(low);
329 }
330 
331 // Round up the text digits starting at `p`.
332 // The buffer must have an extra digit that is known to not need rounding.
333 // This is done below by having an extra '0' digit on the left.
RoundUp(char * p)334 void RoundUp(char *p) {
335   while (*p == '9' || *p == '.') {
336     if (*p == '9') *p = '0';
337     --p;
338   }
339   ++*p;
340 }
341 
342 // Check the previous digit and round up or down to follow the round-to-even
343 // policy.
RoundToEven(char * p)344 void RoundToEven(char *p) {
345   if (*p == '.') --p;
346   if (*p % 2 == 1) RoundUp(p);
347 }
348 
349 // Simple integral decimal digit printing for values that fit in 64-bits.
350 // Returns the pointer to the last written digit.
PrintIntegralDigitsFromRightFast(uint64_t v,char * p)351 char *PrintIntegralDigitsFromRightFast(uint64_t v, char *p) {
352   do {
353     *--p = DivideBy10WithCarry(&v, 0) + '0';
354   } while (v != 0);
355   return p;
356 }
357 
358 // Simple integral decimal digit printing for values that fit in 128-bits.
359 // Returns the pointer to the last written digit.
PrintIntegralDigitsFromRightFast(uint128 v,char * p)360 char *PrintIntegralDigitsFromRightFast(uint128 v, char *p) {
361   auto high = static_cast<uint64_t>(v >> 64);
362   auto low = static_cast<uint64_t>(v);
363 
364   while (high != 0) {
365     uint64_t carry = DivideBy10WithCarry(&high, 0);
366     carry = DivideBy10WithCarry(&low, carry);
367     *--p = carry + '0';
368   }
369   return PrintIntegralDigitsFromRightFast(low, p);
370 }
371 
372 // Simple fractional decimal digit printing for values that fir in 64-bits after
373 // shifting.
374 // Performs rounding if necessary to fit within `precision`.
375 // Returns the pointer to one after the last character written.
PrintFractionalDigitsFast(uint64_t v,char * start,int exp,int precision)376 char *PrintFractionalDigitsFast(uint64_t v, char *start, int exp,
377                                 int precision) {
378   char *p = start;
379   v <<= (64 - exp);
380   while (precision > 0) {
381     if (!v) return p;
382     *p++ = MultiplyBy10WithCarry(&v, uint64_t{0}) + '0';
383     --precision;
384   }
385 
386   // We need to round.
387   if (v < 0x8000000000000000) {
388     // We round down, so nothing to do.
389   } else if (v > 0x8000000000000000) {
390     // We round up.
391     RoundUp(p - 1);
392   } else {
393     RoundToEven(p - 1);
394   }
395 
396   assert(precision == 0);
397   // Precision can only be zero here.
398   return p;
399 }
400 
401 // Simple fractional decimal digit printing for values that fir in 128-bits
402 // after shifting.
403 // Performs rounding if necessary to fit within `precision`.
404 // Returns the pointer to one after the last character written.
PrintFractionalDigitsFast(uint128 v,char * start,int exp,int precision)405 char *PrintFractionalDigitsFast(uint128 v, char *start, int exp,
406                                 int precision) {
407   char *p = start;
408   v <<= (128 - exp);
409   auto high = static_cast<uint64_t>(v >> 64);
410   auto low = static_cast<uint64_t>(v);
411 
412   // While we have digits to print and `low` is not empty, do the long
413   // multiplication.
414   while (precision > 0 && low != 0) {
415     uint64_t carry = MultiplyBy10WithCarry(&low, uint64_t{0});
416     carry = MultiplyBy10WithCarry(&high, carry);
417 
418     *p++ = carry + '0';
419     --precision;
420   }
421 
422   // Now `low` is empty, so use a faster approach for the rest of the digits.
423   // This block is pretty much the same as the main loop for the 64-bit case
424   // above.
425   while (precision > 0) {
426     if (!high) return p;
427     *p++ = MultiplyBy10WithCarry(&high, uint64_t{0}) + '0';
428     --precision;
429   }
430 
431   // We need to round.
432   if (high < 0x8000000000000000) {
433     // We round down, so nothing to do.
434   } else if (high > 0x8000000000000000 || low != 0) {
435     // We round up.
436     RoundUp(p - 1);
437   } else {
438     RoundToEven(p - 1);
439   }
440 
441   assert(precision == 0);
442   // Precision can only be zero here.
443   return p;
444 }
445 
446 struct FormatState {
447   char sign_char;
448   int precision;
449   const FormatConversionSpecImpl &conv;
450   FormatSinkImpl *sink;
451 
452   // In `alt` mode (flag #) we keep the `.` even if there are no fractional
453   // digits. In non-alt mode, we strip it.
ShouldPrintDotabsl::str_format_internal::__anon7b46425c0111::FormatState454   bool ShouldPrintDot() const { return precision != 0 || conv.has_alt_flag(); }
455 };
456 
457 struct Padding {
458   int left_spaces;
459   int zeros;
460   int right_spaces;
461 };
462 
ExtraWidthToPadding(size_t total_size,const FormatState & state)463 Padding ExtraWidthToPadding(size_t total_size, const FormatState &state) {
464   if (state.conv.width() < 0 ||
465       static_cast<size_t>(state.conv.width()) <= total_size) {
466     return {0, 0, 0};
467   }
468   int missing_chars = state.conv.width() - total_size;
469   if (state.conv.has_left_flag()) {
470     return {0, 0, missing_chars};
471   } else if (state.conv.has_zero_flag()) {
472     return {0, missing_chars, 0};
473   } else {
474     return {missing_chars, 0, 0};
475   }
476 }
477 
FinalPrint(const FormatState & state,absl::string_view data,int padding_offset,int trailing_zeros,absl::string_view data_postfix)478 void FinalPrint(const FormatState &state, absl::string_view data,
479                 int padding_offset, int trailing_zeros,
480                 absl::string_view data_postfix) {
481   if (state.conv.width() < 0) {
482     // No width specified. Fast-path.
483     if (state.sign_char != '\0') state.sink->Append(1, state.sign_char);
484     state.sink->Append(data);
485     state.sink->Append(trailing_zeros, '0');
486     state.sink->Append(data_postfix);
487     return;
488   }
489 
490   auto padding = ExtraWidthToPadding((state.sign_char != '\0' ? 1 : 0) +
491                                          data.size() + data_postfix.size() +
492                                          static_cast<size_t>(trailing_zeros),
493                                      state);
494 
495   state.sink->Append(padding.left_spaces, ' ');
496   if (state.sign_char != '\0') state.sink->Append(1, state.sign_char);
497   // Padding in general needs to be inserted somewhere in the middle of `data`.
498   state.sink->Append(data.substr(0, padding_offset));
499   state.sink->Append(padding.zeros, '0');
500   state.sink->Append(data.substr(padding_offset));
501   state.sink->Append(trailing_zeros, '0');
502   state.sink->Append(data_postfix);
503   state.sink->Append(padding.right_spaces, ' ');
504 }
505 
506 // Fastpath %f formatter for when the shifted value fits in a simple integral
507 // type.
508 // Prints `v*2^exp` with the options from `state`.
509 template <typename Int>
FormatFFast(Int v,int exp,const FormatState & state)510 void FormatFFast(Int v, int exp, const FormatState &state) {
511   constexpr int input_bits = sizeof(Int) * 8;
512 
513   static constexpr size_t integral_size =
514       /* in case we need to round up an extra digit */ 1 +
515       /* decimal digits for uint128 */ 40 + 1;
516   char buffer[integral_size + /* . */ 1 + /* max digits uint128 */ 128];
517   buffer[integral_size] = '.';
518   char *const integral_digits_end = buffer + integral_size;
519   char *integral_digits_start;
520   char *const fractional_digits_start = buffer + integral_size + 1;
521   char *fractional_digits_end = fractional_digits_start;
522 
523   if (exp >= 0) {
524     const int total_bits = input_bits - LeadingZeros(v) + exp;
525     integral_digits_start =
526         total_bits <= 64
527             ? PrintIntegralDigitsFromRightFast(static_cast<uint64_t>(v) << exp,
528                                                integral_digits_end)
529             : PrintIntegralDigitsFromRightFast(static_cast<uint128>(v) << exp,
530                                                integral_digits_end);
531   } else {
532     exp = -exp;
533 
534     integral_digits_start = PrintIntegralDigitsFromRightFast(
535         exp < input_bits ? v >> exp : 0, integral_digits_end);
536     // PrintFractionalDigits may pull a carried 1 all the way up through the
537     // integral portion.
538     integral_digits_start[-1] = '0';
539 
540     fractional_digits_end =
541         exp <= 64 ? PrintFractionalDigitsFast(v, fractional_digits_start, exp,
542                                               state.precision)
543                   : PrintFractionalDigitsFast(static_cast<uint128>(v),
544                                               fractional_digits_start, exp,
545                                               state.precision);
546     // There was a carry, so include the first digit too.
547     if (integral_digits_start[-1] != '0') --integral_digits_start;
548   }
549 
550   size_t size = fractional_digits_end - integral_digits_start;
551 
552   // In `alt` mode (flag #) we keep the `.` even if there are no fractional
553   // digits. In non-alt mode, we strip it.
554   if (!state.ShouldPrintDot()) --size;
555   FinalPrint(state, absl::string_view(integral_digits_start, size),
556              /*padding_offset=*/0,
557              static_cast<int>(state.precision - (fractional_digits_end -
558                                                  fractional_digits_start)),
559              /*data_postfix=*/"");
560 }
561 
562 // Slow %f formatter for when the shifted value does not fit in a uint128, and
563 // `exp > 0`.
564 // Prints `v*2^exp` with the options from `state`.
565 // This one is guaranteed to not have fractional digits, so we don't have to
566 // worry about anything after the `.`.
FormatFPositiveExpSlow(uint128 v,int exp,const FormatState & state)567 void FormatFPositiveExpSlow(uint128 v, int exp, const FormatState &state) {
568   BinaryToDecimal::RunConversion(v, exp, [&](BinaryToDecimal btd) {
569     const size_t total_digits =
570         btd.TotalDigits() +
571         (state.ShouldPrintDot() ? static_cast<size_t>(state.precision) + 1 : 0);
572 
573     const auto padding = ExtraWidthToPadding(
574         total_digits + (state.sign_char != '\0' ? 1 : 0), state);
575 
576     state.sink->Append(padding.left_spaces, ' ');
577     if (state.sign_char != '\0') state.sink->Append(1, state.sign_char);
578     state.sink->Append(padding.zeros, '0');
579 
580     do {
581       state.sink->Append(btd.CurrentDigits());
582     } while (btd.AdvanceDigits());
583 
584     if (state.ShouldPrintDot()) state.sink->Append(1, '.');
585     state.sink->Append(state.precision, '0');
586     state.sink->Append(padding.right_spaces, ' ');
587   });
588 }
589 
590 // Slow %f formatter for when the shifted value does not fit in a uint128, and
591 // `exp < 0`.
592 // Prints `v*2^exp` with the options from `state`.
593 // This one is guaranteed to be < 1.0, so we don't have to worry about integral
594 // digits.
FormatFNegativeExpSlow(uint128 v,int exp,const FormatState & state)595 void FormatFNegativeExpSlow(uint128 v, int exp, const FormatState &state) {
596   const size_t total_digits =
597       /* 0 */ 1 +
598       (state.ShouldPrintDot() ? static_cast<size_t>(state.precision) + 1 : 0);
599   auto padding =
600       ExtraWidthToPadding(total_digits + (state.sign_char ? 1 : 0), state);
601   padding.zeros += 1;
602   state.sink->Append(padding.left_spaces, ' ');
603   if (state.sign_char != '\0') state.sink->Append(1, state.sign_char);
604   state.sink->Append(padding.zeros, '0');
605 
606   if (state.ShouldPrintDot()) state.sink->Append(1, '.');
607 
608   // Print digits
609   int digits_to_go = state.precision;
610 
611   FractionalDigitGenerator::RunConversion(
612       v, exp, [&](FractionalDigitGenerator digit_gen) {
613         // There are no digits to print here.
614         if (state.precision == 0) return;
615 
616         // We go one digit at a time, while keeping track of runs of nines.
617         // The runs of nines are used to perform rounding when necessary.
618 
619         while (digits_to_go > 0 && digit_gen.HasMoreDigits()) {
620           auto digits = digit_gen.GetDigits();
621 
622           // Now we have a digit and a run of nines.
623           // See if we can print them all.
624           if (digits.num_nines + 1 < digits_to_go) {
625             // We don't have to round yet, so print them.
626             state.sink->Append(1, digits.digit_before_nine + '0');
627             state.sink->Append(digits.num_nines, '9');
628             digits_to_go -= digits.num_nines + 1;
629 
630           } else {
631             // We can't print all the nines, see where we have to truncate.
632 
633             bool round_up = false;
634             if (digits.num_nines + 1 > digits_to_go) {
635               // We round up at a nine. No need to print them.
636               round_up = true;
637             } else {
638               // We can fit all the nines, but truncate just after it.
639               if (digit_gen.IsGreaterThanHalf()) {
640                 round_up = true;
641               } else if (digit_gen.IsExactlyHalf()) {
642                 // Round to even
643                 round_up =
644                     digits.num_nines != 0 || digits.digit_before_nine % 2 == 1;
645               }
646             }
647 
648             if (round_up) {
649               state.sink->Append(1, digits.digit_before_nine + '1');
650               --digits_to_go;
651               // The rest will be zeros.
652             } else {
653               state.sink->Append(1, digits.digit_before_nine + '0');
654               state.sink->Append(digits_to_go - 1, '9');
655               digits_to_go = 0;
656             }
657             return;
658           }
659         }
660       });
661 
662   state.sink->Append(digits_to_go, '0');
663   state.sink->Append(padding.right_spaces, ' ');
664 }
665 
666 template <typename Int>
FormatF(Int mantissa,int exp,const FormatState & state)667 void FormatF(Int mantissa, int exp, const FormatState &state) {
668   if (exp >= 0) {
669     const int total_bits = sizeof(Int) * 8 - LeadingZeros(mantissa) + exp;
670 
671     // Fallback to the slow stack-based approach if we can't do it in a 64 or
672     // 128 bit state.
673     if (ABSL_PREDICT_FALSE(total_bits > 128)) {
674       return FormatFPositiveExpSlow(mantissa, exp, state);
675     }
676   } else {
677     // Fallback to the slow stack-based approach if we can't do it in a 64 or
678     // 128 bit state.
679     if (ABSL_PREDICT_FALSE(exp < -128)) {
680       return FormatFNegativeExpSlow(mantissa, -exp, state);
681     }
682   }
683   return FormatFFast(mantissa, exp, state);
684 }
685 
686 // Grab the group of four bits (nibble) from `n`. E.g., nibble 1 corresponds to
687 // bits 4-7.
688 template <typename Int>
GetNibble(Int n,int nibble_index)689 uint8_t GetNibble(Int n, int nibble_index) {
690   constexpr Int mask_low_nibble = Int{0xf};
691   int shift = nibble_index * 4;
692   n &= mask_low_nibble << shift;
693   return static_cast<uint8_t>((n >> shift) & 0xf);
694 }
695 
696 // Add one to the given nibble, applying carry to higher nibbles. Returns true
697 // if overflow, false otherwise.
698 template <typename Int>
IncrementNibble(int nibble_index,Int * n)699 bool IncrementNibble(int nibble_index, Int *n) {
700   constexpr int kShift = sizeof(Int) * 8 - 1;
701   constexpr int kNumNibbles = sizeof(Int) * 8 / 4;
702   Int before = *n >> kShift;
703   // Here we essentially want to take the number 1 and move it into the requsted
704   // nibble, then add it to *n to effectively increment the nibble. However,
705   // ASan will complain if we try to shift the 1 beyond the limits of the Int,
706   // i.e., if the nibble_index is out of range. So therefore we check for this
707   // and if we are out of range we just add 0 which leaves *n unchanged, which
708   // seems like the reasonable thing to do in that case.
709   *n += ((nibble_index >= kNumNibbles) ? 0 : (Int{1} << (nibble_index * 4)));
710   Int after = *n >> kShift;
711   return (before && !after) || (nibble_index >= kNumNibbles);
712 }
713 
714 // Return a mask with 1's in the given nibble and all lower nibbles.
715 template <typename Int>
MaskUpToNibbleInclusive(int nibble_index)716 Int MaskUpToNibbleInclusive(int nibble_index) {
717   constexpr int kNumNibbles = sizeof(Int) * 8 / 4;
718   static const Int ones = ~Int{0};
719   return ones >> std::max(0, 4 * (kNumNibbles - nibble_index - 1));
720 }
721 
722 // Return a mask with 1's below the given nibble.
723 template <typename Int>
MaskUpToNibbleExclusive(int nibble_index)724 Int MaskUpToNibbleExclusive(int nibble_index) {
725   return nibble_index <= 0 ? 0 : MaskUpToNibbleInclusive<Int>(nibble_index - 1);
726 }
727 
728 template <typename Int>
MoveToNibble(uint8_t nibble,int nibble_index)729 Int MoveToNibble(uint8_t nibble, int nibble_index) {
730   return Int{nibble} << (4 * nibble_index);
731 }
732 
733 // Given mantissa size, find optimal # of mantissa bits to put in initial digit.
734 //
735 // In the hex representation we keep a single hex digit to the left of the dot.
736 // However, the question as to how many bits of the mantissa should be put into
737 // that hex digit in theory is arbitrary, but in practice it is optimal to
738 // choose based on the size of the mantissa. E.g., for a `double`, there are 53
739 // mantissa bits, so that means that we should put 1 bit to the left of the dot,
740 // thereby leaving 52 bits to the right, which is evenly divisible by four and
741 // thus all fractional digits represent actual precision. For a `long double`,
742 // on the other hand, there are 64 bits of mantissa, thus we can use all four
743 // bits for the initial hex digit and still have a number left over (60) that is
744 // a multiple of four. Once again, the goal is to have all fractional digits
745 // represent real precision.
746 template <typename Float>
HexFloatLeadingDigitSizeInBits()747 constexpr int HexFloatLeadingDigitSizeInBits() {
748   return std::numeric_limits<Float>::digits % 4 > 0
749              ? std::numeric_limits<Float>::digits % 4
750              : 4;
751 }
752 
753 // This function captures the rounding behavior of glibc for hex float
754 // representations. E.g. when rounding 0x1.ab800000 to a precision of .2
755 // ("%.2a") glibc will round up because it rounds toward the even number (since
756 // 0xb is an odd number, it will round up to 0xc). However, when rounding at a
757 // point that is not followed by 800000..., it disregards the parity and rounds
758 // up if > 8 and rounds down if < 8.
759 template <typename Int>
HexFloatNeedsRoundUp(Int mantissa,int final_nibble_displayed,uint8_t leading)760 bool HexFloatNeedsRoundUp(Int mantissa, int final_nibble_displayed,
761                           uint8_t leading) {
762   // If the last nibble (hex digit) to be displayed is the lowest on in the
763   // mantissa then that means that we don't have any further nibbles to inform
764   // rounding, so don't round.
765   if (final_nibble_displayed <= 0) {
766     return false;
767   }
768   int rounding_nibble_idx = final_nibble_displayed - 1;
769   constexpr int kTotalNibbles = sizeof(Int) * 8 / 4;
770   assert(final_nibble_displayed <= kTotalNibbles);
771   Int mantissa_up_to_rounding_nibble_inclusive =
772       mantissa & MaskUpToNibbleInclusive<Int>(rounding_nibble_idx);
773   Int eight = MoveToNibble<Int>(8, rounding_nibble_idx);
774   if (mantissa_up_to_rounding_nibble_inclusive != eight) {
775     return mantissa_up_to_rounding_nibble_inclusive > eight;
776   }
777   // Nibble in question == 8.
778   uint8_t round_if_odd = (final_nibble_displayed == kTotalNibbles)
779                              ? leading
780                              : GetNibble(mantissa, final_nibble_displayed);
781   return round_if_odd % 2 == 1;
782 }
783 
784 // Stores values associated with a Float type needed by the FormatA
785 // implementation in order to avoid templatizing that function by the Float
786 // type.
787 struct HexFloatTypeParams {
788   template <typename Float>
HexFloatTypeParamsabsl::str_format_internal::__anon7b46425c0111::HexFloatTypeParams789   explicit HexFloatTypeParams(Float)
790       : min_exponent(std::numeric_limits<Float>::min_exponent - 1),
791         leading_digit_size_bits(HexFloatLeadingDigitSizeInBits<Float>()) {
792     assert(leading_digit_size_bits >= 1 && leading_digit_size_bits <= 4);
793   }
794 
795   int min_exponent;
796   int leading_digit_size_bits;
797 };
798 
799 // Hex Float Rounding. First check if we need to round; if so, then we do that
800 // by manipulating (incrementing) the mantissa, that way we can later print the
801 // mantissa digits by iterating through them in the same way regardless of
802 // whether a rounding happened.
803 template <typename Int>
FormatARound(bool precision_specified,const FormatState & state,uint8_t * leading,Int * mantissa,int * exp)804 void FormatARound(bool precision_specified, const FormatState &state,
805                   uint8_t *leading, Int *mantissa, int *exp) {
806   constexpr int kTotalNibbles = sizeof(Int) * 8 / 4;
807   // Index of the last nibble that we could display given precision.
808   int final_nibble_displayed =
809       precision_specified ? std::max(0, (kTotalNibbles - state.precision)) : 0;
810   if (HexFloatNeedsRoundUp(*mantissa, final_nibble_displayed, *leading)) {
811     // Need to round up.
812     bool overflow = IncrementNibble(final_nibble_displayed, mantissa);
813     *leading += (overflow ? 1 : 0);
814     if (ABSL_PREDICT_FALSE(*leading > 15)) {
815       // We have overflowed the leading digit. This would mean that we would
816       // need two hex digits to the left of the dot, which is not allowed. So
817       // adjust the mantissa and exponent so that the result is always 1.0eXXX.
818       *leading = 1;
819       *mantissa = 0;
820       *exp += 4;
821     }
822   }
823   // Now that we have handled a possible round-up we can go ahead and zero out
824   // all the nibbles of the mantissa that we won't need.
825   if (precision_specified) {
826     *mantissa &= ~MaskUpToNibbleExclusive<Int>(final_nibble_displayed);
827   }
828 }
829 
830 template <typename Int>
FormatANormalize(const HexFloatTypeParams float_traits,uint8_t * leading,Int * mantissa,int * exp)831 void FormatANormalize(const HexFloatTypeParams float_traits, uint8_t *leading,
832                       Int *mantissa, int *exp) {
833   constexpr int kIntBits = sizeof(Int) * 8;
834   static const Int kHighIntBit = Int{1} << (kIntBits - 1);
835   const int kLeadDigitBitsCount = float_traits.leading_digit_size_bits;
836   // Normalize mantissa so that highest bit set is in MSB position, unless we
837   // get interrupted by the exponent threshold.
838   while (*mantissa && !(*mantissa & kHighIntBit)) {
839     if (ABSL_PREDICT_FALSE(*exp - 1 < float_traits.min_exponent)) {
840       *mantissa >>= (float_traits.min_exponent - *exp);
841       *exp = float_traits.min_exponent;
842       return;
843     }
844     *mantissa <<= 1;
845     --*exp;
846   }
847   // Extract bits for leading digit then shift them away leaving the
848   // fractional part.
849   *leading =
850       static_cast<uint8_t>(*mantissa >> (kIntBits - kLeadDigitBitsCount));
851   *exp -= (*mantissa != 0) ? kLeadDigitBitsCount : *exp;
852   *mantissa <<= kLeadDigitBitsCount;
853 }
854 
855 template <typename Int>
FormatA(const HexFloatTypeParams float_traits,Int mantissa,int exp,bool uppercase,const FormatState & state)856 void FormatA(const HexFloatTypeParams float_traits, Int mantissa, int exp,
857              bool uppercase, const FormatState &state) {
858   // Int properties.
859   constexpr int kIntBits = sizeof(Int) * 8;
860   constexpr int kTotalNibbles = sizeof(Int) * 8 / 4;
861   // Did the user specify a precision explicitly?
862   const bool precision_specified = state.conv.precision() >= 0;
863 
864   // ========== Normalize/Denormalize ==========
865   exp += kIntBits;  // make all digits fractional digits.
866   // This holds the (up to four) bits of leading digit, i.e., the '1' in the
867   // number 0x1.e6fp+2. It's always > 0 unless number is zero or denormal.
868   uint8_t leading = 0;
869   FormatANormalize(float_traits, &leading, &mantissa, &exp);
870 
871   // =============== Rounding ==================
872   // Check if we need to round; if so, then we do that by manipulating
873   // (incrementing) the mantissa before beginning to print characters.
874   FormatARound(precision_specified, state, &leading, &mantissa, &exp);
875 
876   // ============= Format Result ===============
877   // This buffer holds the "0x1.ab1de3" portion of "0x1.ab1de3pe+2". Compute the
878   // size with long double which is the largest of the floats.
879   constexpr size_t kBufSizeForHexFloatRepr =
880       2                                                // 0x
881       + std::numeric_limits<MaxFloatType>::digits / 4  // number of hex digits
882       + 1                                              // round up
883       + 1;                                             // "." (dot)
884   char digits_buffer[kBufSizeForHexFloatRepr];
885   char *digits_iter = digits_buffer;
886   const char *const digits =
887       static_cast<const char *>("0123456789ABCDEF0123456789abcdef") +
888       (uppercase ? 0 : 16);
889 
890   // =============== Hex Prefix ================
891   *digits_iter++ = '0';
892   *digits_iter++ = uppercase ? 'X' : 'x';
893 
894   // ========== Non-Fractional Digit ===========
895   *digits_iter++ = digits[leading];
896 
897   // ================== Dot ====================
898   // There are three reasons we might need a dot. Keep in mind that, at this
899   // point, the mantissa holds only the fractional part.
900   if ((precision_specified && state.precision > 0) ||
901       (!precision_specified && mantissa > 0) || state.conv.has_alt_flag()) {
902     *digits_iter++ = '.';
903   }
904 
905   // ============ Fractional Digits ============
906   int digits_emitted = 0;
907   while (mantissa > 0) {
908     *digits_iter++ = digits[GetNibble(mantissa, kTotalNibbles - 1)];
909     mantissa <<= 4;
910     ++digits_emitted;
911   }
912   int trailing_zeros =
913       precision_specified ? state.precision - digits_emitted : 0;
914   assert(trailing_zeros >= 0);
915   auto digits_result = string_view(digits_buffer, digits_iter - digits_buffer);
916 
917   // =============== Exponent ==================
918   constexpr size_t kBufSizeForExpDecRepr =
919       numbers_internal::kFastToBufferSize  // requred for FastIntToBuffer
920       + 1                                  // 'p' or 'P'
921       + 1;                                 // '+' or '-'
922   char exp_buffer[kBufSizeForExpDecRepr];
923   exp_buffer[0] = uppercase ? 'P' : 'p';
924   exp_buffer[1] = exp >= 0 ? '+' : '-';
925   numbers_internal::FastIntToBuffer(exp < 0 ? -exp : exp, exp_buffer + 2);
926 
927   // ============ Assemble Result ==============
928   FinalPrint(state,           //
929              digits_result,   // 0xN.NNN...
930              2,               // offset in `data` to start padding if needed.
931              trailing_zeros,  // num remaining mantissa padding zeros
932              exp_buffer);     // exponent
933 }
934 
CopyStringTo(absl::string_view v,char * out)935 char *CopyStringTo(absl::string_view v, char *out) {
936   std::memcpy(out, v.data(), v.size());
937   return out + v.size();
938 }
939 
940 template <typename Float>
FallbackToSnprintf(const Float v,const FormatConversionSpecImpl & conv,FormatSinkImpl * sink)941 bool FallbackToSnprintf(const Float v, const FormatConversionSpecImpl &conv,
942                         FormatSinkImpl *sink) {
943   int w = conv.width() >= 0 ? conv.width() : 0;
944   int p = conv.precision() >= 0 ? conv.precision() : -1;
945   char fmt[32];
946   {
947     char *fp = fmt;
948     *fp++ = '%';
949     fp = CopyStringTo(FormatConversionSpecImplFriend::FlagsToString(conv), fp);
950     fp = CopyStringTo("*.*", fp);
951     if (std::is_same<long double, Float>()) {
952       *fp++ = 'L';
953     }
954     *fp++ = FormatConversionCharToChar(conv.conversion_char());
955     *fp = 0;
956     assert(fp < fmt + sizeof(fmt));
957   }
958   std::string space(512, '\0');
959   absl::string_view result;
960   while (true) {
961     int n = snprintf(&space[0], space.size(), fmt, w, p, v);
962     if (n < 0) return false;
963     if (static_cast<size_t>(n) < space.size()) {
964       result = absl::string_view(space.data(), n);
965       break;
966     }
967     space.resize(n + 1);
968   }
969   sink->Append(result);
970   return true;
971 }
972 
973 // 128-bits in decimal: ceil(128*log(2)/log(10))
974 //   or std::numeric_limits<__uint128_t>::digits10
975 constexpr int kMaxFixedPrecision = 39;
976 
977 constexpr int kBufferLength = /*sign*/ 1 +
978                               /*integer*/ kMaxFixedPrecision +
979                               /*point*/ 1 +
980                               /*fraction*/ kMaxFixedPrecision +
981                               /*exponent e+123*/ 5;
982 
983 struct Buffer {
push_frontabsl::str_format_internal::__anon7b46425c0111::Buffer984   void push_front(char c) {
985     assert(begin > data);
986     *--begin = c;
987   }
push_backabsl::str_format_internal::__anon7b46425c0111::Buffer988   void push_back(char c) {
989     assert(end < data + sizeof(data));
990     *end++ = c;
991   }
pop_backabsl::str_format_internal::__anon7b46425c0111::Buffer992   void pop_back() {
993     assert(begin < end);
994     --end;
995   }
996 
backabsl::str_format_internal::__anon7b46425c0111::Buffer997   char &back() {
998     assert(begin < end);
999     return end[-1];
1000   }
1001 
last_digitabsl::str_format_internal::__anon7b46425c0111::Buffer1002   char last_digit() const { return end[-1] == '.' ? end[-2] : end[-1]; }
1003 
sizeabsl::str_format_internal::__anon7b46425c0111::Buffer1004   int size() const { return static_cast<int>(end - begin); }
1005 
1006   char data[kBufferLength];
1007   char *begin;
1008   char *end;
1009 };
1010 
1011 enum class FormatStyle { Fixed, Precision };
1012 
1013 // If the value is Inf or Nan, print it and return true.
1014 // Otherwise, return false.
1015 template <typename Float>
ConvertNonNumericFloats(char sign_char,Float v,const FormatConversionSpecImpl & conv,FormatSinkImpl * sink)1016 bool ConvertNonNumericFloats(char sign_char, Float v,
1017                              const FormatConversionSpecImpl &conv,
1018                              FormatSinkImpl *sink) {
1019   char text[4], *ptr = text;
1020   if (sign_char != '\0') *ptr++ = sign_char;
1021   if (std::isnan(v)) {
1022     ptr = std::copy_n(
1023         FormatConversionCharIsUpper(conv.conversion_char()) ? "NAN" : "nan", 3,
1024         ptr);
1025   } else if (std::isinf(v)) {
1026     ptr = std::copy_n(
1027         FormatConversionCharIsUpper(conv.conversion_char()) ? "INF" : "inf", 3,
1028         ptr);
1029   } else {
1030     return false;
1031   }
1032 
1033   return sink->PutPaddedString(string_view(text, ptr - text), conv.width(), -1,
1034                                conv.has_left_flag());
1035 }
1036 
1037 // Round up the last digit of the value.
1038 // It will carry over and potentially overflow. 'exp' will be adjusted in that
1039 // case.
1040 template <FormatStyle mode>
RoundUp(Buffer * buffer,int * exp)1041 void RoundUp(Buffer *buffer, int *exp) {
1042   char *p = &buffer->back();
1043   while (p >= buffer->begin && (*p == '9' || *p == '.')) {
1044     if (*p == '9') *p = '0';
1045     --p;
1046   }
1047 
1048   if (p < buffer->begin) {
1049     *p = '1';
1050     buffer->begin = p;
1051     if (mode == FormatStyle::Precision) {
1052       std::swap(p[1], p[2]);  // move the .
1053       ++*exp;
1054       buffer->pop_back();
1055     }
1056   } else {
1057     ++*p;
1058   }
1059 }
1060 
PrintExponent(int exp,char e,Buffer * out)1061 void PrintExponent(int exp, char e, Buffer *out) {
1062   out->push_back(e);
1063   if (exp < 0) {
1064     out->push_back('-');
1065     exp = -exp;
1066   } else {
1067     out->push_back('+');
1068   }
1069   // Exponent digits.
1070   if (exp > 99) {
1071     out->push_back(exp / 100 + '0');
1072     out->push_back(exp / 10 % 10 + '0');
1073     out->push_back(exp % 10 + '0');
1074   } else {
1075     out->push_back(exp / 10 + '0');
1076     out->push_back(exp % 10 + '0');
1077   }
1078 }
1079 
1080 template <typename Float, typename Int>
CanFitMantissa()1081 constexpr bool CanFitMantissa() {
1082   return
1083 #if defined(__clang__) && !defined(__SSE3__)
1084       // Workaround for clang bug: https://bugs.llvm.org/show_bug.cgi?id=38289
1085       // Casting from long double to uint64_t is miscompiled and drops bits.
1086       (!std::is_same<Float, long double>::value ||
1087        !std::is_same<Int, uint64_t>::value) &&
1088 #endif
1089       std::numeric_limits<Float>::digits <= std::numeric_limits<Int>::digits;
1090 }
1091 
1092 template <typename Float>
1093 struct Decomposed {
1094   using MantissaType =
1095       absl::conditional_t<std::is_same<long double, Float>::value, uint128,
1096                           uint64_t>;
1097   static_assert(std::numeric_limits<Float>::digits <= sizeof(MantissaType) * 8,
1098                 "");
1099   MantissaType mantissa;
1100   int exponent;
1101 };
1102 
1103 // Decompose the double into an integer mantissa and an exponent.
1104 template <typename Float>
Decompose(Float v)1105 Decomposed<Float> Decompose(Float v) {
1106   int exp;
1107   Float m = std::frexp(v, &exp);
1108   m = std::ldexp(m, std::numeric_limits<Float>::digits);
1109   exp -= std::numeric_limits<Float>::digits;
1110 
1111   return {static_cast<typename Decomposed<Float>::MantissaType>(m), exp};
1112 }
1113 
1114 // Print 'digits' as decimal.
1115 // In Fixed mode, we add a '.' at the end.
1116 // In Precision mode, we add a '.' after the first digit.
1117 template <FormatStyle mode, typename Int>
PrintIntegralDigits(Int digits,Buffer * out)1118 int PrintIntegralDigits(Int digits, Buffer *out) {
1119   int printed = 0;
1120   if (digits) {
1121     for (; digits; digits /= 10) out->push_front(digits % 10 + '0');
1122     printed = out->size();
1123     if (mode == FormatStyle::Precision) {
1124       out->push_front(*out->begin);
1125       out->begin[1] = '.';
1126     } else {
1127       out->push_back('.');
1128     }
1129   } else if (mode == FormatStyle::Fixed) {
1130     out->push_front('0');
1131     out->push_back('.');
1132     printed = 1;
1133   }
1134   return printed;
1135 }
1136 
1137 // Back out 'extra_digits' digits and round up if necessary.
RemoveExtraPrecision(int extra_digits,bool has_leftover_value,Buffer * out,int * exp_out)1138 bool RemoveExtraPrecision(int extra_digits, bool has_leftover_value,
1139                           Buffer *out, int *exp_out) {
1140   if (extra_digits <= 0) return false;
1141 
1142   // Back out the extra digits
1143   out->end -= extra_digits;
1144 
1145   bool needs_to_round_up = [&] {
1146     // We look at the digit just past the end.
1147     // There must be 'extra_digits' extra valid digits after end.
1148     if (*out->end > '5') return true;
1149     if (*out->end < '5') return false;
1150     if (has_leftover_value || std::any_of(out->end + 1, out->end + extra_digits,
1151                                           [](char c) { return c != '0'; }))
1152       return true;
1153 
1154     // Ends in ...50*, round to even.
1155     return out->last_digit() % 2 == 1;
1156   }();
1157 
1158   if (needs_to_round_up) {
1159     RoundUp<FormatStyle::Precision>(out, exp_out);
1160   }
1161   return true;
1162 }
1163 
1164 // Print the value into the buffer.
1165 // This will not include the exponent, which will be returned in 'exp_out' for
1166 // Precision mode.
1167 template <typename Int, typename Float, FormatStyle mode>
FloatToBufferImpl(Int int_mantissa,int exp,int precision,Buffer * out,int * exp_out)1168 bool FloatToBufferImpl(Int int_mantissa, int exp, int precision, Buffer *out,
1169                        int *exp_out) {
1170   assert((CanFitMantissa<Float, Int>()));
1171 
1172   const int int_bits = std::numeric_limits<Int>::digits;
1173 
1174   // In precision mode, we start printing one char to the right because it will
1175   // also include the '.'
1176   // In fixed mode we put the dot afterwards on the right.
1177   out->begin = out->end =
1178       out->data + 1 + kMaxFixedPrecision + (mode == FormatStyle::Precision);
1179 
1180   if (exp >= 0) {
1181     if (std::numeric_limits<Float>::digits + exp > int_bits) {
1182       // The value will overflow the Int
1183       return false;
1184     }
1185     int digits_printed = PrintIntegralDigits<mode>(int_mantissa << exp, out);
1186     int digits_to_zero_pad = precision;
1187     if (mode == FormatStyle::Precision) {
1188       *exp_out = digits_printed - 1;
1189       digits_to_zero_pad -= digits_printed - 1;
1190       if (RemoveExtraPrecision(-digits_to_zero_pad, false, out, exp_out)) {
1191         return true;
1192       }
1193     }
1194     for (; digits_to_zero_pad-- > 0;) out->push_back('0');
1195     return true;
1196   }
1197 
1198   exp = -exp;
1199   // We need at least 4 empty bits for the next decimal digit.
1200   // We will multiply by 10.
1201   if (exp > int_bits - 4) return false;
1202 
1203   const Int mask = (Int{1} << exp) - 1;
1204 
1205   // Print the integral part first.
1206   int digits_printed = PrintIntegralDigits<mode>(int_mantissa >> exp, out);
1207   int_mantissa &= mask;
1208 
1209   int fractional_count = precision;
1210   if (mode == FormatStyle::Precision) {
1211     if (digits_printed == 0) {
1212       // Find the first non-zero digit, when in Precision mode.
1213       *exp_out = 0;
1214       if (int_mantissa) {
1215         while (int_mantissa <= mask) {
1216           int_mantissa *= 10;
1217           --*exp_out;
1218         }
1219       }
1220       out->push_front(static_cast<char>(int_mantissa >> exp) + '0');
1221       out->push_back('.');
1222       int_mantissa &= mask;
1223     } else {
1224       // We already have a digit, and a '.'
1225       *exp_out = digits_printed - 1;
1226       fractional_count -= *exp_out;
1227       if (RemoveExtraPrecision(-fractional_count, int_mantissa != 0, out,
1228                                exp_out)) {
1229         // If we had enough digits, return right away.
1230         // The code below will try to round again otherwise.
1231         return true;
1232       }
1233     }
1234   }
1235 
1236   auto get_next_digit = [&] {
1237     int_mantissa *= 10;
1238     int digit = static_cast<int>(int_mantissa >> exp);
1239     int_mantissa &= mask;
1240     return digit;
1241   };
1242 
1243   // Print fractional_count more digits, if available.
1244   for (; fractional_count > 0; --fractional_count) {
1245     out->push_back(get_next_digit() + '0');
1246   }
1247 
1248   int next_digit = get_next_digit();
1249   if (next_digit > 5 ||
1250       (next_digit == 5 && (int_mantissa || out->last_digit() % 2 == 1))) {
1251     RoundUp<mode>(out, exp_out);
1252   }
1253 
1254   return true;
1255 }
1256 
1257 template <FormatStyle mode, typename Float>
FloatToBuffer(Decomposed<Float> decomposed,int precision,Buffer * out,int * exp)1258 bool FloatToBuffer(Decomposed<Float> decomposed, int precision, Buffer *out,
1259                    int *exp) {
1260   if (precision > kMaxFixedPrecision) return false;
1261 
1262   // Try with uint64_t.
1263   if (CanFitMantissa<Float, std::uint64_t>() &&
1264       FloatToBufferImpl<std::uint64_t, Float, mode>(
1265           static_cast<std::uint64_t>(decomposed.mantissa),
1266           static_cast<std::uint64_t>(decomposed.exponent), precision, out, exp))
1267     return true;
1268 
1269 #if defined(ABSL_HAVE_INTRINSIC_INT128)
1270   // If that is not enough, try with __uint128_t.
1271   return CanFitMantissa<Float, __uint128_t>() &&
1272          FloatToBufferImpl<__uint128_t, Float, mode>(
1273              static_cast<__uint128_t>(decomposed.mantissa),
1274              static_cast<__uint128_t>(decomposed.exponent), precision, out,
1275              exp);
1276 #endif
1277   return false;
1278 }
1279 
WriteBufferToSink(char sign_char,absl::string_view str,const FormatConversionSpecImpl & conv,FormatSinkImpl * sink)1280 void WriteBufferToSink(char sign_char, absl::string_view str,
1281                        const FormatConversionSpecImpl &conv,
1282                        FormatSinkImpl *sink) {
1283   int left_spaces = 0, zeros = 0, right_spaces = 0;
1284   int missing_chars =
1285       conv.width() >= 0 ? std::max(conv.width() - static_cast<int>(str.size()) -
1286                                        static_cast<int>(sign_char != 0),
1287                                    0)
1288                         : 0;
1289   if (conv.has_left_flag()) {
1290     right_spaces = missing_chars;
1291   } else if (conv.has_zero_flag()) {
1292     zeros = missing_chars;
1293   } else {
1294     left_spaces = missing_chars;
1295   }
1296 
1297   sink->Append(left_spaces, ' ');
1298   if (sign_char != '\0') sink->Append(1, sign_char);
1299   sink->Append(zeros, '0');
1300   sink->Append(str);
1301   sink->Append(right_spaces, ' ');
1302 }
1303 
1304 template <typename Float>
FloatToSink(const Float v,const FormatConversionSpecImpl & conv,FormatSinkImpl * sink)1305 bool FloatToSink(const Float v, const FormatConversionSpecImpl &conv,
1306                  FormatSinkImpl *sink) {
1307   // Print the sign or the sign column.
1308   Float abs_v = v;
1309   char sign_char = 0;
1310   if (std::signbit(abs_v)) {
1311     sign_char = '-';
1312     abs_v = -abs_v;
1313   } else if (conv.has_show_pos_flag()) {
1314     sign_char = '+';
1315   } else if (conv.has_sign_col_flag()) {
1316     sign_char = ' ';
1317   }
1318 
1319   // Print nan/inf.
1320   if (ConvertNonNumericFloats(sign_char, abs_v, conv, sink)) {
1321     return true;
1322   }
1323 
1324   int precision = conv.precision() < 0 ? 6 : conv.precision();
1325 
1326   int exp = 0;
1327 
1328   auto decomposed = Decompose(abs_v);
1329 
1330   Buffer buffer;
1331 
1332   FormatConversionChar c = conv.conversion_char();
1333 
1334   if (c == FormatConversionCharInternal::f ||
1335       c == FormatConversionCharInternal::F) {
1336     FormatF(decomposed.mantissa, decomposed.exponent,
1337             {sign_char, precision, conv, sink});
1338     return true;
1339   } else if (c == FormatConversionCharInternal::e ||
1340              c == FormatConversionCharInternal::E) {
1341     if (!FloatToBuffer<FormatStyle::Precision>(decomposed, precision, &buffer,
1342                                                &exp)) {
1343       return FallbackToSnprintf(v, conv, sink);
1344     }
1345     if (!conv.has_alt_flag() && buffer.back() == '.') buffer.pop_back();
1346     PrintExponent(
1347         exp, FormatConversionCharIsUpper(conv.conversion_char()) ? 'E' : 'e',
1348         &buffer);
1349   } else if (c == FormatConversionCharInternal::g ||
1350              c == FormatConversionCharInternal::G) {
1351     precision = std::max(0, precision - 1);
1352     if (!FloatToBuffer<FormatStyle::Precision>(decomposed, precision, &buffer,
1353                                                &exp)) {
1354       return FallbackToSnprintf(v, conv, sink);
1355     }
1356     if (precision + 1 > exp && exp >= -4) {
1357       if (exp < 0) {
1358         // Have 1.23456, needs 0.00123456
1359         // Move the first digit
1360         buffer.begin[1] = *buffer.begin;
1361         // Add some zeros
1362         for (; exp < -1; ++exp) *buffer.begin-- = '0';
1363         *buffer.begin-- = '.';
1364         *buffer.begin = '0';
1365       } else if (exp > 0) {
1366         // Have 1.23456, needs 1234.56
1367         // Move the '.' exp positions to the right.
1368         std::rotate(buffer.begin + 1, buffer.begin + 2, buffer.begin + exp + 2);
1369       }
1370       exp = 0;
1371     }
1372     if (!conv.has_alt_flag()) {
1373       while (buffer.back() == '0') buffer.pop_back();
1374       if (buffer.back() == '.') buffer.pop_back();
1375     }
1376     if (exp) {
1377       PrintExponent(
1378           exp, FormatConversionCharIsUpper(conv.conversion_char()) ? 'E' : 'e',
1379           &buffer);
1380     }
1381   } else if (c == FormatConversionCharInternal::a ||
1382              c == FormatConversionCharInternal::A) {
1383     bool uppercase = (c == FormatConversionCharInternal::A);
1384     FormatA(HexFloatTypeParams(Float{}), decomposed.mantissa,
1385             decomposed.exponent, uppercase, {sign_char, precision, conv, sink});
1386     return true;
1387   } else {
1388     return false;
1389   }
1390 
1391   WriteBufferToSink(sign_char,
1392                     absl::string_view(buffer.begin, buffer.end - buffer.begin),
1393                     conv, sink);
1394 
1395   return true;
1396 }
1397 
1398 }  // namespace
1399 
ConvertFloatImpl(long double v,const FormatConversionSpecImpl & conv,FormatSinkImpl * sink)1400 bool ConvertFloatImpl(long double v, const FormatConversionSpecImpl &conv,
1401                       FormatSinkImpl *sink) {
1402   if (IsDoubleDouble()) {
1403     // This is the `double-double` representation of `long double`. We do not
1404     // handle it natively. Fallback to snprintf.
1405     return FallbackToSnprintf(v, conv, sink);
1406   }
1407 
1408   return FloatToSink(v, conv, sink);
1409 }
1410 
ConvertFloatImpl(float v,const FormatConversionSpecImpl & conv,FormatSinkImpl * sink)1411 bool ConvertFloatImpl(float v, const FormatConversionSpecImpl &conv,
1412                       FormatSinkImpl *sink) {
1413   return FloatToSink(static_cast<double>(v), conv, sink);
1414 }
1415 
ConvertFloatImpl(double v,const FormatConversionSpecImpl & conv,FormatSinkImpl * sink)1416 bool ConvertFloatImpl(double v, const FormatConversionSpecImpl &conv,
1417                       FormatSinkImpl *sink) {
1418   return FloatToSink(v, conv, sink);
1419 }
1420 
1421 }  // namespace str_format_internal
1422 ABSL_NAMESPACE_END
1423 }  // namespace absl
1424