Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.0.0git
NativeFormatting.cpp
Go to the documentation of this file.
1//===- NativeFormatting.cpp - Low level formatting helpers -------*- C++-*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
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"
15
16#include <cmath>
17
18#if defined(_WIN32) && !defined(__MINGW32__)
19#include <float.h>// For _fpclass in llvm::write_double.
20#endif
21
22using namespacellvm;
23
24template<typename T, std::size_t N>
25staticintformat_to_buffer(TValue,char (&Buffer)[N]) {
26char *EndPtr = std::end(Buffer);
27char *CurPtr = EndPtr;
28
29do {
30 *--CurPtr ='0' +char(Value % 10);
31Value /= 10;
32 }while (Value);
33return EndPtr - CurPtr;
34}
35
36staticvoidwriteWithCommas(raw_ostream &S,ArrayRef<char> Buffer) {
37assert(!Buffer.empty());
38
39ArrayRef<char> ThisGroup;
40int InitialDigits = ((Buffer.size() - 1) % 3) + 1;
41 ThisGroup = Buffer.take_front(InitialDigits);
42 S.write(ThisGroup.data(), ThisGroup.size());
43
44 Buffer = Buffer.drop_front(InitialDigits);
45assert(Buffer.size() % 3 == 0);
46while (!Buffer.empty()) {
47 S <<',';
48 ThisGroup = Buffer.take_front(3);
49 S.write(ThisGroup.data(), 3);
50 Buffer = Buffer.drop_front(3);
51 }
52}
53
54template <typename T>
55staticvoidwrite_unsigned_impl(raw_ostream &S,TN,size_t MinDigits,
56IntegerStyle Style,bool IsNegative) {
57static_assert(std::is_unsigned_v<T>,"Value is not unsigned!");
58
59char NumberBuffer[128];
60size_t Len =format_to_buffer(N, NumberBuffer);
61
62if (IsNegative)
63 S <<'-';
64
65if (Len < MinDigits && Style != IntegerStyle::Number) {
66for (size_tI = Len;I < MinDigits; ++I)
67 S <<'0';
68 }
69
70if (Style == IntegerStyle::Number) {
71writeWithCommas(S,ArrayRef<char>(std::end(NumberBuffer) - Len, Len));
72 }else {
73 S.write(std::end(NumberBuffer) - Len, Len);
74 }
75}
76
77template <typename T>
78staticvoidwrite_unsigned(raw_ostream &S,TN,size_t MinDigits,
79IntegerStyle Style,bool IsNegative =false) {
80// Output using 32-bit div/mod if possible.
81if (N ==static_cast<uint32_t>(N))
82write_unsigned_impl(S,static_cast<uint32_t>(N), MinDigits, Style,
83 IsNegative);
84else
85write_unsigned_impl(S,N, MinDigits, Style, IsNegative);
86}
87
88template <typename T>
89staticvoidwrite_signed(raw_ostream &S,TN,size_t MinDigits,
90IntegerStyle Style) {
91static_assert(std::is_signed_v<T>,"Value is not signed!");
92
93usingUnsignedT = std::make_unsigned_t<T>;
94
95if (N >= 0) {
96write_unsigned(S,static_cast<UnsignedT>(N), MinDigits, Style);
97return;
98 }
99
100 UnsignedT UN = -(UnsignedT)N;
101write_unsigned(S, UN, MinDigits, Style,true);
102}
103
104voidllvm::write_integer(raw_ostream &S,unsignedintN,size_t MinDigits,
105IntegerStyle Style) {
106write_unsigned(S,N, MinDigits, Style);
107}
108
109voidllvm::write_integer(raw_ostream &S,intN,size_t MinDigits,
110IntegerStyle Style) {
111write_signed(S,N, MinDigits, Style);
112}
113
114voidllvm::write_integer(raw_ostream &S,unsignedlongN,size_t MinDigits,
115IntegerStyle Style) {
116write_unsigned(S,N, MinDigits, Style);
117}
118
119voidllvm::write_integer(raw_ostream &S,longN,size_t MinDigits,
120IntegerStyle Style) {
121write_signed(S,N, MinDigits, Style);
122}
123
124voidllvm::write_integer(raw_ostream &S,unsignedlonglongN,size_t MinDigits,
125IntegerStyle Style) {
126write_unsigned(S,N, MinDigits, Style);
127}
128
129voidllvm::write_integer(raw_ostream &S,longlongN,size_t MinDigits,
130IntegerStyle Style) {
131write_signed(S,N, MinDigits, Style);
132}
133
134voidllvm::write_hex(raw_ostream &S,uint64_tN,HexPrintStyle Style,
135 std::optional<size_t> Width) {
136constsize_t kMaxWidth = 128u;
137
138size_t W = std::min(kMaxWidth, Width.value_or(0u));
139
140unsigned Nibbles = (llvm::bit_width(N) + 3) / 4;
141bool Prefix = (Style == HexPrintStyle::PrefixLower ||
142 Style == HexPrintStyle::PrefixUpper);
143boolUpper =
144 (Style == HexPrintStyle::Upper || Style == HexPrintStyle::PrefixUpper);
145unsigned PrefixChars = Prefix ? 2 : 0;
146unsigned NumChars =
147 std::max(static_cast<unsigned>(W), std::max(1u, Nibbles) + PrefixChars);
148
149char NumberBuffer[kMaxWidth];
150 ::memset(NumberBuffer,'0', std::size(NumberBuffer));
151if (Prefix)
152 NumberBuffer[1] ='x';
153char *EndPtr = NumberBuffer + NumChars;
154char *CurPtr = EndPtr;
155while (N) {
156unsignedchar x =static_cast<unsignedchar>(N) % 16;
157 *--CurPtr = hexdigit(x, !Upper);
158N /= 16;
159 }
160
161 S.write(NumberBuffer, NumChars);
162}
163
164voidllvm::write_double(raw_ostream &S,doubleN,FloatStyle Style,
165 std::optional<size_t> Precision) {
166size_t Prec = Precision.value_or(getDefaultPrecision(Style));
167
168if (std::isnan(N)) {
169 S <<"nan";
170return;
171 }elseif (std::isinf(N)) {
172 S << (std::signbit(N) ?"-INF" :"INF");
173return;
174 }
175
176char Letter;
177if (Style == FloatStyle::Exponent)
178 Letter ='e';
179elseif (Style == FloatStyle::ExponentUpper)
180 Letter ='E';
181else
182 Letter ='f';
183
184SmallString<8>Spec;
185llvm::raw_svector_ostream Out(Spec);
186 Out <<"%." << Prec << Letter;
187
188if (Style == FloatStyle::Exponent || Style == FloatStyle::ExponentUpper) {
189#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.
195if (N == 0.0 && std::signbit(N)) {
196charNegativeZero[] ="-0.000000e+00";
197if (Style == FloatStyle::ExponentUpper)
198NegativeZero[strlen(NegativeZero) - 4] ='E';
199 S <<NegativeZero;
200return;
201 }
202#else
203int fpcl = _fpclass(N);
204
205// negative zero
206if (fpcl == _FPCLASS_NZ) {
207charNegativeZero[] ="-0.000000e+00";
208if (Style == FloatStyle::ExponentUpper)
209NegativeZero[strlen(NegativeZero) - 4] ='E';
210 S <<NegativeZero;
211return;
212 }
213#endif
214
215char buf[32];
216unsigned len;
217 len =format(Spec.c_str(),N).snprint(buf,sizeof(buf));
218if (len <=sizeof(buf) - 2) {
219if (len >= 5 && (buf[len - 5] =='e' || buf[len - 5] =='E') &&
220 buf[len - 3] =='0') {
221int cs = buf[len - 4];
222if (cs =='+' || cs =='-') {
223int c1 = buf[len - 2];
224int c0 = buf[len - 1];
225if (isdigit(static_cast<unsignedchar>(c1)) &&
226 isdigit(static_cast<unsignedchar>(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;
235return;
236 }
237#endif
238 }
239
240if (Style == FloatStyle::Percent)
241N *= 100.0;
242
243char Buf[32];
244format(Spec.c_str(),N).snprint(Buf,sizeof(Buf));
245 S << Buf;
246if (Style == FloatStyle::Percent)
247 S <<'%';
248}
249
250boolllvm::isPrefixedHexStyle(HexPrintStyle S) {
251return (S == HexPrintStyle::PrefixLower || S == HexPrintStyle::PrefixUpper);
252}
253
254size_tllvm::getDefaultPrecision(FloatStyle Style) {
255switch (Style) {
256case FloatStyle::Exponent:
257case FloatStyle::ExponentUpper:
258return 6;// Number of decimal places.
259case FloatStyle::Fixed:
260case FloatStyle::Percent:
261return 2;// Number of decimal places.
262 }
263llvm_unreachable("Unknown FloatStyle enum");
264}
ArrayRef.h
Format.h
I
#define I(x, y, z)
Definition:MD5.cpp:58
write_unsigned
static void write_unsigned(raw_ostream &S, T N, size_t MinDigits, IntegerStyle Style, bool IsNegative=false)
Definition:NativeFormatting.cpp:78
format_to_buffer
static int format_to_buffer(T Value, char(&Buffer)[N])
Definition:NativeFormatting.cpp:25
write_signed
static void write_signed(raw_ostream &S, T N, size_t MinDigits, IntegerStyle Style)
Definition:NativeFormatting.cpp:89
write_unsigned_impl
static void write_unsigned_impl(raw_ostream &S, T N, size_t MinDigits, IntegerStyle Style, bool IsNegative)
Definition:NativeFormatting.cpp:55
writeWithCommas
static void writeWithCommas(raw_ostream &S, ArrayRef< char > Buffer)
Definition:NativeFormatting.cpp:36
NativeFormatting.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
SmallString.h
This file defines the SmallString class.
StringExtras.h
This file contains some functions that are useful when dealing with strings.
T
char
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition:ArrayRef.h:41
llvm::ArrayRef::take_front
ArrayRef< T > take_front(size_t N=1) const
Return a copy of *this with only the first N elements.
Definition:ArrayRef.h:231
llvm::ArrayRef::drop_front
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition:ArrayRef.h:207
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition:ArrayRef.h:168
llvm::ArrayRef::empty
bool empty() const
empty - Check if the array is empty.
Definition:ArrayRef.h:163
llvm::ArrayRef::data
const T * data() const
Definition:ArrayRef.h:165
llvm::SmallString
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition:SmallString.h:26
llvm::Value
LLVM Value Representation.
Definition:Value.h:74
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition:raw_ostream.h:52
llvm::raw_ostream::write
raw_ostream & write(unsigned char C)
Definition:raw_ostream.cpp:225
llvm::raw_svector_ostream
A raw_ostream that writes to an SmallVector or SmallString.
Definition:raw_ostream.h:691
uint32_t
uint64_t
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition:ErrorHandling.h:143
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::bit_width
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition:bit.h:317
llvm::FloatStyle
FloatStyle
Definition:NativeFormatting.h:17
llvm::write_integer
void write_integer(raw_ostream &S, unsigned int N, size_t MinDigits, IntegerStyle Style)
Definition:NativeFormatting.cpp:104
llvm::HexPrintStyle
HexPrintStyle
Definition:NativeFormatting.h:22
llvm::HexPrintStyle::Upper
@ Upper
llvm::getDefaultPrecision
size_t getDefaultPrecision(FloatStyle Style)
Definition:NativeFormatting.cpp:254
llvm::format
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition:Format.h:125
llvm::write_hex
void write_hex(raw_ostream &S, uint64_t N, HexPrintStyle Style, std::optional< size_t > Width=std::nullopt)
Definition:NativeFormatting.cpp:134
llvm::write_double
void write_double(raw_ostream &S, double D, FloatStyle Style, std::optional< size_t > Precision=std::nullopt)
Definition:NativeFormatting.cpp:164
llvm::isPrefixedHexStyle
bool isPrefixedHexStyle(HexPrintStyle S)
Definition:NativeFormatting.cpp:250
llvm::fltNanEncoding::NegativeZero
@ NegativeZero
llvm::IntegerStyle
IntegerStyle
Definition:NativeFormatting.h:18
raw_ostream.h
N
#define N
llvm::Spec
Definition:FunctionSpecialization.h:128

Generated on Thu Jul 17 2025 12:40:02 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp