xref: /llvm-project/libc/src/stdio/printf_core/float_dec_converter.h (revision a0c4f854cad2b97e44a1b58dc1fd982e1c4d60f3)
1 //===-- Decimal Float Converter for printf ----------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_FLOAT_DEC_CONVERTER_H
10 #define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_FLOAT_DEC_CONVERTER_H
11 
12 #include "src/__support/CPP/string_view.h"
13 #include "src/__support/FPUtil/FPBits.h"
14 #include "src/__support/FPUtil/rounding_mode.h"
15 #include "src/__support/big_int.h" // is_big_int_v
16 #include "src/__support/ctype_utils.h"
17 #include "src/__support/float_to_string.h"
18 #include "src/__support/integer_to_string.h"
19 #include "src/__support/libc_assert.h"
20 #include "src/__support/macros/config.h"
21 #include "src/stdio/printf_core/converter_utils.h"
22 #include "src/stdio/printf_core/core_structs.h"
23 #include "src/stdio/printf_core/float_inf_nan_converter.h"
24 #include "src/stdio/printf_core/writer.h"
25 
26 #include <inttypes.h>
27 #include <stddef.h>
28 
29 namespace LIBC_NAMESPACE_DECL {
30 namespace printf_core {
31 
32 using StorageType = fputil::FPBits<long double>::StorageType;
33 using DecimalString = IntegerToString<intmax_t>;
34 using ExponentString =
35     IntegerToString<intmax_t, radix::Dec::WithWidth<2>::WithSign>;
36 
37 // Returns true if value is divisible by 2^p.
38 template <typename T>
39 LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_integral_v<T> || is_big_int_v<T>,
40                                        bool>
41 multiple_of_power_of_2(T value, uint32_t p) {
42   return (value & ((T(1) << p) - 1)) == 0;
43 }
44 
45 constexpr size_t BLOCK_SIZE = 9;
46 constexpr uint32_t MAX_BLOCK = 999999999;
47 
48 // constexpr size_t BLOCK_SIZE = 18;
49 // constexpr uint32_t MAX_BLOCK = 999999999999999999;
50 constexpr char DECIMAL_POINT = '.';
51 
52 LIBC_INLINE RoundDirection get_round_direction(int last_digit, bool truncated,
53                                                Sign sign) {
54   switch (fputil::quick_get_round()) {
55   case FE_TONEAREST:
56     // Round to nearest, if it's exactly halfway then round to even.
57     if (last_digit != 5) {
58       return last_digit > 5 ? RoundDirection::Up : RoundDirection::Down;
59     } else {
60       return !truncated ? RoundDirection::Even : RoundDirection::Up;
61     }
62   case FE_DOWNWARD:
63     if (sign.is_neg() && (truncated || last_digit > 0)) {
64       return RoundDirection::Up;
65     } else {
66       return RoundDirection::Down;
67     }
68   case FE_UPWARD:
69     if (sign.is_pos() && (truncated || last_digit > 0)) {
70       return RoundDirection::Up;
71     } else {
72       return RoundDirection::Down;
73     }
74     return sign.is_neg() ? RoundDirection::Down : RoundDirection::Up;
75   case FE_TOWARDZERO:
76     return RoundDirection::Down;
77   default:
78     return RoundDirection::Down;
79   }
80 }
81 
82 template <typename T>
83 LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_integral_v<T> || is_big_int_v<T>,
84                                        bool>
85 zero_after_digits(int32_t base_2_exp, int32_t digits_after_point, T mantissa,
86                   const int32_t mant_width) {
87   const int32_t required_twos = -base_2_exp - digits_after_point - 1;
88   // Add 8 to mant width since this is a loose bound.
89   const bool has_trailing_zeros =
90       required_twos <= 0 ||
91       (required_twos < (mant_width + 8) &&
92        multiple_of_power_of_2(mantissa, static_cast<uint32_t>(required_twos)));
93   return has_trailing_zeros;
94 }
95 
96 class PaddingWriter {
97   bool left_justified = false;
98   bool leading_zeroes = false;
99   char sign_char = 0;
100   size_t min_width = 0;
101 
102 public:
103   LIBC_INLINE PaddingWriter() {}
104   LIBC_INLINE PaddingWriter(const FormatSection &to_conv, char init_sign_char)
105       : left_justified((to_conv.flags & FormatFlags::LEFT_JUSTIFIED) > 0),
106         leading_zeroes((to_conv.flags & FormatFlags::LEADING_ZEROES) > 0),
107         sign_char(init_sign_char),
108         min_width(to_conv.min_width > 0 ? to_conv.min_width : 0) {}
109 
110   LIBC_INLINE int write_left_padding(Writer *writer, size_t total_digits) {
111     // The pattern is (spaces) (sign) (zeroes), but only one of spaces and
112     // zeroes can be written, and only if the padding amount is positive.
113     int padding_amount =
114         static_cast<int>(min_width - total_digits - (sign_char > 0 ? 1 : 0));
115     if (left_justified || padding_amount < 0) {
116       if (sign_char > 0) {
117         RET_IF_RESULT_NEGATIVE(writer->write(sign_char));
118       }
119       return 0;
120     }
121     if (!leading_zeroes) {
122       RET_IF_RESULT_NEGATIVE(writer->write(' ', padding_amount));
123     }
124     if (sign_char > 0) {
125       RET_IF_RESULT_NEGATIVE(writer->write(sign_char));
126     }
127     if (leading_zeroes) {
128       RET_IF_RESULT_NEGATIVE(writer->write('0', padding_amount));
129     }
130     return 0;
131   }
132 
133   LIBC_INLINE int write_right_padding(Writer *writer, size_t total_digits) {
134     // If and only if the conversion is left justified, there may be trailing
135     // spaces.
136     int padding_amount =
137         static_cast<int>(min_width - total_digits - (sign_char > 0 ? 1 : 0));
138     if (left_justified && padding_amount > 0) {
139       RET_IF_RESULT_NEGATIVE(writer->write(' ', padding_amount));
140     }
141     return 0;
142   }
143 };
144 
145 /*
146   We only need to round a given segment if all of the segments below it are
147   the max (or this is the last segment). This means that we don't have to
148   write those initially, we can just keep the most recent non-maximal
149   segment and a counter of the number of maximal segments. When we reach a
150   non-maximal segment, we write the stored segment as well as as many 9s as
151   are necessary. Alternately, if we reach the end and have to round up, then
152   we round the stored segment, and write zeroes following it. If this
153   crosses the decimal point, then we have to shift it one space to the
154   right.
155   This FloatWriter class does the buffering and counting, and writes to the
156   output when necessary.
157 */
158 class FloatWriter {
159   char block_buffer[BLOCK_SIZE]; // The buffer that holds a block.
160   size_t buffered_digits = 0;    // The number of digits held in the buffer.
161   bool has_written = false;      // True once any digits have been output.
162   size_t max_block_count = 0; // The # of blocks of all 9s currently buffered.
163   size_t total_digits = 0;    // The number of digits that will be output.
164   size_t digits_before_decimal = 0; // The # of digits to write before the '.'
165   size_t total_digits_written = 0;  // The # of digits that have been output.
166   bool has_decimal_point;           // True if the number has a decimal point.
167   Writer *writer;                   // Writes to the final output.
168   PaddingWriter padding_writer; // Handles prefixes/padding, uses total_digits.
169 
170   LIBC_INLINE int flush_buffer(bool round_up_max_blocks = false) {
171     const char MAX_BLOCK_DIGIT = (round_up_max_blocks ? '0' : '9');
172 
173     // Write the most recent buffered block, and mark has_written
174     if (!has_written) {
175       has_written = true;
176       RET_IF_RESULT_NEGATIVE(
177           padding_writer.write_left_padding(writer, total_digits));
178     }
179 
180     // if the decimal point is the next character, or is in the range covered
181     // by the buffered block, write the appropriate digits and the decimal
182     // point.
183     if (total_digits_written < digits_before_decimal &&
184         total_digits_written + buffered_digits >= digits_before_decimal &&
185         has_decimal_point) {
186       size_t digits_to_write = digits_before_decimal - total_digits_written;
187       if (digits_to_write > 0) {
188         // Write the digits before the decimal point.
189         RET_IF_RESULT_NEGATIVE(writer->write({block_buffer, digits_to_write}));
190       }
191       RET_IF_RESULT_NEGATIVE(writer->write(DECIMAL_POINT));
192       if (buffered_digits - digits_to_write > 0) {
193         // Write the digits after the decimal point.
194         RET_IF_RESULT_NEGATIVE(
195             writer->write({block_buffer + digits_to_write,
196                            (buffered_digits - digits_to_write)}));
197       }
198       // add 1 for the decimal point
199       total_digits_written += buffered_digits + 1;
200       // Mark the buffer as empty.
201       buffered_digits = 0;
202     }
203 
204     // Clear the buffered digits.
205     if (buffered_digits > 0) {
206       RET_IF_RESULT_NEGATIVE(writer->write({block_buffer, buffered_digits}));
207       total_digits_written += buffered_digits;
208       buffered_digits = 0;
209     }
210 
211     // if the decimal point is the next character, or is in the range covered
212     // by the max blocks, write the appropriate digits and the decimal point.
213     if (total_digits_written < digits_before_decimal &&
214         total_digits_written + BLOCK_SIZE * max_block_count >=
215             digits_before_decimal &&
216         has_decimal_point) {
217       size_t digits_to_write = digits_before_decimal - total_digits_written;
218       if (digits_to_write > 0) {
219         RET_IF_RESULT_NEGATIVE(writer->write(MAX_BLOCK_DIGIT, digits_to_write));
220       }
221       RET_IF_RESULT_NEGATIVE(writer->write(DECIMAL_POINT));
222       if ((BLOCK_SIZE * max_block_count) - digits_to_write > 0) {
223         RET_IF_RESULT_NEGATIVE(writer->write(
224             MAX_BLOCK_DIGIT, (BLOCK_SIZE * max_block_count) - digits_to_write));
225       }
226       // add 1 for the decimal point
227       total_digits_written += BLOCK_SIZE * max_block_count + 1;
228       // clear the buffer of max blocks
229       max_block_count = 0;
230     }
231 
232     // Clear the buffer of max blocks
233     if (max_block_count > 0) {
234       RET_IF_RESULT_NEGATIVE(
235           writer->write(MAX_BLOCK_DIGIT, max_block_count * BLOCK_SIZE));
236       total_digits_written += max_block_count * BLOCK_SIZE;
237       max_block_count = 0;
238     }
239     return 0;
240   }
241 
242   // -exponent will never overflow because all long double types we support
243   // have at most 15 bits of mantissa and the C standard defines an int as
244   // being at least 16 bits.
245   static_assert(fputil::FPBits<long double>::EXP_LEN < (sizeof(int) * 8));
246 
247 public:
248   LIBC_INLINE FloatWriter(Writer *init_writer, bool init_has_decimal_point,
249                           const PaddingWriter &init_padding_writer)
250       : has_decimal_point(init_has_decimal_point), writer(init_writer),
251         padding_writer(init_padding_writer) {}
252 
253   LIBC_INLINE void init(size_t init_total_digits,
254                         size_t init_digits_before_decimal) {
255     total_digits = init_total_digits;
256     digits_before_decimal = init_digits_before_decimal;
257   }
258 
259   LIBC_INLINE void write_first_block(BlockInt block, bool exp_format = false) {
260     const DecimalString buf(block);
261     const cpp::string_view int_to_str = buf.view();
262     size_t digits_buffered = int_to_str.size();
263     // Block Buffer is guaranteed to not overflow since block cannot have more
264     // than BLOCK_SIZE digits.
265     // TODO: Replace with memcpy
266     for (size_t count = 0; count < digits_buffered; ++count) {
267       block_buffer[count] = int_to_str[count];
268     }
269     buffered_digits = digits_buffered;
270 
271     // In the exponent format (%e) we know how many digits will be written even
272     // before calculating any blocks, whereas the decimal format (%f) has to
273     // write all of the blocks that would come before the decimal place.
274     if (!exp_format) {
275       total_digits += digits_buffered;
276       digits_before_decimal += digits_buffered;
277     }
278   }
279 
280   LIBC_INLINE int write_middle_block(BlockInt block) {
281     if (block == MAX_BLOCK) { // Buffer max blocks in case of rounding
282       ++max_block_count;
283     } else { // If a non-max block has been found
284       RET_IF_RESULT_NEGATIVE(flush_buffer());
285 
286       // Now buffer the current block. We add 1 + MAX_BLOCK to force the
287       // leading zeroes, and drop the leading one. This is probably inefficient,
288       // but it works. See https://xkcd.com/2021/
289       const DecimalString buf(block + (MAX_BLOCK + 1));
290       const cpp::string_view int_to_str = buf.view();
291       // TODO: Replace with memcpy
292       for (size_t count = 0; count < BLOCK_SIZE; ++count) {
293         block_buffer[count] = int_to_str[count + 1];
294       }
295 
296       buffered_digits = BLOCK_SIZE;
297     }
298     return 0;
299   }
300 
301   LIBC_INLINE int write_last_block(BlockInt block, size_t block_digits,
302                                    RoundDirection round, int exponent = 0,
303                                    char exp_char = '\0') {
304     bool has_exp = (exp_char != '\0');
305 
306     char end_buff[BLOCK_SIZE];
307 
308     {
309       const DecimalString buf(block + (MAX_BLOCK + 1));
310       const cpp::string_view int_to_str = buf.view();
311 
312       // copy the last block_digits characters into the start of end_buff.
313       // TODO: Replace with memcpy
314       for (size_t count = 0; count < block_digits; ++count) {
315         end_buff[count] = int_to_str[count + 1 + (BLOCK_SIZE - block_digits)];
316       }
317     }
318 
319     char low_digit = '0';
320     if (block_digits > 0) {
321       low_digit = end_buff[block_digits - 1];
322     } else if (max_block_count > 0) {
323       low_digit = '9';
324     } else if (buffered_digits > 0) {
325       low_digit = block_buffer[buffered_digits - 1];
326     }
327 
328     bool round_up_max_blocks = false;
329 
330     // Round up
331     if (round == RoundDirection::Up ||
332         (round == RoundDirection::Even && low_digit % 2 != 0)) {
333       bool has_carry = true;
334       round_up_max_blocks = true; // if we're rounding up, we might need to
335                                   // round up the max blocks that are buffered.
336 
337       // handle the low block that we're adding
338       for (int count = static_cast<int>(block_digits) - 1;
339            count >= 0 && has_carry; --count) {
340         if (end_buff[count] == '9') {
341           end_buff[count] = '0';
342         } else {
343           end_buff[count] += 1;
344           has_carry = false;
345           round_up_max_blocks = false; // If the low block isn't all nines, then
346                                        // the max blocks aren't rounded up.
347         }
348       }
349       // handle the high block that's buffered
350       for (int count = static_cast<int>(buffered_digits) - 1;
351            count >= 0 && has_carry; --count) {
352         if (block_buffer[count] == '9') {
353           block_buffer[count] = '0';
354         } else {
355           block_buffer[count] += 1;
356           has_carry = false;
357         }
358       }
359 
360       // has_carry should only be true here if every previous digit is 9, which
361       // implies that the number has never been written.
362       if (has_carry /* && !has_written */) {
363         if (has_exp) { // This is in %e style
364           // Since this is exponential notation, we don't write any more digits
365           // but we do increment the exponent.
366           ++exponent;
367 
368           const ExponentString buf(exponent);
369           const cpp::string_view int_to_str = buf.view();
370 
371           // TODO: also change this to calculate the width of the number more
372           // efficiently.
373           size_t exponent_width = int_to_str.size();
374           size_t number_digits =
375               buffered_digits + (max_block_count * BLOCK_SIZE) + block_digits;
376 
377           // Here we have to recalculate the total number of digits since the
378           // exponent's width may have changed. We're only adding 1 to exponent
379           // width since exp_str appends the sign.
380           total_digits =
381               (has_decimal_point ? 1 : 0) + number_digits + 1 + exponent_width;
382 
383           // Normally write_left_padding is called by flush_buffer but since
384           // we're rounding up all of the digits, the ones in the buffer are
385           // wrong and can't be flushed.
386           RET_IF_RESULT_NEGATIVE(
387               padding_writer.write_left_padding(writer, total_digits));
388           // Now we know we need to print a leading 1, the decimal point, and
389           // then zeroes after it.
390           RET_IF_RESULT_NEGATIVE(writer->write('1'));
391           // digits_before_decimal - 1 to account for the leading '1'
392           if (has_decimal_point) {
393             RET_IF_RESULT_NEGATIVE(writer->write(DECIMAL_POINT));
394             // This is just the length of the number, not including the decimal
395             // point, or exponent.
396 
397             if (number_digits > 1) {
398               RET_IF_RESULT_NEGATIVE(writer->write('0', number_digits - 1));
399             }
400           }
401           RET_IF_RESULT_NEGATIVE(writer->write(exp_char));
402           RET_IF_RESULT_NEGATIVE(writer->write(int_to_str));
403 
404           total_digits_written = total_digits;
405           return WRITE_OK;
406         } else { // This is in %f style
407           ++total_digits;
408           ++digits_before_decimal;
409           // Normally write_left_padding is called by flush_buffer but since
410           // we're rounding up all of the digits, the ones in the buffer are
411           // wrong and can't be flushed.
412           RET_IF_RESULT_NEGATIVE(
413               padding_writer.write_left_padding(writer, total_digits));
414           // Now we know we need to print a leading 1, zeroes up to the decimal
415           // point, the decimal point, and then finally digits after it.
416           RET_IF_RESULT_NEGATIVE(writer->write('1'));
417           // digits_before_decimal - 1 to account for the leading '1'
418           RET_IF_RESULT_NEGATIVE(writer->write('0', digits_before_decimal - 1));
419           if (has_decimal_point) {
420             RET_IF_RESULT_NEGATIVE(writer->write(DECIMAL_POINT));
421             // add one to digits_before_decimal to account for the decimal point
422             // itself.
423             if (total_digits > digits_before_decimal + 1) {
424               RET_IF_RESULT_NEGATIVE(writer->write(
425                   '0', total_digits - (digits_before_decimal + 1)));
426             }
427           }
428           total_digits_written = total_digits;
429           return WRITE_OK;
430         }
431       }
432     }
433     // Either we intend to round down, or the rounding up is complete. Flush the
434     // buffers.
435 
436     RET_IF_RESULT_NEGATIVE(flush_buffer(round_up_max_blocks));
437 
438     // And then write the final block. It's written via the buffer so that if
439     // this is also the first block, the decimal point will be placed correctly.
440 
441     // TODO: Replace with memcpy
442     for (size_t count = 0; count < block_digits; ++count) {
443       block_buffer[count] = end_buff[count];
444     }
445     buffered_digits = block_digits;
446     RET_IF_RESULT_NEGATIVE(flush_buffer());
447 
448     if (has_exp) {
449       RET_IF_RESULT_NEGATIVE(writer->write(exp_char));
450       const ExponentString buf(exponent);
451       RET_IF_RESULT_NEGATIVE(writer->write(buf.view()));
452     }
453     total_digits_written = total_digits;
454 
455     return WRITE_OK;
456   }
457 
458   LIBC_INLINE int write_zeroes(uint32_t num_zeroes) {
459     RET_IF_RESULT_NEGATIVE(flush_buffer());
460     RET_IF_RESULT_NEGATIVE(writer->write('0', num_zeroes));
461     return 0;
462   }
463 
464   LIBC_INLINE int right_pad() {
465     return padding_writer.write_right_padding(writer, total_digits);
466   }
467 };
468 
469 // This implementation is based on the Ryu Printf algorithm by Ulf Adams:
470 // Ulf Adams. 2019. Ryū revisited: printf floating point conversion.
471 // Proc. ACM Program. Lang. 3, OOPSLA, Article 169 (October 2019), 23 pages.
472 // https://doi.org/10.1145/3360595
473 template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
474 LIBC_INLINE int convert_float_decimal_typed(Writer *writer,
475                                             const FormatSection &to_conv,
476                                             fputil::FPBits<T> float_bits) {
477   // signed because later we use -FRACTION_LEN
478   constexpr int32_t FRACTION_LEN = fputil::FPBits<T>::FRACTION_LEN;
479   int exponent = float_bits.get_explicit_exponent();
480 
481   char sign_char = 0;
482 
483   if (float_bits.is_neg())
484     sign_char = '-';
485   else if ((to_conv.flags & FormatFlags::FORCE_SIGN) == FormatFlags::FORCE_SIGN)
486     sign_char = '+'; // FORCE_SIGN has precedence over SPACE_PREFIX
487   else if ((to_conv.flags & FormatFlags::SPACE_PREFIX) ==
488            FormatFlags::SPACE_PREFIX)
489     sign_char = ' ';
490 
491   // If to_conv doesn't specify a precision, the precision defaults to 6.
492   const unsigned int precision = to_conv.precision < 0 ? 6 : to_conv.precision;
493   bool has_decimal_point =
494       (precision > 0) || ((to_conv.flags & FormatFlags::ALTERNATE_FORM) != 0);
495 
496   // nonzero is false until a nonzero digit is found. It is used to determine if
497   // leading zeroes should be printed, since before the first digit they are
498   // ignored.
499   bool nonzero = false;
500 
501   PaddingWriter padding_writer(to_conv, sign_char);
502   FloatWriter float_writer(writer, has_decimal_point, padding_writer);
503   FloatToString<T> float_converter(float_bits.get_val());
504 
505   const size_t positive_blocks = float_converter.get_positive_blocks();
506 
507   // This loop iterates through the number a block at a time until it finds a
508   // block that is not zero or it hits the decimal point. This is because all
509   // zero blocks before the first nonzero digit or the decimal point are
510   // ignored (no leading zeroes, at least at this stage).
511   for (int32_t i = static_cast<int32_t>(positive_blocks) - 1; i >= 0; --i) {
512     BlockInt digits = float_converter.get_positive_block(i);
513     if (nonzero) {
514       RET_IF_RESULT_NEGATIVE(float_writer.write_middle_block(digits));
515     } else if (digits != 0) {
516       size_t blocks_before_decimal = i;
517       float_writer.init((blocks_before_decimal * BLOCK_SIZE) +
518                             (has_decimal_point ? 1 : 0) + precision,
519                         blocks_before_decimal * BLOCK_SIZE);
520       float_writer.write_first_block(digits);
521 
522       nonzero = true;
523     }
524   }
525 
526   // if we haven't yet found a valid digit, buffer a zero.
527   if (!nonzero) {
528     float_writer.init((has_decimal_point ? 1 : 0) + precision, 0);
529     float_writer.write_first_block(0);
530   }
531 
532   if (exponent < FRACTION_LEN) {
533     const uint32_t blocks = (precision / static_cast<uint32_t>(BLOCK_SIZE)) + 1;
534     uint32_t i = 0;
535     // if all the blocks we should write are zero
536     if (blocks <= float_converter.zero_blocks_after_point()) {
537       i = blocks; // just write zeroes up to precision
538       RET_IF_RESULT_NEGATIVE(float_writer.write_zeroes(precision));
539     } else if (i < float_converter.zero_blocks_after_point()) {
540       // else if there are some blocks that are zeroes
541       i = static_cast<uint32_t>(float_converter.zero_blocks_after_point());
542       // write those blocks as zeroes.
543       RET_IF_RESULT_NEGATIVE(float_writer.write_zeroes(9 * i));
544     }
545     // for each unwritten block
546     for (; i < blocks; ++i) {
547       if (float_converter.is_lowest_block(i)) {
548         const uint32_t fill = precision - 9 * i;
549         RET_IF_RESULT_NEGATIVE(float_writer.write_zeroes(fill));
550         break;
551       }
552       BlockInt digits = float_converter.get_negative_block(i);
553       if (i < blocks - 1) {
554         RET_IF_RESULT_NEGATIVE(float_writer.write_middle_block(digits));
555       } else {
556 
557         const uint32_t maximum =
558             static_cast<uint32_t>(precision - BLOCK_SIZE * i);
559         uint32_t last_digit = 0;
560         for (uint32_t k = 0; k < BLOCK_SIZE - maximum; ++k) {
561           last_digit = digits % 10;
562           digits /= 10;
563         }
564         RoundDirection round;
565         const bool truncated = !zero_after_digits(
566             exponent - FRACTION_LEN, precision,
567             float_bits.get_explicit_mantissa(), FRACTION_LEN);
568         round = get_round_direction(last_digit, truncated, float_bits.sign());
569 
570         RET_IF_RESULT_NEGATIVE(
571             float_writer.write_last_block(digits, maximum, round));
572         break;
573       }
574     }
575   } else {
576     RET_IF_RESULT_NEGATIVE(float_writer.write_zeroes(precision));
577   }
578   RET_IF_RESULT_NEGATIVE(float_writer.right_pad());
579   return WRITE_OK;
580 }
581 
582 template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
583 LIBC_INLINE int convert_float_dec_exp_typed(Writer *writer,
584                                             const FormatSection &to_conv,
585                                             fputil::FPBits<T> float_bits) {
586   // signed because later we use -FRACTION_LEN
587   constexpr int32_t FRACTION_LEN = fputil::FPBits<T>::FRACTION_LEN;
588   int exponent = float_bits.get_explicit_exponent();
589   StorageType mantissa = float_bits.get_explicit_mantissa();
590 
591   char sign_char = 0;
592 
593   if (float_bits.is_neg())
594     sign_char = '-';
595   else if ((to_conv.flags & FormatFlags::FORCE_SIGN) == FormatFlags::FORCE_SIGN)
596     sign_char = '+'; // FORCE_SIGN has precedence over SPACE_PREFIX
597   else if ((to_conv.flags & FormatFlags::SPACE_PREFIX) ==
598            FormatFlags::SPACE_PREFIX)
599     sign_char = ' ';
600 
601   // If to_conv doesn't specify a precision, the precision defaults to 6.
602   const unsigned int precision = to_conv.precision < 0 ? 6 : to_conv.precision;
603   bool has_decimal_point =
604       (precision > 0) || ((to_conv.flags & FormatFlags::ALTERNATE_FORM) != 0);
605 
606   PaddingWriter padding_writer(to_conv, sign_char);
607   FloatWriter float_writer(writer, has_decimal_point, padding_writer);
608   FloatToString<T> float_converter(float_bits.get_val());
609 
610   size_t digits_written = 0;
611   int final_exponent = 0;
612 
613   // Here we would subtract 1 to account for the fact that block 0 counts as a
614   // positive block, but the loop below accounts for this by starting with
615   // subtracting 1 from cur_block.
616   int cur_block;
617 
618   if (exponent < 0) {
619     cur_block = -static_cast<int>(float_converter.zero_blocks_after_point());
620   } else {
621     cur_block = static_cast<int>(float_converter.get_positive_blocks());
622   }
623 
624   BlockInt digits = 0;
625 
626   // If the mantissa is 0, then the number is 0, meaning that looping until a
627   // non-zero block is found will loop forever. The first block is just 0.
628   if (mantissa != 0) {
629     // This loop finds the first block.
630     while (digits == 0) {
631       --cur_block;
632       digits = float_converter.get_block(cur_block);
633     }
634   } else {
635     cur_block = 0;
636   }
637 
638   const size_t block_width = IntegerToString<intmax_t>(digits).size();
639 
640   final_exponent = static_cast<int>(cur_block * BLOCK_SIZE) +
641                    static_cast<int>(block_width - 1);
642   int positive_exponent = final_exponent < 0 ? -final_exponent : final_exponent;
643 
644   size_t exponent_width = IntegerToString<intmax_t>(positive_exponent).size();
645 
646   // Calculate the total number of digits in the number.
647   // 1 - the digit before the decimal point
648   // 1 - the decimal point (optional)
649   // precision - the number of digits after the decimal point
650   // 1 - the 'e' at the start of the exponent
651   // 1 - the sign at the start of the exponent
652   // max(2, exp width) - the digits of the exponent, min 2.
653 
654   float_writer.init(1 + (has_decimal_point ? 1 : 0) + precision + 2 +
655                         (exponent_width < 2 ? 2 : exponent_width),
656                     1);
657 
658   // If this block is not the last block
659   if (block_width <= precision + 1) {
660     float_writer.write_first_block(digits, true);
661     digits_written += block_width;
662     --cur_block;
663   }
664 
665   // For each middle block.
666   for (; digits_written + BLOCK_SIZE < precision + 1; --cur_block) {
667     digits = float_converter.get_block(cur_block);
668 
669     RET_IF_RESULT_NEGATIVE(float_writer.write_middle_block(digits));
670     digits_written += BLOCK_SIZE;
671   }
672 
673   digits = float_converter.get_block(cur_block);
674 
675   size_t last_block_size = BLOCK_SIZE;
676 
677   // if the last block is also the first block, then ignore leading zeroes.
678   if (digits_written == 0) {
679     last_block_size = IntegerToString<intmax_t>(digits).size();
680   }
681 
682   // This tracks if the number is truncated, that meaning that the digits after
683   // last_digit are non-zero.
684   bool truncated = false;
685 
686   // This is the last block.
687   const size_t maximum = precision + 1 - digits_written;
688   uint32_t last_digit = 0;
689   for (uint32_t k = 0; k < last_block_size - maximum; ++k) {
690     if (last_digit > 0)
691       truncated = true;
692 
693     last_digit = digits % 10;
694     digits /= 10;
695   }
696 
697   // If the last block we read doesn't have the digit after the end of what
698   // we'll print, then we need to read the next block to get that digit.
699   if (maximum == last_block_size) {
700     --cur_block;
701     BlockInt extra_block = float_converter.get_block(cur_block);
702     last_digit = extra_block / ((MAX_BLOCK / 10) + 1);
703     if (extra_block % ((MAX_BLOCK / 10) + 1) > 0) {
704       truncated = true;
705     }
706   }
707 
708   RoundDirection round;
709 
710   // If we've already seen a truncated digit, then we don't need to check any
711   // more.
712   if (!truncated) {
713     // Check the blocks above the decimal point
714     if (cur_block >= 0) {
715       // Check every block until the decimal point for non-zero digits.
716       for (int cur_extra_block = cur_block - 1; cur_extra_block >= 0;
717            --cur_extra_block) {
718         BlockInt extra_block = float_converter.get_block(cur_extra_block);
719         if (extra_block > 0) {
720           truncated = true;
721           break;
722         }
723       }
724     }
725     // If it's still not truncated and there are digits below the decimal point
726     if (!truncated && exponent - FRACTION_LEN < 0) {
727       // Use the formula from %f.
728       truncated = !zero_after_digits(
729           exponent - FRACTION_LEN, precision - final_exponent,
730           float_bits.get_explicit_mantissa(), FRACTION_LEN);
731     }
732   }
733   round = get_round_direction(last_digit, truncated, float_bits.sign());
734 
735   RET_IF_RESULT_NEGATIVE(float_writer.write_last_block(
736       digits, maximum, round, final_exponent,
737       internal::islower(to_conv.conv_name) ? 'e' : 'E'));
738 
739   RET_IF_RESULT_NEGATIVE(float_writer.right_pad());
740   return WRITE_OK;
741 }
742 
743 template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
744 LIBC_INLINE int convert_float_dec_auto_typed(Writer *writer,
745                                              const FormatSection &to_conv,
746                                              fputil::FPBits<T> float_bits) {
747   // signed because later we use -FRACTION_LEN
748   constexpr int32_t FRACTION_LEN = fputil::FPBits<T>::FRACTION_LEN;
749   int exponent = float_bits.get_explicit_exponent();
750   StorageType mantissa = float_bits.get_explicit_mantissa();
751 
752   // From the standard: Let P (init_precision) equal the precision if nonzero, 6
753   // if the precision is omitted, or 1 if the precision is zero.
754   const unsigned int init_precision = to_conv.precision <= 0
755                                           ? (to_conv.precision == 0 ? 1 : 6)
756                                           : to_conv.precision;
757 
758   //  Then, if a conversion with style E would have an exponent of X
759   //  (base_10_exp):
760   int base_10_exp = 0;
761   // If P > X >= -4 the conversion is with style F and precision P - (X + 1).
762   // Otherwise, the conversion is with style E and precision P - 1.
763 
764   // For calculating the base 10 exponent, we need to process the number as if
765   // it has style E, so here we calculate the precision we'll use in that case.
766   const unsigned int exp_precision = init_precision - 1;
767 
768   FloatToString<T> float_converter(float_bits.get_val());
769 
770   // Here we would subtract 1 to account for the fact that block 0 counts as a
771   // positive block, but the loop below accounts for this by starting with
772   // subtracting 1 from cur_block.
773   int cur_block;
774 
775   if (exponent < 0) {
776     cur_block = -static_cast<int>(float_converter.zero_blocks_after_point());
777   } else {
778     cur_block = static_cast<int>(float_converter.get_positive_blocks());
779   }
780 
781   BlockInt digits = 0;
782 
783   // If the mantissa is 0, then the number is 0, meaning that looping until a
784   // non-zero block is found will loop forever.
785   if (mantissa != 0) {
786     // This loop finds the first non-zero block.
787     while (digits == 0) {
788       --cur_block;
789       digits = float_converter.get_block(cur_block);
790     }
791   } else {
792     // In the case of 0.0, then it's always decimal format. If we don't have alt
793     // form then the trailing zeroes are trimmed to make "0", else the precision
794     // is 1 less than specified by the user.
795     FormatSection new_conv = to_conv;
796     if ((to_conv.flags & FormatFlags::ALTERNATE_FORM) != 0) {
797       // This is a style F conversion, making the precision P - 1 - X, but since
798       // this is for the number 0, X (the base 10 exponent) is always 0.
799       new_conv.precision = init_precision - 1;
800     } else {
801       new_conv.precision = 0;
802     }
803     return convert_float_decimal_typed<T>(writer, new_conv, float_bits);
804   }
805 
806   const size_t block_width = IntegerToString<intmax_t>(digits).size();
807 
808   size_t digits_checked = 0;
809   // TODO: look into unifying trailing_zeroes and trailing_nines. The number can
810   // end in a nine or a zero, but not both.
811   size_t trailing_zeroes = 0;
812   size_t trailing_nines = 0;
813 
814   base_10_exp = static_cast<int>(cur_block * BLOCK_SIZE) +
815                 static_cast<int>(block_width - 1);
816 
817   // If the first block is not also the last block
818   if (block_width <= exp_precision + 1) {
819     const DecimalString buf(digits);
820     const cpp::string_view int_to_str = buf.view();
821 
822     for (size_t i = 0; i < block_width; ++i) {
823       if (int_to_str[i] == '9') {
824         ++trailing_nines;
825         trailing_zeroes = 0;
826       } else if (int_to_str[i] == '0') {
827         ++trailing_zeroes;
828         trailing_nines = 0;
829       } else {
830         trailing_nines = 0;
831         trailing_zeroes = 0;
832       }
833     }
834     digits_checked += block_width;
835     --cur_block;
836   }
837 
838   // Handle middle blocks
839   for (; digits_checked + BLOCK_SIZE < exp_precision + 1; --cur_block) {
840     digits = float_converter.get_block(cur_block);
841     digits_checked += BLOCK_SIZE;
842     if (digits == MAX_BLOCK) {
843       trailing_nines += 9;
844       trailing_zeroes = 0;
845     } else if (digits == 0) {
846       trailing_zeroes += 9;
847       trailing_nines = 0;
848     } else {
849       // The block is neither all nines nor all zeroes, so we need to figure out
850       // what it ends with.
851       trailing_nines = 0;
852       trailing_zeroes = 0;
853       BlockInt copy_of_digits = digits;
854       BlockInt cur_last_digit = copy_of_digits % 10;
855       // We only care if it ends in nines or zeroes.
856       while (copy_of_digits > 0 &&
857              (cur_last_digit == 9 || cur_last_digit == 0)) {
858         // If the next digit is not the same as the previous one, then there are
859         // no more contiguous trailing digits.
860         if (copy_of_digits % 10 != cur_last_digit) {
861           break;
862         }
863         if (cur_last_digit == 9) {
864           ++trailing_nines;
865         } else if (cur_last_digit == 0) {
866           ++trailing_zeroes;
867         } else {
868           break;
869         }
870         copy_of_digits /= 10;
871       }
872     }
873   }
874 
875   // Handle the last block
876 
877   digits = float_converter.get_block(cur_block);
878 
879   size_t last_block_size = BLOCK_SIZE;
880 
881   const DecimalString buf(digits);
882   const cpp::string_view int_to_str = buf.view();
883 
884   size_t implicit_leading_zeroes = BLOCK_SIZE - int_to_str.size();
885 
886   // if the last block is also the first block, then ignore leading zeroes.
887   if (digits_checked == 0) {
888     last_block_size = int_to_str.size();
889     implicit_leading_zeroes = 0;
890   }
891 
892   unsigned int digits_requested =
893       (exp_precision + 1) - static_cast<unsigned int>(digits_checked);
894 
895   int digits_to_check =
896       digits_requested - static_cast<int>(implicit_leading_zeroes);
897   if (digits_to_check < 0) {
898     digits_to_check = 0;
899   }
900 
901   // If the block is not the maximum size, that means it has leading
902   // zeroes, and zeroes are not nines.
903   if (implicit_leading_zeroes > 0) {
904     trailing_nines = 0;
905   }
906 
907   // But leading zeroes are zeroes (that could be trailing). We take the
908   // minimum of the leading zeroes and digits requested because if there are
909   // more requested digits than leading zeroes we shouldn't count those.
910   trailing_zeroes +=
911       (implicit_leading_zeroes > digits_requested ? digits_requested
912                                                   : implicit_leading_zeroes);
913 
914   // Check the upper digits of this block.
915   for (int i = 0; i < digits_to_check; ++i) {
916     if (int_to_str[i] == '9') {
917       ++trailing_nines;
918       trailing_zeroes = 0;
919     } else if (int_to_str[i] == '0') {
920       ++trailing_zeroes;
921       trailing_nines = 0;
922     } else {
923       trailing_nines = 0;
924       trailing_zeroes = 0;
925     }
926   }
927 
928   bool truncated = false;
929 
930   // Find the digit after the lowest digit that we'll actually print to
931   // determine the rounding.
932   const uint32_t maximum =
933       exp_precision + 1 - static_cast<uint32_t>(digits_checked);
934   uint32_t last_digit = 0;
935   for (uint32_t k = 0; k < last_block_size - maximum; ++k) {
936     if (last_digit > 0)
937       truncated = true;
938 
939     last_digit = digits % 10;
940     digits /= 10;
941   }
942 
943   // If the last block we read doesn't have the digit after the end of what
944   // we'll print, then we need to read the next block to get that digit.
945   if (maximum == last_block_size) {
946     --cur_block;
947     BlockInt extra_block = float_converter.get_block(cur_block);
948     last_digit = extra_block / ((MAX_BLOCK / 10) + 1);
949 
950     if (extra_block % ((MAX_BLOCK / 10) + 1) > 0)
951       truncated = true;
952   }
953 
954   // TODO: unify this code across the three float conversions.
955   RoundDirection round;
956 
957   // If we've already seen a truncated digit, then we don't need to check any
958   // more.
959   if (!truncated) {
960     // Check the blocks above the decimal point
961     if (cur_block >= 0) {
962       // Check every block until the decimal point for non-zero digits.
963       for (int cur_extra_block = cur_block - 1; cur_extra_block >= 0;
964            --cur_extra_block) {
965         BlockInt extra_block = float_converter.get_block(cur_extra_block);
966         if (extra_block > 0) {
967           truncated = true;
968           break;
969         }
970       }
971     }
972     // If it's still not truncated and there are digits below the decimal point
973     if (!truncated && exponent - FRACTION_LEN < 0) {
974       // Use the formula from %f.
975       truncated = !zero_after_digits(
976           exponent - FRACTION_LEN, exp_precision - base_10_exp,
977           float_bits.get_explicit_mantissa(), FRACTION_LEN);
978     }
979   }
980 
981   round = get_round_direction(last_digit, truncated, float_bits.sign());
982 
983   bool round_up;
984   if (round == RoundDirection::Up) {
985     round_up = true;
986   } else if (round == RoundDirection::Down) {
987     round_up = false;
988   } else {
989     // RoundDirection is even, so check the lowest digit that will be printed.
990     uint32_t low_digit;
991 
992     // maximum is the number of digits that will remain in digits after getting
993     // last_digit. If it's greater than zero, we can just check the lowest digit
994     // in digits.
995     if (maximum > 0) {
996       low_digit = digits % 10;
997     } else {
998       // Else if there are trailing nines, then the low digit is a nine, same
999       // with zeroes.
1000       if (trailing_nines > 0) {
1001         low_digit = 9;
1002       } else if (trailing_zeroes > 0) {
1003         low_digit = 0;
1004       } else {
1005         // If there are no trailing zeroes or nines, then the round direction
1006         // doesn't actually matter here. Since this conversion passes off the
1007         // value to another one for final conversion, rounding only matters to
1008         // determine if the exponent is higher than expected (with an all nine
1009         // number) or to determine the trailing zeroes to trim. In this case
1010         // low_digit is set to 0, but it could be set to any number.
1011 
1012         low_digit = 0;
1013       }
1014     }
1015     round_up = (low_digit % 2) != 0;
1016   }
1017 
1018   digits_checked += digits_requested;
1019   LIBC_ASSERT(digits_checked == init_precision);
1020   // At this point we should have checked all the digits requested by the
1021   // precision. We may increment this number 1 more if we round up all of the
1022   // digits, but at this point in the code digits_checked should always equal
1023   // init_precision.
1024 
1025   if (round_up) {
1026     // If all the digits that would be printed are nines, then rounding up means
1027     // that the base 10 exponent is one higher and all those nines turn to
1028     // zeroes (e.g. 999 -> 1000).
1029     if (trailing_nines == init_precision) {
1030       ++base_10_exp;
1031       trailing_zeroes = digits_checked;
1032       ++digits_checked;
1033     } else {
1034       // If there are trailing nines, they turn into trailing zeroes when
1035       // they're rounded up.
1036       if (trailing_nines > 0) {
1037         trailing_zeroes += trailing_nines;
1038       } else if (trailing_zeroes > 0) {
1039         // If there are trailing zeroes, then the last digit will be rounded up
1040         // to a 1 so they aren't trailing anymore.
1041         trailing_zeroes = 0;
1042       }
1043     }
1044   }
1045 
1046   // if P > X >= -4, the conversion is with style f (or F) and precision equals
1047   //  P - (X + 1).
1048   if (static_cast<int>(init_precision) > base_10_exp && base_10_exp >= -4) {
1049     FormatSection new_conv = to_conv;
1050     const int conv_precision = init_precision - (base_10_exp + 1);
1051 
1052     if ((to_conv.flags & FormatFlags::ALTERNATE_FORM) != 0) {
1053       new_conv.precision = conv_precision;
1054     } else {
1055       // If alt form isn't set, then we need to determine the number of trailing
1056       // zeroes and set the precision such that they are removed.
1057 
1058       /*
1059       Here's a diagram of an example:
1060 
1061       printf("%.15g", 22.25);
1062 
1063                             +--- init_precision = 15
1064                             |
1065                             +-------------------+
1066                             |                   |
1067                             |  ++--- trimmed_precision = 2
1068                             |  ||               |
1069                             22.250000000000000000
1070                             ||   |              |
1071                             ++   +--------------+
1072                              |   |
1073        base_10_exp + 1 = 2 --+   +--- trailing_zeroes = 11
1074       */
1075       int trimmed_precision = static_cast<int>(
1076           digits_checked - (base_10_exp + 1) - trailing_zeroes);
1077       if (trimmed_precision < 0) {
1078         trimmed_precision = 0;
1079       }
1080       new_conv.precision = (trimmed_precision > conv_precision)
1081                                ? conv_precision
1082                                : trimmed_precision;
1083     }
1084 
1085     return convert_float_decimal_typed<T>(writer, new_conv, float_bits);
1086   } else {
1087     // otherwise, the conversion is with style e (or E) and precision equals
1088     // P - 1
1089     const int conv_precision = init_precision - 1;
1090     FormatSection new_conv = to_conv;
1091     if ((to_conv.flags & FormatFlags::ALTERNATE_FORM) != 0) {
1092       new_conv.precision = conv_precision;
1093     } else {
1094       // If alt form isn't set, then we need to determine the number of trailing
1095       // zeroes and set the precision such that they are removed.
1096       int trimmed_precision =
1097           static_cast<int>(digits_checked - 1 - trailing_zeroes);
1098       if (trimmed_precision < 0) {
1099         trimmed_precision = 0;
1100       }
1101       new_conv.precision = (trimmed_precision > conv_precision)
1102                                ? conv_precision
1103                                : trimmed_precision;
1104     }
1105     return convert_float_dec_exp_typed<T>(writer, new_conv, float_bits);
1106   }
1107 }
1108 
1109 // TODO: unify the float converters to remove the duplicated checks for inf/nan.
1110 LIBC_INLINE int convert_float_decimal(Writer *writer,
1111                                       const FormatSection &to_conv) {
1112   if (to_conv.length_modifier == LengthModifier::L) {
1113     fputil::FPBits<long double>::StorageType float_raw = to_conv.conv_val_raw;
1114     fputil::FPBits<long double> float_bits(float_raw);
1115     if (!float_bits.is_inf_or_nan()) {
1116       return convert_float_decimal_typed<long double>(writer, to_conv,
1117                                                       float_bits);
1118     }
1119   } else {
1120     fputil::FPBits<double>::StorageType float_raw =
1121         static_cast<fputil::FPBits<double>::StorageType>(to_conv.conv_val_raw);
1122     fputil::FPBits<double> float_bits(float_raw);
1123     if (!float_bits.is_inf_or_nan()) {
1124       return convert_float_decimal_typed<double>(writer, to_conv, float_bits);
1125     }
1126   }
1127 
1128   return convert_inf_nan(writer, to_conv);
1129 }
1130 
1131 LIBC_INLINE int convert_float_dec_exp(Writer *writer,
1132                                       const FormatSection &to_conv) {
1133   if (to_conv.length_modifier == LengthModifier::L) {
1134     fputil::FPBits<long double>::StorageType float_raw = to_conv.conv_val_raw;
1135     fputil::FPBits<long double> float_bits(float_raw);
1136     if (!float_bits.is_inf_or_nan()) {
1137       return convert_float_dec_exp_typed<long double>(writer, to_conv,
1138                                                       float_bits);
1139     }
1140   } else {
1141     fputil::FPBits<double>::StorageType float_raw =
1142         static_cast<fputil::FPBits<double>::StorageType>(to_conv.conv_val_raw);
1143     fputil::FPBits<double> float_bits(float_raw);
1144     if (!float_bits.is_inf_or_nan()) {
1145       return convert_float_dec_exp_typed<double>(writer, to_conv, float_bits);
1146     }
1147   }
1148 
1149   return convert_inf_nan(writer, to_conv);
1150 }
1151 
1152 LIBC_INLINE int convert_float_dec_auto(Writer *writer,
1153                                        const FormatSection &to_conv) {
1154   if (to_conv.length_modifier == LengthModifier::L) {
1155     fputil::FPBits<long double>::StorageType float_raw = to_conv.conv_val_raw;
1156     fputil::FPBits<long double> float_bits(float_raw);
1157     if (!float_bits.is_inf_or_nan()) {
1158       return convert_float_dec_auto_typed<long double>(writer, to_conv,
1159                                                        float_bits);
1160     }
1161   } else {
1162     fputil::FPBits<double>::StorageType float_raw =
1163         static_cast<fputil::FPBits<double>::StorageType>(to_conv.conv_val_raw);
1164     fputil::FPBits<double> float_bits(float_raw);
1165     if (!float_bits.is_inf_or_nan()) {
1166       return convert_float_dec_auto_typed<double>(writer, to_conv, float_bits);
1167     }
1168   }
1169 
1170   return convert_inf_nan(writer, to_conv);
1171 }
1172 
1173 } // namespace printf_core
1174 } // namespace LIBC_NAMESPACE_DECL
1175 
1176 #endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_FLOAT_DEC_CONVERTER_H
1177