1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
//===--- Floating.h - Types for the constexpr VM ----------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Defines the VM types and helpers operating on types.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_AST_INTERP_FLOATING_H
#define LLVM_CLANG_AST_INTERP_FLOATING_H
#include "Primitives.h"
#include "clang/AST/APValue.h"
#include "llvm/ADT/APFloat.h"
// XXX This is just a debugging help. Setting this to 1 will heap-allocate ALL
// floating values.
#define ALLOCATE_ALL 0
namespace clang {
namespace interp {
using APFloat = llvm::APFloat;
using APSInt = llvm::APSInt;
using APInt = llvm::APInt;
/// If a Floating is constructed from Memory, it DOES NOT OWN THAT MEMORY.
/// It will NOT copy the memory (unless, of course, copy() is called) and it
/// won't alllocate anything. The allocation should happen via InterpState or
/// Program.
class Floating final {
private:
union {
uint64_t Val = 0;
uint64_t *Memory;
};
llvm::APFloatBase::Semantics Semantics;
APFloat getValue() const {
unsigned BitWidth = bitWidth();
if (singleWord())
return APFloat(getSemantics(), APInt(BitWidth, Val));
unsigned NumWords = numWords();
return APFloat(getSemantics(), APInt(BitWidth, NumWords, Memory));
}
public:
Floating() = default;
Floating(llvm::APFloatBase::Semantics Semantics)
: Val(0), Semantics(Semantics) {}
Floating(const APFloat &F) {
Semantics = llvm::APFloatBase::SemanticsToEnum(F.getSemantics());
this->copy(F);
}
Floating(uint64_t *Memory, llvm::APFloatBase::Semantics Semantics)
: Memory(Memory), Semantics(Semantics) {}
APFloat getAPFloat() const { return getValue(); }
bool operator<(Floating RHS) const { return getValue() < RHS.getValue(); }
bool operator>(Floating RHS) const { return getValue() > RHS.getValue(); }
bool operator<=(Floating RHS) const { return getValue() <= RHS.getValue(); }
bool operator>=(Floating RHS) const { return getValue() >= RHS.getValue(); }
APFloat::opStatus convertToInteger(APSInt &Result) const {
bool IsExact;
return getValue().convertToInteger(Result, llvm::APFloat::rmTowardZero,
&IsExact);
}
void toSemantics(const llvm::fltSemantics *Sem, llvm::RoundingMode RM,
Floating *Result) const {
APFloat Copy = getValue();
bool LosesInfo;
Copy.convert(*Sem, RM, &LosesInfo);
(void)LosesInfo;
Result->copy(Copy);
}
APSInt toAPSInt(unsigned NumBits = 0) const {
return APSInt(getValue().bitcastToAPInt());
}
APValue toAPValue(const ASTContext &) const { return APValue(getValue()); }
void print(llvm::raw_ostream &OS) const {
// Can't use APFloat::print() since it appends a newline.
SmallVector<char, 16> Buffer;
getValue().toString(Buffer);
OS << Buffer;
}
std::string toDiagnosticString(const ASTContext &Ctx) const {
std::string NameStr;
llvm::raw_string_ostream OS(NameStr);
print(OS);
return NameStr;
}
unsigned bitWidth() const {
return llvm::APFloatBase::semanticsSizeInBits(getSemantics());
}
unsigned numWords() const { return llvm::APInt::getNumWords(bitWidth()); }
bool singleWord() const {
#if ALLOCATE_ALL
return false;
#endif
return numWords() == 1;
}
static bool singleWord(const llvm::fltSemantics &Sem) {
#if ALLOCATE_ALL
return false;
#endif
return APInt::getNumWords(llvm::APFloatBase::getSizeInBits(Sem)) == 1;
}
const llvm::fltSemantics &getSemantics() const {
return llvm::APFloatBase::EnumToSemantics(Semantics);
}
void copy(const APFloat &F) {
if (singleWord()) {
Val = F.bitcastToAPInt().getZExtValue();
} else {
assert(Memory);
std::memcpy(Memory, F.bitcastToAPInt().getRawData(),
numWords() * sizeof(uint64_t));
}
}
void take(uint64_t *NewMemory) {
if (singleWord())
return;
if (Memory)
std::memcpy(NewMemory, Memory, numWords() * sizeof(uint64_t));
Memory = NewMemory;
}
bool isSigned() const { return true; }
bool isNegative() const { return getValue().isNegative(); }
bool isZero() const { return getValue().isZero(); }
bool isNonZero() const { return getValue().isNonZero(); }
bool isMin() const { return getValue().isSmallest(); }
bool isMinusOne() const { return getValue().isExactlyValue(-1.0); }
bool isNan() const { return getValue().isNaN(); }
bool isSignaling() const { return getValue().isSignaling(); }
bool isInf() const { return getValue().isInfinity(); }
bool isFinite() const { return getValue().isFinite(); }
bool isNormal() const { return getValue().isNormal(); }
bool isDenormal() const { return getValue().isDenormal(); }
llvm::FPClassTest classify() const { return getValue().classify(); }
APFloat::fltCategory getCategory() const { return getValue().getCategory(); }
ComparisonCategoryResult compare(const Floating &RHS) const {
llvm::APFloatBase::cmpResult CmpRes = getValue().compare(RHS.getValue());
switch (CmpRes) {
case llvm::APFloatBase::cmpLessThan:
return ComparisonCategoryResult::Less;
case llvm::APFloatBase::cmpEqual:
return ComparisonCategoryResult::Equal;
case llvm::APFloatBase::cmpGreaterThan:
return ComparisonCategoryResult::Greater;
case llvm::APFloatBase::cmpUnordered:
return ComparisonCategoryResult::Unordered;
}
llvm_unreachable("Inavlid cmpResult value");
}
static APFloat::opStatus fromIntegral(APSInt Val,
const llvm::fltSemantics &Sem,
llvm::RoundingMode RM,
Floating *Result) {
APFloat F = APFloat(Sem);
APFloat::opStatus Status = F.convertFromAPInt(Val, Val.isSigned(), RM);
Result->copy(F);
return Status;
}
static void bitcastFromMemory(const std::byte *Buff,
const llvm::fltSemantics &Sem,
Floating *Result) {
size_t Size = APFloat::semanticsSizeInBits(Sem);
llvm::APInt API(Size, true);
llvm::LoadIntFromMemory(API, (const uint8_t *)Buff, Size / 8);
Result->copy(APFloat(Sem, API));
}
void bitcastToMemory(std::byte *Buff) const {
llvm::APInt API = getValue().bitcastToAPInt();
llvm::StoreIntToMemory(API, (uint8_t *)Buff, bitWidth() / 8);
}
// === Serialization support ===
size_t bytesToSerialize() const {
return sizeof(Semantics) + (numWords() * sizeof(uint64_t));
}
void serialize(std::byte *Buff) const {
std::memcpy(Buff, &Semantics, sizeof(Semantics));
if (singleWord()) {
std::memcpy(Buff + sizeof(Semantics), &Val, sizeof(uint64_t));
} else {
std::memcpy(Buff + sizeof(Semantics), Memory,
numWords() * sizeof(uint64_t));
}
}
static llvm::APFloatBase::Semantics
deserializeSemantics(const std::byte *Buff) {
return *reinterpret_cast<const llvm::APFloatBase::Semantics *>(Buff);
}
static void deserialize(const std::byte *Buff, Floating *Result) {
llvm::APFloatBase::Semantics Semantics;
std::memcpy(&Semantics, Buff, sizeof(Semantics));
unsigned BitWidth = llvm::APFloat::semanticsSizeInBits(
llvm::APFloatBase::EnumToSemantics(Semantics));
unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
Result->Semantics = Semantics;
if (NumWords == 1 && !ALLOCATE_ALL) {
std::memcpy(&Result->Val, Buff + sizeof(Semantics), sizeof(uint64_t));
} else {
assert(Result->Memory);
std::memcpy(Result->Memory, Buff + sizeof(Semantics),
NumWords * sizeof(uint64_t));
}
}
// -------
static APFloat::opStatus add(const Floating &A, const Floating &B,
llvm::RoundingMode RM, Floating *R) {
APFloat LHS = A.getValue();
APFloat RHS = B.getValue();
auto Status = LHS.add(RHS, RM);
R->copy(LHS);
return Status;
}
static APFloat::opStatus increment(const Floating &A, llvm::RoundingMode RM,
Floating *R) {
APFloat One(A.getSemantics(), 1);
APFloat LHS = A.getValue();
auto Status = LHS.add(One, RM);
R->copy(LHS);
return Status;
}
static APFloat::opStatus sub(const Floating &A, const Floating &B,
llvm::RoundingMode RM, Floating *R) {
APFloat LHS = A.getValue();
APFloat RHS = B.getValue();
auto Status = LHS.subtract(RHS, RM);
R->copy(LHS);
return Status;
}
static APFloat::opStatus decrement(const Floating &A, llvm::RoundingMode RM,
Floating *R) {
APFloat One(A.getSemantics(), 1);
APFloat LHS = A.getValue();
auto Status = LHS.subtract(One, RM);
R->copy(LHS);
return Status;
}
static APFloat::opStatus mul(const Floating &A, const Floating &B,
llvm::RoundingMode RM, Floating *R) {
APFloat LHS = A.getValue();
APFloat RHS = B.getValue();
auto Status = LHS.multiply(RHS, RM);
R->copy(LHS);
return Status;
}
static APFloat::opStatus div(const Floating &A, const Floating &B,
llvm::RoundingMode RM, Floating *R) {
APFloat LHS = A.getValue();
APFloat RHS = B.getValue();
auto Status = LHS.divide(RHS, RM);
R->copy(LHS);
return Status;
}
static bool neg(const Floating &A, Floating *R) {
R->copy(-A.getValue());
return false;
}
};
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, Floating F);
Floating getSwappedBytes(Floating F);
} // namespace interp
} // namespace clang
#endif
|