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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include "rcc/ADT/APInt.h"
#include <algorithm>
#include <bit>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <ranges>
#include <span>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
namespace rcc {
namespace {
[[nodiscard]] constexpr std::size_t wordCount(std::uint32_t BitWidth) noexcept {
return (static_cast<std::size_t>(BitWidth) + 63U) / 64U;
}
[[nodiscard]] constexpr std::uint64_t highWordMask(std::uint32_t BitWidth) noexcept {
const std::uint32_t UsedBits = BitWidth % 64U;
if (UsedBits == 0)
return std::numeric_limits<std::uint64_t>::max();
return (std::uint64_t{1} << UsedBits) - 1U;
}
[[nodiscard]] std::size_t significantWordCount(std::span<const std::uint64_t> Words) noexcept {
std::size_t Count = Words.size();
while (Count != 0 && Words[Count - 1U] == 0)
--Count;
return Count;
}
[[nodiscard]] Expected<void> verifyAPInt(std::uint32_t BitWidth,
std::span<const std::uint64_t> Words, ErrorCode Code) {
if (BitWidth == 0 || BitWidth > APInt::MaxBitWidth)
return makeUnexpected(Code, "APInt bit width is outside the supported range");
if (Words.size() != wordCount(BitWidth))
return makeUnexpected(Code, "APInt bit pattern has the wrong word count");
if ((Words.back() & ~highWordMask(BitWidth)) != 0)
return makeUnexpected(Code, "APInt has nonzero bits above its declared width");
return {};
}
[[nodiscard]] bool isHexDigit(char Character) noexcept {
return (Character >= '0' && Character <= '9') || (Character >= 'a' && Character <= 'f') ||
(Character >= 'A' && Character <= 'F');
}
[[nodiscard]] unsigned hexDigitValue(char Character) noexcept {
if (Character >= '0' && Character <= '9')
return static_cast<unsigned>(Character - '0');
if (Character >= 'a' && Character <= 'f')
return static_cast<unsigned>(Character - 'a') + 10U;
return static_cast<unsigned>(Character - 'A') + 10U;
}
void negateWords(std::vector<std::uint64_t> &Words, std::uint32_t BitWidth) {
for (std::uint64_t &Word : Words)
Word = ~Word;
std::uint64_t Carry = 1;
for (std::uint64_t &Word : Words) {
const std::uint64_t Previous = Word;
Word += Carry;
Carry = Carry != 0 && Word < Previous ? 1U : 0U;
}
Words.back() &= highWordMask(BitWidth);
}
[[nodiscard]] std::string unsignedDecimal(std::vector<std::uint64_t> Words) {
if (std::ranges::all_of(Words, [](std::uint64_t Word) { return Word == 0; }))
return "0";
std::string Digits;
while (!std::ranges::all_of(Words, [](std::uint64_t Word) { return Word == 0; })) {
unsigned __int128 Remainder = 0;
for (auto Iterator = Words.rbegin(); Iterator != Words.rend(); ++Iterator) {
const unsigned __int128 Dividend = (Remainder << 64U) | *Iterator;
*Iterator = static_cast<std::uint64_t>(Dividend / 10U);
Remainder = Dividend % 10U;
}
Digits.push_back(static_cast<char>('0' + static_cast<unsigned>(Remainder)));
}
std::ranges::reverse(Digits);
return Digits;
}
} // namespace
APInt::APInt(std::uint64_t Value) {
if (Value == 0)
return;
Words.push_back(Value);
BitWidth = static_cast<std::uint32_t>(std::bit_width(Value));
}
Expected<APInt> APInt::create(std::uint32_t BitWidth, std::span<const std::uint64_t> Words) {
auto Verification = verifyAPInt(BitWidth, Words, ErrorCode::InvalidArgument);
if (!Verification)
return std::unexpected(Verification.error());
return APInt(BitWidth, std::vector<std::uint64_t>(Words.begin(), Words.end()));
}
Expected<APInt> APInt::fromUnsigned(std::uint32_t BitWidth, std::uint64_t Value) {
if (BitWidth == 0 || BitWidth > MaxBitWidth)
return makeUnexpected(ErrorCode::InvalidArgument,
"APInt bit width is outside the supported range");
if (BitWidth < 64U && (Value >> BitWidth) != 0)
return makeUnexpected(ErrorCode::InvalidArgument,
"unsigned value does not fit the APInt bit width");
std::vector<std::uint64_t> Words(wordCount(BitWidth), 0);
Words.front() = Value;
return create(BitWidth, Words);
}
Expected<APInt> APInt::fromSigned(std::uint32_t BitWidth, std::int64_t Value) {
if (BitWidth == 0 || BitWidth > MaxBitWidth)
return makeUnexpected(ErrorCode::InvalidArgument,
"APInt bit width is outside the supported range");
if (BitWidth < 64U) {
const std::uint32_t MagnitudeBits = BitWidth - 1U;
const std::int64_t Minimum = -(std::int64_t{1} << MagnitudeBits);
const std::int64_t Maximum = (std::int64_t{1} << MagnitudeBits) - 1;
if (Value < Minimum || Value > Maximum)
return makeUnexpected(ErrorCode::InvalidArgument,
"signed value does not fit the APInt bit width");
}
const std::uint64_t Fill =
Value < 0 ? std::numeric_limits<std::uint64_t>::max() : std::uint64_t{0};
std::vector<std::uint64_t> Words(wordCount(BitWidth), Fill);
Words.front() = static_cast<std::uint64_t>(Value);
Words.back() &= highWordMask(BitWidth);
return create(BitWidth, Words);
}
Expected<APInt> APInt::getLowBitsSet(std::uint32_t BitWidth,
std::uint32_t SetBitCount) {
if (BitWidth == 0 || BitWidth > MaxBitWidth || SetBitCount > BitWidth)
return makeUnexpected(ErrorCode::InvalidArgument,
"APInt low-bit mask has an invalid width");
std::vector<std::uint64_t> Words(wordCount(BitWidth), 0);
const std::size_t FullWords = SetBitCount / 64U;
std::ranges::fill_n(
Words.begin(),
static_cast<std::vector<std::uint64_t>::difference_type>(FullWords),
std::numeric_limits<std::uint64_t>::max());
const std::uint32_t RemainingBits = SetBitCount % 64U;
if (RemainingBits != 0)
Words[FullWords] = (std::uint64_t{1} << RemainingBits) - 1U;
return create(BitWidth, Words);
}
Expected<APInt> APInt::parseDecimal(std::uint32_t BitWidth, std::string_view Text) {
if (BitWidth == 0 || BitWidth > MaxBitWidth)
return makeUnexpected(ErrorCode::ParseError, "APInt bit width is outside the supported range");
const bool Negative = Text.starts_with('-');
if (Negative)
Text.remove_prefix(1);
if (Text.empty())
return makeUnexpected(ErrorCode::ParseError, "APInt has no decimal digits");
std::vector<std::uint64_t> Words(wordCount(BitWidth), 0);
for (const char Character : Text) {
if (Character < '0' || Character > '9')
return makeUnexpected(ErrorCode::ParseError, "APInt contains a non-decimal digit");
std::uint64_t Carry = static_cast<unsigned>(Character - '0');
for (std::uint64_t &Word : Words) {
const unsigned __int128 Product = static_cast<unsigned __int128>(Word) * 10U + Carry;
Word = static_cast<std::uint64_t>(Product);
Carry = static_cast<std::uint64_t>(Product >> 64U);
}
}
Words.back() &= highWordMask(BitWidth);
if (Negative)
negateWords(Words, BitWidth);
return create(BitWidth, Words);
}
Expected<APInt> APInt::parseLLVMHex(std::uint32_t BitWidth, std::string_view Text) {
if (Text.size() < 4 || (Text[0] != 'u' && Text[0] != 's') || Text[1] != '0' || Text[2] != 'x') {
return makeUnexpected(ErrorCode::ParseError,
"integer hexadecimal spelling must use u0x or s0x");
}
const std::string_view Digits = Text.substr(3);
if (Digits.empty() || !std::ranges::all_of(Digits, isHexDigit)) {
return makeUnexpected(ErrorCode::ParseError,
"integer hexadecimal spelling contains a non-hexadecimal digit");
}
const auto *const FirstNonZero =
std::ranges::find_if(Digits, [](char Character) { return hexDigitValue(Character) != 0; });
std::size_t ActiveBits = 0;
if (FirstNonZero != Digits.end()) {
const unsigned MostSignificantNibble = hexDigitValue(*FirstNonZero);
const auto MostSignificantBits = static_cast<unsigned>(std::bit_width(MostSignificantNibble));
ActiveBits =
static_cast<std::size_t>(Digits.end() - FirstNonZero - 1U) * 4U + MostSignificantBits;
}
if (BitWidth == 0 || BitWidth > MaxBitWidth)
return makeUnexpected(ErrorCode::ParseError, "APInt bit width is outside the supported range");
std::vector<std::uint64_t> Words(wordCount(BitWidth), 0);
std::size_t Nibble = 0;
for (auto Iterator = Digits.rbegin(); Iterator != Digits.rend(); ++Iterator, ++Nibble) {
const std::size_t BitOffset = Nibble * 4U;
if (BitOffset >= BitWidth)
break;
Words[BitOffset / 64U] |= static_cast<std::uint64_t>(hexDigitValue(*Iterator))
<< (BitOffset % 64U);
}
if (Text[0] == 's' && ActiveBits != 0 && ActiveBits < BitWidth) {
const std::size_t FirstWord = ActiveBits / 64U;
const unsigned FirstBit = static_cast<unsigned>(ActiveBits % 64U);
Words[FirstWord] |= std::numeric_limits<std::uint64_t>::max() << FirstBit;
for (std::size_t Index = FirstWord + 1U; Index < Words.size(); ++Index)
Words[Index] = std::numeric_limits<std::uint64_t>::max();
}
Words.back() &= highWordMask(BitWidth);
return create(BitWidth, Words);
}
bool APInt::isZero() const noexcept {
return std::ranges::all_of(Words, [](std::uint64_t Word) { return Word == 0; });
}
bool APInt::isNegative() const noexcept {
if (BitWidth == 0 || Words.empty())
return false;
const std::uint32_t SignBit = BitWidth - 1U;
return (Words[SignBit / 64U] & (std::uint64_t{1} << (SignBit % 64U))) != 0;
}
std::size_t APInt::activeBitWidth() const noexcept {
const std::size_t Count = significantWordCount(Words);
if (Count == 0)
return 0;
return (Count - 1U) * 64U + static_cast<std::size_t>(std::bit_width(Words[Count - 1U]));
}
std::size_t APInt::countrZero() const noexcept {
std::size_t Result = 0;
for (const std::uint64_t Word : Words) {
if (Word == 0) {
Result += 64U;
continue;
}
return Result + static_cast<std::size_t>(std::countr_zero(Word));
}
return Result;
}
std::uint64_t APInt::word(std::size_t Index) const noexcept {
return Index < Words.size() ? Words[Index] : 0;
}
Expected<APInt> APInt::trunc(std::uint32_t NewBitWidth) const {
if (NewBitWidth == 0 || NewBitWidth >= BitWidth)
return makeUnexpected(ErrorCode::InvalidArgument,
"APInt truncation requires a smaller nonzero width");
std::vector<std::uint64_t> ResultWords(wordCount(NewBitWidth), 0);
std::ranges::copy_n(
Words.begin(),
static_cast<std::vector<std::uint64_t>::difference_type>(
ResultWords.size()),
ResultWords.begin());
ResultWords.back() &= highWordMask(NewBitWidth);
return create(NewBitWidth, ResultWords);
}
Expected<APInt> APInt::zext(std::uint32_t NewBitWidth) const {
if (NewBitWidth <= BitWidth || NewBitWidth > MaxBitWidth)
return makeUnexpected(ErrorCode::InvalidArgument,
"APInt zero extension requires a larger supported width");
std::vector<std::uint64_t> ResultWords(wordCount(NewBitWidth), 0);
std::ranges::copy(Words, ResultWords.begin());
return create(NewBitWidth, ResultWords);
}
Expected<APInt> APInt::sext(std::uint32_t NewBitWidth) const {
if (NewBitWidth <= BitWidth || NewBitWidth > MaxBitWidth)
return makeUnexpected(ErrorCode::InvalidArgument,
"APInt sign extension requires a larger supported width");
std::vector<std::uint64_t> ResultWords(
wordCount(NewBitWidth),
isNegative() ? std::numeric_limits<std::uint64_t>::max() : std::uint64_t{0});
std::ranges::copy(Words, ResultWords.begin());
if (isNegative() && BitWidth % 64U != 0)
ResultWords[wordCount(BitWidth) - 1U] |= ~highWordMask(BitWidth);
ResultWords.back() &= highWordMask(NewBitWidth);
return create(NewBitWidth, ResultWords);
}
int APInt::compare(const APInt &Other) const noexcept {
const std::size_t LeftCount = significantWordCount(Words);
const std::size_t RightCount = significantWordCount(Other.Words);
if (LeftCount != RightCount)
return LeftCount < RightCount ? -1 : 1;
for (std::size_t Index = LeftCount; Index != 0; --Index) {
if (Words[Index - 1U] != Other.Words[Index - 1U])
return Words[Index - 1U] < Other.Words[Index - 1U] ? -1 : 1;
}
return 0;
}
int APInt::compareToShifted(const APInt &Other, std::size_t OtherShift) const noexcept {
if (Other.isZero())
return isZero() ? 0 : 1;
if (isZero())
return -1;
const std::size_t LeftBits = activeBitWidth();
const std::size_t RightBits = Other.activeBitWidth();
if (OtherShift > std::numeric_limits<std::size_t>::max() - RightBits)
return -1;
const std::size_t ShiftedBits = RightBits + OtherShift;
if (LeftBits != ShiftedBits)
return LeftBits < ShiftedBits ? -1 : 1;
return compare(Other.shiftedLeft(OtherShift));
}
void APInt::multiplySmall(std::uint32_t Multiplier) {
if (Multiplier == 0 || isZero()) {
Words.clear();
BitWidth = 0;
return;
}
unsigned __int128 Carry = 0;
for (std::uint64_t &Word : Words) {
const unsigned __int128 Product = static_cast<unsigned __int128>(Word) * Multiplier + Carry;
Word = static_cast<std::uint64_t>(Product);
Carry = Product >> 64U;
}
if (Carry != 0)
Words.push_back(static_cast<std::uint64_t>(Carry));
normalizeMagnitude();
}
void APInt::addSmall(std::uint32_t Value) {
unsigned __int128 Carry = Value;
std::size_t Index = 0;
while (Carry != 0 && Index < Words.size()) {
const unsigned __int128 Sum = static_cast<unsigned __int128>(Words[Index]) + Carry;
Words[Index] = static_cast<std::uint64_t>(Sum);
Carry = Sum >> 64U;
++Index;
}
if (Carry != 0)
Words.push_back(static_cast<std::uint64_t>(Carry));
normalizeMagnitude();
}
void APInt::setBit(std::size_t Bit) {
const std::size_t Index = Bit / 64U;
if (Index >= Words.size())
Words.resize(Index + 1U, 0);
Words[Index] |= std::uint64_t{1} << (Bit % 64U);
normalizeMagnitude();
}
void APInt::clearBit(std::size_t Bit) noexcept {
const std::size_t Index = Bit / 64U;
if (Index >= Words.size())
return;
Words[Index] &= ~(std::uint64_t{1} << (Bit % 64U));
normalizeMagnitude();
}
void APInt::shiftRight(std::size_t Amount) noexcept {
if (Amount == 0 || isZero())
return;
const std::size_t WordOffset = Amount / 64U;
if (WordOffset >= Words.size()) {
Words.clear();
BitWidth = 0;
return;
}
const unsigned BitOffset = static_cast<unsigned>(Amount % 64U);
const std::size_t NewSize = Words.size() - WordOffset;
for (std::size_t Index = 0; Index < NewSize; ++Index) {
std::uint64_t Value = Words[Index + WordOffset] >> BitOffset;
if (BitOffset != 0 && Index + WordOffset + 1U < Words.size())
Value |= Words[Index + WordOffset + 1U] << (64U - BitOffset);
Words[Index] = Value;
}
Words.resize(NewSize);
normalizeMagnitude();
}
APInt APInt::shiftedLeft(std::size_t Amount) const {
if (isZero() || Amount == 0)
return *this;
const std::size_t WordOffset = Amount / 64U;
const unsigned BitOffset = static_cast<unsigned>(Amount % 64U);
APInt Result;
Result.Words.assign(Words.size() + WordOffset + (BitOffset == 0 ? 0U : 1U), 0);
for (std::size_t Index = 0; Index < Words.size(); ++Index) {
const unsigned __int128 Shifted = static_cast<unsigned __int128>(Words[Index]) << BitOffset;
Result.Words[Index + WordOffset] |= static_cast<std::uint64_t>(Shifted);
if (BitOffset != 0)
Result.Words[Index + WordOffset + 1U] |= static_cast<std::uint64_t>(Shifted >> 64U);
}
Result.normalizeMagnitude();
return Result;
}
void APInt::subtract(const APInt &Other) noexcept {
std::uint64_t Borrow = 0;
for (std::size_t Index = 0; Index < Words.size(); ++Index) {
const unsigned __int128 Subtrahend =
(Index < Other.Words.size() ? static_cast<unsigned __int128>(Other.Words[Index]) : 0) +
Borrow;
const unsigned __int128 Current = Words[Index];
Words[Index] = static_cast<std::uint64_t>(Current - Subtrahend);
Borrow = Current < Subtrahend ? 1U : 0U;
}
normalizeMagnitude();
}
std::string APInt::toUnsignedDecimal() const {
return unsignedDecimal(Words);
}
std::string APInt::toSignedDecimal() const {
if (!isNegative())
return toUnsignedDecimal();
std::vector<std::uint64_t> Magnitude = Words;
negateWords(Magnitude, BitWidth);
return "-" + unsignedDecimal(std::move(Magnitude));
}
Expected<void> APInt::verify() const {
return verifyAPInt(BitWidth, Words, ErrorCode::VerificationError);
}
void APInt::normalizeMagnitude() noexcept {
while (!Words.empty() && Words.back() == 0)
Words.pop_back();
const std::size_t ActiveBits = activeBitWidth();
BitWidth = static_cast<std::uint32_t>(ActiveBits);
}
} // namespace rcc
|