blob: 3731e0c5635991d2ac373118f4d56a8bec3b1d49 [file] [log] [blame]
Zachary Turner733be512016-10-11 19:24:45 +00001//===- NativeFormatting.cpp - Low level formatting helpers -------*- C++-*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Zachary Turner733be512016-10-11 19:24:45 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/Support/NativeFormatting.h"
10
Zachary Turner5b2243e2016-10-29 00:27:22 +000011#include "llvm/ADT/ArrayRef.h"
Zachary Turner733be512016-10-11 19:24:45 +000012#include "llvm/ADT/SmallString.h"
13#include "llvm/ADT/StringExtras.h"
14#include "llvm/Support/Format.h"
15
Peter Collingbourne719c1f72018-01-18 20:49:33 +000016#include <float.h>
17
Zachary Turner733be512016-10-11 19:24:45 +000018using namespace llvm;
19
20template<typename T, std::size_t N>
21static int format_to_buffer(T Value, char (&Buffer)[N]) {
22 char *EndPtr = std::end(Buffer);
23 char *CurPtr = EndPtr;
24
Zachary Turner5b2243e2016-10-29 00:27:22 +000025 do {
Zachary Turner733be512016-10-11 19:24:45 +000026 *--CurPtr = '0' + char(Value % 10);
27 Value /= 10;
Zachary Turner5b2243e2016-10-29 00:27:22 +000028 } while (Value);
Zachary Turner733be512016-10-11 19:24:45 +000029 return EndPtr - CurPtr;
30}
31
Zachary Turner5b2243e2016-10-29 00:27:22 +000032static void writeWithCommas(raw_ostream &S, ArrayRef<char> Buffer) {
33 assert(!Buffer.empty());
Zachary Turner733be512016-10-11 19:24:45 +000034
Zachary Turner5b2243e2016-10-29 00:27:22 +000035 ArrayRef<char> ThisGroup;
36 int InitialDigits = ((Buffer.size() - 1) % 3) + 1;
37 ThisGroup = Buffer.take_front(InitialDigits);
38 S.write(ThisGroup.data(), ThisGroup.size());
39
40 Buffer = Buffer.drop_front(InitialDigits);
41 assert(Buffer.size() % 3 == 0);
42 while (!Buffer.empty()) {
43 S << ',';
44 ThisGroup = Buffer.take_front(3);
45 S.write(ThisGroup.data(), 3);
46 Buffer = Buffer.drop_front(3);
47 }
Zachary Turner99eef2d2016-10-17 20:57:45 +000048}
49
Zachary Turner5b2243e2016-10-29 00:27:22 +000050template <typename T>
Zachary Turner11db2642016-11-11 23:57:40 +000051static void write_unsigned_impl(raw_ostream &S, T N, size_t MinDigits,
52 IntegerStyle Style, bool IsNegative) {
Zachary Turner5b2243e2016-10-29 00:27:22 +000053 static_assert(std::is_unsigned<T>::value, "Value is not unsigned!");
54
55 char NumberBuffer[128];
56 std::memset(NumberBuffer, '0', sizeof(NumberBuffer));
57
58 size_t Len = 0;
59 Len = format_to_buffer(N, NumberBuffer);
60
61 if (IsNegative)
62 S << '-';
Zachary Turner11db2642016-11-11 23:57:40 +000063
64 if (Len < MinDigits && Style != IntegerStyle::Number) {
65 for (size_t I = Len; I < MinDigits; ++I)
66 S << '0';
67 }
68
Zachary Turner5b2243e2016-10-29 00:27:22 +000069 if (Style == IntegerStyle::Number) {
70 writeWithCommas(S, ArrayRef<char>(std::end(NumberBuffer) - Len, Len));
71 } else {
72 S.write(std::end(NumberBuffer) - Len, Len);
73 }
74}
75
76template <typename T>
Zachary Turner11db2642016-11-11 23:57:40 +000077static void write_unsigned(raw_ostream &S, T N, size_t MinDigits,
78 IntegerStyle Style, bool IsNegative = false) {
Zachary Turner5b2243e2016-10-29 00:27:22 +000079 // Output using 32-bit div/mod if possible.
80 if (N == static_cast<uint32_t>(N))
Zachary Turner11db2642016-11-11 23:57:40 +000081 write_unsigned_impl(S, static_cast<uint32_t>(N), MinDigits, Style,
82 IsNegative);
Zachary Turner5b2243e2016-10-29 00:27:22 +000083 else
Zachary Turner11db2642016-11-11 23:57:40 +000084 write_unsigned_impl(S, N, MinDigits, Style, IsNegative);
Zachary Turner5b2243e2016-10-29 00:27:22 +000085}
86
87template <typename T>
Zachary Turner11db2642016-11-11 23:57:40 +000088static void write_signed(raw_ostream &S, T N, size_t MinDigits,
89 IntegerStyle Style) {
Zachary Turner5b2243e2016-10-29 00:27:22 +000090 static_assert(std::is_signed<T>::value, "Value is not signed!");
91
92 using UnsignedT = typename std::make_unsigned<T>::type;
93
Zachary Turner99eef2d2016-10-17 20:57:45 +000094 if (N >= 0) {
Zachary Turner11db2642016-11-11 23:57:40 +000095 write_unsigned(S, static_cast<UnsignedT>(N), MinDigits, Style);
Zachary Turner99eef2d2016-10-17 20:57:45 +000096 return;
97 }
98
Zachary Turner5b2243e2016-10-29 00:27:22 +000099 UnsignedT UN = -(UnsignedT)N;
Zachary Turner11db2642016-11-11 23:57:40 +0000100 write_unsigned(S, UN, MinDigits, Style, true);
Renato Golin9ce50742016-10-18 09:30:18 +0000101}
102
Zachary Turner11db2642016-11-11 23:57:40 +0000103void llvm::write_integer(raw_ostream &S, unsigned int N, size_t MinDigits,
Zachary Turner5b2243e2016-10-29 00:27:22 +0000104 IntegerStyle Style) {
Zachary Turner11db2642016-11-11 23:57:40 +0000105 write_unsigned(S, N, MinDigits, Style);
Zachary Turner5b2243e2016-10-29 00:27:22 +0000106}
107
Zachary Turner11db2642016-11-11 23:57:40 +0000108void llvm::write_integer(raw_ostream &S, int N, size_t MinDigits,
109 IntegerStyle Style) {
110 write_signed(S, N, MinDigits, Style);
111}
112
113void llvm::write_integer(raw_ostream &S, unsigned long N, size_t MinDigits,
114 IntegerStyle Style) {
115 write_unsigned(S, N, MinDigits, Style);
116}
117
118void llvm::write_integer(raw_ostream &S, long N, size_t MinDigits,
119 IntegerStyle Style) {
120 write_signed(S, N, MinDigits, Style);
121}
122
123void llvm::write_integer(raw_ostream &S, unsigned long long N, size_t MinDigits,
124 IntegerStyle Style) {
125 write_unsigned(S, N, MinDigits, Style);
126}
127
128void llvm::write_integer(raw_ostream &S, long long N, size_t MinDigits,
129 IntegerStyle Style) {
130 write_signed(S, N, MinDigits, Style);
Zachary Turner5b2243e2016-10-29 00:27:22 +0000131}
132
133void llvm::write_hex(raw_ostream &S, uint64_t N, HexPrintStyle Style,
134 Optional<size_t> Width) {
135 const size_t kMaxWidth = 128u;
136
137 size_t W = std::min(kMaxWidth, Width.getValueOr(0u));
138
Zachary Turner733be512016-10-11 19:24:45 +0000139 unsigned Nibbles = (64 - countLeadingZeros(N) + 3) / 4;
Zachary Turner5b2243e2016-10-29 00:27:22 +0000140 bool Prefix = (Style == HexPrintStyle::PrefixLower ||
141 Style == HexPrintStyle::PrefixUpper);
142 bool Upper =
143 (Style == HexPrintStyle::Upper || Style == HexPrintStyle::PrefixUpper);
Zachary Turner733be512016-10-11 19:24:45 +0000144 unsigned PrefixChars = Prefix ? 2 : 0;
Zachary Turner5b2243e2016-10-29 00:27:22 +0000145 unsigned NumChars =
146 std::max(static_cast<unsigned>(W), std::max(1u, Nibbles) + PrefixChars);
Zachary Turner733be512016-10-11 19:24:45 +0000147
Zachary Turner5b2243e2016-10-29 00:27:22 +0000148 char NumberBuffer[kMaxWidth];
149 ::memset(NumberBuffer, '0', llvm::array_lengthof(NumberBuffer));
150 if (Prefix)
151 NumberBuffer[1] = 'x';
152 char *EndPtr = NumberBuffer + NumChars;
Zachary Turner733be512016-10-11 19:24:45 +0000153 char *CurPtr = EndPtr;
154 while (N) {
155 unsigned char x = static_cast<unsigned char>(N) % 16;
156 *--CurPtr = hexdigit(x, !Upper);
157 N /= 16;
158 }
159
Zachary Turner5b2243e2016-10-29 00:27:22 +0000160 S.write(NumberBuffer, NumChars);
Zachary Turner733be512016-10-11 19:24:45 +0000161}
162
Zachary Turner5b2243e2016-10-29 00:27:22 +0000163void llvm::write_double(raw_ostream &S, double N, FloatStyle Style,
164 Optional<size_t> Precision) {
165 size_t Prec = Precision.getValueOr(getDefaultPrecision(Style));
166
167 if (std::isnan(N)) {
168 S << "nan";
169 return;
170 } else if (std::isinf(N)) {
171 S << "INF";
172 return;
173 }
174
175 char Letter;
176 if (Style == FloatStyle::Exponent)
177 Letter = 'e';
178 else if (Style == FloatStyle::ExponentUpper)
179 Letter = 'E';
180 else
181 Letter = 'f';
182
Zachary Turner733be512016-10-11 19:24:45 +0000183 SmallString<8> Spec;
184 llvm::raw_svector_ostream Out(Spec);
Zachary Turner5b2243e2016-10-29 00:27:22 +0000185 Out << "%." << Prec << Letter;
Zachary Turner733be512016-10-11 19:24:45 +0000186
Zachary Turner5b2243e2016-10-29 00:27:22 +0000187 if (Style == FloatStyle::Exponent || Style == FloatStyle::ExponentUpper) {
Zachary Turner733be512016-10-11 19:24:45 +0000188#ifdef _WIN32
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)) {
Zachary Turner11db2642016-11-11 23:57:40 +0000195 char NegativeZero[] = "-0.000000e+00";
196 if (Style == FloatStyle::ExponentUpper)
197 NegativeZero[strlen(NegativeZero) - 4] = 'E';
Zachary Turner5b2243e2016-10-29 00:27:22 +0000198 S << NegativeZero;
Zachary Turner733be512016-10-11 19:24:45 +0000199 return;
200 }
201#else
202 int fpcl = _fpclass(N);
203
204 // negative zero
205 if (fpcl == _FPCLASS_NZ) {
Zachary Turner11db2642016-11-11 23:57:40 +0000206 char NegativeZero[] = "-0.000000e+00";
207 if (Style == FloatStyle::ExponentUpper)
208 NegativeZero[strlen(NegativeZero) - 4] = 'E';
Zachary Turner5b2243e2016-10-29 00:27:22 +0000209 S << NegativeZero;
Zachary Turner733be512016-10-11 19:24:45 +0000210 return;
211 }
212#endif
213
Zachary Turner5b2243e2016-10-29 00:27:22 +0000214 char buf[32];
Zachary Turner733be512016-10-11 19:24:45 +0000215 unsigned len;
216 len = format(Spec.c_str(), N).snprint(buf, sizeof(buf));
217 if (len <= sizeof(buf) - 2) {
Zachary Turner5b2243e2016-10-29 00:27:22 +0000218 if (len >= 5 && (buf[len - 5] == 'e' || buf[len - 5] == 'E') &&
219 buf[len - 3] == '0') {
Zachary Turner733be512016-10-11 19:24:45 +0000220 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"
227 buf[len - 3] = c1;
228 buf[len - 2] = c0;
229 buf[--len] = 0;
230 }
231 }
232 }
233 S << buf;
234 return;
235 }
236#endif
237 }
238
Zachary Turner5b2243e2016-10-29 00:27:22 +0000239 if (Style == FloatStyle::Percent)
240 N *= 100.0;
241
242 char Buf[32];
Eric Christopher0192e972017-01-04 19:47:10 +0000243 format(Spec.c_str(), N).snprint(Buf, sizeof(Buf));
Zachary Turner5b2243e2016-10-29 00:27:22 +0000244 S << Buf;
245 if (Style == FloatStyle::Percent)
246 S << '%';
247}
248
Zachary Turner11db2642016-11-11 23:57:40 +0000249bool llvm::isPrefixedHexStyle(HexPrintStyle S) {
250 return (S == HexPrintStyle::PrefixLower || S == HexPrintStyle::PrefixUpper);
251}
252
Zachary Turner5b2243e2016-10-29 00:27:22 +0000253size_t llvm::getDefaultPrecision(FloatStyle Style) {
254 switch (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.
261 }
262 LLVM_BUILTIN_UNREACHABLE;
Zachary Turner733be512016-10-11 19:24:45 +0000263}