Skip to content

Commit 97f1035

Browse files
address simple comments
1 parent 6106eb4 commit 97f1035

File tree

3 files changed

+816
-801
lines changed

3 files changed

+816
-801
lines changed

libc/docs/dev/printf_behavior.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,10 @@ not based on the Ryu algorithm, instead generating the digits by
9494
multiplying/dividing the written-out number by 10^9 to get blocks. It's
9595
significantly faster than INT_CALC, only about 10x slower than MEGA_TABLE,
9696
and is small in binary size. Its downside is that it always calculates all
97-
of the digits above the decimal point, making it slightly ineffecient for %e
98-
calls with large exponents. This is the default. If this flag is not set, no
99-
other flags will change the long double behavior.
97+
of the digits above the decimal point, making it slightly inefficient for %e
98+
calls with large exponents. This is the default. This specialization overrides
99+
other flags, so this flag must be set for other flags to effect the long double
100+
behavior.
100101

101102
LIBC_COPT_FLOAT_TO_STR_USE_MEGA_LONG_DOUBLE_TABLE
102103
-------------------------------------------------

libc/src/__support/float_to_string.h

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
// multiplying/dividing the written-out number by 10^9 to get blocks. It's
3232
// significantly faster than INT_CALC, only about 10x slower than MEGA_TABLE,
3333
// and is small in binary size. Its downside is that it always calculates all
34-
// of the digits above the decimal point, making it ineffecient for %e calls
35-
// with large exponents. If this flag is not set, no other flags will change
36-
// the long double behavior.
34+
// of the digits above the decimal point, making it inefficient for %e calls
35+
// with large exponents. This specialization overrides other flags, so this
36+
// flag must be set for other flags to effect the long double behavior.
3737

3838
// LIBC_COPT_FLOAT_TO_STR_USE_MEGA_LONG_DOUBLE_TABLE
3939
// The Mega Table is ~5 megabytes when compiled. It lists the constants needed
@@ -609,12 +609,12 @@ class FloatToString {
609609

610610
LIBC_INLINE constexpr size_t zero_blocks_after_point() {
611611
#ifdef LIBC_COPT_FLOAT_TO_STR_NO_TABLE
612-
if (exponent < -MANT_WIDTH) {
612+
if (exponent < -FRACTION_LEN) {
613613
const int pos_exp = -exponent - 1;
614614
const uint32_t pos_idx =
615615
static_cast<uint32_t>(pos_exp + (IDX_SIZE - 1)) / IDX_SIZE;
616616
const int32_t pos_len = ((internal::ceil_log10_pow2(pos_idx * IDX_SIZE) -
617-
internal::ceil_log10_pow2(MANT_WIDTH + 1)) /
617+
internal::ceil_log10_pow2(FRACTION_LEN + 1)) /
618618
BLOCK_SIZE) -
619619
1;
620620
const uint32_t len = static_cast<uint32_t>(pos_len > 0 ? pos_len : 0);
@@ -640,17 +640,15 @@ template <> class FloatToString<long double> {
640640
static constexpr int FRACTION_LEN = fputil::FPBits<long double>::FRACTION_LEN;
641641
static constexpr int EXP_BIAS = fputil::FPBits<long double>::EXP_BIAS;
642642

643-
// static constexpr size_t FLOAT_AS_INT_WIDTH = 16384;
644643
static constexpr size_t FLOAT_AS_INT_WIDTH =
645644
internal::div_ceil(fputil::FPBits<long double>::MAX_BIASED_EXPONENT -
646645
FloatProp::EXP_BIAS,
647646
64) *
648647
64;
649-
// static constexpr size_t EXTRA_INT_WIDTH = 128;
650648
static constexpr size_t EXTRA_INT_WIDTH =
651649
internal::div_ceil(sizeof(long double) * 8, 64) * 64;
652650

653-
cpp::BigInt<FLOAT_AS_INT_WIDTH + EXTRA_INT_WIDTH, false> float_as_int = 0;
651+
cpp::UInt<FLOAT_AS_INT_WIDTH + EXTRA_INT_WIDTH> float_as_int = 0;
654652
int int_block_index = 0;
655653

656654
static constexpr size_t BLOCK_BUFFER_LEN =
@@ -659,8 +657,7 @@ template <> class FloatToString<long double> {
659657
size_t block_buffer_valid = 0;
660658

661659
template <size_t Bits>
662-
LIBC_INLINE static constexpr BlockInt
663-
grab_digits(cpp::BigInt<Bits, false> &int_num) {
660+
LIBC_INLINE static constexpr BlockInt grab_digits(cpp::UInt<Bits> &int_num) {
664661
BlockInt cur_block = 0;
665662
auto wide_result = int_num.div_uint32_times_pow_2(1953125, 9);
666663
// the optional only comes into effect when dividing by 0, which will
@@ -671,8 +668,8 @@ template <> class FloatToString<long double> {
671668
}
672669

673670
LIBC_INLINE static constexpr void zero_leading_digits(
674-
cpp::BigInt<FLOAT_AS_INT_WIDTH + EXTRA_INT_WIDTH, false> &int_num) {
675-
// 64 is the width of the numbers used to internally represent the BigInt
671+
cpp::UInt<FLOAT_AS_INT_WIDTH + EXTRA_INT_WIDTH> &int_num) {
672+
// 64 is the width of the numbers used to internally represent the UInt
676673
for (size_t i = 0; i < EXTRA_INT_WIDTH / 64; ++i) {
677674
int_num[i + (FLOAT_AS_INT_WIDTH / 64)] = 0;
678675
}
@@ -703,7 +700,7 @@ template <> class FloatToString<long double> {
703700
block_buffer_valid = int_block_index;
704701

705702
} else {
706-
// if the exponent not positive, then the number is at least partially
703+
// if the exponent is not positive, then the number is at least partially
707704
// below the decimal point. Shift left to make the int a fixed point
708705
// representation with the decimal point after the top EXTRA_INT_WIDTH
709706
// bits.
@@ -713,7 +710,7 @@ template <> class FloatToString<long double> {
713710

714711
// If there are still digits above the decimal point, handle those.
715712
if (float_as_int.clz() < EXTRA_INT_WIDTH) {
716-
cpp::BigInt<EXTRA_INT_WIDTH, false> above_decimal_point =
713+
cpp::UInt<EXTRA_INT_WIDTH> above_decimal_point =
717714
float_as_int >> FLOAT_AS_INT_WIDTH;
718715

719716
size_t positive_int_block_index = 0;
@@ -810,7 +807,7 @@ template <> class FloatToString<long double> {
810807
int block_index = -1 - negative_block_index;
811808

812809
// If we're currently after the requested block (remember these are
813-
// negative indices) the reset the number to the start. This is only
810+
// negative indices) we reset the number to the start. This is only
814811
// likely to happen in %g calls. This will also reset int_block_index.
815812
if (block_index > int_block_index) {
816813
init_convert();

0 commit comments

Comments
 (0)