blob: b951a88a38db531a599ec6fea34e69cabc1e3266 [file] [log] [blame]
Zachary Turner733be512016-10-11 19:24:45 +00001//===- NativeFormatting.cpp - Low level formatting helpers -------*- C++-*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/Support/NativeFormatting.h"
11
Zachary Turner5b2243e2016-10-29 00:27:22 +000012#include "llvm/ADT/ArrayRef.h"
Zachary Turner733be512016-10-11 19:24:45 +000013#include "llvm/ADT/SmallString.h"
14#include "llvm/ADT/StringExtras.h"
15#include "llvm/Support/Format.h"
16
17using namespace llvm;
18
19template<typename T, std::size_t N>
20static int format_to_buffer(T Value, char (&Buffer)[N]) {
21 char *EndPtr = std::end(Buffer);
22 char *CurPtr = EndPtr;
23
Zachary Turner5b2243e2016-10-29 00:27:22 +000024 do {
Zachary Turner733be512016-10-11 19:24:45 +000025 *--CurPtr = '0' + char(Value % 10);
26 Value /= 10;
Zachary Turner5b2243e2016-10-29 00:27:22 +000027 } while (Value);
Zachary Turner733be512016-10-11 19:24:45 +000028 return EndPtr - CurPtr;
29}
30
Zachary Turner5b2243e2016-10-29 00:27:22 +000031static void writeWithCommas(raw_ostream &S, ArrayRef<char> Buffer) {
32 assert(!Buffer.empty());
Zachary Turner733be512016-10-11 19:24:45 +000033
Zachary Turner5b2243e2016-10-29 00:27:22 +000034 ArrayRef<char> ThisGroup;
35 int InitialDigits = ((Buffer.size() - 1) % 3) + 1;
36 ThisGroup = Buffer.take_front(InitialDigits);
37 S.write(ThisGroup.data(), ThisGroup.size());
38
39 Buffer = Buffer.drop_front(InitialDigits);
40 assert(Buffer.size() % 3 == 0);
41 while (!Buffer.empty()) {
42 S << ',';
43 ThisGroup = Buffer.take_front(3);
44 S.write(ThisGroup.data(), 3);
45 Buffer = Buffer.drop_front(3);
46 }
Zachary Turner99eef2d2016-10-17 20:57:45 +000047}
48
Zachary Turner5b2243e2016-10-29 00:27:22 +000049template <typename T>
Zachary Turner11db2642016-11-11 23:57:40 +000050static void write_unsigned_impl(raw_ostream &S, T N, size_t MinDigits,
51 IntegerStyle Style, bool IsNegative) {
Zachary Turner5b2243e2016-10-29 00:27:22 +000052 static_assert(std::is_unsigned<T>::value, "Value is not unsigned!");
53
54 char NumberBuffer[128];
55 std::memset(NumberBuffer, '0', sizeof(NumberBuffer));
56
57 size_t Len = 0;
58 Len = format_to_buffer(N, NumberBuffer);
59
60 if (IsNegative)
61 S << '-';
Zachary Turner11db2642016-11-11 23:57:40 +000062
63 if (Len < MinDigits && Style != IntegerStyle::Number) {
64 for (size_t I = Len; I < MinDigits; ++I)
65 S << '0';
66 }
67
Zachary Turner5b2243e2016-10-29 00:27:22 +000068 if (Style == IntegerStyle::Number) {
69 writeWithCommas(S, ArrayRef<char>(std::end(NumberBuffer) - Len, Len));
70 } else {
71 S.write(std::end(NumberBuffer) - Len, Len);
72 }
73}
74
75template <typename T>
Zachary Turner11db2642016-11-11 23:57:40 +000076static void write_unsigned(raw_ostream &S, T N, size_t MinDigits,
77 IntegerStyle Style, bool IsNegative = false) {
Zachary Turner5b2243e2016-10-29 00:27:22 +000078 // Output using 32-bit div/mod if possible.
79 if (N == static_cast<uint32_t>(N))
Zachary Turner11db2642016-11-11 23:57:40 +000080 write_unsigned_impl(S, static_cast<uint32_t>(N), MinDigits, Style,
81 IsNegative);
Zachary Turner5b2243e2016-10-29 00:27:22 +000082 else
Zachary Turner11db2642016-11-11 23:57:40 +000083 write_unsigned_impl(S, N, MinDigits, Style, IsNegative);
Zachary Turner5b2243e2016-10-29 00:27:22 +000084}
85
86template <typename T>
Zachary Turner11db2642016-11-11 23:57:40 +000087static void write_signed(raw_ostream &S, T N, size_t MinDigits,
88 IntegerStyle Style) {
Zachary Turner5b2243e2016-10-29 00:27:22 +000089 static_assert(std::is_signed<T>::value, "Value is not signed!");
90
91 using UnsignedT = typename std::make_unsigned<T>::type;
92
Zachary Turner99eef2d2016-10-17 20:57:45 +000093 if (N >= 0) {
Zachary Turner11db2642016-11-11 23:57:40 +000094 write_unsigned(S, static_cast<UnsignedT>(N), MinDigits, Style);
Zachary Turner99eef2d2016-10-17 20:57:45 +000095 return;
96 }
97
Zachary Turner5b2243e2016-10-29 00:27:22 +000098 UnsignedT UN = -(UnsignedT)N;
Zachary Turner11db2642016-11-11 23:57:40 +000099 write_unsigned(S, UN, MinDigits, Style, true);
Renato Golin9ce50742016-10-18 09:30:18 +0000100}
101
Zachary Turner11db2642016-11-11 23:57:40 +0000102void llvm::write_integer(raw_ostream &S, unsigned int N, size_t MinDigits,
Zachary Turner5b2243e2016-10-29 00:27:22 +0000103 IntegerStyle Style) {
Zachary Turner11db2642016-11-11 23:57:40 +0000104 write_unsigned(S, N, MinDigits, Style);
Zachary Turner5b2243e2016-10-29 00:27:22 +0000105}
106
Zachary Turner11db2642016-11-11 23:57:40 +0000107void llvm::write_integer(raw_ostream &S, int N, size_t MinDigits,
108 IntegerStyle Style) {
109 write_signed(S, N, MinDigits, Style);
110}
111
112void llvm::write_integer(raw_ostream &S, unsigned long N, size_t MinDigits,
113 IntegerStyle Style) {
114 write_unsigned(S, N, MinDigits, Style);
115}
116
117void llvm::write_integer(raw_ostream &S, long N, size_t MinDigits,
118 IntegerStyle Style) {
119 write_signed(S, N, MinDigits, Style);
120}
121
122void llvm::write_integer(raw_ostream &S, unsigned long long N, size_t MinDigits,
123 IntegerStyle Style) {
124 write_unsigned(S, N, MinDigits, Style);
125}
126
127void llvm::write_integer(raw_ostream &S, long long N, size_t MinDigits,
128 IntegerStyle Style) {
129 write_signed(S, N, MinDigits, Style);
Zachary Turner5b2243e2016-10-29 00:27:22 +0000130}
131
132void llvm::write_hex(raw_ostream &S, uint64_t N, HexPrintStyle Style,
133 Optional<size_t> Width) {
134 const size_t kMaxWidth = 128u;
135
136 size_t W = std::min(kMaxWidth, Width.getValueOr(0u));
137
Zachary Turner733be512016-10-11 19:24:45 +0000138 unsigned Nibbles = (64 - countLeadingZeros(N) + 3) / 4;
Zachary Turner5b2243e2016-10-29 00:27:22 +0000139 bool Prefix = (Style == HexPrintStyle::PrefixLower ||
140 Style == HexPrintStyle::PrefixUpper);
141 bool Upper =
142 (Style == HexPrintStyle::Upper || Style == HexPrintStyle::PrefixUpper);
Zachary Turner733be512016-10-11 19:24:45 +0000143 unsigned PrefixChars = Prefix ? 2 : 0;
Zachary Turner5b2243e2016-10-29 00:27:22 +0000144 unsigned NumChars =
145 std::max(static_cast<unsigned>(W), std::max(1u, Nibbles) + PrefixChars);
Zachary Turner733be512016-10-11 19:24:45 +0000146
Zachary Turner5b2243e2016-10-29 00:27:22 +0000147 char NumberBuffer[kMaxWidth];
148 ::memset(NumberBuffer, '0', llvm::array_lengthof(NumberBuffer));
149 if (Prefix)
150 NumberBuffer[1] = 'x';
151 char *EndPtr = NumberBuffer + NumChars;
Zachary Turner733be512016-10-11 19:24:45 +0000152 char *CurPtr = EndPtr;
153 while (N) {
154 unsigned char x = static_cast<unsigned char>(N) % 16;
155 *--CurPtr = hexdigit(x, !Upper);
156 N /= 16;
157 }
158
Zachary Turner5b2243e2016-10-29 00:27:22 +0000159 S.write(NumberBuffer, NumChars);
Zachary Turner733be512016-10-11 19:24:45 +0000160}
161
Zachary Turner5b2243e2016-10-29 00:27:22 +0000162void llvm::write_double(raw_ostream &S, double N, FloatStyle Style,
163 Optional<size_t> Precision) {
164 size_t Prec = Precision.getValueOr(getDefaultPrecision(Style));
165
166 if (std::isnan(N)) {
167 S << "nan";
168 return;
169 } else if (std::isinf(N)) {
170 S << "INF";
171 return;
172 }
173
174 char Letter;
175 if (Style == FloatStyle::Exponent)
176 Letter = 'e';
177 else if (Style == FloatStyle::ExponentUpper)
178 Letter = 'E';
179 else
180 Letter = 'f';
181
Zachary Turner733be512016-10-11 19:24:45 +0000182 SmallString<8> Spec;
183 llvm::raw_svector_ostream Out(Spec);
Zachary Turner5b2243e2016-10-29 00:27:22 +0000184 Out << "%." << Prec << Letter;
Zachary Turner733be512016-10-11 19:24:45 +0000185
Zachary Turner5b2243e2016-10-29 00:27:22 +0000186 if (Style == FloatStyle::Exponent || Style == FloatStyle::ExponentUpper) {
Zachary Turner733be512016-10-11 19:24:45 +0000187#ifdef _WIN32
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)) {
Zachary Turner11db2642016-11-11 23:57:40 +0000194 char NegativeZero[] = "-0.000000e+00";
195 if (Style == FloatStyle::ExponentUpper)
196 NegativeZero[strlen(NegativeZero) - 4] = 'E';
Zachary Turner5b2243e2016-10-29 00:27:22 +0000197 S << NegativeZero;
Zachary Turner733be512016-10-11 19:24:45 +0000198 return;
199 }
200#else
201 int fpcl = _fpclass(N);
202
203 // negative zero
204 if (fpcl == _FPCLASS_NZ) {
Zachary Turner11db2642016-11-11 23:57:40 +0000205 char NegativeZero[] = "-0.000000e+00";
206 if (Style == FloatStyle::ExponentUpper)
207 NegativeZero[strlen(NegativeZero) - 4] = 'E';
Zachary Turner5b2243e2016-10-29 00:27:22 +0000208 S << NegativeZero;
Zachary Turner733be512016-10-11 19:24:45 +0000209 return;
210 }
211#endif
212
Zachary Turner5b2243e2016-10-29 00:27:22 +0000213 char buf[32];
Zachary Turner733be512016-10-11 19:24:45 +0000214 unsigned len;
215 len = format(Spec.c_str(), N).snprint(buf, sizeof(buf));
216 if (len <= sizeof(buf) - 2) {
Zachary Turner5b2243e2016-10-29 00:27:22 +0000217 if (len >= 5 && (buf[len - 5] == 'e' || buf[len - 5] == 'E') &&
218 buf[len - 3] == '0') {
Zachary Turner733be512016-10-11 19:24:45 +0000219 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"
226 buf[len - 3] = c1;
227 buf[len - 2] = c0;
228 buf[--len] = 0;
229 }
230 }
231 }
232 S << buf;
233 return;
234 }
235#endif
236 }
237
Zachary Turner5b2243e2016-10-29 00:27:22 +0000238 if (Style == FloatStyle::Percent)
239 N *= 100.0;
240
241 char Buf[32];
Eric Christopher0192e972017-01-04 19:47:10 +0000242 format(Spec.c_str(), N).snprint(Buf, sizeof(Buf));
Zachary Turner5b2243e2016-10-29 00:27:22 +0000243 S << Buf;
244 if (Style == FloatStyle::Percent)
245 S << '%';
246}
247
Zachary Turner11db2642016-11-11 23:57:40 +0000248bool llvm::isPrefixedHexStyle(HexPrintStyle S) {
249 return (S == HexPrintStyle::PrefixLower || S == HexPrintStyle::PrefixUpper);
250}
251
Zachary Turner5b2243e2016-10-29 00:27:22 +0000252size_t llvm::getDefaultPrecision(FloatStyle Style) {
253 switch (Style) {
254 case FloatStyle::Exponent:
255 case FloatStyle::ExponentUpper:
256 return 6; // Number of decimal places.
257 case FloatStyle::Fixed:
258 case FloatStyle::Percent:
259 return 2; // Number of decimal places.
260 }
261 LLVM_BUILTIN_UNREACHABLE;
Zachary Turner733be512016-10-11 19:24:45 +0000262}