blob: 85b4bfb81568dd1bda1f1e971cf743a84b1e15e5 [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
Peter Collingbourne719c1f72018-01-18 20:49:33 +000017#include <float.h>
18
Zachary Turner733be512016-10-11 19:24:45 +000019using namespace llvm;
20
21template<typename T, std::size_t N>
22static int format_to_buffer(T Value, char (&Buffer)[N]) {
23 char *EndPtr = std::end(Buffer);
24 char *CurPtr = EndPtr;
25
Zachary Turner5b2243e2016-10-29 00:27:22 +000026 do {
Zachary Turner733be512016-10-11 19:24:45 +000027 *--CurPtr = '0' + char(Value % 10);
28 Value /= 10;
Zachary Turner5b2243e2016-10-29 00:27:22 +000029 } while (Value);
Zachary Turner733be512016-10-11 19:24:45 +000030 return EndPtr - CurPtr;
31}
32
Zachary Turner5b2243e2016-10-29 00:27:22 +000033static void writeWithCommas(raw_ostream &S, ArrayRef<char> Buffer) {
34 assert(!Buffer.empty());
Zachary Turner733be512016-10-11 19:24:45 +000035
Zachary Turner5b2243e2016-10-29 00:27:22 +000036 ArrayRef<char> ThisGroup;
37 int InitialDigits = ((Buffer.size() - 1) % 3) + 1;
38 ThisGroup = Buffer.take_front(InitialDigits);
39 S.write(ThisGroup.data(), ThisGroup.size());
40
41 Buffer = Buffer.drop_front(InitialDigits);
42 assert(Buffer.size() % 3 == 0);
43 while (!Buffer.empty()) {
44 S << ',';
45 ThisGroup = Buffer.take_front(3);
46 S.write(ThisGroup.data(), 3);
47 Buffer = Buffer.drop_front(3);
48 }
Zachary Turner99eef2d2016-10-17 20:57:45 +000049}
50
Zachary Turner5b2243e2016-10-29 00:27:22 +000051template <typename T>
Zachary Turner11db2642016-11-11 23:57:40 +000052static void write_unsigned_impl(raw_ostream &S, T N, size_t MinDigits,
53 IntegerStyle Style, bool IsNegative) {
Zachary Turner5b2243e2016-10-29 00:27:22 +000054 static_assert(std::is_unsigned<T>::value, "Value is not unsigned!");
55
56 char NumberBuffer[128];
57 std::memset(NumberBuffer, '0', sizeof(NumberBuffer));
58
59 size_t Len = 0;
60 Len = format_to_buffer(N, NumberBuffer);
61
62 if (IsNegative)
63 S << '-';
Zachary Turner11db2642016-11-11 23:57:40 +000064
65 if (Len < MinDigits && Style != IntegerStyle::Number) {
66 for (size_t I = Len; I < MinDigits; ++I)
67 S << '0';
68 }
69
Zachary Turner5b2243e2016-10-29 00:27:22 +000070 if (Style == IntegerStyle::Number) {
71 writeWithCommas(S, ArrayRef<char>(std::end(NumberBuffer) - Len, Len));
72 } else {
73 S.write(std::end(NumberBuffer) - Len, Len);
74 }
75}
76
77template <typename T>
Zachary Turner11db2642016-11-11 23:57:40 +000078static void write_unsigned(raw_ostream &S, T N, size_t MinDigits,
79 IntegerStyle Style, bool IsNegative = false) {
Zachary Turner5b2243e2016-10-29 00:27:22 +000080 // Output using 32-bit div/mod if possible.
81 if (N == static_cast<uint32_t>(N))
Zachary Turner11db2642016-11-11 23:57:40 +000082 write_unsigned_impl(S, static_cast<uint32_t>(N), MinDigits, Style,
83 IsNegative);
Zachary Turner5b2243e2016-10-29 00:27:22 +000084 else
Zachary Turner11db2642016-11-11 23:57:40 +000085 write_unsigned_impl(S, N, MinDigits, Style, IsNegative);
Zachary Turner5b2243e2016-10-29 00:27:22 +000086}
87
88template <typename T>
Zachary Turner11db2642016-11-11 23:57:40 +000089static void write_signed(raw_ostream &S, T N, size_t MinDigits,
90 IntegerStyle Style) {
Zachary Turner5b2243e2016-10-29 00:27:22 +000091 static_assert(std::is_signed<T>::value, "Value is not signed!");
92
93 using UnsignedT = typename std::make_unsigned<T>::type;
94
Zachary Turner99eef2d2016-10-17 20:57:45 +000095 if (N >= 0) {
Zachary Turner11db2642016-11-11 23:57:40 +000096 write_unsigned(S, static_cast<UnsignedT>(N), MinDigits, Style);
Zachary Turner99eef2d2016-10-17 20:57:45 +000097 return;
98 }
99
Zachary Turner5b2243e2016-10-29 00:27:22 +0000100 UnsignedT UN = -(UnsignedT)N;
Zachary Turner11db2642016-11-11 23:57:40 +0000101 write_unsigned(S, UN, MinDigits, Style, true);
Renato Golin9ce50742016-10-18 09:30:18 +0000102}
103
Zachary Turner11db2642016-11-11 23:57:40 +0000104void llvm::write_integer(raw_ostream &S, unsigned int N, size_t MinDigits,
Zachary Turner5b2243e2016-10-29 00:27:22 +0000105 IntegerStyle Style) {
Zachary Turner11db2642016-11-11 23:57:40 +0000106 write_unsigned(S, N, MinDigits, Style);
Zachary Turner5b2243e2016-10-29 00:27:22 +0000107}
108
Zachary Turner11db2642016-11-11 23:57:40 +0000109void llvm::write_integer(raw_ostream &S, int N, size_t MinDigits,
110 IntegerStyle Style) {
111 write_signed(S, N, MinDigits, Style);
112}
113
114void llvm::write_integer(raw_ostream &S, unsigned long N, size_t MinDigits,
115 IntegerStyle Style) {
116 write_unsigned(S, N, MinDigits, Style);
117}
118
119void llvm::write_integer(raw_ostream &S, long N, size_t MinDigits,
120 IntegerStyle Style) {
121 write_signed(S, N, MinDigits, Style);
122}
123
124void llvm::write_integer(raw_ostream &S, unsigned long long N, size_t MinDigits,
125 IntegerStyle Style) {
126 write_unsigned(S, N, MinDigits, Style);
127}
128
129void llvm::write_integer(raw_ostream &S, long long N, size_t MinDigits,
130 IntegerStyle Style) {
131 write_signed(S, N, MinDigits, Style);
Zachary Turner5b2243e2016-10-29 00:27:22 +0000132}
133
134void llvm::write_hex(raw_ostream &S, uint64_t N, HexPrintStyle Style,
135 Optional<size_t> Width) {
136 const size_t kMaxWidth = 128u;
137
138 size_t W = std::min(kMaxWidth, Width.getValueOr(0u));
139
Zachary Turner733be512016-10-11 19:24:45 +0000140 unsigned Nibbles = (64 - countLeadingZeros(N) + 3) / 4;
Zachary Turner5b2243e2016-10-29 00:27:22 +0000141 bool Prefix = (Style == HexPrintStyle::PrefixLower ||
142 Style == HexPrintStyle::PrefixUpper);
143 bool Upper =
144 (Style == HexPrintStyle::Upper || Style == HexPrintStyle::PrefixUpper);
Zachary Turner733be512016-10-11 19:24:45 +0000145 unsigned PrefixChars = Prefix ? 2 : 0;
Zachary Turner5b2243e2016-10-29 00:27:22 +0000146 unsigned NumChars =
147 std::max(static_cast<unsigned>(W), std::max(1u, Nibbles) + PrefixChars);
Zachary Turner733be512016-10-11 19:24:45 +0000148
Zachary Turner5b2243e2016-10-29 00:27:22 +0000149 char NumberBuffer[kMaxWidth];
150 ::memset(NumberBuffer, '0', llvm::array_lengthof(NumberBuffer));
151 if (Prefix)
152 NumberBuffer[1] = 'x';
153 char *EndPtr = NumberBuffer + NumChars;
Zachary Turner733be512016-10-11 19:24:45 +0000154 char *CurPtr = EndPtr;
155 while (N) {
156 unsigned char x = static_cast<unsigned char>(N) % 16;
157 *--CurPtr = hexdigit(x, !Upper);
158 N /= 16;
159 }
160
Zachary Turner5b2243e2016-10-29 00:27:22 +0000161 S.write(NumberBuffer, NumChars);
Zachary Turner733be512016-10-11 19:24:45 +0000162}
163
Zachary Turner5b2243e2016-10-29 00:27:22 +0000164void llvm::write_double(raw_ostream &S, double N, FloatStyle Style,
165 Optional<size_t> Precision) {
166 size_t Prec = Precision.getValueOr(getDefaultPrecision(Style));
167
168 if (std::isnan(N)) {
169 S << "nan";
170 return;
171 } else if (std::isinf(N)) {
172 S << "INF";
173 return;
174 }
175
176 char Letter;
177 if (Style == FloatStyle::Exponent)
178 Letter = 'e';
179 else if (Style == FloatStyle::ExponentUpper)
180 Letter = 'E';
181 else
182 Letter = 'f';
183
Zachary Turner733be512016-10-11 19:24:45 +0000184 SmallString<8> Spec;
185 llvm::raw_svector_ostream Out(Spec);
Zachary Turner5b2243e2016-10-29 00:27:22 +0000186 Out << "%." << Prec << Letter;
Zachary Turner733be512016-10-11 19:24:45 +0000187
Zachary Turner5b2243e2016-10-29 00:27:22 +0000188 if (Style == FloatStyle::Exponent || Style == FloatStyle::ExponentUpper) {
Zachary Turner733be512016-10-11 19:24:45 +0000189#ifdef _WIN32
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)) {
Zachary Turner11db2642016-11-11 23:57:40 +0000196 char NegativeZero[] = "-0.000000e+00";
197 if (Style == FloatStyle::ExponentUpper)
198 NegativeZero[strlen(NegativeZero) - 4] = 'E';
Zachary Turner5b2243e2016-10-29 00:27:22 +0000199 S << NegativeZero;
Zachary Turner733be512016-10-11 19:24:45 +0000200 return;
201 }
202#else
203 int fpcl = _fpclass(N);
204
205 // negative zero
206 if (fpcl == _FPCLASS_NZ) {
Zachary Turner11db2642016-11-11 23:57:40 +0000207 char NegativeZero[] = "-0.000000e+00";
208 if (Style == FloatStyle::ExponentUpper)
209 NegativeZero[strlen(NegativeZero) - 4] = 'E';
Zachary Turner5b2243e2016-10-29 00:27:22 +0000210 S << NegativeZero;
Zachary Turner733be512016-10-11 19:24:45 +0000211 return;
212 }
213#endif
214
Zachary Turner5b2243e2016-10-29 00:27:22 +0000215 char buf[32];
Zachary Turner733be512016-10-11 19:24:45 +0000216 unsigned len;
217 len = format(Spec.c_str(), N).snprint(buf, sizeof(buf));
218 if (len <= sizeof(buf) - 2) {
Zachary Turner5b2243e2016-10-29 00:27:22 +0000219 if (len >= 5 && (buf[len - 5] == 'e' || buf[len - 5] == 'E') &&
220 buf[len - 3] == '0') {
Zachary Turner733be512016-10-11 19:24:45 +0000221 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"
228 buf[len - 3] = c1;
229 buf[len - 2] = c0;
230 buf[--len] = 0;
231 }
232 }
233 }
234 S << buf;
235 return;
236 }
237#endif
238 }
239
Zachary Turner5b2243e2016-10-29 00:27:22 +0000240 if (Style == FloatStyle::Percent)
241 N *= 100.0;
242
243 char Buf[32];
Eric Christopher0192e972017-01-04 19:47:10 +0000244 format(Spec.c_str(), N).snprint(Buf, sizeof(Buf));
Zachary Turner5b2243e2016-10-29 00:27:22 +0000245 S << Buf;
246 if (Style == FloatStyle::Percent)
247 S << '%';
248}
249
Zachary Turner11db2642016-11-11 23:57:40 +0000250bool llvm::isPrefixedHexStyle(HexPrintStyle S) {
251 return (S == HexPrintStyle::PrefixLower || S == HexPrintStyle::PrefixUpper);
252}
253
Zachary Turner5b2243e2016-10-29 00:27:22 +0000254size_t llvm::getDefaultPrecision(FloatStyle Style) {
255 switch (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.
262 }
263 LLVM_BUILTIN_UNREACHABLE;
Zachary Turner733be512016-10-11 19:24:45 +0000264}