1//===- NativeFormatting.cpp - Low level formatting helpers -------*- C++-*-===// 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 7//===----------------------------------------------------------------------===// 18#if defined(_WIN32) && !defined(__MINGW32__) 19#include <float.h>// For _fpclass in llvm::write_double. 24template<
typename T, std::
size_t N>
26char *EndPtr = std::end(Buffer);
33return EndPtr - CurPtr;
40int InitialDigits = ((Buffer.
size() - 1) % 3) + 1;
46while (!Buffer.
empty()) {
57static_assert(std::is_unsigned_v<T>,
"Value is not unsigned!");
59char NumberBuffer[128];
65if (Len < MinDigits && Style != IntegerStyle::Number) {
66for (
size_tI = Len;
I < MinDigits; ++
I)
70if (Style == IntegerStyle::Number) {
73 S.
write(std::end(NumberBuffer) - Len, Len);
80// Output using 32-bit div/mod if possible. 91static_assert(std::is_signed_v<T>,
"Value is not signed!");
93usingUnsignedT = std::make_unsigned_t<T>;
100 UnsignedT UN = -(UnsignedT)
N;
135 std::optional<size_t> Width) {
136constsize_t kMaxWidth = 128u;
138size_t W = std::min(kMaxWidth, Width.value_or(0u));
141bool Prefix = (Style == HexPrintStyle::PrefixLower ||
142 Style == HexPrintStyle::PrefixUpper);
144 (Style == HexPrintStyle::Upper || Style == HexPrintStyle::PrefixUpper);
145unsigned PrefixChars = Prefix ? 2 : 0;
147 std::max(
static_cast<unsigned>(W), std::max(1u, Nibbles) + PrefixChars);
149char NumberBuffer[kMaxWidth];
150 ::memset(NumberBuffer,
'0', std::size(NumberBuffer));
152 NumberBuffer[1] =
'x';
153char *EndPtr = NumberBuffer + NumChars;
154char *CurPtr = EndPtr;
156unsignedchar x =
static_cast<unsignedchar>(
N) % 16;
157 *--CurPtr = hexdigit(x, !
Upper);
161 S.
write(NumberBuffer, NumChars);
165 std::optional<size_t> Precision) {
171 }
elseif (std::isinf(
N)) {
172 S << (std::signbit(
N) ?
"-INF" :
"INF");
177if (Style == FloatStyle::Exponent)
179elseif (Style == FloatStyle::ExponentUpper)
186 Out <<
"%." << Prec << Letter;
188if (Style == FloatStyle::Exponent || Style == FloatStyle::ExponentUpper) {
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)) {
197if (Style == FloatStyle::ExponentUpper)
203int fpcl = _fpclass(
N);
206if (fpcl == _FPCLASS_NZ) {
208if (Style == FloatStyle::ExponentUpper)
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" 240if (Style == FloatStyle::Percent)
246if (Style == FloatStyle::Percent)
251return (S == HexPrintStyle::PrefixLower || S == HexPrintStyle::PrefixUpper);
256case FloatStyle::Exponent:
257case FloatStyle::ExponentUpper:
258return 6;
// Number of decimal places. 259case FloatStyle::Fixed:
260case FloatStyle::Percent:
261return 2;
// Number of decimal places. static void write_unsigned(raw_ostream &S, T N, size_t MinDigits, IntegerStyle Style, bool IsNegative=false)
static int format_to_buffer(T Value, char(&Buffer)[N])
static void write_signed(raw_ostream &S, T N, size_t MinDigits, IntegerStyle Style)
static void write_unsigned_impl(raw_ostream &S, T N, size_t MinDigits, IntegerStyle Style, bool IsNegative)
static void writeWithCommas(raw_ostream &S, ArrayRef< char > Buffer)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallString class.
This file contains some functions that are useful when dealing with strings.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
ArrayRef< T > take_front(size_t N=1) const
Return a copy of *this with only the first N elements.
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
size_t size() const
size - Get the array size.
bool empty() const
empty - Check if the array is empty.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
LLVM Value Representation.
This class implements an extremely fast bulk output stream that can only output to a stream.
raw_ostream & write(unsigned char C)
A raw_ostream that writes to an SmallVector or SmallString.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
void write_integer(raw_ostream &S, unsigned int N, size_t MinDigits, IntegerStyle Style)
size_t getDefaultPrecision(FloatStyle Style)
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
void write_hex(raw_ostream &S, uint64_t N, HexPrintStyle Style, std::optional< size_t > Width=std::nullopt)
void write_double(raw_ostream &S, double D, FloatStyle Style, std::optional< size_t > Precision=std::nullopt)
bool isPrefixedHexStyle(HexPrintStyle S)