Format IEEE 754 single-precision bits as decimal text.
No FP arithmetic is performed.
439 {
440 if (precision < 0) precision = 0;
441 if (precision > 9) precision = 9;
442
443 const bool neg = (bits >> 31) & 1u;
444 const int biased_exp = static_cast<int>((bits >> 23) & 0xFFu);
445 const u32 mant_bits = bits & 0x7FFFFFu;
446
447
448 if (biased_exp == 0xFF) {
451 }
452
454
455 auto append_zero_with_precision = [&]() {
456 s += "0";
457 if (precision > 0) {
458 s += ".";
459 for (int i = 0; i < precision; ++i) s += "0";
460 }
461 };
462
463
464
465 if (biased_exp == 0) {
466 if (neg) s += "-";
467 append_zero_with_precision();
468 return s;
469 }
470
471
472
473 const u32 mantissa_full = mant_bits | 0x800000u;
474 const int bin_exp_raw = biased_exp - 127 - 23;
475
476
477
478
479 const u64 mant64 =
static_cast<u64>(mantissa_full) << 40;
480 const int bin_exp = bin_exp_raw - 40;
481
482
483 const fl::size idx =
static_cast<fl::size
>(precision -
kPow10KMin);
486
487
489 int scaled_bin_exp = bin_exp + pow_exp + 64;
490 if ((scaled_hi & 0x8000000000000000ull) == 0) {
491 scaled_hi <<= 1;
492 --scaled_bin_exp;
493 }
494
495
496
497
498
500 if (scaled_bin_exp >= 0) {
501 if (scaled_bin_exp > 0) {
502
503
504 return neg ? fl::string(
"-inf") :
fl::
string(
"inf");
505 }
506 scaled_int = scaled_hi;
507 } else {
508 const int shift = -scaled_bin_exp;
509 if (shift >= 64) {
510 scaled_int = 0;
511 } else {
512
513 const u64 mask = (shift == 64) ? ~0ull : ((1ull << shift) - 1ull);
514 const u64 low = scaled_hi & mask;
515 scaled_int = scaled_hi >> shift;
516 const u64 half = (shift == 0) ? 0 : (1ull << (shift - 1));
517 if (low > half || (low == half && (scaled_int & 1ull))) {
518 ++scaled_int;
519 }
520 }
521 }
522
523
524
525
526 static constexpr u32 kPow10IntExact[] = {
527 1u, 10u, 100u, 1000u,
528 10000u, 100000u, 1000000u, 10000000u,
529 100000000u, 1000000000u,
530 };
531 const u64 pow10_exact = kPow10IntExact[precision];
532
533 const u64 int_part = scaled_int / pow10_exact;
534 const u64 frac_part = scaled_int % pow10_exact;
535
536 if (neg && (int_part != 0 || frac_part != 0)) {
537 s += "-";
538 }
540
541 if (precision > 0) {
542 s += ".";
543
544
545
546 char buf[24];
548 for (int i = n; i < precision; ++i) s += "0";
549 for (int i = 0; i < n; ++i) s += fl::string(1, buf[i]);
550 }
551
552 return s;
553}
static constexpr fl::u64 kPow10Mant[kPow10Count]
static constexpr fl::i16 kPow10BExp[kPow10Count]
fl::u64 mul_hi_u64(fl::u64 a, fl::u64 b) FL_NOEXCEPT
static constexpr int kPow10KMin
int utoa64(u64 value, char *sp, int radix)
Convert unsigned 64-bit integer to string buffer in given radix.
static void append_u64_decimal(fl::string &out, fl::u64 value) FL_NOEXCEPT
Base definition for an LED controller.