Skip to content

Commit d5439cc

Browse files
address more comments
1 parent 97f1035 commit d5439cc

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

libc/src/__support/UInt.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ namespace LIBC_NAMESPACE::cpp {
2727

2828
template <size_t Bits, bool Signed> struct BigInt {
2929

30+
// This being hardcoded as 64 is okay because we're using uint64_t as our
31+
// internal type which will always be 64 bits.
32+
LIBC_INLINE_VAR static constexpr size_t WORD_SIZE = 64;
33+
34+
// TODO: Replace references to 64 with WORD_SIZE.
3035
static_assert(Bits > 0 && Bits % 64 == 0,
3136
"Number of bits in BigInt should be a multiple of 64.");
3237
LIBC_INLINE_VAR static constexpr size_t WORDCOUNT = Bits / 64;

libc/src/__support/float_to_string.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -670,8 +670,8 @@ template <> class FloatToString<long double> {
670670
LIBC_INLINE static constexpr void zero_leading_digits(
671671
cpp::UInt<FLOAT_AS_INT_WIDTH + EXTRA_INT_WIDTH> &int_num) {
672672
// 64 is the width of the numbers used to internally represent the UInt
673-
for (size_t i = 0; i < EXTRA_INT_WIDTH / 64; ++i) {
674-
int_num[i + (FLOAT_AS_INT_WIDTH / 64)] = 0;
673+
for (size_t i = 0; i < EXTRA_INT_WIDTH / int_num.WORD_SIZE; ++i) {
674+
int_num[i + (FLOAT_AS_INT_WIDTH / int_num.WORD_SIZE)] = 0;
675675
}
676676
}
677677

@@ -809,10 +809,14 @@ template <> class FloatToString<long double> {
809809
// If we're currently after the requested block (remember these are
810810
// negative indices) we reset the number to the start. This is only
811811
// likely to happen in %g calls. This will also reset int_block_index.
812-
if (block_index > int_block_index) {
813-
init_convert();
814-
}
815-
812+
// if (block_index > int_block_index) {
813+
// init_convert();
814+
// }
815+
816+
// Printf is the only existing user of this code and it will only ever move
817+
// downwards, except for %g but that currently creates a second
818+
// float_to_string object so this assertion still holds. If a new user needs
819+
// the ability to step backwards, uncomment the code above.
816820
LIBC_ASSERT(block_index <= int_block_index);
817821

818822
// If we are currently before the requested block. Step until we reach the

libc/test/src/stdio/sprintf_test.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2985,6 +2985,9 @@ TEST_F(LlvmLibcSPrintfTest, FloatAutoLongDoubleConv) {
29852985
written = LIBC_NAMESPACE::sprintf(buff, "%Lg", 0xa.aaaaaaaaaaaaaabp-7L);
29862986
ASSERT_STREQ_LEN(written, buff, "0.0833333");
29872987

2988+
written = LIBC_NAMESPACE::sprintf(buff, "%Lg", 9.99999999999e-100L);
2989+
ASSERT_STREQ_LEN(written, buff, "1e-99");
2990+
29882991
#endif // LIBC_LONG_DOUBLE_IS_X86_FLOAT80
29892992

29902993
// TODO: Uncomment the below tests after long double support is added

0 commit comments

Comments
 (0)