Skip to content

Commit 5a1a346

Browse files
committed
[NFC] Move NumDigits to MathExtras.h and update some users of log10 to use NumDigits
1 parent a352f1e commit 5a1a346

File tree

6 files changed

+47
-47
lines changed

6 files changed

+47
-47
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/Support/MathExtras.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,49 @@ 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 NumDigits(uint64_t N) {
800+
if (N < 10ULL)
801+
return 1;
802+
if (N < 100ULL)
803+
return 2;
804+
if (N < 1000ULL)
805+
return 3;
806+
if (N < 10000ULL)
807+
return 4;
808+
if (N < 100000ULL)
809+
return 5;
810+
if (N < 1000000ULL)
811+
return 6;
812+
if (N < 10000000ULL)
813+
return 7;
814+
if (N < 100000000ULL)
815+
return 8;
816+
if (N < 1000000000ULL)
817+
return 9;
818+
if (N < 10000000000ULL)
819+
return 10;
820+
if (N < 100000000000ULL)
821+
return 11;
822+
if (N < 1000000000000ULL)
823+
return 12;
824+
if (N < 10000000000000ULL)
825+
return 13;
826+
if (N < 100000000000000ULL)
827+
return 14;
828+
if (N < 1000000000000000ULL)
829+
return 15;
830+
if (N < 10000000000000000ULL)
831+
return 16;
832+
if (N < 100000000000000000ULL)
833+
return 17;
834+
if (N < 1000000000000000000ULL)
835+
return 18;
836+
if (N < 10000000000000000000ULL)
837+
return 19;
838+
return 20;
839+
}
840+
798841
} // namespace llvm
799842

800843
#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 = NumDigits(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+
NumDigits(Depth) + 1)
225225
<< ' ' << format_ptr(StackTrace[i]) << ' ';
226226
};
227227
if (!Modules[i]) {

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 = NumDigits(MaxValue);
115115
FOS << "Instruction";
116116
FOS.PadToColumn(MaxMnemonic + 1) << "Count\n";
117117
FOS << "-----------";

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 = NumDigits(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)