1 //===- NativeFormatting.cpp - Low level formatting helpers -------*- C++-*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/NativeFormatting.h"
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/ADT/StringExtras.h"
15 #include "llvm/Support/Format.h"
19 template<typename T, std::size_t N>
20 static int format_to_buffer(T Value, char (&Buffer)[N]) {
21 char *EndPtr = std::end(Buffer);
22 char *CurPtr = EndPtr;
25 *--CurPtr = '0' + char(Value % 10);
28 return EndPtr - CurPtr;
31 static void writeWithCommas(raw_ostream &S, ArrayRef<char> Buffer) {
32 assert(!Buffer.empty());
34 ArrayRef<char> ThisGroup;
35 int InitialDigits = ((Buffer.size() - 1) % 3) + 1;
36 ThisGroup = Buffer.take_front(InitialDigits);
37 S.write(ThisGroup.data(), ThisGroup.size());
39 Buffer = Buffer.drop_front(InitialDigits);
40 assert(Buffer.size() % 3 == 0);
41 while (!Buffer.empty()) {
43 ThisGroup = Buffer.take_front(3);
44 S.write(ThisGroup.data(), 3);
45 Buffer = Buffer.drop_front(3);
50 static void write_unsigned_impl(raw_ostream &S, T N, size_t MinDigits,
51 IntegerStyle Style, bool IsNegative) {
52 static_assert(std::is_unsigned<T>::value, "Value is not unsigned!");
54 char NumberBuffer[128];
55 std::memset(NumberBuffer, '0', sizeof(NumberBuffer));
58 Len = format_to_buffer(N, NumberBuffer);
63 if (Len < MinDigits && Style != IntegerStyle::Number) {
64 for (size_t I = Len; I < MinDigits; ++I)
68 if (Style == IntegerStyle::Number) {
69 writeWithCommas(S, ArrayRef<char>(std::end(NumberBuffer) - Len, Len));
71 S.write(std::end(NumberBuffer) - Len, Len);
76 static void write_unsigned(raw_ostream &S, T N, size_t MinDigits,
77 IntegerStyle Style, bool IsNegative = false) {
78 // Output using 32-bit div/mod if possible.
79 if (N == static_cast<uint32_t>(N))
80 write_unsigned_impl(S, static_cast<uint32_t>(N), MinDigits, Style,
83 write_unsigned_impl(S, N, MinDigits, Style, IsNegative);
87 static void write_signed(raw_ostream &S, T N, size_t MinDigits,
89 static_assert(std::is_signed<T>::value, "Value is not signed!");
91 using UnsignedT = typename std::make_unsigned<T>::type;
94 write_unsigned(S, static_cast<UnsignedT>(N), MinDigits, Style);
98 UnsignedT UN = -(UnsignedT)N;
99 write_unsigned(S, UN, MinDigits, Style, true);
102 void llvm::write_integer(raw_ostream &S, unsigned int N, size_t MinDigits,
103 IntegerStyle Style) {
104 write_unsigned(S, N, MinDigits, Style);
107 void llvm::write_integer(raw_ostream &S, int N, size_t MinDigits,
108 IntegerStyle Style) {
109 write_signed(S, N, MinDigits, Style);
112 void llvm::write_integer(raw_ostream &S, unsigned long N, size_t MinDigits,
113 IntegerStyle Style) {
114 write_unsigned(S, N, MinDigits, Style);
117 void llvm::write_integer(raw_ostream &S, long N, size_t MinDigits,
118 IntegerStyle Style) {
119 write_signed(S, N, MinDigits, Style);
122 void llvm::write_integer(raw_ostream &S, unsigned long long N, size_t MinDigits,
123 IntegerStyle Style) {
124 write_unsigned(S, N, MinDigits, Style);
127 void llvm::write_integer(raw_ostream &S, long long N, size_t MinDigits,
128 IntegerStyle Style) {
129 write_signed(S, N, MinDigits, Style);
132 void llvm::write_hex(raw_ostream &S, uint64_t N, HexPrintStyle Style,
133 Optional<size_t> Width) {
134 const size_t kMaxWidth = 128u;
136 size_t W = std::min(kMaxWidth, Width.getValueOr(0u));
138 unsigned Nibbles = (64 - countLeadingZeros(N) + 3) / 4;
139 bool Prefix = (Style == HexPrintStyle::PrefixLower ||
140 Style == HexPrintStyle::PrefixUpper);
142 (Style == HexPrintStyle::Upper || Style == HexPrintStyle::PrefixUpper);
143 unsigned PrefixChars = Prefix ? 2 : 0;
145 std::max(static_cast<unsigned>(W), std::max(1u, Nibbles) + PrefixChars);
147 char NumberBuffer[kMaxWidth];
148 ::memset(NumberBuffer, '0', llvm::array_lengthof(NumberBuffer));
150 NumberBuffer[1] = 'x';
151 char *EndPtr = NumberBuffer + NumChars;
152 char *CurPtr = EndPtr;
154 unsigned char x = static_cast<unsigned char>(N) % 16;
155 *--CurPtr = hexdigit(x, !Upper);
159 S.write(NumberBuffer, NumChars);
162 void llvm::write_double(raw_ostream &S, double N, FloatStyle Style,
163 Optional<size_t> Precision) {
164 size_t Prec = Precision.getValueOr(getDefaultPrecision(Style));
169 } else if (std::isinf(N)) {
175 if (Style == FloatStyle::Exponent)
177 else if (Style == FloatStyle::ExponentUpper)
183 llvm::raw_svector_ostream Out(Spec);
184 Out << "%." << Prec << Letter;
186 if (Style == FloatStyle::Exponent || Style == FloatStyle::ExponentUpper) {
188 // On MSVCRT and compatible, output of %e is incompatible to Posix
189 // by default. Number of exponent digits should be at least 2. "%+03d"
190 // FIXME: Implement our formatter to here or Support/Format.h!
191 #if defined(__MINGW32__)
192 // FIXME: It should be generic to C++11.
193 if (N == 0.0 && std::signbit(N)) {
194 char NegativeZero[] = "-0.000000e+00";
195 if (Style == FloatStyle::ExponentUpper)
196 NegativeZero[strlen(NegativeZero) - 4] = 'E';
201 int fpcl = _fpclass(N);
204 if (fpcl == _FPCLASS_NZ) {
205 char NegativeZero[] = "-0.000000e+00";
206 if (Style == FloatStyle::ExponentUpper)
207 NegativeZero[strlen(NegativeZero) - 4] = 'E';
215 len = format(Spec.c_str(), N).snprint(buf, sizeof(buf));
216 if (len <= sizeof(buf) - 2) {
217 if (len >= 5 && (buf[len - 5] == 'e' || buf[len - 5] == 'E') &&
218 buf[len - 3] == '0') {
219 int cs = buf[len - 4];
220 if (cs == '+' || cs == '-') {
221 int c1 = buf[len - 2];
222 int c0 = buf[len - 1];
223 if (isdigit(static_cast<unsigned char>(c1)) &&
224 isdigit(static_cast<unsigned char>(c0))) {
225 // Trim leading '0': "...e+012" -> "...e+12\0"
238 if (Style == FloatStyle::Percent)
243 Len = format(Spec.c_str(), N).snprint(Buf, sizeof(Buf));
244 if (Style == FloatStyle::Percent)
247 if (Style == FloatStyle::Percent)
251 bool llvm::isPrefixedHexStyle(HexPrintStyle S) {
252 return (S == HexPrintStyle::PrefixLower || S == HexPrintStyle::PrefixUpper);
255 size_t llvm::getDefaultPrecision(FloatStyle Style) {
257 case FloatStyle::Exponent:
258 case FloatStyle::ExponentUpper:
259 return 6; // Number of decimal places.
260 case FloatStyle::Fixed:
261 case FloatStyle::Percent:
262 return 2; // Number of decimal places.
264 LLVM_BUILTIN_UNREACHABLE;