Skip to content

Commit 0f99d72

Browse files
committed
[NFC] implement NumDigitsBase10 in MathExtras.h and update some users of log10
1 parent c426f39 commit 0f99d72

File tree

11 files changed

+69
-62
lines changed

11 files changed

+69
-62
lines changed

llvm/include/llvm/DebugInfo/PDB/Native/FormatUtil.h

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -62,49 +62,6 @@ LLVM_ABI std::string formatChunkKind(codeview::DebugSubsectionKind Kind,
6262
LLVM_ABI std::string formatSymbolKind(codeview::SymbolKind K);
6363
LLVM_ABI std::string formatTypeLeafKind(codeview::TypeLeafKind K);
6464

65-
/// Returns the number of digits in the given integer.
66-
inline int NumDigits(uint64_t N) {
67-
if (N < 10ULL)
68-
return 1;
69-
if (N < 100ULL)
70-
return 2;
71-
if (N < 1000ULL)
72-
return 3;
73-
if (N < 10000ULL)
74-
return 4;
75-
if (N < 100000ULL)
76-
return 5;
77-
if (N < 1000000ULL)
78-
return 6;
79-
if (N < 10000000ULL)
80-
return 7;
81-
if (N < 100000000ULL)
82-
return 8;
83-
if (N < 1000000000ULL)
84-
return 9;
85-
if (N < 10000000000ULL)
86-
return 10;
87-
if (N < 100000000000ULL)
88-
return 11;
89-
if (N < 1000000000000ULL)
90-
return 12;
91-
if (N < 10000000000000ULL)
92-
return 13;
93-
if (N < 100000000000000ULL)
94-
return 14;
95-
if (N < 1000000000000000ULL)
96-
return 15;
97-
if (N < 10000000000000000ULL)
98-
return 16;
99-
if (N < 100000000000000000ULL)
100-
return 17;
101-
if (N < 1000000000000000000ULL)
102-
return 18;
103-
if (N < 10000000000000000000ULL)
104-
return 19;
105-
return 20;
106-
}
107-
10865
namespace detail {
10966
template <typename T>
11067
struct EndianAdapter final

llvm/include/llvm/DebugInfo/PDB/Native/InputFile.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ Error iterateSymbolGroups(InputFile &Input, const PrintScope &HeaderScope,
183183
if (Filters.DumpModi) {
184184
uint32_t Modi = *Filters.DumpModi;
185185
SymbolGroup SG(&Input, Modi);
186-
return iterateOneModule(Input, withLabelWidth(HeaderScope, NumDigits(Modi)),
186+
return iterateOneModule(Input, withLabelWidth(HeaderScope, NumDigitsBase10(Modi)),
187187
SG, Modi, Callback);
188188
}
189189

@@ -192,7 +192,7 @@ Error iterateSymbolGroups(InputFile &Input, const PrintScope &HeaderScope,
192192
for (const auto &SG : Input.symbol_groups()) {
193193
if (shouldDumpSymbolGroup(I, SG, Filters))
194194
if (auto Err =
195-
iterateOneModule(Input, withLabelWidth(HeaderScope, NumDigits(I)),
195+
iterateOneModule(Input, withLabelWidth(HeaderScope, NumDigitsBase10(I)),
196196
SG, I, Callback))
197197
return Err;
198198

llvm/include/llvm/Support/MathExtras.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,41 @@ using stack_float_t = volatile float;
795795
using stack_float_t = float;
796796
#endif
797797

798+
/// Returns the number of digits in the given integer.
799+
inline int NumDigitsBase10(uint64_t X) {
800+
static constexpr struct ConstexprData {
801+
uint8_t AtLeast[65] = {};
802+
uint64_t Boundaries[20] = {};
803+
static constexpr int NumDigitsConstexpr(uint64_t N) {
804+
int res = 1;
805+
while (N >= 10) {
806+
res++;
807+
N /= 10;
808+
}
809+
return res;
810+
}
811+
constexpr ConstexprData() {
812+
uint64_t Val = ~0ull;
813+
for (uint64_t i = 0; i <= 64; i++) {
814+
uint64_t Digits = NumDigitsConstexpr(Val) - 1;
815+
AtLeast[i] = Digits;
816+
Val >>= 1;
817+
}
818+
// Special case because X=0 should return 1 and not 0
819+
Boundaries[0] = 0;
820+
Val = 10;
821+
for (uint64_t i = 1; i < 20; i++) {
822+
Boundaries[i] = Val;
823+
Val *= 10;
824+
}
825+
}
826+
} Data;
827+
828+
uint64_t Base2 = X ? countl_zero(X) : 64;
829+
uint64_t Digits = Data.AtLeast[Base2];
830+
return Digits + (X >= Data.Boundaries[Digits]);
831+
}
832+
798833
} // namespace llvm
799834

800835
#endif

llvm/lib/DebugInfo/Symbolize/DIPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class SourceCode {
8484
void format(raw_ostream &OS) {
8585
if (!PrunedSource)
8686
return;
87-
size_t MaxLineNumberWidth = std::ceil(std::log10(LastLine));
87+
size_t MaxLineNumberWidth = NumDigitsBase10(LastLine);
8888
int64_t L = FirstLine;
8989
for (size_t Pos = 0; Pos < PrunedSource->size(); ++L) {
9090
size_t PosEnd = PrunedSource->find('\n', Pos);

llvm/lib/Support/Signals.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ static bool printSymbolizedStackTrace(StringRef Argv0, void **StackTrace,
221221
for (int i = 0; i < Depth; i++) {
222222
auto PrintLineHeader = [&]() {
223223
OS << right_justify(formatv("#{0}", frame_no++).str(),
224-
std::log10(Depth) + 2)
224+
NumDigitsBase10(Depth) + 1)
225225
<< ' ' << format_ptr(StackTrace[i]) << ' ';
226226
};
227227
if (!Modules[i]) {

llvm/tools/llvm-pdbutil/BytesOutputStyle.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ static void iterateModules(PDBFile &File, LinePrinter &P, uint32_t IndentLevel,
370370
Callback);
371371
} else {
372372
uint32_t Count = Modules.getModuleCount();
373-
uint32_t Digits = NumDigits(Count);
373+
uint32_t Digits = NumDigitsBase10(Count);
374374
for (uint32_t I = 0; I < Count; ++I) {
375375
iterateOneModule(File, P, Modules, I, Digits, IndentLevel, Callback);
376376
}

llvm/tools/llvm-pdbutil/DumpOutputStyle.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -363,16 +363,16 @@ Error DumpOutputStyle::dumpStreamSummary() {
363363
for (uint32_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) {
364364
P.formatLine(
365365
"Stream {0} ({1} bytes): [{2}]",
366-
fmt_align(StreamIdx, AlignStyle::Right, NumDigits(StreamCount)),
366+
fmt_align(StreamIdx, AlignStyle::Right, NumDigitsBase10(StreamCount)),
367367
fmt_align(getPdb().getStreamByteSize(StreamIdx), AlignStyle::Right,
368-
NumDigits(MaxStreamSize)),
368+
NumDigitsBase10(MaxStreamSize)),
369369
StreamPurposes[StreamIdx].getLongName());
370370

371371
if (opts::dump::DumpStreamBlocks) {
372372
auto Blocks = getPdb().getStreamBlockList(StreamIdx);
373373
std::vector<uint32_t> BV(Blocks.begin(), Blocks.end());
374374
P.formatLine(" {0} Blocks: [{1}]",
375-
fmt_repeat(' ', NumDigits(StreamCount)),
375+
fmt_repeat(' ', NumDigitsBase10(StreamCount)),
376376
make_range(BV.begin(), BV.end()));
377377
}
378378
}
@@ -572,7 +572,7 @@ Error DumpOutputStyle::dumpSymbolStats() {
572572
if (StreamIdx == kInvalidStreamIndex) {
573573
P.formatLine(
574574
"Mod {0} (debug info not present): [{1}]",
575-
fmt_align(Modi, AlignStyle::Right, NumDigits(ModCount)),
575+
fmt_align(Modi, AlignStyle::Right, NumDigitsBase10(ModCount)),
576576
Desc.getModuleName());
577577
return Error::success();
578578
}
@@ -749,11 +749,11 @@ Error DumpOutputStyle::dumpUdtStats() {
749749
// separators.
750750
StringRef CountHeader("Count");
751751
StringRef SizeHeader("Size");
752-
size_t CD = NumDigits(UdtStats.Totals.Count);
752+
size_t CD = NumDigitsBase10(UdtStats.Totals.Count);
753753
CD += (CD - 1) / 3;
754754
CD = std::max(CD, CountHeader.size());
755755

756-
size_t SD = NumDigits(UdtStats.Totals.Size);
756+
size_t SD = NumDigitsBase10(UdtStats.Totals.Size);
757757
SD += (SD - 1) / 3;
758758
SD = std::max(SD, SizeHeader.size());
759759

@@ -1071,7 +1071,7 @@ Error DumpOutputStyle::dumpStringTableFromPdb() {
10711071
P.formatLine("Empty");
10721072
else {
10731073
auto MaxID = llvm::max_element(IS->name_ids());
1074-
uint32_t Digits = NumDigits(*MaxID);
1074+
uint32_t Digits = NumDigitsBase10(*MaxID);
10751075

10761076
P.formatLine("{0} | {1}", fmt_align("ID", AlignStyle::Right, Digits),
10771077
"String");
@@ -1205,7 +1205,7 @@ dumpFullTypeStream(LinePrinter &Printer, LazyRandomTypeCollection &Types,
12051205
TpiStream *Stream, bool Bytes, bool Extras) {
12061206

12071207
Printer.formatLine("Showing {0:N} records", NumTypeRecords);
1208-
uint32_t Width = NumDigits(TypeIndex::FirstNonSimpleIndex + NumTypeRecords);
1208+
uint32_t Width = NumDigitsBase10(TypeIndex::FirstNonSimpleIndex + NumTypeRecords);
12091209

12101210
MinimalTypeDumpVisitor V(Printer, Width + 2, Bytes, Extras, Types, RefTracker,
12111211
NumHashBuckets, HashValues, Stream);
@@ -1222,7 +1222,7 @@ static void dumpPartialTypeStream(LinePrinter &Printer,
12221222
TpiStream &Stream, ArrayRef<TypeIndex> TiList,
12231223
bool Bytes, bool Extras, bool Deps) {
12241224
uint32_t Width =
1225-
NumDigits(TypeIndex::FirstNonSimpleIndex + Stream.getNumTypeRecords());
1225+
NumDigitsBase10(TypeIndex::FirstNonSimpleIndex + Stream.getNumTypeRecords());
12261226

12271227
MinimalTypeDumpVisitor V(Printer, Width + 2, Bytes, Extras, Types, RefTracker,
12281228
Stream.getNumHashBuckets(), Stream.getHashValues(),

llvm/tools/llvm-pdbutil/MinimalTypeDumper.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ Error MinimalTypeDumpVisitor::visitKnownRecord(CVType &CVR,
309309
return Error::success();
310310

311311
auto Max = llvm::max_element(Indices);
312-
uint32_t W = NumDigits(Max->getIndex()) + 2;
312+
uint32_t W = NumDigitsBase10(Max->getIndex()) + 2;
313313

314314
for (auto I : Indices)
315315
P.formatLine("{0}: `{1}`", fmt_align(I, AlignStyle::Right, W),
@@ -324,7 +324,7 @@ Error MinimalTypeDumpVisitor::visitKnownRecord(CVType &CVR,
324324
return Error::success();
325325

326326
auto Max = llvm::max_element(Indices);
327-
uint32_t W = NumDigits(Max->getIndex()) + 2;
327+
uint32_t W = NumDigitsBase10(Max->getIndex()) + 2;
328328

329329
for (auto I : Indices)
330330
P.formatLine("{0}: `{1}`", fmt_align(I, AlignStyle::Right, W),
@@ -494,7 +494,7 @@ Error MinimalTypeDumpVisitor::visitKnownRecord(CVType &CVR,
494494
return Error::success();
495495

496496
auto Max = llvm::max_element(Indices);
497-
uint32_t W = NumDigits(Max->getIndex()) + 2;
497+
uint32_t W = NumDigitsBase10(Max->getIndex()) + 2;
498498

499499
for (auto I : Indices)
500500
P.formatLine("{0}: `{1}`", fmt_align(I, AlignStyle::Right, W),

llvm/tools/llvm-remarkutil/RemarkInstructionMix.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ static Error tryInstructionMix() {
111111
Mix.begin(), Mix.end(), 1, [](unsigned MaxValue, const MixEntry &Elt) {
112112
return std::max(MaxValue, Elt.second);
113113
});
114-
unsigned ValueWidth = std::log10(MaxValue) + 1;
114+
unsigned ValueWidth = NumDigitsBase10(MaxValue);
115115
FOS << "Instruction";
116116
FOS.PadToColumn(MaxMnemonic + 1) << "Count\n";
117117
FOS << "-----------";

llvm/unittests/Support/MathExtrasTest.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,4 +692,19 @@ TYPED_TEST(OverflowTest, MulResultZero) {
692692
EXPECT_FALSE(MulOverflow<TypeParam>(0, -5, Result));
693693
EXPECT_EQ(Result, TypeParam(0));
694694
}
695+
696+
TEST(MathExtras, NumDigitsBase10) {
697+
EXPECT_EQ(NumDigitsBase10(0), 1);
698+
EXPECT_EQ(NumDigitsBase10(1), 1);
699+
700+
uint64_t Val = 10;
701+
for (int i = 2; i <= 20; i++) {
702+
EXPECT_EQ(NumDigitsBase10(Val - 1), i - 1);
703+
EXPECT_EQ(NumDigitsBase10(Val), i);
704+
Val *= 10;
705+
}
706+
707+
EXPECT_EQ(NumDigitsBase10(std::numeric_limits<uint64_t>::max()), 20);
708+
}
709+
695710
} // namespace

llvm/utils/FileCheck/FileCheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ static void DumpAnnotatedInput(raw_ostream &OS, const FileCheckRequest &Req,
595595
unsigned LineCount = InputFileText.count('\n');
596596
if (InputFileEnd[-1] != '\n')
597597
++LineCount;
598-
unsigned LineNoWidth = std::log10(LineCount) + 1;
598+
unsigned LineNoWidth = NumDigitsBase10(LineCount);
599599
// +3 below adds spaces (1) to the left of the (right-aligned) line numbers
600600
// on input lines and (2) to the right of the (left-aligned) labels on
601601
// annotation lines so that input lines and annotation lines are more

0 commit comments

Comments
 (0)