1 //===- NativeFormatting.cpp - Low level formatting helpers -------*- C++-*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/NativeFormatting.h"
11 #include "llvm/ADT/ArrayRef.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/StringExtras.h"
14 #include "llvm/Support/Format.h"
20 template<typename T
, std::size_t N
>
21 static int format_to_buffer(T Value
, char (&Buffer
)[N
]) {
22 char *EndPtr
= std::end(Buffer
);
23 char *CurPtr
= EndPtr
;
26 *--CurPtr
= '0' + char(Value
% 10);
29 return EndPtr
- CurPtr
;
32 static void writeWithCommas(raw_ostream
&S
, ArrayRef
<char> Buffer
) {
33 assert(!Buffer
.empty());
35 ArrayRef
<char> ThisGroup
;
36 int InitialDigits
= ((Buffer
.size() - 1) % 3) + 1;
37 ThisGroup
= Buffer
.take_front(InitialDigits
);
38 S
.write(ThisGroup
.data(), ThisGroup
.size());
40 Buffer
= Buffer
.drop_front(InitialDigits
);
41 assert(Buffer
.size() % 3 == 0);
42 while (!Buffer
.empty()) {
44 ThisGroup
= Buffer
.take_front(3);
45 S
.write(ThisGroup
.data(), 3);
46 Buffer
= Buffer
.drop_front(3);
51 static void write_unsigned_impl(raw_ostream
&S
, T N
, size_t MinDigits
,
52 IntegerStyle Style
, bool IsNegative
) {
53 static_assert(std::is_unsigned
<T
>::value
, "Value is not unsigned!");
55 char NumberBuffer
[128];
56 std::memset(NumberBuffer
, '0', sizeof(NumberBuffer
));
59 Len
= format_to_buffer(N
, NumberBuffer
);
64 if (Len
< MinDigits
&& Style
!= IntegerStyle::Number
) {
65 for (size_t I
= Len
; I
< MinDigits
; ++I
)
69 if (Style
== IntegerStyle::Number
) {
70 writeWithCommas(S
, ArrayRef
<char>(std::end(NumberBuffer
) - Len
, Len
));
72 S
.write(std::end(NumberBuffer
) - Len
, Len
);
77 static void write_unsigned(raw_ostream
&S
, T N
, size_t MinDigits
,
78 IntegerStyle Style
, bool IsNegative
= false) {
79 // Output using 32-bit div/mod if possible.
80 if (N
== static_cast<uint32_t>(N
))
81 write_unsigned_impl(S
, static_cast<uint32_t>(N
), MinDigits
, Style
,
84 write_unsigned_impl(S
, N
, MinDigits
, Style
, IsNegative
);
88 static void write_signed(raw_ostream
&S
, T N
, size_t MinDigits
,
90 static_assert(std::is_signed
<T
>::value
, "Value is not signed!");
92 using UnsignedT
= typename
std::make_unsigned
<T
>::type
;
95 write_unsigned(S
, static_cast<UnsignedT
>(N
), MinDigits
, Style
);
99 UnsignedT UN
= -(UnsignedT
)N
;
100 write_unsigned(S
, UN
, MinDigits
, Style
, true);
103 void llvm::write_integer(raw_ostream
&S
, unsigned int N
, size_t MinDigits
,
104 IntegerStyle Style
) {
105 write_unsigned(S
, N
, MinDigits
, Style
);
108 void llvm::write_integer(raw_ostream
&S
, int N
, size_t MinDigits
,
109 IntegerStyle Style
) {
110 write_signed(S
, N
, MinDigits
, Style
);
113 void llvm::write_integer(raw_ostream
&S
, unsigned long N
, size_t MinDigits
,
114 IntegerStyle Style
) {
115 write_unsigned(S
, N
, MinDigits
, Style
);
118 void llvm::write_integer(raw_ostream
&S
, long N
, size_t MinDigits
,
119 IntegerStyle Style
) {
120 write_signed(S
, N
, MinDigits
, Style
);
123 void llvm::write_integer(raw_ostream
&S
, unsigned long long N
, size_t MinDigits
,
124 IntegerStyle Style
) {
125 write_unsigned(S
, N
, MinDigits
, Style
);
128 void llvm::write_integer(raw_ostream
&S
, long long N
, size_t MinDigits
,
129 IntegerStyle Style
) {
130 write_signed(S
, N
, MinDigits
, Style
);
133 void llvm::write_hex(raw_ostream
&S
, uint64_t N
, HexPrintStyle Style
,
134 Optional
<size_t> Width
) {
135 const size_t kMaxWidth
= 128u;
137 size_t W
= std::min(kMaxWidth
, Width
.getValueOr(0u));
139 unsigned Nibbles
= (64 - countLeadingZeros(N
) + 3) / 4;
140 bool Prefix
= (Style
== HexPrintStyle::PrefixLower
||
141 Style
== HexPrintStyle::PrefixUpper
);
143 (Style
== HexPrintStyle::Upper
|| Style
== HexPrintStyle::PrefixUpper
);
144 unsigned PrefixChars
= Prefix
? 2 : 0;
146 std::max(static_cast<unsigned>(W
), std::max(1u, Nibbles
) + PrefixChars
);
148 char NumberBuffer
[kMaxWidth
];
149 ::memset(NumberBuffer
, '0', llvm::array_lengthof(NumberBuffer
));
151 NumberBuffer
[1] = 'x';
152 char *EndPtr
= NumberBuffer
+ NumChars
;
153 char *CurPtr
= EndPtr
;
155 unsigned char x
= static_cast<unsigned char>(N
) % 16;
156 *--CurPtr
= hexdigit(x
, !Upper
);
160 S
.write(NumberBuffer
, NumChars
);
163 void llvm::write_double(raw_ostream
&S
, double N
, FloatStyle Style
,
164 Optional
<size_t> Precision
) {
165 size_t Prec
= Precision
.getValueOr(getDefaultPrecision(Style
));
170 } else if (std::isinf(N
)) {
176 if (Style
== FloatStyle::Exponent
)
178 else if (Style
== FloatStyle::ExponentUpper
)
184 llvm::raw_svector_ostream
Out(Spec
);
185 Out
<< "%." << Prec
<< Letter
;
187 if (Style
== FloatStyle::Exponent
|| Style
== FloatStyle::ExponentUpper
) {
189 // On MSVCRT and compatible, output of %e is incompatible to Posix
190 // by default. Number of exponent digits should be at least 2. "%+03d"
191 // FIXME: Implement our formatter to here or Support/Format.h!
192 #if defined(__MINGW32__)
193 // FIXME: It should be generic to C++11.
194 if (N
== 0.0 && std::signbit(N
)) {
195 char NegativeZero
[] = "-0.000000e+00";
196 if (Style
== FloatStyle::ExponentUpper
)
197 NegativeZero
[strlen(NegativeZero
) - 4] = 'E';
202 int fpcl
= _fpclass(N
);
205 if (fpcl
== _FPCLASS_NZ
) {
206 char NegativeZero
[] = "-0.000000e+00";
207 if (Style
== FloatStyle::ExponentUpper
)
208 NegativeZero
[strlen(NegativeZero
) - 4] = 'E';
216 len
= format(Spec
.c_str(), N
).snprint(buf
, sizeof(buf
));
217 if (len
<= sizeof(buf
) - 2) {
218 if (len
>= 5 && (buf
[len
- 5] == 'e' || buf
[len
- 5] == 'E') &&
219 buf
[len
- 3] == '0') {
220 int cs
= buf
[len
- 4];
221 if (cs
== '+' || cs
== '-') {
222 int c1
= buf
[len
- 2];
223 int c0
= buf
[len
- 1];
224 if (isdigit(static_cast<unsigned char>(c1
)) &&
225 isdigit(static_cast<unsigned char>(c0
))) {
226 // Trim leading '0': "...e+012" -> "...e+12\0"
239 if (Style
== FloatStyle::Percent
)
243 format(Spec
.c_str(), N
).snprint(Buf
, sizeof(Buf
));
245 if (Style
== FloatStyle::Percent
)
249 bool llvm::isPrefixedHexStyle(HexPrintStyle S
) {
250 return (S
== HexPrintStyle::PrefixLower
|| S
== HexPrintStyle::PrefixUpper
);
253 size_t llvm::getDefaultPrecision(FloatStyle Style
) {
255 case FloatStyle::Exponent
:
256 case FloatStyle::ExponentUpper
:
257 return 6; // Number of decimal places.
258 case FloatStyle::Fixed
:
259 case FloatStyle::Percent
:
260 return 2; // Number of decimal places.
262 LLVM_BUILTIN_UNREACHABLE
;