LLVM: lib/Support/NativeFormatting.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

15

16#include

17

18#if defined(_WIN32) && !defined(__MINGW32__)

19#include <float.h>

20#endif

21

22using namespace llvm;

23

24template<typename T, std::size_t N>

26 char *EndPtr = std::end(Buffer);

27 char *CurPtr = EndPtr;

28

29 do {

30 *--CurPtr = '0' + char(Value % 10);

33 return EndPtr - CurPtr;

34}

35

38

40 int InitialDigits = ((Buffer.size() - 1) % 3) + 1;

41 ThisGroup = Buffer.take_front(InitialDigits);

43

44 Buffer = Buffer.drop_front(InitialDigits);

46 while (!Buffer.empty()) {

47 S << ',';

51 }

52}

53

54template

57 static_assert(std::is_unsigned_v, "Value is not unsigned!");

58

59 char NumberBuffer[128];

61

62 if (IsNegative)

63 S << '-';

64

66 for (size_t I = Len; I < MinDigits; ++I)

67 S << '0';

68 }

69

72 } else {

73 S.write(std::end(NumberBuffer) - Len, Len);

74 }

75}

76

77template

79 IntegerStyle Style, bool IsNegative = false) {

80

83 IsNegative);

84 else

86}

87

88template

91 static_assert(std::is_signed_v, "Value is not signed!");

92

93 using UnsignedT = std::make_unsigned_t;

94

95 if (N >= 0) {

96 write_unsigned(S, static_cast<UnsignedT>(N), MinDigits, Style);

97 return;

98 }

99

100 UnsignedT UN = -(UnsignedT)N;

102}

103

108

113

118

123

128

133

135 std::optional<size_t> Width) {

136 const size_t kMaxWidth = 128u;

137

138 size_t W = std::min(kMaxWidth, Width.value_or(0u));

139

145 unsigned PrefixChars = Prefix ? 2 : 0;

146 unsigned NumChars =

147 std::max(static_cast<unsigned>(W), std::max(1u, Nibbles) + PrefixChars);

148

149 char NumberBuffer[kMaxWidth];

150 ::memset(NumberBuffer, '0', std::size(NumberBuffer));

151 if (Prefix)

152 NumberBuffer[1] = 'x';

153 char *EndPtr = NumberBuffer + NumChars;

154 char *CurPtr = EndPtr;

155 while (N) {

156 unsigned char x = static_cast<unsigned char>(N) % 16;

158 N /= 16;

159 }

160

161 S.write(NumberBuffer, NumChars);

162}

163

165 std::optional<size_t> Precision) {

167

168 if (std::isnan(N)) {

169 S << "nan";

170 return;

171 } else if (std::isinf(N)) {

172 S << (std::signbit(N) ? "-INF" : "INF");

173 return;

174 }

175

176 char Letter;

178 Letter = 'e';

180 Letter = 'E';

181 else

182 Letter = 'f';

183

186 Out << "%." << Prec << Letter;

187

189#ifdef _WIN32

190

191

192

193#if defined(__MINGW32__)

194

195 if (N == 0.0 && std::signbit(N)) {

200 return;

201 }

202#else

203 int fpcl = _fpclass(N);

204

205

206 if (fpcl == _FPCLASS_NZ) {

211 return;

212 }

213#endif

214

215 char buf[32];

216 unsigned len;

217 len = format(Spec.c_str(), N).snprint(buf, sizeof(buf));

218 if (len <= sizeof(buf) - 2) {

219 if (len >= 5 && (buf[len - 5] == 'e' || buf[len - 5] == 'E') &&

220 buf[len - 3] == '0') {

221 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

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

241 N *= 100.0;

242

243 char Buf[32];

244 format(Spec.c_str(), N).snprint(Buf, sizeof(Buf));

245 S << Buf;

247 S << '%';

248}

249

253

255 switch (Style) {

258 return 6;

261 return 2;

262 }

264}

assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")

static void write_unsigned(raw_ostream &S, T N, size_t MinDigits, IntegerStyle Style, bool IsNegative=false)

Definition NativeFormatting.cpp:78

static int format_to_buffer(T Value, char(&Buffer)[N])

Definition NativeFormatting.cpp:25

static void write_signed(raw_ostream &S, T N, size_t MinDigits, IntegerStyle Style)

Definition NativeFormatting.cpp:89

static void write_unsigned_impl(raw_ostream &S, T N, size_t MinDigits, IntegerStyle Style, bool IsNegative)

Definition NativeFormatting.cpp:55

static void writeWithCommas(raw_ostream &S, ArrayRef< char > Buffer)

Definition NativeFormatting.cpp:36

This file defines the SmallString class.

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.

LLVM_ABI void write_integer(raw_ostream &S, unsigned int N, size_t MinDigits, IntegerStyle Style)

Definition NativeFormatting.cpp:104

LLVM_ABI size_t getDefaultPrecision(FloatStyle Style)

Definition NativeFormatting.cpp:254

char hexdigit(unsigned X, bool LowerCase=false)

hexdigit - Return the hexadecimal character for the given number X (which should be less than 16).

format_object< Ts... > format(const char *Fmt, const Ts &... Vals)

These are helper functions used to produce formatted output.

LLVM_ABI void write_hex(raw_ostream &S, uint64_t N, HexPrintStyle Style, std::optional< size_t > Width=std::nullopt)

Definition NativeFormatting.cpp:134

LLVM_ABI void write_double(raw_ostream &S, double D, FloatStyle Style, std::optional< size_t > Precision=std::nullopt)

Definition NativeFormatting.cpp:164

LLVM_ABI bool isPrefixedHexStyle(HexPrintStyle S)

Definition NativeFormatting.cpp:250