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"
10 #include "llvm/ADT/ArrayRef.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/ADT/StringExtras.h"
13 #include "llvm/Support/Format.h"
14 #include "llvm/Support/raw_ostream.h"
18 #if defined(_WIN32) && !defined(__MINGW32__)
19 #include <float.h> // For _fpclass in llvm::write_double.
24 template<typename T
, std::size_t N
>
25 static int format_to_buffer(T Value
, char (&Buffer
)[N
]) {
26 char *EndPtr
= std::end(Buffer
);
27 char *CurPtr
= EndPtr
;
30 *--CurPtr
= '0' + char(Value
% 10);
33 return EndPtr
- CurPtr
;
36 static void writeWithCommas(raw_ostream
&S
, ArrayRef
<char> Buffer
) {
37 assert(!Buffer
.empty());
39 ArrayRef
<char> ThisGroup
;
40 int InitialDigits
= ((Buffer
.size() - 1) % 3) + 1;
41 ThisGroup
= Buffer
.take_front(InitialDigits
);
42 S
.write(ThisGroup
.data(), ThisGroup
.size());
44 Buffer
= Buffer
.drop_front(InitialDigits
);
45 assert(Buffer
.size() % 3 == 0);
46 while (!Buffer
.empty()) {
48 ThisGroup
= Buffer
.take_front(3);
49 S
.write(ThisGroup
.data(), 3);
50 Buffer
= Buffer
.drop_front(3);
55 static void write_unsigned_impl(raw_ostream
&S
, T N
, size_t MinDigits
,
56 IntegerStyle Style
, bool IsNegative
) {
57 static_assert(std::is_unsigned_v
<T
>, "Value is not unsigned!");
59 char NumberBuffer
[128];
60 size_t Len
= format_to_buffer(N
, NumberBuffer
);
65 if (Len
< MinDigits
&& Style
!= IntegerStyle::Number
) {
66 for (size_t I
= Len
; I
< MinDigits
; ++I
)
70 if (Style
== IntegerStyle::Number
) {
71 writeWithCommas(S
, ArrayRef
<char>(std::end(NumberBuffer
) - Len
, Len
));
73 S
.write(std::end(NumberBuffer
) - Len
, Len
);
78 static void write_unsigned(raw_ostream
&S
, T N
, size_t MinDigits
,
79 IntegerStyle Style
, bool IsNegative
= false) {
80 // Output using 32-bit div/mod if possible.
81 if (N
== static_cast<uint32_t>(N
))
82 write_unsigned_impl(S
, static_cast<uint32_t>(N
), MinDigits
, Style
,
85 write_unsigned_impl(S
, N
, MinDigits
, Style
, IsNegative
);
89 static void write_signed(raw_ostream
&S
, T N
, size_t MinDigits
,
91 static_assert(std::is_signed_v
<T
>, "Value is not signed!");
93 using UnsignedT
= std::make_unsigned_t
<T
>;
96 write_unsigned(S
, static_cast<UnsignedT
>(N
), MinDigits
, Style
);
100 UnsignedT UN
= -(UnsignedT
)N
;
101 write_unsigned(S
, UN
, MinDigits
, Style
, true);
104 void llvm::write_integer(raw_ostream
&S
, unsigned int N
, size_t MinDigits
,
105 IntegerStyle Style
) {
106 write_unsigned(S
, N
, MinDigits
, Style
);
109 void llvm::write_integer(raw_ostream
&S
, int N
, size_t MinDigits
,
110 IntegerStyle Style
) {
111 write_signed(S
, N
, MinDigits
, Style
);
114 void llvm::write_integer(raw_ostream
&S
, unsigned long N
, size_t MinDigits
,
115 IntegerStyle Style
) {
116 write_unsigned(S
, N
, MinDigits
, Style
);
119 void llvm::write_integer(raw_ostream
&S
, long N
, size_t MinDigits
,
120 IntegerStyle Style
) {
121 write_signed(S
, N
, MinDigits
, Style
);
124 void llvm::write_integer(raw_ostream
&S
, unsigned long long N
, size_t MinDigits
,
125 IntegerStyle Style
) {
126 write_unsigned(S
, N
, MinDigits
, Style
);
129 void llvm::write_integer(raw_ostream
&S
, long long N
, size_t MinDigits
,
130 IntegerStyle Style
) {
131 write_signed(S
, N
, MinDigits
, Style
);
134 void llvm::write_hex(raw_ostream
&S
, uint64_t N
, HexPrintStyle Style
,
135 std::optional
<size_t> Width
) {
136 const size_t kMaxWidth
= 128u;
138 size_t W
= std::min(kMaxWidth
, Width
.value_or(0u));
140 unsigned Nibbles
= (llvm::bit_width(N
) + 3) / 4;
141 bool Prefix
= (Style
== HexPrintStyle::PrefixLower
||
142 Style
== HexPrintStyle::PrefixUpper
);
144 (Style
== HexPrintStyle::Upper
|| Style
== HexPrintStyle::PrefixUpper
);
145 unsigned PrefixChars
= Prefix
? 2 : 0;
147 std::max(static_cast<unsigned>(W
), std::max(1u, Nibbles
) + PrefixChars
);
149 char NumberBuffer
[kMaxWidth
];
150 ::memset(NumberBuffer
, '0', std::size(NumberBuffer
));
152 NumberBuffer
[1] = 'x';
153 char *EndPtr
= NumberBuffer
+ NumChars
;
154 char *CurPtr
= EndPtr
;
156 unsigned char x
= static_cast<unsigned char>(N
) % 16;
157 *--CurPtr
= hexdigit(x
, !Upper
);
161 S
.write(NumberBuffer
, NumChars
);
164 void llvm::write_double(raw_ostream
&S
, double N
, FloatStyle Style
,
165 std::optional
<size_t> Precision
) {
166 size_t Prec
= Precision
.value_or(getDefaultPrecision(Style
));
171 } else if (std::isinf(N
)) {
172 S
<< (std::signbit(N
) ? "-INF" : "INF");
177 if (Style
== FloatStyle::Exponent
)
179 else if (Style
== FloatStyle::ExponentUpper
)
185 llvm::raw_svector_ostream
Out(Spec
);
186 Out
<< "%." << Prec
<< Letter
;
188 if (Style
== FloatStyle::Exponent
|| Style
== FloatStyle::ExponentUpper
) {
190 // On MSVCRT and compatible, output of %e is incompatible to Posix
191 // by default. Number of exponent digits should be at least 2. "%+03d"
192 // FIXME: Implement our formatter to here or Support/Format.h!
193 #if defined(__MINGW32__)
194 // FIXME: It should be generic to C++11.
195 if (N
== 0.0 && std::signbit(N
)) {
196 char NegativeZero
[] = "-0.000000e+00";
197 if (Style
== FloatStyle::ExponentUpper
)
198 NegativeZero
[strlen(NegativeZero
) - 4] = 'E';
203 int fpcl
= _fpclass(N
);
206 if (fpcl
== _FPCLASS_NZ
) {
207 char NegativeZero
[] = "-0.000000e+00";
208 if (Style
== FloatStyle::ExponentUpper
)
209 NegativeZero
[strlen(NegativeZero
) - 4] = 'E';
217 len
= format(Spec
.c_str(), N
).snprint(buf
, sizeof(buf
));
218 if (len
<= sizeof(buf
) - 2) {
219 if (len
>= 5 && (buf
[len
- 5] == 'e' || buf
[len
- 5] == 'E') &&
220 buf
[len
- 3] == '0') {
221 int cs
= buf
[len
- 4];
222 if (cs
== '+' || cs
== '-') {
223 int c1
= buf
[len
- 2];
224 int c0
= buf
[len
- 1];
225 if (isdigit(static_cast<unsigned char>(c1
)) &&
226 isdigit(static_cast<unsigned char>(c0
))) {
227 // Trim leading '0': "...e+012" -> "...e+12\0"
240 if (Style
== FloatStyle::Percent
)
244 format(Spec
.c_str(), N
).snprint(Buf
, sizeof(Buf
));
246 if (Style
== FloatStyle::Percent
)
250 bool llvm::isPrefixedHexStyle(HexPrintStyle S
) {
251 return (S
== HexPrintStyle::PrefixLower
|| S
== HexPrintStyle::PrefixUpper
);
254 size_t llvm::getDefaultPrecision(FloatStyle Style
) {
256 case FloatStyle::Exponent
:
257 case FloatStyle::ExponentUpper
:
258 return 6; // Number of decimal places.
259 case FloatStyle::Fixed
:
260 case FloatStyle::Percent
:
261 return 2; // Number of decimal places.
263 llvm_unreachable("Unknown FloatStyle enum");