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