clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name APInt.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libLLVM/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Analysis -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ASMParser -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/BinaryFormat -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitcode -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitcode -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitstream -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /include/llvm/CodeGen -I /include/llvm/CodeGen/PBQP -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/IR -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/IR -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Coroutines -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ProfileData/Coverage -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/CodeView -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/DWARF -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/MSF -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/PDB -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Demangle -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine/JITLink -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine/Orc -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend/OpenACC -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend/OpenMP -I /include/llvm/CodeGen/GlobalISel -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/IRReader -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/InstCombine -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/Transforms/InstCombine -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/LTO -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Linker -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/MC -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/MC/MCParser -I /include/llvm/CodeGen/MIRParser -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Object -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Option -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Passes -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ProfileData -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Scalar -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ADT -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Support -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/Symbolize -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Target -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Utils -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Vectorize -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/IPO -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libLLVM/../include -I /usr/src/gnu/usr.bin/clang/libLLVM/obj -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -D PIC -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libLLVM/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -D_RET_PROTECTOR -ret-protector -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Support/APInt.cpp
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | #include "llvm/ADT/APInt.h" |
15 | #include "llvm/ADT/ArrayRef.h" |
16 | #include "llvm/ADT/FoldingSet.h" |
17 | #include "llvm/ADT/Hashing.h" |
18 | #include "llvm/ADT/Optional.h" |
19 | #include "llvm/ADT/SmallString.h" |
20 | #include "llvm/ADT/StringRef.h" |
21 | #include "llvm/ADT/bit.h" |
22 | #include "llvm/Config/llvm-config.h" |
23 | #include "llvm/Support/Debug.h" |
24 | #include "llvm/Support/ErrorHandling.h" |
25 | #include "llvm/Support/MathExtras.h" |
26 | #include "llvm/Support/raw_ostream.h" |
27 | #include <climits> |
28 | #include <cmath> |
29 | #include <cstdlib> |
30 | #include <cstring> |
31 | using namespace llvm; |
32 | |
33 | #define DEBUG_TYPE "apint" |
34 | |
35 | |
36 | |
37 | inline static uint64_t* getClearedMemory(unsigned numWords) { |
38 | uint64_t *result = new uint64_t[numWords]; |
39 | memset(result, 0, numWords * sizeof(uint64_t)); |
40 | return result; |
41 | } |
42 | |
43 | |
44 | |
45 | inline static uint64_t* getMemory(unsigned numWords) { |
46 | return new uint64_t[numWords]; |
| |
47 | } |
48 | |
49 | |
50 | inline static unsigned getDigit(char cdigit, uint8_t radix) { |
51 | unsigned r; |
52 | |
53 | if (radix == 16 || radix == 36) { |
54 | r = cdigit - '0'; |
55 | if (r <= 9) |
56 | return r; |
57 | |
58 | r = cdigit - 'A'; |
59 | if (r <= radix - 11U) |
60 | return r + 10; |
61 | |
62 | r = cdigit - 'a'; |
63 | if (r <= radix - 11U) |
64 | return r + 10; |
65 | |
66 | radix = 10; |
67 | } |
68 | |
69 | r = cdigit - '0'; |
70 | if (r < radix) |
71 | return r; |
72 | |
73 | return -1U; |
74 | } |
75 | |
76 | |
77 | void APInt::initSlowCase(uint64_t val, bool isSigned) { |
78 | U.pVal = getClearedMemory(getNumWords()); |
79 | U.pVal[0] = val; |
80 | if (isSigned && int64_t(val) < 0) |
81 | for (unsigned i = 1; i < getNumWords(); ++i) |
82 | U.pVal[i] = WORDTYPE_MAX; |
83 | clearUnusedBits(); |
84 | } |
85 | |
86 | void APInt::initSlowCase(const APInt& that) { |
87 | U.pVal = getMemory(getNumWords()); |
| |
| 10 | | Returned allocated memory | |
|
88 | memcpy(U.pVal, that.U.pVal, getNumWords() * APINT_WORD_SIZE); |
89 | } |
90 | |
91 | void APInt::initFromArray(ArrayRef<uint64_t> bigVal) { |
92 | assert(BitWidth && "Bitwidth too small"); |
93 | assert(bigVal.data() && "Null pointer detected!"); |
94 | if (isSingleWord()) |
95 | U.VAL = bigVal[0]; |
96 | else { |
97 | |
98 | U.pVal = getClearedMemory(getNumWords()); |
99 | |
100 | unsigned words = std::min<unsigned>(bigVal.size(), getNumWords()); |
101 | |
102 | memcpy(U.pVal, bigVal.data(), words * APINT_WORD_SIZE); |
103 | } |
104 | |
105 | clearUnusedBits(); |
106 | } |
107 | |
108 | APInt::APInt(unsigned numBits, ArrayRef<uint64_t> bigVal) |
109 | : BitWidth(numBits) { |
110 | initFromArray(bigVal); |
111 | } |
112 | |
113 | APInt::APInt(unsigned numBits, unsigned numWords, const uint64_t bigVal[]) |
114 | : BitWidth(numBits) { |
115 | initFromArray(makeArrayRef(bigVal, numWords)); |
116 | } |
117 | |
118 | APInt::APInt(unsigned numbits, StringRef Str, uint8_t radix) |
119 | : BitWidth(numbits) { |
120 | assert(BitWidth && "Bitwidth too small"); |
121 | fromString(numbits, Str, radix); |
122 | } |
123 | |
124 | void APInt::reallocate(unsigned NewBitWidth) { |
125 | |
126 | if (getNumWords() == getNumWords(NewBitWidth)) { |
127 | BitWidth = NewBitWidth; |
128 | return; |
129 | } |
130 | |
131 | |
132 | if (!isSingleWord()) |
133 | delete [] U.pVal; |
134 | |
135 | |
136 | BitWidth = NewBitWidth; |
137 | |
138 | |
139 | if (!isSingleWord()) |
140 | U.pVal = getMemory(getNumWords()); |
141 | } |
142 | |
143 | void APInt::AssignSlowCase(const APInt& RHS) { |
144 | |
145 | if (this == &RHS) |
146 | return; |
147 | |
148 | |
149 | reallocate(RHS.getBitWidth()); |
150 | |
151 | |
152 | if (isSingleWord()) |
153 | U.VAL = RHS.U.VAL; |
154 | else |
155 | memcpy(U.pVal, RHS.U.pVal, getNumWords() * APINT_WORD_SIZE); |
156 | } |
157 | |
158 | |
159 | void APInt::Profile(FoldingSetNodeID& ID) const { |
160 | ID.AddInteger(BitWidth); |
161 | |
162 | if (isSingleWord()) { |
163 | ID.AddInteger(U.VAL); |
164 | return; |
165 | } |
166 | |
167 | unsigned NumWords = getNumWords(); |
168 | for (unsigned i = 0; i < NumWords; ++i) |
169 | ID.AddInteger(U.pVal[i]); |
170 | } |
171 | |
172 | |
173 | APInt& APInt::operator++() { |
174 | if (isSingleWord()) |
175 | ++U.VAL; |
176 | else |
177 | tcIncrement(U.pVal, getNumWords()); |
178 | return clearUnusedBits(); |
179 | } |
180 | |
181 | |
182 | APInt& APInt::operator--() { |
183 | if (isSingleWord()) |
184 | --U.VAL; |
185 | else |
186 | tcDecrement(U.pVal, getNumWords()); |
187 | return clearUnusedBits(); |
188 | } |
189 | |
190 | |
191 | |
192 | |
193 | APInt& APInt::operator+=(const APInt& RHS) { |
194 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
195 | if (isSingleWord()) |
196 | U.VAL += RHS.U.VAL; |
197 | else |
198 | tcAdd(U.pVal, RHS.U.pVal, 0, getNumWords()); |
199 | return clearUnusedBits(); |
200 | } |
201 | |
202 | APInt& APInt::operator+=(uint64_t RHS) { |
203 | if (isSingleWord()) |
204 | U.VAL += RHS; |
205 | else |
206 | tcAddPart(U.pVal, RHS, getNumWords()); |
207 | return clearUnusedBits(); |
208 | } |
209 | |
210 | |
211 | |
212 | |
213 | APInt& APInt::operator-=(const APInt& RHS) { |
214 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
215 | if (isSingleWord()) |
216 | U.VAL -= RHS.U.VAL; |
217 | else |
218 | tcSubtract(U.pVal, RHS.U.pVal, 0, getNumWords()); |
219 | return clearUnusedBits(); |
220 | } |
221 | |
222 | APInt& APInt::operator-=(uint64_t RHS) { |
223 | if (isSingleWord()) |
224 | U.VAL -= RHS; |
225 | else |
226 | tcSubtractPart(U.pVal, RHS, getNumWords()); |
227 | return clearUnusedBits(); |
228 | } |
229 | |
230 | APInt APInt::operator*(const APInt& RHS) const { |
231 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
232 | if (isSingleWord()) |
233 | return APInt(BitWidth, U.VAL * RHS.U.VAL); |
234 | |
235 | APInt Result(getMemory(getNumWords()), getBitWidth()); |
236 | |
237 | tcMultiply(Result.U.pVal, U.pVal, RHS.U.pVal, getNumWords()); |
238 | |
239 | Result.clearUnusedBits(); |
240 | return Result; |
241 | } |
242 | |
243 | void APInt::AndAssignSlowCase(const APInt& RHS) { |
244 | tcAnd(U.pVal, RHS.U.pVal, getNumWords()); |
245 | } |
246 | |
247 | void APInt::OrAssignSlowCase(const APInt& RHS) { |
248 | tcOr(U.pVal, RHS.U.pVal, getNumWords()); |
249 | } |
250 | |
251 | void APInt::XorAssignSlowCase(const APInt& RHS) { |
252 | tcXor(U.pVal, RHS.U.pVal, getNumWords()); |
253 | } |
254 | |
255 | APInt& APInt::operator*=(const APInt& RHS) { |
256 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
257 | *this = *this * RHS; |
258 | return *this; |
259 | } |
260 | |
261 | APInt& APInt::operator*=(uint64_t RHS) { |
262 | if (isSingleWord()) { |
263 | U.VAL *= RHS; |
264 | } else { |
265 | unsigned NumWords = getNumWords(); |
266 | tcMultiplyPart(U.pVal, U.pVal, RHS, 0, NumWords, NumWords, false); |
267 | } |
268 | return clearUnusedBits(); |
269 | } |
270 | |
271 | bool APInt::EqualSlowCase(const APInt& RHS) const { |
272 | return std::equal(U.pVal, U.pVal + getNumWords(), RHS.U.pVal); |
273 | } |
274 | |
275 | int APInt::compare(const APInt& RHS) const { |
276 | assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison"); |
277 | if (isSingleWord()) |
278 | return U.VAL < RHS.U.VAL ? -1 : U.VAL > RHS.U.VAL; |
279 | |
280 | return tcCompare(U.pVal, RHS.U.pVal, getNumWords()); |
281 | } |
282 | |
283 | int APInt::compareSigned(const APInt& RHS) const { |
284 | assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison"); |
285 | if (isSingleWord()) { |
286 | int64_t lhsSext = SignExtend64(U.VAL, BitWidth); |
287 | int64_t rhsSext = SignExtend64(RHS.U.VAL, BitWidth); |
288 | return lhsSext < rhsSext ? -1 : lhsSext > rhsSext; |
289 | } |
290 | |
291 | bool lhsNeg = isNegative(); |
292 | bool rhsNeg = RHS.isNegative(); |
293 | |
294 | |
295 | if (lhsNeg != rhsNeg) |
296 | return lhsNeg ? -1 : 1; |
297 | |
298 | |
299 | |
300 | return tcCompare(U.pVal, RHS.U.pVal, getNumWords()); |
301 | } |
302 | |
303 | void APInt::setBitsSlowCase(unsigned loBit, unsigned hiBit) { |
304 | unsigned loWord = whichWord(loBit); |
305 | unsigned hiWord = whichWord(hiBit); |
306 | |
307 | |
308 | uint64_t loMask = WORDTYPE_MAX << whichBit(loBit); |
309 | |
310 | |
311 | unsigned hiShiftAmt = whichBit(hiBit); |
312 | if (hiShiftAmt != 0) { |
313 | |
314 | uint64_t hiMask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - hiShiftAmt); |
315 | |
316 | |
317 | if (hiWord == loWord) |
318 | loMask &= hiMask; |
319 | else |
320 | U.pVal[hiWord] |= hiMask; |
321 | } |
322 | |
323 | U.pVal[loWord] |= loMask; |
324 | |
325 | |
326 | for (unsigned word = loWord + 1; word < hiWord; ++word) |
327 | U.pVal[word] = WORDTYPE_MAX; |
328 | } |
329 | |
330 | |
331 | void APInt::flipAllBitsSlowCase() { |
332 | tcComplement(U.pVal, getNumWords()); |
333 | clearUnusedBits(); |
334 | } |
335 | |
336 | |
337 | |
338 | |
339 | void APInt::flipBit(unsigned bitPosition) { |
340 | assert(bitPosition < BitWidth && "Out of the bit-width range!"); |
341 | setBitVal(bitPosition, !(*this)[bitPosition]); |
342 | } |
343 | |
344 | void APInt::insertBits(const APInt &subBits, unsigned bitPosition) { |
345 | unsigned subBitWidth = subBits.getBitWidth(); |
346 | assert(0 < subBitWidth && (subBitWidth + bitPosition) <= BitWidth && |
347 | "Illegal bit insertion"); |
348 | |
349 | |
350 | if (subBitWidth == BitWidth) { |
351 | *this = subBits; |
352 | return; |
353 | } |
354 | |
355 | |
356 | if (isSingleWord()) { |
357 | uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - subBitWidth); |
358 | U.VAL &= ~(mask << bitPosition); |
359 | U.VAL |= (subBits.U.VAL << bitPosition); |
360 | return; |
361 | } |
362 | |
363 | unsigned loBit = whichBit(bitPosition); |
364 | unsigned loWord = whichWord(bitPosition); |
365 | unsigned hi1Word = whichWord(bitPosition + subBitWidth - 1); |
366 | |
367 | |
368 | if (loWord == hi1Word) { |
369 | uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - subBitWidth); |
370 | U.pVal[loWord] &= ~(mask << loBit); |
371 | U.pVal[loWord] |= (subBits.U.VAL << loBit); |
372 | return; |
373 | } |
374 | |
375 | |
376 | if (loBit == 0) { |
377 | |
378 | unsigned numWholeSubWords = subBitWidth / APINT_BITS_PER_WORD; |
379 | memcpy(U.pVal + loWord, subBits.getRawData(), |
380 | numWholeSubWords * APINT_WORD_SIZE); |
381 | |
382 | |
383 | unsigned remainingBits = subBitWidth % APINT_BITS_PER_WORD; |
384 | if (remainingBits != 0) { |
385 | uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - remainingBits); |
386 | U.pVal[hi1Word] &= ~mask; |
387 | U.pVal[hi1Word] |= subBits.getWord(subBitWidth - 1); |
388 | } |
389 | return; |
390 | } |
391 | |
392 | |
393 | |
394 | |
395 | for (unsigned i = 0; i != subBitWidth; ++i) |
396 | setBitVal(bitPosition + i, subBits[i]); |
397 | } |
398 | |
399 | void APInt::insertBits(uint64_t subBits, unsigned bitPosition, unsigned numBits) { |
400 | uint64_t maskBits = maskTrailingOnes<uint64_t>(numBits); |
401 | subBits &= maskBits; |
402 | if (isSingleWord()) { |
403 | U.VAL &= ~(maskBits << bitPosition); |
404 | U.VAL |= subBits << bitPosition; |
405 | return; |
406 | } |
407 | |
408 | unsigned loBit = whichBit(bitPosition); |
409 | unsigned loWord = whichWord(bitPosition); |
410 | unsigned hiWord = whichWord(bitPosition + numBits - 1); |
411 | if (loWord == hiWord) { |
412 | U.pVal[loWord] &= ~(maskBits << loBit); |
413 | U.pVal[loWord] |= subBits << loBit; |
414 | return; |
415 | } |
416 | |
417 | static_assert(8 * sizeof(WordType) <= 64, "This code assumes only two words affected"); |
418 | unsigned wordBits = 8 * sizeof(WordType); |
419 | U.pVal[loWord] &= ~(maskBits << loBit); |
420 | U.pVal[loWord] |= subBits << loBit; |
421 | |
422 | U.pVal[hiWord] &= ~(maskBits >> (wordBits - loBit)); |
423 | U.pVal[hiWord] |= subBits >> (wordBits - loBit); |
424 | } |
425 | |
426 | APInt APInt::extractBits(unsigned numBits, unsigned bitPosition) const { |
427 | assert(numBits > 0 && "Can't extract zero bits"); |
428 | assert(bitPosition < BitWidth && (numBits + bitPosition) <= BitWidth && |
429 | "Illegal bit extraction"); |
430 | |
431 | if (isSingleWord()) |
432 | return APInt(numBits, U.VAL >> bitPosition); |
433 | |
434 | unsigned loBit = whichBit(bitPosition); |
435 | unsigned loWord = whichWord(bitPosition); |
436 | unsigned hiWord = whichWord(bitPosition + numBits - 1); |
437 | |
438 | |
439 | if (loWord == hiWord) |
440 | return APInt(numBits, U.pVal[loWord] >> loBit); |
441 | |
442 | |
443 | |
444 | if (loBit == 0) |
445 | return APInt(numBits, makeArrayRef(U.pVal + loWord, 1 + hiWord - loWord)); |
446 | |
447 | |
448 | APInt Result(numBits, 0); |
449 | unsigned NumSrcWords = getNumWords(); |
450 | unsigned NumDstWords = Result.getNumWords(); |
451 | |
452 | uint64_t *DestPtr = Result.isSingleWord() ? &Result.U.VAL : Result.U.pVal; |
453 | for (unsigned word = 0; word < NumDstWords; ++word) { |
454 | uint64_t w0 = U.pVal[loWord + word]; |
455 | uint64_t w1 = |
456 | (loWord + word + 1) < NumSrcWords ? U.pVal[loWord + word + 1] : 0; |
457 | DestPtr[word] = (w0 >> loBit) | (w1 << (APINT_BITS_PER_WORD - loBit)); |
458 | } |
459 | |
460 | return Result.clearUnusedBits(); |
461 | } |
462 | |
463 | uint64_t APInt::extractBitsAsZExtValue(unsigned numBits, |
464 | unsigned bitPosition) const { |
465 | assert(numBits > 0 && "Can't extract zero bits"); |
466 | assert(bitPosition < BitWidth && (numBits + bitPosition) <= BitWidth && |
467 | "Illegal bit extraction"); |
468 | assert(numBits <= 64 && "Illegal bit extraction"); |
469 | |
470 | uint64_t maskBits = maskTrailingOnes<uint64_t>(numBits); |
471 | if (isSingleWord()) |
472 | return (U.VAL >> bitPosition) & maskBits; |
473 | |
474 | unsigned loBit = whichBit(bitPosition); |
475 | unsigned loWord = whichWord(bitPosition); |
476 | unsigned hiWord = whichWord(bitPosition + numBits - 1); |
477 | if (loWord == hiWord) |
478 | return (U.pVal[loWord] >> loBit) & maskBits; |
479 | |
480 | static_assert(8 * sizeof(WordType) <= 64, "This code assumes only two words affected"); |
481 | unsigned wordBits = 8 * sizeof(WordType); |
482 | uint64_t retBits = U.pVal[loWord] >> loBit; |
483 | retBits |= U.pVal[hiWord] << (wordBits - loBit); |
484 | retBits &= maskBits; |
485 | return retBits; |
486 | } |
487 | |
488 | unsigned APInt::getBitsNeeded(StringRef str, uint8_t radix) { |
489 | assert(!str.empty() && "Invalid string length"); |
490 | assert((radix == 10 || radix == 8 || radix == 16 || radix == 2 || |
491 | radix == 36) && |
492 | "Radix should be 2, 8, 10, 16, or 36!"); |
493 | |
494 | size_t slen = str.size(); |
495 | |
496 | |
497 | StringRef::iterator p = str.begin(); |
498 | unsigned isNegative = *p == '-'; |
499 | if (*p == '-' || *p == '+') { |
500 | p++; |
501 | slen--; |
502 | assert(slen && "String is only a sign, needs a value."); |
503 | } |
504 | |
505 | |
506 | |
507 | if (radix == 2) |
508 | return slen + isNegative; |
509 | if (radix == 8) |
510 | return slen * 3 + isNegative; |
511 | if (radix == 16) |
512 | return slen * 4 + isNegative; |
513 | |
514 | |
515 | |
516 | |
517 | |
518 | |
519 | |
520 | |
521 | |
522 | |
523 | |
524 | unsigned sufficient |
525 | = radix == 10? (slen == 1 ? 4 : slen * 64/18) |
526 | : (slen == 1 ? 7 : slen * 16/3); |
527 | |
528 | |
529 | APInt tmp(sufficient, StringRef(p, slen), radix); |
530 | |
531 | |
532 | |
533 | |
534 | unsigned log = tmp.logBase2(); |
535 | if (log == (unsigned)-1) { |
536 | return isNegative + 1; |
537 | } else if (isNegative && tmp.isPowerOf2()) { |
538 | return isNegative + log; |
539 | } else { |
540 | return isNegative + log + 1; |
541 | } |
542 | } |
543 | |
544 | hash_code llvm::hash_value(const APInt &Arg) { |
545 | if (Arg.isSingleWord()) |
546 | return hash_combine(Arg.BitWidth, Arg.U.VAL); |
547 | |
548 | return hash_combine( |
549 | Arg.BitWidth, |
550 | hash_combine_range(Arg.U.pVal, Arg.U.pVal + Arg.getNumWords())); |
551 | } |
552 | |
553 | unsigned DenseMapInfo<APInt>::getHashValue(const APInt &Key) { |
554 | return static_cast<unsigned>(hash_value(Key)); |
555 | } |
556 | |
557 | bool APInt::isSplat(unsigned SplatSizeInBits) const { |
558 | assert(getBitWidth() % SplatSizeInBits == 0 && |
559 | "SplatSizeInBits must divide width!"); |
560 | |
561 | |
562 | return *this == rotl(SplatSizeInBits); |
563 | } |
564 | |
565 | |
566 | APInt APInt::getHiBits(unsigned numBits) const { |
567 | return this->lshr(BitWidth - numBits); |
568 | } |
569 | |
570 | |
571 | APInt APInt::getLoBits(unsigned numBits) const { |
572 | APInt Result(getLowBitsSet(BitWidth, numBits)); |
573 | Result &= *this; |
574 | return Result; |
575 | } |
576 | |
577 | |
578 | APInt APInt::getSplat(unsigned NewLen, const APInt &V) { |
579 | assert(NewLen >= V.getBitWidth() && "Can't splat to smaller bit width!"); |
580 | |
581 | APInt Val = V.zextOrSelf(NewLen); |
582 | for (unsigned I = V.getBitWidth(); I < NewLen; I <<= 1) |
583 | Val |= Val << I; |
584 | |
585 | return Val; |
586 | } |
587 | |
588 | unsigned APInt::countLeadingZerosSlowCase() const { |
589 | unsigned Count = 0; |
590 | for (int i = getNumWords()-1; i >= 0; --i) { |
591 | uint64_t V = U.pVal[i]; |
592 | if (V == 0) |
593 | Count += APINT_BITS_PER_WORD; |
594 | else { |
595 | Count += llvm::countLeadingZeros(V); |
596 | break; |
597 | } |
598 | } |
599 | |
600 | unsigned Mod = BitWidth % APINT_BITS_PER_WORD; |
601 | Count -= Mod > 0 ? APINT_BITS_PER_WORD - Mod : 0; |
602 | return Count; |
603 | } |
604 | |
605 | unsigned APInt::countLeadingOnesSlowCase() const { |
606 | unsigned highWordBits = BitWidth % APINT_BITS_PER_WORD; |
607 | unsigned shift; |
608 | if (!highWordBits) { |
609 | highWordBits = APINT_BITS_PER_WORD; |
610 | shift = 0; |
611 | } else { |
612 | shift = APINT_BITS_PER_WORD - highWordBits; |
613 | } |
614 | int i = getNumWords() - 1; |
615 | unsigned Count = llvm::countLeadingOnes(U.pVal[i] << shift); |
616 | if (Count == highWordBits) { |
617 | for (i--; i >= 0; --i) { |
618 | if (U.pVal[i] == WORDTYPE_MAX) |
619 | Count += APINT_BITS_PER_WORD; |
620 | else { |
621 | Count += llvm::countLeadingOnes(U.pVal[i]); |
622 | break; |
623 | } |
624 | } |
625 | } |
626 | return Count; |
627 | } |
628 | |
629 | unsigned APInt::countTrailingZerosSlowCase() const { |
630 | unsigned Count = 0; |
631 | unsigned i = 0; |
632 | for (; i < getNumWords() && U.pVal[i] == 0; ++i) |
633 | Count += APINT_BITS_PER_WORD; |
634 | if (i < getNumWords()) |
635 | Count += llvm::countTrailingZeros(U.pVal[i]); |
636 | return std::min(Count, BitWidth); |
637 | } |
638 | |
639 | unsigned APInt::countTrailingOnesSlowCase() const { |
640 | unsigned Count = 0; |
641 | unsigned i = 0; |
642 | for (; i < getNumWords() && U.pVal[i] == WORDTYPE_MAX; ++i) |
643 | Count += APINT_BITS_PER_WORD; |
644 | if (i < getNumWords()) |
645 | Count += llvm::countTrailingOnes(U.pVal[i]); |
646 | assert(Count <= BitWidth); |
647 | return Count; |
648 | } |
649 | |
650 | unsigned APInt::countPopulationSlowCase() const { |
651 | unsigned Count = 0; |
652 | for (unsigned i = 0; i < getNumWords(); ++i) |
653 | Count += llvm::countPopulation(U.pVal[i]); |
654 | return Count; |
655 | } |
656 | |
657 | bool APInt::intersectsSlowCase(const APInt &RHS) const { |
658 | for (unsigned i = 0, e = getNumWords(); i != e; ++i) |
659 | if ((U.pVal[i] & RHS.U.pVal[i]) != 0) |
660 | return true; |
661 | |
662 | return false; |
663 | } |
664 | |
665 | bool APInt::isSubsetOfSlowCase(const APInt &RHS) const { |
666 | for (unsigned i = 0, e = getNumWords(); i != e; ++i) |
667 | if ((U.pVal[i] & ~RHS.U.pVal[i]) != 0) |
668 | return false; |
669 | |
670 | return true; |
671 | } |
672 | |
673 | APInt APInt::byteSwap() const { |
674 | assert(BitWidth >= 16 && BitWidth % 8 == 0 && "Cannot byteswap!"); |
675 | if (BitWidth == 16) |
676 | return APInt(BitWidth, ByteSwap_16(uint16_t(U.VAL))); |
677 | if (BitWidth == 32) |
678 | return APInt(BitWidth, ByteSwap_32(unsigned(U.VAL))); |
679 | if (BitWidth <= 64) { |
680 | uint64_t Tmp1 = ByteSwap_64(U.VAL); |
681 | Tmp1 >>= (64 - BitWidth); |
682 | return APInt(BitWidth, Tmp1); |
683 | } |
684 | |
685 | APInt Result(getNumWords() * APINT_BITS_PER_WORD, 0); |
686 | for (unsigned I = 0, N = getNumWords(); I != N; ++I) |
687 | Result.U.pVal[I] = ByteSwap_64(U.pVal[N - I - 1]); |
688 | if (Result.BitWidth != BitWidth) { |
689 | Result.lshrInPlace(Result.BitWidth - BitWidth); |
690 | Result.BitWidth = BitWidth; |
691 | } |
692 | return Result; |
693 | } |
694 | |
695 | APInt APInt::reverseBits() const { |
696 | switch (BitWidth) { |
697 | case 64: |
698 | return APInt(BitWidth, llvm::reverseBits<uint64_t>(U.VAL)); |
699 | case 32: |
700 | return APInt(BitWidth, llvm::reverseBits<uint32_t>(U.VAL)); |
701 | case 16: |
702 | return APInt(BitWidth, llvm::reverseBits<uint16_t>(U.VAL)); |
703 | case 8: |
704 | return APInt(BitWidth, llvm::reverseBits<uint8_t>(U.VAL)); |
705 | default: |
706 | break; |
707 | } |
708 | |
709 | APInt Val(*this); |
710 | APInt Reversed(BitWidth, 0); |
711 | unsigned S = BitWidth; |
712 | |
713 | for (; Val != 0; Val.lshrInPlace(1)) { |
714 | Reversed <<= 1; |
715 | Reversed |= Val[0]; |
716 | --S; |
717 | } |
718 | |
719 | Reversed <<= S; |
720 | return Reversed; |
721 | } |
722 | |
723 | APInt llvm::APIntOps::GreatestCommonDivisor(APInt A, APInt B) { |
724 | |
725 | if (A == B) return A; |
726 | |
727 | |
728 | if (!A) return B; |
729 | if (!B) return A; |
730 | |
731 | |
732 | unsigned Pow2; |
733 | { |
734 | unsigned Pow2_A = A.countTrailingZeros(); |
735 | unsigned Pow2_B = B.countTrailingZeros(); |
736 | if (Pow2_A > Pow2_B) { |
737 | A.lshrInPlace(Pow2_A - Pow2_B); |
738 | Pow2 = Pow2_B; |
739 | } else if (Pow2_B > Pow2_A) { |
740 | B.lshrInPlace(Pow2_B - Pow2_A); |
741 | Pow2 = Pow2_A; |
742 | } else { |
743 | Pow2 = Pow2_A; |
744 | } |
745 | } |
746 | |
747 | |
748 | |
749 | |
750 | |
751 | |
752 | |
753 | while (A != B) { |
754 | if (A.ugt(B)) { |
755 | A -= B; |
756 | A.lshrInPlace(A.countTrailingZeros() - Pow2); |
757 | } else { |
758 | B -= A; |
759 | B.lshrInPlace(B.countTrailingZeros() - Pow2); |
760 | } |
761 | } |
762 | |
763 | return A; |
764 | } |
765 | |
766 | APInt llvm::APIntOps::RoundDoubleToAPInt(double Double, unsigned width) { |
767 | uint64_t I = bit_cast<uint64_t>(Double); |
768 | |
769 | |
770 | bool isNeg = I >> 63; |
771 | |
772 | |
773 | int64_t exp = ((I >> 52) & 0x7ff) - 1023; |
774 | |
775 | |
776 | if (exp < 0) |
777 | return APInt(width, 0u); |
778 | |
779 | |
780 | uint64_t mantissa = (I & (~0ULL >> 12)) | 1ULL << 52; |
781 | |
782 | |
783 | if (exp < 52) |
784 | return isNeg ? -APInt(width, mantissa >> (52 - exp)) : |
785 | APInt(width, mantissa >> (52 - exp)); |
786 | |
787 | |
788 | |
789 | if (width <= exp - 52) |
790 | return APInt(width, 0); |
791 | |
792 | |
793 | APInt Tmp(width, mantissa); |
794 | Tmp <<= (unsigned)exp - 52; |
795 | return isNeg ? -Tmp : Tmp; |
796 | } |
797 | |
798 | |
799 | |
800 | |
801 | |
802 | |
803 | |
804 | |
805 | double APInt::roundToDouble(bool isSigned) const { |
806 | |
807 | |
808 | |
809 | if (isSingleWord() || getActiveBits() <= APINT_BITS_PER_WORD) { |
810 | if (isSigned) { |
811 | int64_t sext = SignExtend64(getWord(0), BitWidth); |
812 | return double(sext); |
813 | } else |
814 | return double(getWord(0)); |
815 | } |
816 | |
817 | |
818 | bool isNeg = isSigned ? (*this)[BitWidth-1] : false; |
819 | |
820 | |
821 | APInt Tmp(isNeg ? -(*this) : (*this)); |
822 | |
823 | |
824 | unsigned n = Tmp.getActiveBits(); |
825 | |
826 | |
827 | |
828 | |
829 | uint64_t exp = n; |
830 | |
831 | |
832 | if (exp > 1023) { |
833 | if (!isSigned || !isNeg) |
834 | return std::numeric_limits<double>::infinity(); |
835 | else |
836 | return -std::numeric_limits<double>::infinity(); |
837 | } |
838 | exp += 1023; |
839 | |
840 | |
841 | |
842 | uint64_t mantissa; |
843 | unsigned hiWord = whichWord(n-1); |
844 | if (hiWord == 0) { |
845 | mantissa = Tmp.U.pVal[0]; |
846 | if (n > 52) |
847 | mantissa >>= n - 52; |
848 | } else { |
849 | assert(hiWord > 0 && "huh?"); |
850 | uint64_t hibits = Tmp.U.pVal[hiWord] << (52 - n % APINT_BITS_PER_WORD); |
851 | uint64_t lobits = Tmp.U.pVal[hiWord-1] >> (11 + n % APINT_BITS_PER_WORD); |
852 | mantissa = hibits | lobits; |
853 | } |
854 | |
855 | |
856 | uint64_t sign = isNeg ? (1ULL << (APINT_BITS_PER_WORD - 1)) : 0; |
857 | uint64_t I = sign | (exp << 52) | mantissa; |
858 | return bit_cast<double>(I); |
859 | } |
860 | |
861 | |
862 | APInt APInt::trunc(unsigned width) const { |
863 | assert(width < BitWidth && "Invalid APInt Truncate request"); |
864 | assert(width && "Can't truncate to 0 bits"); |
865 | |
866 | if (width <= APINT_BITS_PER_WORD) |
867 | return APInt(width, getRawData()[0]); |
868 | |
869 | APInt Result(getMemory(getNumWords(width)), width); |
870 | |
871 | |
872 | unsigned i; |
873 | for (i = 0; i != width / APINT_BITS_PER_WORD; i++) |
874 | Result.U.pVal[i] = U.pVal[i]; |
875 | |
876 | |
877 | unsigned bits = (0 - width) % APINT_BITS_PER_WORD; |
878 | if (bits != 0) |
879 | Result.U.pVal[i] = U.pVal[i] << bits >> bits; |
880 | |
881 | return Result; |
882 | } |
883 | |
884 | |
885 | APInt APInt::truncUSat(unsigned width) const { |
886 | assert(width < BitWidth && "Invalid APInt Truncate request"); |
887 | assert(width && "Can't truncate to 0 bits"); |
888 | |
889 | |
890 | if (isIntN(width)) |
891 | return trunc(width); |
892 | |
893 | return APInt::getMaxValue(width); |
894 | } |
895 | |
896 | |
897 | APInt APInt::truncSSat(unsigned width) const { |
898 | assert(width < BitWidth && "Invalid APInt Truncate request"); |
899 | assert(width && "Can't truncate to 0 bits"); |
900 | |
901 | |
902 | if (isSignedIntN(width)) |
903 | return trunc(width); |
904 | |
905 | return isNegative() ? APInt::getSignedMinValue(width) |
906 | : APInt::getSignedMaxValue(width); |
907 | } |
908 | |
909 | |
910 | APInt APInt::sext(unsigned Width) const { |
911 | assert(Width > BitWidth && "Invalid APInt SignExtend request"); |
912 | |
913 | if (Width <= APINT_BITS_PER_WORD) |
914 | return APInt(Width, SignExtend64(U.VAL, BitWidth)); |
915 | |
916 | APInt Result(getMemory(getNumWords(Width)), Width); |
917 | |
918 | |
919 | std::memcpy(Result.U.pVal, getRawData(), getNumWords() * APINT_WORD_SIZE); |
920 | |
921 | |
922 | Result.U.pVal[getNumWords() - 1] = |
923 | SignExtend64(Result.U.pVal[getNumWords() - 1], |
924 | ((BitWidth - 1) % APINT_BITS_PER_WORD) + 1); |
925 | |
926 | |
927 | std::memset(Result.U.pVal + getNumWords(), isNegative() ? -1 : 0, |
928 | (Result.getNumWords() - getNumWords()) * APINT_WORD_SIZE); |
929 | Result.clearUnusedBits(); |
930 | return Result; |
931 | } |
932 | |
933 | |
934 | APInt APInt::zext(unsigned width) const { |
935 | assert(width > BitWidth && "Invalid APInt ZeroExtend request"); |
936 | |
937 | if (width <= APINT_BITS_PER_WORD) |
938 | return APInt(width, U.VAL); |
939 | |
940 | APInt Result(getMemory(getNumWords(width)), width); |
941 | |
942 | |
943 | std::memcpy(Result.U.pVal, getRawData(), getNumWords() * APINT_WORD_SIZE); |
944 | |
945 | |
946 | std::memset(Result.U.pVal + getNumWords(), 0, |
947 | (Result.getNumWords() - getNumWords()) * APINT_WORD_SIZE); |
948 | |
949 | return Result; |
950 | } |
951 | |
952 | APInt APInt::zextOrTrunc(unsigned width) const { |
953 | if (BitWidth < width) |
954 | return zext(width); |
955 | if (BitWidth > width) |
956 | return trunc(width); |
957 | return *this; |
958 | } |
959 | |
960 | APInt APInt::sextOrTrunc(unsigned width) const { |
961 | if (BitWidth < width) |
962 | return sext(width); |
963 | if (BitWidth > width) |
964 | return trunc(width); |
965 | return *this; |
966 | } |
967 | |
968 | APInt APInt::truncOrSelf(unsigned width) const { |
969 | if (BitWidth > width) |
970 | return trunc(width); |
971 | return *this; |
972 | } |
973 | |
974 | APInt APInt::zextOrSelf(unsigned width) const { |
975 | if (BitWidth < width) |
976 | return zext(width); |
977 | return *this; |
978 | } |
979 | |
980 | APInt APInt::sextOrSelf(unsigned width) const { |
981 | if (BitWidth < width) |
982 | return sext(width); |
983 | return *this; |
984 | } |
985 | |
986 | |
987 | |
988 | void APInt::ashrInPlace(const APInt &shiftAmt) { |
989 | ashrInPlace((unsigned)shiftAmt.getLimitedValue(BitWidth)); |
990 | } |
991 | |
992 | |
993 | |
994 | void APInt::ashrSlowCase(unsigned ShiftAmt) { |
995 | |
996 | if (!ShiftAmt) |
997 | return; |
998 | |
999 | |
1000 | bool Negative = isNegative(); |
1001 | |
1002 | |
1003 | unsigned WordShift = ShiftAmt / APINT_BITS_PER_WORD; |
1004 | unsigned BitShift = ShiftAmt % APINT_BITS_PER_WORD; |
1005 | |
1006 | unsigned WordsToMove = getNumWords() - WordShift; |
1007 | if (WordsToMove != 0) { |
1008 | |
1009 | U.pVal[getNumWords() - 1] = SignExtend64( |
1010 | U.pVal[getNumWords() - 1], ((BitWidth - 1) % APINT_BITS_PER_WORD) + 1); |
1011 | |
1012 | |
1013 | if (BitShift == 0) { |
1014 | std::memmove(U.pVal, U.pVal + WordShift, WordsToMove * APINT_WORD_SIZE); |
1015 | } else { |
1016 | |
1017 | for (unsigned i = 0; i != WordsToMove - 1; ++i) |
1018 | U.pVal[i] = (U.pVal[i + WordShift] >> BitShift) | |
1019 | (U.pVal[i + WordShift + 1] << (APINT_BITS_PER_WORD - BitShift)); |
1020 | |
1021 | |
1022 | U.pVal[WordsToMove - 1] = U.pVal[WordShift + WordsToMove - 1] >> BitShift; |
1023 | |
1024 | U.pVal[WordsToMove - 1] = |
1025 | SignExtend64(U.pVal[WordsToMove - 1], APINT_BITS_PER_WORD - BitShift); |
1026 | } |
1027 | } |
1028 | |
1029 | |
1030 | std::memset(U.pVal + WordsToMove, Negative ? -1 : 0, |
1031 | WordShift * APINT_WORD_SIZE); |
1032 | clearUnusedBits(); |
1033 | } |
1034 | |
1035 | |
1036 | |
1037 | void APInt::lshrInPlace(const APInt &shiftAmt) { |
1038 | lshrInPlace((unsigned)shiftAmt.getLimitedValue(BitWidth)); |
1039 | } |
1040 | |
1041 | |
1042 | |
1043 | void APInt::lshrSlowCase(unsigned ShiftAmt) { |
1044 | tcShiftRight(U.pVal, getNumWords(), ShiftAmt); |
1045 | } |
1046 | |
1047 | |
1048 | |
1049 | APInt &APInt::operator<<=(const APInt &shiftAmt) { |
1050 | |
1051 | *this <<= (unsigned)shiftAmt.getLimitedValue(BitWidth); |
1052 | return *this; |
1053 | } |
1054 | |
1055 | void APInt::shlSlowCase(unsigned ShiftAmt) { |
1056 | tcShiftLeft(U.pVal, getNumWords(), ShiftAmt); |
1057 | clearUnusedBits(); |
1058 | } |
1059 | |
1060 | |
1061 | static unsigned rotateModulo(unsigned BitWidth, const APInt &rotateAmt) { |
1062 | unsigned rotBitWidth = rotateAmt.getBitWidth(); |
1063 | APInt rot = rotateAmt; |
1064 | if (rotBitWidth < BitWidth) { |
1065 | |
1066 | |
1067 | rot = rotateAmt.zext(BitWidth); |
1068 | } |
1069 | rot = rot.urem(APInt(rot.getBitWidth(), BitWidth)); |
1070 | return rot.getLimitedValue(BitWidth); |
1071 | } |
1072 | |
1073 | APInt APInt::rotl(const APInt &rotateAmt) const { |
1074 | return rotl(rotateModulo(BitWidth, rotateAmt)); |
1075 | } |
1076 | |
1077 | APInt APInt::rotl(unsigned rotateAmt) const { |
1078 | rotateAmt %= BitWidth; |
1079 | if (rotateAmt == 0) |
1080 | return *this; |
1081 | return shl(rotateAmt) | lshr(BitWidth - rotateAmt); |
1082 | } |
1083 | |
1084 | APInt APInt::rotr(const APInt &rotateAmt) const { |
1085 | return rotr(rotateModulo(BitWidth, rotateAmt)); |
1086 | } |
1087 | |
1088 | APInt APInt::rotr(unsigned rotateAmt) const { |
1089 | rotateAmt %= BitWidth; |
1090 | if (rotateAmt == 0) |
1091 | return *this; |
1092 | return lshr(rotateAmt) | shl(BitWidth - rotateAmt); |
1093 | } |
1094 | |
1095 | |
1096 | |
1097 | |
1098 | |
1099 | |
1100 | |
1101 | |
1102 | APInt APInt::sqrt() const { |
1103 | |
1104 | |
1105 | unsigned magnitude = getActiveBits(); |
1106 | |
1107 | |
1108 | |
1109 | if (magnitude <= 5) { |
1110 | static const uint8_t results[32] = { |
1111 | 0, |
1112 | 1, 1, |
1113 | 2, 2, 2, 2, |
1114 | 3, 3, 3, 3, 3, 3, |
1115 | 4, 4, 4, 4, 4, 4, 4, 4, |
1116 | 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
1117 | 6 |
1118 | }; |
1119 | return APInt(BitWidth, results[ (isSingleWord() ? U.VAL : U.pVal[0]) ]); |
1120 | } |
1121 | |
1122 | |
1123 | |
1124 | |
1125 | |
1126 | if (magnitude < 52) { |
1127 | return APInt(BitWidth, |
1128 | uint64_t(::round(::sqrt(double(isSingleWord() ? U.VAL |
1129 | : U.pVal[0]))))); |
1130 | } |
1131 | |
1132 | |
1133 | |
1134 | |
1135 | |
1136 | |
1137 | unsigned nbits = BitWidth, i = 4; |
1138 | APInt testy(BitWidth, 16); |
1139 | APInt x_old(BitWidth, 1); |
1140 | APInt x_new(BitWidth, 0); |
1141 | APInt two(BitWidth, 2); |
1142 | |
1143 | |
1144 | for (;; i += 2, testy = testy.shl(2)) |
1145 | if (i >= nbits || this->ule(testy)) { |
1146 | x_old = x_old.shl(i / 2); |
1147 | break; |
1148 | } |
1149 | |
1150 | |
1151 | for (;;) { |
1152 | x_new = (this->udiv(x_old) + x_old).udiv(two); |
1153 | if (x_old.ule(x_new)) |
1154 | break; |
1155 | x_old = x_new; |
1156 | } |
1157 | |
1158 | |
1159 | |
1160 | |
1161 | |
1162 | |
1163 | |
1164 | APInt square(x_old * x_old); |
1165 | APInt nextSquare((x_old + 1) * (x_old +1)); |
1166 | if (this->ult(square)) |
1167 | return x_old; |
1168 | assert(this->ule(nextSquare) && "Error in APInt::sqrt computation"); |
1169 | APInt midpoint((nextSquare - square).udiv(two)); |
1170 | APInt offset(*this - square); |
1171 | if (offset.ult(midpoint)) |
1172 | return x_old; |
1173 | return x_old + 1; |
1174 | } |
1175 | |
1176 | |
1177 | |
1178 | |
1179 | |
1180 | |
1181 | |
1182 | |
1183 | APInt APInt::multiplicativeInverse(const APInt& modulo) const { |
1184 | assert(ult(modulo) && "This APInt must be smaller than the modulo"); |
1185 | |
1186 | |
1187 | |
1188 | |
1189 | |
1190 | |
1191 | |
1192 | |
1193 | |
1194 | APInt r[2] = { modulo, *this }; |
1195 | APInt t[2] = { APInt(BitWidth, 0), APInt(BitWidth, 1) }; |
1196 | APInt q(BitWidth, 0); |
1197 | |
1198 | unsigned i; |
1199 | for (i = 0; r[i^1] != 0; i ^= 1) { |
1200 | |
1201 | |
1202 | |
1203 | |
1204 | udivrem(r[i], r[i^1], q, r[i]); |
1205 | t[i] -= t[i^1] * q; |
1206 | } |
1207 | |
1208 | |
1209 | |
1210 | |
1211 | |
1212 | if (r[i] != 1) |
1213 | return APInt(BitWidth, 0); |
1214 | |
1215 | |
1216 | |
1217 | |
1218 | |
1219 | if (t[i].isNegative()) |
1220 | t[i] += modulo; |
1221 | |
1222 | return std::move(t[i]); |
1223 | } |
1224 | |
1225 | |
1226 | |
1227 | |
1228 | |
1229 | APInt::ms APInt::magic() const { |
1230 | const APInt& d = *this; |
1231 | unsigned p; |
1232 | APInt ad, anc, delta, q1, r1, q2, r2, t; |
1233 | APInt signedMin = APInt::getSignedMinValue(d.getBitWidth()); |
1234 | struct ms mag; |
1235 | |
1236 | ad = d.abs(); |
1237 | t = signedMin + (d.lshr(d.getBitWidth() - 1)); |
1238 | anc = t - 1 - t.urem(ad); |
1239 | p = d.getBitWidth() - 1; |
1240 | q1 = signedMin.udiv(anc); |
1241 | r1 = signedMin - q1*anc; |
1242 | q2 = signedMin.udiv(ad); |
1243 | r2 = signedMin - q2*ad; |
1244 | do { |
1245 | p = p + 1; |
1246 | q1 = q1<<1; |
1247 | r1 = r1<<1; |
1248 | if (r1.uge(anc)) { |
1249 | q1 = q1 + 1; |
1250 | r1 = r1 - anc; |
1251 | } |
1252 | q2 = q2<<1; |
1253 | r2 = r2<<1; |
1254 | if (r2.uge(ad)) { |
1255 | q2 = q2 + 1; |
1256 | r2 = r2 - ad; |
1257 | } |
1258 | delta = ad - r2; |
1259 | } while (q1.ult(delta) || (q1 == delta && r1 == 0)); |
1260 | |
1261 | mag.m = q2 + 1; |
1262 | if (d.isNegative()) mag.m = -mag.m; |
1263 | mag.s = p - d.getBitWidth(); |
1264 | return mag; |
1265 | } |
1266 | |
1267 | |
1268 | |
1269 | |
1270 | |
1271 | |
1272 | |
1273 | APInt::mu APInt::magicu(unsigned LeadingZeros) const { |
1274 | const APInt& d = *this; |
1275 | unsigned p; |
1276 | APInt nc, delta, q1, r1, q2, r2; |
1277 | struct mu magu; |
1278 | magu.a = 0; |
1279 | APInt allOnes = APInt::getAllOnesValue(d.getBitWidth()).lshr(LeadingZeros); |
1280 | APInt signedMin = APInt::getSignedMinValue(d.getBitWidth()); |
1281 | APInt signedMax = APInt::getSignedMaxValue(d.getBitWidth()); |
1282 | |
1283 | nc = allOnes - (allOnes - d).urem(d); |
1284 | p = d.getBitWidth() - 1; |
1285 | q1 = signedMin.udiv(nc); |
1286 | r1 = signedMin - q1*nc; |
1287 | q2 = signedMax.udiv(d); |
1288 | r2 = signedMax - q2*d; |
1289 | do { |
1290 | p = p + 1; |
1291 | if (r1.uge(nc - r1)) { |
1292 | q1 = q1 + q1 + 1; |
1293 | r1 = r1 + r1 - nc; |
1294 | } |
1295 | else { |
1296 | q1 = q1+q1; |
1297 | r1 = r1+r1; |
1298 | } |
1299 | if ((r2 + 1).uge(d - r2)) { |
1300 | if (q2.uge(signedMax)) magu.a = 1; |
1301 | q2 = q2+q2 + 1; |
1302 | r2 = r2+r2 + 1 - d; |
1303 | } |
1304 | else { |
1305 | if (q2.uge(signedMin)) magu.a = 1; |
1306 | q2 = q2+q2; |
1307 | r2 = r2+r2 + 1; |
1308 | } |
1309 | delta = d - 1 - r2; |
1310 | } while (p < d.getBitWidth()*2 && |
1311 | (q1.ult(delta) || (q1 == delta && r1 == 0))); |
1312 | magu.m = q2 + 1; |
1313 | magu.s = p - d.getBitWidth(); |
1314 | return magu; |
1315 | } |
1316 | |
1317 | |
1318 | |
1319 | |
1320 | |
1321 | static void KnuthDiv(uint32_t *u, uint32_t *v, uint32_t *q, uint32_t* r, |
1322 | unsigned m, unsigned n) { |
1323 | assert(u && "Must provide dividend"); |
1324 | assert(v && "Must provide divisor"); |
1325 | assert(q && "Must provide quotient"); |
1326 | assert(u != v && u != q && v != q && "Must use different memory"); |
1327 | assert(n>1 && "n must be > 1"); |
1328 | |
1329 | |
1330 | const uint64_t b = uint64_t(1) << 32; |
1331 | |
1332 | |
1333 | |
1334 | #ifdef KNUTH_DEBUG |
1335 | #define DEBUG_KNUTH(X) LLVM_DEBUG(X) |
1336 | #else |
1337 | #define DEBUG_KNUTH(X) do {} while(false) |
1338 | #endif |
1339 | |
1340 | DEBUG_KNUTH(dbgs() << "KnuthDiv: m=" << m << " n=" << n << '\n'); |
1341 | DEBUG_KNUTH(dbgs() << "KnuthDiv: original:"); |
1342 | DEBUG_KNUTH(for (int i = m + n; i >= 0; i--) dbgs() << " " << u[i]); |
1343 | DEBUG_KNUTH(dbgs() << " by"); |
1344 | DEBUG_KNUTH(for (int i = n; i > 0; i--) dbgs() << " " << v[i - 1]); |
1345 | DEBUG_KNUTH(dbgs() << '\n'); |
1346 | |
1347 | |
1348 | |
1349 | |
1350 | |
1351 | |
1352 | |
1353 | |
1354 | unsigned shift = countLeadingZeros(v[n-1]); |
1355 | uint32_t v_carry = 0; |
1356 | uint32_t u_carry = 0; |
1357 | if (shift) { |
1358 | for (unsigned i = 0; i < m+n; ++i) { |
1359 | uint32_t u_tmp = u[i] >> (32 - shift); |
1360 | u[i] = (u[i] << shift) | u_carry; |
1361 | u_carry = u_tmp; |
1362 | } |
1363 | for (unsigned i = 0; i < n; ++i) { |
1364 | uint32_t v_tmp = v[i] >> (32 - shift); |
1365 | v[i] = (v[i] << shift) | v_carry; |
1366 | v_carry = v_tmp; |
1367 | } |
1368 | } |
1369 | u[m+n] = u_carry; |
1370 | |
1371 | DEBUG_KNUTH(dbgs() << "KnuthDiv: normal:"); |
1372 | DEBUG_KNUTH(for (int i = m + n; i >= 0; i--) dbgs() << " " << u[i]); |
1373 | DEBUG_KNUTH(dbgs() << " by"); |
1374 | DEBUG_KNUTH(for (int i = n; i > 0; i--) dbgs() << " " << v[i - 1]); |
1375 | DEBUG_KNUTH(dbgs() << '\n'); |
1376 | |
1377 | |
1378 | int j = m; |
1379 | do { |
1380 | DEBUG_KNUTH(dbgs() << "KnuthDiv: quotient digit #" << j << '\n'); |
1381 | |
1382 | |
1383 | |
1384 | |
1385 | |
1386 | |
1387 | |
1388 | |
1389 | uint64_t dividend = Make_64(u[j+n], u[j+n-1]); |
1390 | DEBUG_KNUTH(dbgs() << "KnuthDiv: dividend == " << dividend << '\n'); |
1391 | uint64_t qp = dividend / v[n-1]; |
1392 | uint64_t rp = dividend % v[n-1]; |
1393 | if (qp == b || qp*v[n-2] > b*rp + u[j+n-2]) { |
1394 | qp--; |
1395 | rp += v[n-1]; |
1396 | if (rp < b && (qp == b || qp*v[n-2] > b*rp + u[j+n-2])) |
1397 | qp--; |
1398 | } |
1399 | DEBUG_KNUTH(dbgs() << "KnuthDiv: qp == " << qp << ", rp == " << rp << '\n'); |
1400 | |
1401 | |
1402 | |
1403 | |
1404 | |
1405 | |
1406 | |
1407 | |
1408 | |
1409 | int64_t borrow = 0; |
1410 | for (unsigned i = 0; i < n; ++i) { |
1411 | uint64_t p = uint64_t(qp) * uint64_t(v[i]); |
1412 | int64_t subres = int64_t(u[j+i]) - borrow - Lo_32(p); |
1413 | u[j+i] = Lo_32(subres); |
1414 | borrow = Hi_32(p) - Hi_32(subres); |
1415 | DEBUG_KNUTH(dbgs() << "KnuthDiv: u[j+i] = " << u[j + i] |
1416 | << ", borrow = " << borrow << '\n'); |
1417 | } |
1418 | bool isNeg = u[j+n] < borrow; |
1419 | u[j+n] -= Lo_32(borrow); |
1420 | |
1421 | DEBUG_KNUTH(dbgs() << "KnuthDiv: after subtraction:"); |
1422 | DEBUG_KNUTH(for (int i = m + n; i >= 0; i--) dbgs() << " " << u[i]); |
1423 | DEBUG_KNUTH(dbgs() << '\n'); |
1424 | |
1425 | |
1426 | |
1427 | q[j] = Lo_32(qp); |
1428 | if (isNeg) { |
1429 | |
1430 | |
1431 | |
1432 | q[j]--; |
1433 | |
1434 | |
1435 | |
1436 | bool carry = false; |
1437 | for (unsigned i = 0; i < n; i++) { |
1438 | uint32_t limit = std::min(u[j+i],v[i]); |
1439 | u[j+i] += v[i] + carry; |
1440 | carry = u[j+i] < limit || (carry && u[j+i] == limit); |
1441 | } |
1442 | u[j+n] += carry; |
1443 | } |
1444 | DEBUG_KNUTH(dbgs() << "KnuthDiv: after correction:"); |
1445 | DEBUG_KNUTH(for (int i = m + n; i >= 0; i--) dbgs() << " " << u[i]); |
1446 | DEBUG_KNUTH(dbgs() << "\nKnuthDiv: digit result = " << q[j] << '\n'); |
1447 | |
1448 | |
1449 | } while (--j >= 0); |
1450 | |
1451 | DEBUG_KNUTH(dbgs() << "KnuthDiv: quotient:"); |
1452 | DEBUG_KNUTH(for (int i = m; i >= 0; i--) dbgs() << " " << q[i]); |
1453 | DEBUG_KNUTH(dbgs() << '\n'); |
1454 | |
1455 | |
1456 | |
1457 | |
1458 | if (r) { |
1459 | |
1460 | |
1461 | |
1462 | if (shift) { |
1463 | uint32_t carry = 0; |
1464 | DEBUG_KNUTH(dbgs() << "KnuthDiv: remainder:"); |
1465 | for (int i = n-1; i >= 0; i--) { |
1466 | r[i] = (u[i] >> shift) | carry; |
1467 | carry = u[i] << (32 - shift); |
1468 | DEBUG_KNUTH(dbgs() << " " << r[i]); |
1469 | } |
1470 | } else { |
1471 | for (int i = n-1; i >= 0; i--) { |
1472 | r[i] = u[i]; |
1473 | DEBUG_KNUTH(dbgs() << " " << r[i]); |
1474 | } |
1475 | } |
1476 | DEBUG_KNUTH(dbgs() << '\n'); |
1477 | } |
1478 | DEBUG_KNUTH(dbgs() << '\n'); |
1479 | } |
1480 | |
1481 | void APInt::divide(const WordType *LHS, unsigned lhsWords, const WordType *RHS, |
1482 | unsigned rhsWords, WordType *Quotient, WordType *Remainder) { |
1483 | assert(lhsWords >= rhsWords && "Fractional result"); |
1484 | |
1485 | |
1486 | |
1487 | |
1488 | |
1489 | |
1490 | |
1491 | |
1492 | unsigned n = rhsWords * 2; |
1493 | unsigned m = (lhsWords * 2) - n; |
1494 | |
1495 | |
1496 | |
1497 | uint32_t SPACE[128]; |
1498 | uint32_t *U = nullptr; |
1499 | uint32_t *V = nullptr; |
1500 | uint32_t *Q = nullptr; |
1501 | uint32_t *R = nullptr; |
1502 | if ((Remainder?4:3)*n+2*m+1 <= 128) { |
1503 | U = &SPACE[0]; |
1504 | V = &SPACE[m+n+1]; |
1505 | Q = &SPACE[(m+n+1) + n]; |
1506 | if (Remainder) |
1507 | R = &SPACE[(m+n+1) + n + (m+n)]; |
1508 | } else { |
1509 | U = new uint32_t[m + n + 1]; |
1510 | V = new uint32_t[n]; |
1511 | Q = new uint32_t[m+n]; |
1512 | if (Remainder) |
1513 | R = new uint32_t[n]; |
1514 | } |
1515 | |
1516 | |
1517 | memset(U, 0, (m+n+1)*sizeof(uint32_t)); |
1518 | for (unsigned i = 0; i < lhsWords; ++i) { |
1519 | uint64_t tmp = LHS[i]; |
1520 | U[i * 2] = Lo_32(tmp); |
1521 | U[i * 2 + 1] = Hi_32(tmp); |
1522 | } |
1523 | U[m+n] = 0; |
1524 | |
1525 | |
1526 | memset(V, 0, (n)*sizeof(uint32_t)); |
1527 | for (unsigned i = 0; i < rhsWords; ++i) { |
1528 | uint64_t tmp = RHS[i]; |
1529 | V[i * 2] = Lo_32(tmp); |
1530 | V[i * 2 + 1] = Hi_32(tmp); |
1531 | } |
1532 | |
1533 | |
1534 | memset(Q, 0, (m+n) * sizeof(uint32_t)); |
1535 | if (Remainder) |
1536 | memset(R, 0, n * sizeof(uint32_t)); |
1537 | |
1538 | |
1539 | |
1540 | |
1541 | |
1542 | for (unsigned i = n; i > 0 && V[i-1] == 0; i--) { |
1543 | n--; |
1544 | m++; |
1545 | } |
1546 | for (unsigned i = m+n; i > 0 && U[i-1] == 0; i--) |
1547 | m--; |
1548 | |
1549 | |
1550 | |
1551 | |
1552 | |
1553 | |
1554 | |
1555 | assert(n != 0 && "Divide by zero?"); |
1556 | if (n == 1) { |
1557 | uint32_t divisor = V[0]; |
1558 | uint32_t remainder = 0; |
1559 | for (int i = m; i >= 0; i--) { |
1560 | uint64_t partial_dividend = Make_64(remainder, U[i]); |
1561 | if (partial_dividend == 0) { |
1562 | Q[i] = 0; |
1563 | remainder = 0; |
1564 | } else if (partial_dividend < divisor) { |
1565 | Q[i] = 0; |
1566 | remainder = Lo_32(partial_dividend); |
1567 | } else if (partial_dividend == divisor) { |
1568 | Q[i] = 1; |
1569 | remainder = 0; |
1570 | } else { |
1571 | Q[i] = Lo_32(partial_dividend / divisor); |
1572 | remainder = Lo_32(partial_dividend - (Q[i] * divisor)); |
1573 | } |
1574 | } |
1575 | if (R) |
1576 | R[0] = remainder; |
1577 | } else { |
1578 | |
1579 | |
1580 | KnuthDiv(U, V, Q, R, m, n); |
1581 | } |
1582 | |
1583 | |
1584 | if (Quotient) { |
1585 | for (unsigned i = 0; i < lhsWords; ++i) |
1586 | Quotient[i] = Make_64(Q[i*2+1], Q[i*2]); |
1587 | } |
1588 | |
1589 | |
1590 | if (Remainder) { |
1591 | for (unsigned i = 0; i < rhsWords; ++i) |
1592 | Remainder[i] = Make_64(R[i*2+1], R[i*2]); |
1593 | } |
1594 | |
1595 | |
1596 | if (U != &SPACE[0]) { |
1597 | delete [] U; |
1598 | delete [] V; |
1599 | delete [] Q; |
1600 | delete [] R; |
1601 | } |
1602 | } |
1603 | |
1604 | APInt APInt::udiv(const APInt &RHS) const { |
1605 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
1606 | |
1607 | |
1608 | if (isSingleWord()) { |
1609 | assert(RHS.U.VAL != 0 && "Divide by zero?"); |
1610 | return APInt(BitWidth, U.VAL / RHS.U.VAL); |
1611 | } |
1612 | |
1613 | |
1614 | unsigned lhsWords = getNumWords(getActiveBits()); |
1615 | unsigned rhsBits = RHS.getActiveBits(); |
1616 | unsigned rhsWords = getNumWords(rhsBits); |
1617 | assert(rhsWords && "Divided by zero???"); |
1618 | |
1619 | |
1620 | if (!lhsWords) |
1621 | |
1622 | return APInt(BitWidth, 0); |
1623 | if (rhsBits == 1) |
1624 | |
1625 | return *this; |
1626 | if (lhsWords < rhsWords || this->ult(RHS)) |
1627 | |
1628 | return APInt(BitWidth, 0); |
1629 | if (*this == RHS) |
1630 | |
1631 | return APInt(BitWidth, 1); |
1632 | if (lhsWords == 1) |
1633 | |
1634 | return APInt(BitWidth, this->U.pVal[0] / RHS.U.pVal[0]); |
1635 | |
1636 | |
1637 | APInt Quotient(BitWidth, 0); |
1638 | divide(U.pVal, lhsWords, RHS.U.pVal, rhsWords, Quotient.U.pVal, nullptr); |
1639 | return Quotient; |
1640 | } |
1641 | |
1642 | APInt APInt::udiv(uint64_t RHS) const { |
1643 | assert(RHS != 0 && "Divide by zero?"); |
1644 | |
1645 | |
1646 | if (isSingleWord()) |
1647 | return APInt(BitWidth, U.VAL / RHS); |
1648 | |
1649 | |
1650 | unsigned lhsWords = getNumWords(getActiveBits()); |
1651 | |
1652 | |
1653 | if (!lhsWords) |
1654 | |
1655 | return APInt(BitWidth, 0); |
1656 | if (RHS == 1) |
1657 | |
1658 | return *this; |
1659 | if (this->ult(RHS)) |
1660 | |
1661 | return APInt(BitWidth, 0); |
1662 | if (*this == RHS) |
1663 | |
1664 | return APInt(BitWidth, 1); |
1665 | if (lhsWords == 1) |
1666 | |
1667 | return APInt(BitWidth, this->U.pVal[0] / RHS); |
1668 | |
1669 | |
1670 | APInt Quotient(BitWidth, 0); |
1671 | divide(U.pVal, lhsWords, &RHS, 1, Quotient.U.pVal, nullptr); |
1672 | return Quotient; |
1673 | } |
1674 | |
1675 | APInt APInt::sdiv(const APInt &RHS) const { |
1676 | if (isNegative()) { |
1677 | if (RHS.isNegative()) |
1678 | return (-(*this)).udiv(-RHS); |
1679 | return -((-(*this)).udiv(RHS)); |
1680 | } |
1681 | if (RHS.isNegative()) |
1682 | return -(this->udiv(-RHS)); |
1683 | return this->udiv(RHS); |
1684 | } |
1685 | |
1686 | APInt APInt::sdiv(int64_t RHS) const { |
1687 | if (isNegative()) { |
1688 | if (RHS < 0) |
1689 | return (-(*this)).udiv(-RHS); |
1690 | return -((-(*this)).udiv(RHS)); |
1691 | } |
1692 | if (RHS < 0) |
1693 | return -(this->udiv(-RHS)); |
1694 | return this->udiv(RHS); |
1695 | } |
1696 | |
1697 | APInt APInt::urem(const APInt &RHS) const { |
1698 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
1699 | if (isSingleWord()) { |
1700 | assert(RHS.U.VAL != 0 && "Remainder by zero?"); |
1701 | return APInt(BitWidth, U.VAL % RHS.U.VAL); |
1702 | } |
1703 | |
1704 | |
1705 | unsigned lhsWords = getNumWords(getActiveBits()); |
1706 | |
1707 | |
1708 | unsigned rhsBits = RHS.getActiveBits(); |
1709 | unsigned rhsWords = getNumWords(rhsBits); |
1710 | assert(rhsWords && "Performing remainder operation by zero ???"); |
1711 | |
1712 | |
1713 | if (lhsWords == 0) |
1714 | |
1715 | return APInt(BitWidth, 0); |
1716 | if (rhsBits == 1) |
1717 | |
1718 | return APInt(BitWidth, 0); |
1719 | if (lhsWords < rhsWords || this->ult(RHS)) |
1720 | |
1721 | return *this; |
1722 | if (*this == RHS) |
1723 | |
1724 | return APInt(BitWidth, 0); |
1725 | if (lhsWords == 1) |
1726 | |
1727 | return APInt(BitWidth, U.pVal[0] % RHS.U.pVal[0]); |
1728 | |
1729 | |
1730 | APInt Remainder(BitWidth, 0); |
1731 | divide(U.pVal, lhsWords, RHS.U.pVal, rhsWords, nullptr, Remainder.U.pVal); |
1732 | return Remainder; |
1733 | } |
1734 | |
1735 | uint64_t APInt::urem(uint64_t RHS) const { |
1736 | assert(RHS != 0 && "Remainder by zero?"); |
1737 | |
1738 | if (isSingleWord()) |
1739 | return U.VAL % RHS; |
1740 | |
1741 | |
1742 | unsigned lhsWords = getNumWords(getActiveBits()); |
1743 | |
1744 | |
1745 | if (lhsWords == 0) |
1746 | |
1747 | return 0; |
1748 | if (RHS == 1) |
1749 | |
1750 | return 0; |
1751 | if (this->ult(RHS)) |
1752 | |
1753 | return getZExtValue(); |
1754 | if (*this == RHS) |
1755 | |
1756 | return 0; |
1757 | if (lhsWords == 1) |
1758 | |
1759 | return U.pVal[0] % RHS; |
1760 | |
1761 | |
1762 | uint64_t Remainder; |
1763 | divide(U.pVal, lhsWords, &RHS, 1, nullptr, &Remainder); |
1764 | return Remainder; |
1765 | } |
1766 | |
1767 | APInt APInt::srem(const APInt &RHS) const { |
1768 | if (isNegative()) { |
1769 | if (RHS.isNegative()) |
1770 | return -((-(*this)).urem(-RHS)); |
1771 | return -((-(*this)).urem(RHS)); |
1772 | } |
1773 | if (RHS.isNegative()) |
1774 | return this->urem(-RHS); |
1775 | return this->urem(RHS); |
1776 | } |
1777 | |
1778 | int64_t APInt::srem(int64_t RHS) const { |
1779 | if (isNegative()) { |
1780 | if (RHS < 0) |
1781 | return -((-(*this)).urem(-RHS)); |
1782 | return -((-(*this)).urem(RHS)); |
1783 | } |
1784 | if (RHS < 0) |
1785 | return this->urem(-RHS); |
1786 | return this->urem(RHS); |
1787 | } |
1788 | |
1789 | void APInt::udivrem(const APInt &LHS, const APInt &RHS, |
1790 | APInt &Quotient, APInt &Remainder) { |
1791 | assert(LHS.BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
1792 | unsigned BitWidth = LHS.BitWidth; |
1793 | |
1794 | |
1795 | if (LHS.isSingleWord()) { |
1796 | assert(RHS.U.VAL != 0 && "Divide by zero?"); |
1797 | uint64_t QuotVal = LHS.U.VAL / RHS.U.VAL; |
1798 | uint64_t RemVal = LHS.U.VAL % RHS.U.VAL; |
1799 | Quotient = APInt(BitWidth, QuotVal); |
1800 | Remainder = APInt(BitWidth, RemVal); |
1801 | return; |
1802 | } |
1803 | |
1804 | |
1805 | unsigned lhsWords = getNumWords(LHS.getActiveBits()); |
1806 | unsigned rhsBits = RHS.getActiveBits(); |
1807 | unsigned rhsWords = getNumWords(rhsBits); |
1808 | assert(rhsWords && "Performing divrem operation by zero ???"); |
1809 | |
1810 | |
1811 | if (lhsWords == 0) { |
1812 | Quotient = APInt(BitWidth, 0); |
1813 | Remainder = APInt(BitWidth, 0); |
1814 | return; |
1815 | } |
1816 | |
1817 | if (rhsBits == 1) { |
1818 | Quotient = LHS; |
1819 | Remainder = APInt(BitWidth, 0); |
1820 | } |
1821 | |
1822 | if (lhsWords < rhsWords || LHS.ult(RHS)) { |
1823 | Remainder = LHS; |
1824 | Quotient = APInt(BitWidth, 0); |
1825 | return; |
1826 | } |
1827 | |
1828 | if (LHS == RHS) { |
1829 | Quotient = APInt(BitWidth, 1); |
1830 | Remainder = APInt(BitWidth, 0); |
1831 | return; |
1832 | } |
1833 | |
1834 | |
1835 | |
1836 | |
1837 | |
1838 | Quotient.reallocate(BitWidth); |
1839 | Remainder.reallocate(BitWidth); |
1840 | |
1841 | if (lhsWords == 1) { |
1842 | |
1843 | uint64_t lhsValue = LHS.U.pVal[0]; |
1844 | uint64_t rhsValue = RHS.U.pVal[0]; |
1845 | Quotient = lhsValue / rhsValue; |
1846 | Remainder = lhsValue % rhsValue; |
1847 | return; |
1848 | } |
1849 | |
1850 | |
1851 | divide(LHS.U.pVal, lhsWords, RHS.U.pVal, rhsWords, Quotient.U.pVal, |
1852 | Remainder.U.pVal); |
1853 | |
1854 | std::memset(Quotient.U.pVal + lhsWords, 0, |
1855 | (getNumWords(BitWidth) - lhsWords) * APINT_WORD_SIZE); |
1856 | std::memset(Remainder.U.pVal + rhsWords, 0, |
1857 | (getNumWords(BitWidth) - rhsWords) * APINT_WORD_SIZE); |
1858 | } |
1859 | |
1860 | void APInt::udivrem(const APInt &LHS, uint64_t RHS, APInt &Quotient, |
1861 | uint64_t &Remainder) { |
1862 | assert(RHS != 0 && "Divide by zero?"); |
1863 | unsigned BitWidth = LHS.BitWidth; |
1864 | |
1865 | |
1866 | if (LHS.isSingleWord()) { |
| |
| |
1867 | uint64_t QuotVal = LHS.U.VAL / RHS; |
1868 | Remainder = LHS.U.VAL % RHS; |
1869 | Quotient = APInt(BitWidth, QuotVal); |
1870 | return; |
1871 | } |
1872 | |
1873 | |
1874 | unsigned lhsWords = getNumWords(LHS.getActiveBits()); |
1875 | |
1876 | |
1877 | if (lhsWords == 0) { |
| 19 | | Assuming 'lhsWords' is equal to 0 | |
|
| |
| 29 | | Assuming 'lhsWords' is not equal to 0 | |
|
| |
1878 | Quotient = APInt(BitWidth, 0); |
| 21 | | Calling move assignment operator for 'APInt' | |
|
| 24 | | Returning; memory was released | |
|
1879 | Remainder = 0; |
1880 | return; |
1881 | } |
1882 | |
1883 | if (RHS == 1) { |
| |
1884 | Quotient = LHS; |
1885 | Remainder = 0; |
1886 | return; |
1887 | } |
1888 | |
1889 | if (LHS.ult(RHS)) { |
| |
1890 | Remainder = LHS.getZExtValue(); |
1891 | Quotient = APInt(BitWidth, 0); |
1892 | return; |
1893 | } |
1894 | |
1895 | if (LHS == RHS) { |
| |
1896 | Quotient = APInt(BitWidth, 1); |
1897 | Remainder = 0; |
1898 | return; |
1899 | } |
1900 | |
1901 | |
1902 | |
1903 | |
1904 | Quotient.reallocate(BitWidth); |
1905 | |
1906 | if (lhsWords == 1) { |
| 34 | | Assuming 'lhsWords' is equal to 1 | |
|
| |
1907 | |
1908 | uint64_t lhsValue = LHS.U.pVal[0]; |
| 36 | | Use of memory after it is freed |
|
1909 | Quotient = lhsValue / RHS; |
1910 | Remainder = lhsValue % RHS; |
1911 | return; |
1912 | } |
1913 | |
1914 | |
1915 | divide(LHS.U.pVal, lhsWords, &RHS, 1, Quotient.U.pVal, &Remainder); |
1916 | |
1917 | std::memset(Quotient.U.pVal + lhsWords, 0, |
1918 | (getNumWords(BitWidth) - lhsWords) * APINT_WORD_SIZE); |
1919 | } |
1920 | |
1921 | void APInt::sdivrem(const APInt &LHS, const APInt &RHS, |
1922 | APInt &Quotient, APInt &Remainder) { |
1923 | if (LHS.isNegative()) { |
1924 | if (RHS.isNegative()) |
1925 | APInt::udivrem(-LHS, -RHS, Quotient, Remainder); |
1926 | else { |
1927 | APInt::udivrem(-LHS, RHS, Quotient, Remainder); |
1928 | Quotient.negate(); |
1929 | } |
1930 | Remainder.negate(); |
1931 | } else if (RHS.isNegative()) { |
1932 | APInt::udivrem(LHS, -RHS, Quotient, Remainder); |
1933 | Quotient.negate(); |
1934 | } else { |
1935 | APInt::udivrem(LHS, RHS, Quotient, Remainder); |
1936 | } |
1937 | } |
1938 | |
1939 | void APInt::sdivrem(const APInt &LHS, int64_t RHS, |
1940 | APInt &Quotient, int64_t &Remainder) { |
1941 | uint64_t R = Remainder; |
1942 | if (LHS.isNegative()) { |
1943 | if (RHS < 0) |
1944 | APInt::udivrem(-LHS, -RHS, Quotient, R); |
1945 | else { |
1946 | APInt::udivrem(-LHS, RHS, Quotient, R); |
1947 | Quotient.negate(); |
1948 | } |
1949 | R = -R; |
1950 | } else if (RHS < 0) { |
1951 | APInt::udivrem(LHS, -RHS, Quotient, R); |
1952 | Quotient.negate(); |
1953 | } else { |
1954 | APInt::udivrem(LHS, RHS, Quotient, R); |
1955 | } |
1956 | Remainder = R; |
1957 | } |
1958 | |
1959 | APInt APInt::sadd_ov(const APInt &RHS, bool &Overflow) const { |
1960 | APInt Res = *this+RHS; |
1961 | Overflow = isNonNegative() == RHS.isNonNegative() && |
1962 | Res.isNonNegative() != isNonNegative(); |
1963 | return Res; |
1964 | } |
1965 | |
1966 | APInt APInt::uadd_ov(const APInt &RHS, bool &Overflow) const { |
1967 | APInt Res = *this+RHS; |
1968 | Overflow = Res.ult(RHS); |
1969 | return Res; |
1970 | } |
1971 | |
1972 | APInt APInt::ssub_ov(const APInt &RHS, bool &Overflow) const { |
1973 | APInt Res = *this - RHS; |
1974 | Overflow = isNonNegative() != RHS.isNonNegative() && |
1975 | Res.isNonNegative() != isNonNegative(); |
1976 | return Res; |
1977 | } |
1978 | |
1979 | APInt APInt::usub_ov(const APInt &RHS, bool &Overflow) const { |
1980 | APInt Res = *this-RHS; |
1981 | Overflow = Res.ugt(*this); |
1982 | return Res; |
1983 | } |
1984 | |
1985 | APInt APInt::sdiv_ov(const APInt &RHS, bool &Overflow) const { |
1986 | |
1987 | Overflow = isMinSignedValue() && RHS.isAllOnesValue(); |
1988 | return sdiv(RHS); |
1989 | } |
1990 | |
1991 | APInt APInt::smul_ov(const APInt &RHS, bool &Overflow) const { |
1992 | APInt Res = *this * RHS; |
1993 | |
1994 | if (*this != 0 && RHS != 0) |
1995 | Overflow = Res.sdiv(RHS) != *this || Res.sdiv(*this) != RHS; |
1996 | else |
1997 | Overflow = false; |
1998 | return Res; |
1999 | } |
2000 | |
2001 | APInt APInt::umul_ov(const APInt &RHS, bool &Overflow) const { |
2002 | if (countLeadingZeros() + RHS.countLeadingZeros() + 2 <= BitWidth) { |
2003 | Overflow = true; |
2004 | return *this * RHS; |
2005 | } |
2006 | |
2007 | APInt Res = lshr(1) * RHS; |
2008 | Overflow = Res.isNegative(); |
2009 | Res <<= 1; |
2010 | if ((*this)[0]) { |
2011 | Res += RHS; |
2012 | if (Res.ult(RHS)) |
2013 | Overflow = true; |
2014 | } |
2015 | return Res; |
2016 | } |
2017 | |
2018 | APInt APInt::sshl_ov(const APInt &ShAmt, bool &Overflow) const { |
2019 | Overflow = ShAmt.uge(getBitWidth()); |
2020 | if (Overflow) |
2021 | return APInt(BitWidth, 0); |
2022 | |
2023 | if (isNonNegative()) |
2024 | Overflow = ShAmt.uge(countLeadingZeros()); |
2025 | else |
2026 | Overflow = ShAmt.uge(countLeadingOnes()); |
2027 | |
2028 | return *this << ShAmt; |
2029 | } |
2030 | |
2031 | APInt APInt::ushl_ov(const APInt &ShAmt, bool &Overflow) const { |
2032 | Overflow = ShAmt.uge(getBitWidth()); |
2033 | if (Overflow) |
2034 | return APInt(BitWidth, 0); |
2035 | |
2036 | Overflow = ShAmt.ugt(countLeadingZeros()); |
2037 | |
2038 | return *this << ShAmt; |
2039 | } |
2040 | |
2041 | APInt APInt::sadd_sat(const APInt &RHS) const { |
2042 | bool Overflow; |
2043 | APInt Res = sadd_ov(RHS, Overflow); |
2044 | if (!Overflow) |
2045 | return Res; |
2046 | |
2047 | return isNegative() ? APInt::getSignedMinValue(BitWidth) |
2048 | : APInt::getSignedMaxValue(BitWidth); |
2049 | } |
2050 | |
2051 | APInt APInt::uadd_sat(const APInt &RHS) const { |
2052 | bool Overflow; |
2053 | APInt Res = uadd_ov(RHS, Overflow); |
2054 | if (!Overflow) |
2055 | return Res; |
2056 | |
2057 | return APInt::getMaxValue(BitWidth); |
2058 | } |
2059 | |
2060 | APInt APInt::ssub_sat(const APInt &RHS) const { |
2061 | bool Overflow; |
2062 | APInt Res = ssub_ov(RHS, Overflow); |
2063 | if (!Overflow) |
2064 | return Res; |
2065 | |
2066 | return isNegative() ? APInt::getSignedMinValue(BitWidth) |
2067 | : APInt::getSignedMaxValue(BitWidth); |
2068 | } |
2069 | |
2070 | APInt APInt::usub_sat(const APInt &RHS) const { |
2071 | bool Overflow; |
2072 | APInt Res = usub_ov(RHS, Overflow); |
2073 | if (!Overflow) |
2074 | return Res; |
2075 | |
2076 | return APInt(BitWidth, 0); |
2077 | } |
2078 | |
2079 | APInt APInt::smul_sat(const APInt &RHS) const { |
2080 | bool Overflow; |
2081 | APInt Res = smul_ov(RHS, Overflow); |
2082 | if (!Overflow) |
2083 | return Res; |
2084 | |
2085 | |
2086 | bool ResIsNegative = isNegative() ^ RHS.isNegative(); |
2087 | |
2088 | return ResIsNegative ? APInt::getSignedMinValue(BitWidth) |
2089 | : APInt::getSignedMaxValue(BitWidth); |
2090 | } |
2091 | |
2092 | APInt APInt::umul_sat(const APInt &RHS) const { |
2093 | bool Overflow; |
2094 | APInt Res = umul_ov(RHS, Overflow); |
2095 | if (!Overflow) |
2096 | return Res; |
2097 | |
2098 | return APInt::getMaxValue(BitWidth); |
2099 | } |
2100 | |
2101 | APInt APInt::sshl_sat(const APInt &RHS) const { |
2102 | bool Overflow; |
2103 | APInt Res = sshl_ov(RHS, Overflow); |
2104 | if (!Overflow) |
2105 | return Res; |
2106 | |
2107 | return isNegative() ? APInt::getSignedMinValue(BitWidth) |
2108 | : APInt::getSignedMaxValue(BitWidth); |
2109 | } |
2110 | |
2111 | APInt APInt::ushl_sat(const APInt &RHS) const { |
2112 | bool Overflow; |
2113 | APInt Res = ushl_ov(RHS, Overflow); |
2114 | if (!Overflow) |
2115 | return Res; |
2116 | |
2117 | return APInt::getMaxValue(BitWidth); |
2118 | } |
2119 | |
2120 | void APInt::fromString(unsigned numbits, StringRef str, uint8_t radix) { |
2121 | |
2122 | assert(!str.empty() && "Invalid string length"); |
2123 | assert((radix == 10 || radix == 8 || radix == 16 || radix == 2 || |
2124 | radix == 36) && |
2125 | "Radix should be 2, 8, 10, 16, or 36!"); |
2126 | |
2127 | StringRef::iterator p = str.begin(); |
2128 | size_t slen = str.size(); |
2129 | bool isNeg = *p == '-'; |
2130 | if (*p == '-' || *p == '+') { |
2131 | p++; |
2132 | slen--; |
2133 | assert(slen && "String is only a sign, needs a value."); |
2134 | } |
2135 | assert((slen <= numbits || radix != 2) && "Insufficient bit width"); |
2136 | assert(((slen-1)*3 <= numbits || radix != 8) && "Insufficient bit width"); |
2137 | assert(((slen-1)*4 <= numbits || radix != 16) && "Insufficient bit width"); |
2138 | assert((((slen-1)*64)/22 <= numbits || radix != 10) && |
2139 | "Insufficient bit width"); |
2140 | |
2141 | |
2142 | if (isSingleWord()) |
2143 | U.VAL = 0; |
2144 | else |
2145 | U.pVal = getClearedMemory(getNumWords()); |
2146 | |
2147 | |
2148 | unsigned shift = (radix == 16 ? 4 : radix == 8 ? 3 : radix == 2 ? 1 : 0); |
2149 | |
2150 | |
2151 | for (StringRef::iterator e = str.end(); p != e; ++p) { |
2152 | unsigned digit = getDigit(*p, radix); |
2153 | assert(digit < radix && "Invalid character in digit string"); |
2154 | |
2155 | |
2156 | if (slen > 1) { |
2157 | if (shift) |
2158 | *this <<= shift; |
2159 | else |
2160 | *this *= radix; |
2161 | } |
2162 | |
2163 | |
2164 | *this += digit; |
2165 | } |
2166 | |
2167 | if (isNeg) |
2168 | this->negate(); |
2169 | } |
2170 | |
2171 | void APInt::toString(SmallVectorImpl<char> &Str, unsigned Radix, |
2172 | bool Signed, bool formatAsCLiteral) const { |
2173 | assert((Radix == 10 || Radix == 8 || Radix == 16 || Radix == 2 || |
2174 | Radix == 36) && |
2175 | "Radix should be 2, 8, 10, 16, or 36!"); |
2176 | |
2177 | const char *Prefix = ""; |
2178 | if (formatAsCLiteral) { |
| |
2179 | switch (Radix) { |
2180 | case 2: |
2181 | |
2182 | |
2183 | Prefix = "0b"; |
2184 | break; |
2185 | case 8: |
2186 | Prefix = "0"; |
2187 | break; |
2188 | case 10: |
2189 | break; |
2190 | case 16: |
2191 | Prefix = "0x"; |
2192 | break; |
2193 | default: |
2194 | llvm_unreachable("Invalid radix!"); |
2195 | } |
2196 | } |
2197 | |
2198 | |
2199 | if (*this == 0) { |
| |
2200 | while (*Prefix) { |
2201 | Str.push_back(*Prefix); |
2202 | ++Prefix; |
2203 | }; |
2204 | Str.push_back('0'); |
2205 | return; |
2206 | } |
2207 | |
2208 | static const char Digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
2209 | |
2210 | if (isSingleWord()) { |
| |
2211 | char Buffer[65]; |
2212 | char *BufPtr = std::end(Buffer); |
2213 | |
2214 | uint64_t N; |
2215 | if (!Signed) { |
2216 | N = getZExtValue(); |
2217 | } else { |
2218 | int64_t I = getSExtValue(); |
2219 | if (I >= 0) { |
2220 | N = I; |
2221 | } else { |
2222 | Str.push_back('-'); |
2223 | N = -(uint64_t)I; |
2224 | } |
2225 | } |
2226 | |
2227 | while (*Prefix) { |
2228 | Str.push_back(*Prefix); |
2229 | ++Prefix; |
2230 | }; |
2231 | |
2232 | while (N) { |
2233 | *--BufPtr = Digits[N % Radix]; |
2234 | N /= Radix; |
2235 | } |
2236 | Str.append(BufPtr, std::end(Buffer)); |
2237 | return; |
2238 | } |
2239 | |
2240 | APInt Tmp(*this); |
| 5 | | Calling copy constructor for 'APInt' | |
|
| 12 | | Returning from copy constructor for 'APInt' | |
|
2241 | |
2242 | if (Signed && isNegative()) { |
| 13 | | Assuming 'Signed' is false | |
|
2243 | |
2244 | |
2245 | |
2246 | Tmp.negate(); |
2247 | Str.push_back('-'); |
2248 | } |
2249 | |
2250 | while (*Prefix) { |
| 14 | | Loop condition is false. Execution continues on line 2256 | |
|
2251 | Str.push_back(*Prefix); |
2252 | ++Prefix; |
2253 | }; |
2254 | |
2255 | |
2256 | unsigned StartDig = Str.size(); |
2257 | |
2258 | |
2259 | |
2260 | |
2261 | if (Radix == 2 || Radix == 8 || Radix == 16) { |
| |
2262 | |
2263 | unsigned ShiftAmt = (Radix == 16 ? 4 : (Radix == 8 ? 3 : 1)); |
2264 | unsigned MaskAmt = Radix - 1; |
2265 | |
2266 | while (Tmp.getBoolValue()) { |
2267 | unsigned Digit = unsigned(Tmp.getRawData()[0]) & MaskAmt; |
2268 | Str.push_back(Digits[Digit]); |
2269 | Tmp.lshrInPlace(ShiftAmt); |
2270 | } |
2271 | } else { |
2272 | while (Tmp.getBoolValue()) { |
| 16 | | Loop condition is true. Entering loop body | |
|
| 26 | | Loop condition is true. Entering loop body | |
|
2273 | uint64_t Digit; |
2274 | udivrem(Tmp, Radix, Tmp, Digit); |
| 17 | | Calling 'APInt::udivrem' | |
|
| 25 | | Returning; memory was released | |
|
| 27 | | Calling 'APInt::udivrem' | |
|
2275 | assert(Digit < Radix && "divide failed"); |
2276 | Str.push_back(Digits[Digit]); |
2277 | } |
2278 | } |
2279 | |
2280 | |
2281 | std::reverse(Str.begin()+StartDig, Str.end()); |
2282 | } |
2283 | |
2284 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
2285 | LLVM_DUMP_METHOD void APInt::dump() const { |
2286 | SmallString<40> S, U; |
2287 | this->toStringUnsigned(U); |
2288 | this->toStringSigned(S); |
2289 | dbgs() << "APInt(" << BitWidth << "b, " |
2290 | << U << "u " << S << "s)\n"; |
2291 | } |
2292 | #endif |
2293 | |
2294 | void APInt::print(raw_ostream &OS, bool isSigned) const { |
2295 | SmallString<40> S; |
2296 | this->toString(S, 10, isSigned, false); |
| 1 | Calling 'APInt::toString' | |
|
2297 | OS << S; |
2298 | } |
2299 | |
2300 | |
2301 | |
2302 | |
2303 | |
2304 | |
2305 | static_assert(APInt::APINT_BITS_PER_WORD % 2 == 0, |
2306 | "Part width must be divisible by 2!"); |
2307 | |
2308 | |
2309 | |
2310 | |
2311 | |
2312 | static inline APInt::WordType lowBitMask(unsigned bits) { |
2313 | assert(bits != 0 && bits <= APInt::APINT_BITS_PER_WORD); |
2314 | |
2315 | return ~(APInt::WordType) 0 >> (APInt::APINT_BITS_PER_WORD - bits); |
2316 | } |
2317 | |
2318 | |
2319 | static inline APInt::WordType lowHalf(APInt::WordType part) { |
2320 | return part & lowBitMask(APInt::APINT_BITS_PER_WORD / 2); |
2321 | } |
2322 | |
2323 | |
2324 | static inline APInt::WordType highHalf(APInt::WordType part) { |
2325 | return part >> (APInt::APINT_BITS_PER_WORD / 2); |
2326 | } |
2327 | |
2328 | |
2329 | |
2330 | static unsigned partMSB(APInt::WordType value) { |
2331 | return findLastSet(value, ZB_Max); |
2332 | } |
2333 | |
2334 | |
2335 | |
2336 | static unsigned partLSB(APInt::WordType value) { |
2337 | return findFirstSet(value, ZB_Max); |
2338 | } |
2339 | |
2340 | |
2341 | |
2342 | void APInt::tcSet(WordType *dst, WordType part, unsigned parts) { |
2343 | assert(parts > 0); |
2344 | |
2345 | dst[0] = part; |
2346 | for (unsigned i = 1; i < parts; i++) |
2347 | dst[i] = 0; |
2348 | } |
2349 | |
2350 | |
2351 | void APInt::tcAssign(WordType *dst, const WordType *src, unsigned parts) { |
2352 | for (unsigned i = 0; i < parts; i++) |
2353 | dst[i] = src[i]; |
2354 | } |
2355 | |
2356 | |
2357 | bool APInt::tcIsZero(const WordType *src, unsigned parts) { |
2358 | for (unsigned i = 0; i < parts; i++) |
2359 | if (src[i]) |
2360 | return false; |
2361 | |
2362 | return true; |
2363 | } |
2364 | |
2365 | |
2366 | int APInt::tcExtractBit(const WordType *parts, unsigned bit) { |
2367 | return (parts[whichWord(bit)] & maskBit(bit)) != 0; |
2368 | } |
2369 | |
2370 | |
2371 | void APInt::tcSetBit(WordType *parts, unsigned bit) { |
2372 | parts[whichWord(bit)] |= maskBit(bit); |
2373 | } |
2374 | |
2375 | |
2376 | void APInt::tcClearBit(WordType *parts, unsigned bit) { |
2377 | parts[whichWord(bit)] &= ~maskBit(bit); |
2378 | } |
2379 | |
2380 | |
2381 | |
2382 | unsigned APInt::tcLSB(const WordType *parts, unsigned n) { |
2383 | for (unsigned i = 0; i < n; i++) { |
2384 | if (parts[i] != 0) { |
2385 | unsigned lsb = partLSB(parts[i]); |
2386 | |
2387 | return lsb + i * APINT_BITS_PER_WORD; |
2388 | } |
2389 | } |
2390 | |
2391 | return -1U; |
2392 | } |
2393 | |
2394 | |
2395 | |
2396 | unsigned APInt::tcMSB(const WordType *parts, unsigned n) { |
2397 | do { |
2398 | --n; |
2399 | |
2400 | if (parts[n] != 0) { |
2401 | unsigned msb = partMSB(parts[n]); |
2402 | |
2403 | return msb + n * APINT_BITS_PER_WORD; |
2404 | } |
2405 | } while (n); |
2406 | |
2407 | return -1U; |
2408 | } |
2409 | |
2410 | |
2411 | |
2412 | |
2413 | |
2414 | void |
2415 | APInt::tcExtract(WordType *dst, unsigned dstCount, const WordType *src, |
2416 | unsigned srcBits, unsigned srcLSB) { |
2417 | unsigned dstParts = (srcBits + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD; |
2418 | assert(dstParts <= dstCount); |
2419 | |
2420 | unsigned firstSrcPart = srcLSB / APINT_BITS_PER_WORD; |
2421 | tcAssign (dst, src + firstSrcPart, dstParts); |
2422 | |
2423 | unsigned shift = srcLSB % APINT_BITS_PER_WORD; |
2424 | tcShiftRight (dst, dstParts, shift); |
2425 | |
2426 | |
2427 | |
2428 | |
2429 | unsigned n = dstParts * APINT_BITS_PER_WORD - shift; |
2430 | if (n < srcBits) { |
2431 | WordType mask = lowBitMask (srcBits - n); |
2432 | dst[dstParts - 1] |= ((src[firstSrcPart + dstParts] & mask) |
2433 | << n % APINT_BITS_PER_WORD); |
2434 | } else if (n > srcBits) { |
2435 | if (srcBits % APINT_BITS_PER_WORD) |
2436 | dst[dstParts - 1] &= lowBitMask (srcBits % APINT_BITS_PER_WORD); |
2437 | } |
2438 | |
2439 | |
2440 | while (dstParts < dstCount) |
2441 | dst[dstParts++] = 0; |
2442 | } |
2443 | |
2444 | |
2445 | APInt::WordType APInt::tcAdd(WordType *dst, const WordType *rhs, |
2446 | WordType c, unsigned parts) { |
2447 | assert(c <= 1); |
2448 | |
2449 | for (unsigned i = 0; i < parts; i++) { |
2450 | WordType l = dst[i]; |
2451 | if (c) { |
2452 | dst[i] += rhs[i] + 1; |
2453 | c = (dst[i] <= l); |
2454 | } else { |
2455 | dst[i] += rhs[i]; |
2456 | c = (dst[i] < l); |
2457 | } |
2458 | } |
2459 | |
2460 | return c; |
2461 | } |
2462 | |
2463 | |
2464 | |
2465 | |
2466 | |
2467 | APInt::WordType APInt::tcAddPart(WordType *dst, WordType src, |
2468 | unsigned parts) { |
2469 | for (unsigned i = 0; i < parts; ++i) { |
2470 | dst[i] += src; |
2471 | if (dst[i] >= src) |
2472 | return 0; |
2473 | src = 1; |
2474 | } |
2475 | |
2476 | return 1; |
2477 | } |
2478 | |
2479 | |
2480 | APInt::WordType APInt::tcSubtract(WordType *dst, const WordType *rhs, |
2481 | WordType c, unsigned parts) { |
2482 | assert(c <= 1); |
2483 | |
2484 | for (unsigned i = 0; i < parts; i++) { |
2485 | WordType l = dst[i]; |
2486 | if (c) { |
2487 | dst[i] -= rhs[i] + 1; |
2488 | c = (dst[i] >= l); |
2489 | } else { |
2490 | dst[i] -= rhs[i]; |
2491 | c = (dst[i] > l); |
2492 | } |
2493 | } |
2494 | |
2495 | return c; |
2496 | } |
2497 | |
2498 | |
2499 | |
2500 | |
2501 | |
2502 | |
2503 | |
2504 | |
2505 | APInt::WordType APInt::tcSubtractPart(WordType *dst, WordType src, |
2506 | unsigned parts) { |
2507 | for (unsigned i = 0; i < parts; ++i) { |
2508 | WordType Dst = dst[i]; |
2509 | dst[i] -= src; |
2510 | if (src <= Dst) |
2511 | return 0; |
2512 | src = 1; |
2513 | } |
2514 | |
2515 | return 1; |
2516 | } |
2517 | |
2518 | |
2519 | void APInt::tcNegate(WordType *dst, unsigned parts) { |
2520 | tcComplement(dst, parts); |
2521 | tcIncrement(dst, parts); |
2522 | } |
2523 | |
2524 | |
2525 | |
2526 | |
2527 | |
2528 | |
2529 | |
2530 | |
2531 | |
2532 | |
2533 | |
2534 | |
2535 | int APInt::tcMultiplyPart(WordType *dst, const WordType *src, |
2536 | WordType multiplier, WordType carry, |
2537 | unsigned srcParts, unsigned dstParts, |
2538 | bool add) { |
2539 | |
2540 | assert(dst <= src || dst >= src + srcParts); |
2541 | assert(dstParts <= srcParts + 1); |
2542 | |
2543 | |
2544 | unsigned n = std::min(dstParts, srcParts); |
2545 | |
2546 | for (unsigned i = 0; i < n; i++) { |
2547 | WordType low, mid, high, srcPart; |
2548 | |
2549 | |
2550 | |
2551 | |
2552 | |
2553 | |
2554 | |
2555 | |
2556 | |
2557 | srcPart = src[i]; |
2558 | |
2559 | if (multiplier == 0 || srcPart == 0) { |
2560 | low = carry; |
2561 | high = 0; |
2562 | } else { |
2563 | low = lowHalf(srcPart) * lowHalf(multiplier); |
2564 | high = highHalf(srcPart) * highHalf(multiplier); |
2565 | |
2566 | mid = lowHalf(srcPart) * highHalf(multiplier); |
2567 | high += highHalf(mid); |
2568 | mid <<= APINT_BITS_PER_WORD / 2; |
2569 | if (low + mid < low) |
2570 | high++; |
2571 | low += mid; |
2572 | |
2573 | mid = highHalf(srcPart) * lowHalf(multiplier); |
2574 | high += highHalf(mid); |
2575 | mid <<= APINT_BITS_PER_WORD / 2; |
2576 | if (low + mid < low) |
2577 | high++; |
2578 | low += mid; |
2579 | |
2580 | |
2581 | if (low + carry < low) |
2582 | high++; |
2583 | low += carry; |
2584 | } |
2585 | |
2586 | if (add) { |
2587 | |
2588 | if (low + dst[i] < low) |
2589 | high++; |
2590 | dst[i] += low; |
2591 | } else |
2592 | dst[i] = low; |
2593 | |
2594 | carry = high; |
2595 | } |
2596 | |
2597 | if (srcParts < dstParts) { |
2598 | |
2599 | assert(srcParts + 1 == dstParts); |
2600 | dst[srcParts] = carry; |
2601 | return 0; |
2602 | } |
2603 | |
2604 | |
2605 | if (carry) |
2606 | return 1; |
2607 | |
2608 | |
2609 | |
2610 | |
2611 | if (multiplier) |
2612 | for (unsigned i = dstParts; i < srcParts; i++) |
2613 | if (src[i]) |
2614 | return 1; |
2615 | |
2616 | |
2617 | return 0; |
2618 | } |
2619 | |
2620 | |
2621 | |
2622 | |
2623 | |
2624 | int APInt::tcMultiply(WordType *dst, const WordType *lhs, |
2625 | const WordType *rhs, unsigned parts) { |
2626 | assert(dst != lhs && dst != rhs); |
2627 | |
2628 | int overflow = 0; |
2629 | tcSet(dst, 0, parts); |
2630 | |
2631 | for (unsigned i = 0; i < parts; i++) |
2632 | overflow |= tcMultiplyPart(&dst[i], lhs, rhs[i], 0, parts, |
2633 | parts - i, true); |
2634 | |
2635 | return overflow; |
2636 | } |
2637 | |
2638 | |
2639 | |
2640 | void APInt::tcFullMultiply(WordType *dst, const WordType *lhs, |
2641 | const WordType *rhs, unsigned lhsParts, |
2642 | unsigned rhsParts) { |
2643 | |
2644 | if (lhsParts > rhsParts) |
2645 | return tcFullMultiply (dst, rhs, lhs, rhsParts, lhsParts); |
2646 | |
2647 | assert(dst != lhs && dst != rhs); |
2648 | |
2649 | tcSet(dst, 0, rhsParts); |
2650 | |
2651 | for (unsigned i = 0; i < lhsParts; i++) |
2652 | tcMultiplyPart(&dst[i], rhs, lhs[i], 0, rhsParts, rhsParts + 1, true); |
2653 | } |
2654 | |
2655 | |
2656 | |
2657 | |
2658 | |
2659 | |
2660 | |
2661 | |
2662 | |
2663 | |
2664 | |
2665 | int APInt::tcDivide(WordType *lhs, const WordType *rhs, |
2666 | WordType *remainder, WordType *srhs, |
2667 | unsigned parts) { |
2668 | assert(lhs != remainder && lhs != srhs && remainder != srhs); |
2669 | |
2670 | unsigned shiftCount = tcMSB(rhs, parts) + 1; |
2671 | if (shiftCount == 0) |
2672 | return true; |
2673 | |
2674 | shiftCount = parts * APINT_BITS_PER_WORD - shiftCount; |
2675 | unsigned n = shiftCount / APINT_BITS_PER_WORD; |
2676 | WordType mask = (WordType) 1 << (shiftCount % APINT_BITS_PER_WORD); |
2677 | |
2678 | tcAssign(srhs, rhs, parts); |
2679 | tcShiftLeft(srhs, parts, shiftCount); |
2680 | tcAssign(remainder, lhs, parts); |
2681 | tcSet(lhs, 0, parts); |
2682 | |
2683 | |
2684 | |
2685 | for (;;) { |
2686 | int compare = tcCompare(remainder, srhs, parts); |
2687 | if (compare >= 0) { |
2688 | tcSubtract(remainder, srhs, 0, parts); |
2689 | lhs[n] |= mask; |
2690 | } |
2691 | |
2692 | if (shiftCount == 0) |
2693 | break; |
2694 | shiftCount--; |
2695 | tcShiftRight(srhs, parts, 1); |
2696 | if ((mask >>= 1) == 0) { |
2697 | mask = (WordType) 1 << (APINT_BITS_PER_WORD - 1); |
2698 | n--; |
2699 | } |
2700 | } |
2701 | |
2702 | return false; |
2703 | } |
2704 | |
2705 | |
2706 | |
2707 | void APInt::tcShiftLeft(WordType *Dst, unsigned Words, unsigned Count) { |
2708 | |
2709 | if (!Count) |
2710 | return; |
2711 | |
2712 | |
2713 | unsigned WordShift = std::min(Count / APINT_BITS_PER_WORD, Words); |
2714 | unsigned BitShift = Count % APINT_BITS_PER_WORD; |
2715 | |
2716 | |
2717 | if (BitShift == 0) { |
2718 | std::memmove(Dst + WordShift, Dst, (Words - WordShift) * APINT_WORD_SIZE); |
2719 | } else { |
2720 | while (Words-- > WordShift) { |
2721 | Dst[Words] = Dst[Words - WordShift] << BitShift; |
2722 | if (Words > WordShift) |
2723 | Dst[Words] |= |
2724 | Dst[Words - WordShift - 1] >> (APINT_BITS_PER_WORD - BitShift); |
2725 | } |
2726 | } |
2727 | |
2728 | |
2729 | std::memset(Dst, 0, WordShift * APINT_WORD_SIZE); |
2730 | } |
2731 | |
2732 | |
2733 | |
2734 | void APInt::tcShiftRight(WordType *Dst, unsigned Words, unsigned Count) { |
2735 | |
2736 | if (!Count) |
2737 | return; |
2738 | |
2739 | |
2740 | unsigned WordShift = std::min(Count / APINT_BITS_PER_WORD, Words); |
2741 | unsigned BitShift = Count % APINT_BITS_PER_WORD; |
2742 | |
2743 | unsigned WordsToMove = Words - WordShift; |
2744 | |
2745 | if (BitShift == 0) { |
2746 | std::memmove(Dst, Dst + WordShift, WordsToMove * APINT_WORD_SIZE); |
2747 | } else { |
2748 | for (unsigned i = 0; i != WordsToMove; ++i) { |
2749 | Dst[i] = Dst[i + WordShift] >> BitShift; |
2750 | if (i + 1 != WordsToMove) |
2751 | Dst[i] |= Dst[i + WordShift + 1] << (APINT_BITS_PER_WORD - BitShift); |
2752 | } |
2753 | } |
2754 | |
2755 | |
2756 | std::memset(Dst + WordsToMove, 0, WordShift * APINT_WORD_SIZE); |
2757 | } |
2758 | |
2759 | |
2760 | void APInt::tcAnd(WordType *dst, const WordType *rhs, unsigned parts) { |
2761 | for (unsigned i = 0; i < parts; i++) |
2762 | dst[i] &= rhs[i]; |
2763 | } |
2764 | |
2765 | |
2766 | void APInt::tcOr(WordType *dst, const WordType *rhs, unsigned parts) { |
2767 | for (unsigned i = 0; i < parts; i++) |
2768 | dst[i] |= rhs[i]; |
2769 | } |
2770 | |
2771 | |
2772 | void APInt::tcXor(WordType *dst, const WordType *rhs, unsigned parts) { |
2773 | for (unsigned i = 0; i < parts; i++) |
2774 | dst[i] ^= rhs[i]; |
2775 | } |
2776 | |
2777 | |
2778 | void APInt::tcComplement(WordType *dst, unsigned parts) { |
2779 | for (unsigned i = 0; i < parts; i++) |
2780 | dst[i] = ~dst[i]; |
2781 | } |
2782 | |
2783 | |
2784 | int APInt::tcCompare(const WordType *lhs, const WordType *rhs, |
2785 | unsigned parts) { |
2786 | while (parts) { |
2787 | parts--; |
2788 | if (lhs[parts] != rhs[parts]) |
2789 | return (lhs[parts] > rhs[parts]) ? 1 : -1; |
2790 | } |
2791 | |
2792 | return 0; |
2793 | } |
2794 | |
2795 | |
2796 | |
2797 | void APInt::tcSetLeastSignificantBits(WordType *dst, unsigned parts, |
2798 | unsigned bits) { |
2799 | unsigned i = 0; |
2800 | while (bits > APINT_BITS_PER_WORD) { |
2801 | dst[i++] = ~(WordType) 0; |
2802 | bits -= APINT_BITS_PER_WORD; |
2803 | } |
2804 | |
2805 | if (bits) |
2806 | dst[i++] = ~(WordType) 0 >> (APINT_BITS_PER_WORD - bits); |
2807 | |
2808 | while (i < parts) |
2809 | dst[i++] = 0; |
2810 | } |
2811 | |
2812 | APInt llvm::APIntOps::RoundingUDiv(const APInt &A, const APInt &B, |
2813 | APInt::Rounding RM) { |
2814 | |
2815 | switch (RM) { |
2816 | case APInt::Rounding::DOWN: |
2817 | case APInt::Rounding::TOWARD_ZERO: |
2818 | return A.udiv(B); |
2819 | case APInt::Rounding::UP: { |
2820 | APInt Quo, Rem; |
2821 | APInt::udivrem(A, B, Quo, Rem); |
2822 | if (Rem == 0) |
2823 | return Quo; |
2824 | return Quo + 1; |
2825 | } |
2826 | } |
2827 | llvm_unreachable("Unknown APInt::Rounding enum"); |
2828 | } |
2829 | |
2830 | APInt llvm::APIntOps::RoundingSDiv(const APInt &A, const APInt &B, |
2831 | APInt::Rounding RM) { |
2832 | switch (RM) { |
2833 | case APInt::Rounding::DOWN: |
2834 | case APInt::Rounding::UP: { |
2835 | APInt Quo, Rem; |
2836 | APInt::sdivrem(A, B, Quo, Rem); |
2837 | if (Rem == 0) |
2838 | return Quo; |
2839 | |
2840 | |
2841 | |
2842 | |
2843 | |
2844 | if (RM == APInt::Rounding::DOWN) { |
2845 | if (Rem.isNegative() != B.isNegative()) |
2846 | return Quo - 1; |
2847 | return Quo; |
2848 | } |
2849 | if (Rem.isNegative() != B.isNegative()) |
2850 | return Quo; |
2851 | return Quo + 1; |
2852 | } |
2853 | |
2854 | case APInt::Rounding::TOWARD_ZERO: |
2855 | return A.sdiv(B); |
2856 | } |
2857 | llvm_unreachable("Unknown APInt::Rounding enum"); |
2858 | } |
2859 | |
2860 | Optional<APInt> |
2861 | llvm::APIntOps::SolveQuadraticEquationWrap(APInt A, APInt B, APInt C, |
2862 | unsigned RangeWidth) { |
2863 | unsigned CoeffWidth = A.getBitWidth(); |
2864 | assert(CoeffWidth == B.getBitWidth() && CoeffWidth == C.getBitWidth()); |
2865 | assert(RangeWidth <= CoeffWidth && |
2866 | "Value range width should be less than coefficient width"); |
2867 | assert(RangeWidth > 1 && "Value range bit width should be > 1"); |
2868 | |
2869 | LLVM_DEBUG(dbgs() << __func__ << ": solving " << A << "x^2 + " << B |
2870 | << "x + " << C << ", rw:" << RangeWidth << '\n'); |
2871 | |
2872 | |
2873 | if (C.sextOrTrunc(RangeWidth).isNullValue() ) { |
2874 | LLVM_DEBUG(dbgs() << __func__ << ": zero solution\n"); |
2875 | return APInt(CoeffWidth, 0); |
2876 | } |
2877 | |
2878 | |
2879 | |
2880 | |
2881 | |
2882 | |
2883 | |
2884 | |
2885 | |
2886 | |
2887 | |
2888 | |
2889 | |
2890 | |
2891 | |
2892 | CoeffWidth *= 3; |
2893 | A = A.sext(CoeffWidth); |
2894 | B = B.sext(CoeffWidth); |
2895 | C = C.sext(CoeffWidth); |
2896 | |
2897 | |
2898 | |
2899 | if (A.isNegative()) { |
2900 | A.negate(); |
2901 | B.negate(); |
2902 | C.negate(); |
2903 | } |
2904 | |
2905 | |
2906 | |
2907 | |
2908 | |
2909 | |
2910 | |
2911 | |
2912 | |
2913 | |
2914 | |
2915 | |
2916 | |
2917 | |
2918 | |
2919 | |
2920 | |
2921 | |
2922 | |
2923 | |
2924 | |
2925 | |
2926 | |
2927 | APInt R = APInt::getOneBitSet(CoeffWidth, RangeWidth); |
2928 | APInt TwoA = 2 * A; |
2929 | APInt SqrB = B * B; |
2930 | bool PickLow; |
2931 | |
2932 | auto RoundUp = [] (const APInt &V, const APInt &A) -> APInt { |
2933 | assert(A.isStrictlyPositive()); |
2934 | APInt T = V.abs().urem(A); |
2935 | if (T.isNullValue()) |
2936 | return V; |
2937 | return V.isNegative() ? V+T : V+(A-T); |
2938 | }; |
2939 | |
2940 | |
2941 | |
2942 | if (B.isNonNegative()) { |
2943 | |
2944 | |
2945 | |
2946 | |
2947 | C = C.srem(R); |
2948 | if (C.isStrictlyPositive()) |
2949 | C -= R; |
2950 | |
2951 | PickLow = false; |
2952 | } else { |
2953 | |
2954 | |
2955 | |
2956 | |
2957 | APInt LowkR = C - SqrB.udiv(2*TwoA); |
2958 | |
2959 | LowkR = RoundUp(LowkR, R); |
2960 | |
2961 | |
2962 | |
2963 | |
2964 | |
2965 | |
2966 | if (C.sgt(LowkR)) { |
2967 | |
2968 | |
2969 | C -= -RoundUp(-C, R); |
2970 | |
2971 | PickLow = true; |
2972 | } else { |
2973 | |
2974 | |
2975 | |
2976 | |
2977 | |
2978 | |
2979 | |
2980 | C -= LowkR; |
2981 | |
2982 | PickLow = false; |
2983 | } |
2984 | } |
2985 | |
2986 | LLVM_DEBUG(dbgs() << __func__ << ": updated coefficients " << A << "x^2 + " |
2987 | << B << "x + " << C << ", rw:" << RangeWidth << '\n'); |
2988 | |
2989 | APInt D = SqrB - 4*A*C; |
2990 | assert(D.isNonNegative() && "Negative discriminant"); |
2991 | APInt SQ = D.sqrt(); |
2992 | |
2993 | APInt Q = SQ * SQ; |
2994 | bool InexactSQ = Q != D; |
2995 | |
2996 | |
2997 | if (Q.sgt(D)) |
2998 | SQ -= 1; |
2999 | |
3000 | APInt X; |
3001 | APInt Rem; |
3002 | |
3003 | |
3004 | |
3005 | |
3006 | |
3007 | |
3008 | |
3009 | if (PickLow) |
3010 | APInt::sdivrem(-B - (SQ+InexactSQ), TwoA, X, Rem); |
3011 | else |
3012 | APInt::sdivrem(-B + SQ, TwoA, X, Rem); |
3013 | |
3014 | |
3015 | |
3016 | |
3017 | assert(X.isNonNegative() && "Solution should be non-negative"); |
3018 | |
3019 | if (!InexactSQ && Rem.isNullValue()) { |
3020 | LLVM_DEBUG(dbgs() << __func__ << ": solution (root): " << X << '\n'); |
3021 | return X; |
3022 | } |
3023 | |
3024 | assert((SQ*SQ).sle(D) && "SQ = |_sqrt(D)_|, so SQ*SQ <= D"); |
3025 | |
3026 | |
3027 | |
3028 | |
3029 | |
3030 | |
3031 | |
3032 | |
3033 | APInt VX = (A*X + B)*X + C; |
3034 | APInt VY = VX + TwoA*X + A + B; |
3035 | bool SignChange = VX.isNegative() != VY.isNegative() || |
3036 | VX.isNullValue() != VY.isNullValue(); |
3037 | |
3038 | |
3039 | |
3040 | if (!SignChange) { |
3041 | LLVM_DEBUG(dbgs() << __func__ << ": no valid solution\n"); |
3042 | return None; |
3043 | } |
3044 | |
3045 | X += 1; |
3046 | LLVM_DEBUG(dbgs() << __func__ << ": solution (wrap): " << X << '\n'); |
3047 | return X; |
3048 | } |
3049 | |
3050 | Optional<unsigned> |
3051 | llvm::APIntOps::GetMostSignificantDifferentBit(const APInt &A, const APInt &B) { |
3052 | assert(A.getBitWidth() == B.getBitWidth() && "Must have the same bitwidth"); |
3053 | if (A == B) |
3054 | return llvm::None; |
3055 | return A.getBitWidth() - ((A ^ B).countLeadingZeros() + 1); |
3056 | } |
3057 | |
3058 | |
3059 | |
3060 | void llvm::StoreIntToMemory(const APInt &IntVal, uint8_t *Dst, |
3061 | unsigned StoreBytes) { |
3062 | assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!"); |
3063 | const uint8_t *Src = (const uint8_t *)IntVal.getRawData(); |
3064 | |
3065 | if (sys::IsLittleEndianHost) { |
3066 | |
3067 | |
3068 | memcpy(Dst, Src, StoreBytes); |
3069 | } else { |
3070 | |
3071 | |
3072 | |
3073 | while (StoreBytes > sizeof(uint64_t)) { |
3074 | StoreBytes -= sizeof(uint64_t); |
3075 | |
3076 | memcpy(Dst + StoreBytes, Src, sizeof(uint64_t)); |
3077 | Src += sizeof(uint64_t); |
3078 | } |
3079 | |
3080 | memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes); |
3081 | } |
3082 | } |
3083 | |
3084 | |
3085 | |
3086 | void llvm::LoadIntFromMemory(APInt &IntVal, const uint8_t *Src, |
3087 | unsigned LoadBytes) { |
3088 | assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!"); |
3089 | uint8_t *Dst = reinterpret_cast<uint8_t *>( |
3090 | const_cast<uint64_t *>(IntVal.getRawData())); |
3091 | |
3092 | if (sys::IsLittleEndianHost) |
3093 | |
3094 | |
3095 | memcpy(Dst, Src, LoadBytes); |
3096 | else { |
3097 | |
3098 | |
3099 | |
3100 | |
3101 | while (LoadBytes > sizeof(uint64_t)) { |
3102 | LoadBytes -= sizeof(uint64_t); |
3103 | |
3104 | memcpy(Dst, Src + LoadBytes, sizeof(uint64_t)); |
3105 | Dst += sizeof(uint64_t); |
3106 | } |
3107 | |
3108 | memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes); |
3109 | } |
3110 | } |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | #ifndef LLVM_ADT_APINT_H |
16 | #define LLVM_ADT_APINT_H |
17 | |
18 | #include "llvm/Support/Compiler.h" |
19 | #include "llvm/Support/MathExtras.h" |
20 | #include <cassert> |
21 | #include <climits> |
22 | #include <cstring> |
23 | #include <utility> |
24 | |
25 | namespace llvm { |
26 | class FoldingSetNodeID; |
27 | class StringRef; |
28 | class hash_code; |
29 | class raw_ostream; |
30 | |
31 | template <typename T> class SmallVectorImpl; |
32 | template <typename T> class ArrayRef; |
33 | template <typename T> class Optional; |
34 | template <typename T> struct DenseMapInfo; |
35 | |
36 | class APInt; |
37 | |
38 | inline APInt operator-(APInt); |
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 | class LLVM_NODISCARD APInt { |
71 | public: |
72 | typedef uint64_t WordType; |
73 | |
74 | |
75 | enum : unsigned { |
76 | |
77 | APINT_WORD_SIZE = sizeof(WordType), |
78 | |
79 | APINT_BITS_PER_WORD = APINT_WORD_SIZE * CHAR_BIT |
80 | }; |
81 | |
82 | enum class Rounding { |
83 | DOWN, |
84 | TOWARD_ZERO, |
85 | UP, |
86 | }; |
87 | |
88 | static constexpr WordType WORDTYPE_MAX = ~WordType(0); |
89 | |
90 | private: |
91 | |
92 | |
93 | union { |
94 | uint64_t VAL; |
95 | uint64_t *pVal; |
96 | } U; |
97 | |
98 | unsigned BitWidth; |
99 | |
100 | friend struct DenseMapInfo<APInt>; |
101 | |
102 | friend class APSInt; |
103 | |
104 | |
105 | |
106 | |
107 | |
108 | APInt(uint64_t *val, unsigned bits) : BitWidth(bits) { |
109 | U.pVal = val; |
110 | } |
111 | |
112 | |
113 | |
114 | |
115 | static unsigned whichWord(unsigned bitPosition) { |
116 | return bitPosition / APINT_BITS_PER_WORD; |
117 | } |
118 | |
119 | |
120 | |
121 | |
122 | |
123 | static unsigned whichBit(unsigned bitPosition) { |
124 | return bitPosition % APINT_BITS_PER_WORD; |
125 | } |
126 | |
127 | |
128 | |
129 | |
130 | |
131 | |
132 | |
133 | static uint64_t maskBit(unsigned bitPosition) { |
134 | return 1ULL << whichBit(bitPosition); |
135 | } |
136 | |
137 | |
138 | |
139 | |
140 | |
141 | |
142 | |
143 | APInt &clearUnusedBits() { |
144 | |
145 | unsigned WordBits = ((BitWidth-1) % APINT_BITS_PER_WORD) + 1; |
146 | |
147 | |
148 | uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - WordBits); |
149 | if (isSingleWord()) |
150 | U.VAL &= mask; |
151 | else |
152 | U.pVal[getNumWords() - 1] &= mask; |
153 | return *this; |
154 | } |
155 | |
156 | |
157 | |
158 | uint64_t getWord(unsigned bitPosition) const { |
159 | return isSingleWord() ? U.VAL : U.pVal[whichWord(bitPosition)]; |
160 | } |
161 | |
162 | |
163 | |
164 | |
165 | void reallocate(unsigned NewBitWidth); |
166 | |
167 | |
168 | |
169 | |
170 | |
171 | |
172 | |
173 | |
174 | |
175 | |
176 | |
177 | |
178 | |
179 | void fromString(unsigned numBits, StringRef str, uint8_t radix); |
180 | |
181 | |
182 | |
183 | |
184 | |
185 | |
186 | |
187 | static void divide(const WordType *LHS, unsigned lhsWords, |
188 | const WordType *RHS, unsigned rhsWords, WordType *Quotient, |
189 | WordType *Remainder); |
190 | |
191 | |
192 | void initSlowCase(uint64_t val, bool isSigned); |
193 | |
194 | |
195 | void initFromArray(ArrayRef<uint64_t> array); |
196 | |
197 | |
198 | void initSlowCase(const APInt &that); |
199 | |
200 | |
201 | void shlSlowCase(unsigned ShiftAmt); |
202 | |
203 | |
204 | void lshrSlowCase(unsigned ShiftAmt); |
205 | |
206 | |
207 | void ashrSlowCase(unsigned ShiftAmt); |
208 | |
209 | |
210 | void AssignSlowCase(const APInt &RHS); |
211 | |
212 | |
213 | bool EqualSlowCase(const APInt &RHS) const LLVM_READONLY; |
214 | |
215 | |
216 | unsigned countLeadingZerosSlowCase() const LLVM_READONLY; |
217 | |
218 | |
219 | unsigned countLeadingOnesSlowCase() const LLVM_READONLY; |
220 | |
221 | |
222 | unsigned countTrailingZerosSlowCase() const LLVM_READONLY; |
223 | |
224 | |
225 | unsigned countTrailingOnesSlowCase() const LLVM_READONLY; |
226 | |
227 | |
228 | unsigned countPopulationSlowCase() const LLVM_READONLY; |
229 | |
230 | |
231 | bool intersectsSlowCase(const APInt &RHS) const LLVM_READONLY; |
232 | |
233 | |
234 | bool isSubsetOfSlowCase(const APInt &RHS) const LLVM_READONLY; |
235 | |
236 | |
237 | void setBitsSlowCase(unsigned loBit, unsigned hiBit); |
238 | |
239 | |
240 | void flipAllBitsSlowCase(); |
241 | |
242 | |
243 | void AndAssignSlowCase(const APInt& RHS); |
244 | |
245 | |
246 | void OrAssignSlowCase(const APInt& RHS); |
247 | |
248 | |
249 | void XorAssignSlowCase(const APInt& RHS); |
250 | |
251 | |
252 | |
253 | int compare(const APInt &RHS) const LLVM_READONLY; |
254 | |
255 | |
256 | |
257 | int compareSigned(const APInt &RHS) const LLVM_READONLY; |
258 | |
259 | public: |
260 | |
261 | |
262 | |
263 | |
264 | |
265 | |
266 | |
267 | |
268 | |
269 | |
270 | |
271 | |
272 | |
273 | APInt(unsigned numBits, uint64_t val, bool isSigned = false) |
274 | : BitWidth(numBits) { |
275 | assert(BitWidth && "bitwidth too small"); |
276 | if (isSingleWord()) { |
277 | U.VAL = val; |
278 | clearUnusedBits(); |
279 | } else { |
280 | initSlowCase(val, isSigned); |
281 | } |
282 | } |
283 | |
284 | |
285 | |
286 | |
287 | |
288 | |
289 | |
290 | |
291 | APInt(unsigned numBits, ArrayRef<uint64_t> bigVal); |
292 | |
293 | |
294 | |
295 | |
296 | |
297 | |
298 | |
299 | |
300 | APInt(unsigned numBits, unsigned numWords, const uint64_t bigVal[]); |
301 | |
302 | |
303 | |
304 | |
305 | |
306 | |
307 | |
308 | |
309 | |
310 | |
311 | |
312 | |
313 | APInt(unsigned numBits, StringRef str, uint8_t radix); |
314 | |
315 | |
316 | |
317 | APInt(const APInt &that) : BitWidth(that.BitWidth) { |
318 | if (isSingleWord()) |
| |
319 | U.VAL = that.U.VAL; |
320 | else |
321 | initSlowCase(that); |
| 7 | | Calling 'APInt::initSlowCase' | |
|
| 11 | | Returned allocated memory | |
|
322 | } |
323 | |
324 | |
325 | APInt(APInt &&that) : BitWidth(that.BitWidth) { |
326 | memcpy(&U, &that.U, sizeof(U)); |
327 | that.BitWidth = 0; |
328 | } |
329 | |
330 | |
331 | ~APInt() { |
332 | if (needsCleanup()) |
333 | delete[] U.pVal; |
334 | } |
335 | |
336 | |
337 | |
338 | |
339 | |
340 | |
341 | explicit APInt() : BitWidth(1) { U.VAL = 0; } |
342 | |
343 | |
344 | bool needsCleanup() const { return !isSingleWord(); } |
345 | |
346 | |
347 | |
348 | void Profile(FoldingSetNodeID &id) const; |
349 | |
350 | |
351 | |
352 | |
353 | |
354 | |
355 | |
356 | |
357 | bool isSingleWord() const { return BitWidth <= APINT_BITS_PER_WORD; } |
358 | |
359 | |
360 | |
361 | |
362 | |
363 | |
364 | bool isNegative() const { return (*this)[BitWidth - 1]; } |
365 | |
366 | |
367 | |
368 | |
369 | bool isNonNegative() const { return !isNegative(); } |
370 | |
371 | |
372 | |
373 | |
374 | |
375 | |
376 | bool isSignBitSet() const { return (*this)[BitWidth-1]; } |
377 | |
378 | |
379 | |
380 | |
381 | |
382 | |
383 | bool isSignBitClear() const { return !isSignBitSet(); } |
384 | |
385 | |
386 | |
387 | |
388 | |
389 | |
390 | |
391 | bool isStrictlyPositive() const { return isNonNegative() && !isNullValue(); } |
392 | |
393 | |
394 | |
395 | |
396 | bool isNonPositive() const { return !isStrictlyPositive(); } |
397 | |
398 | |
399 | |
400 | |
401 | bool isAllOnesValue() const { |
402 | if (isSingleWord()) |
403 | return U.VAL == WORDTYPE_MAX >> (APINT_BITS_PER_WORD - BitWidth); |
404 | return countTrailingOnesSlowCase() == BitWidth; |
405 | } |
406 | |
407 | |
408 | |
409 | |
410 | |
411 | bool isNullValue() const { return !*this; } |
412 | |
413 | |
414 | |
415 | |
416 | bool isOneValue() const { |
417 | if (isSingleWord()) |
418 | return U.VAL == 1; |
419 | return countLeadingZerosSlowCase() == BitWidth - 1; |
420 | } |
421 | |
422 | |
423 | |
424 | |
425 | |
426 | bool isMaxValue() const { return isAllOnesValue(); } |
427 | |
428 | |
429 | |
430 | |
431 | |
432 | bool isMaxSignedValue() const { |
433 | if (isSingleWord()) |
434 | return U.VAL == ((WordType(1) << (BitWidth - 1)) - 1); |
435 | return !isNegative() && countTrailingOnesSlowCase() == BitWidth - 1; |
436 | } |
437 | |
438 | |
439 | |
440 | |
441 | |
442 | bool isMinValue() const { return isNullValue(); } |
443 | |
444 | |
445 | |
446 | |
447 | |
448 | bool isMinSignedValue() const { |
449 | if (isSingleWord()) |
450 | return U.VAL == (WordType(1) << (BitWidth - 1)); |
451 | return isNegative() && countTrailingZerosSlowCase() == BitWidth - 1; |
452 | } |
453 | |
454 | |
455 | bool isIntN(unsigned N) const { |
456 | assert(N && "N == 0 ???"); |
457 | return getActiveBits() <= N; |
458 | } |
459 | |
460 | |
461 | bool isSignedIntN(unsigned N) const { |
462 | assert(N && "N == 0 ???"); |
463 | return getMinSignedBits() <= N; |
464 | } |
465 | |
466 | |
467 | |
468 | |
469 | bool isPowerOf2() const { |
470 | if (isSingleWord()) |
471 | return isPowerOf2_64(U.VAL); |
472 | return countPopulationSlowCase() == 1; |
473 | } |
474 | |
475 | |
476 | |
477 | |
478 | bool isSignMask() const { return isMinSignedValue(); } |
479 | |
480 | |
481 | |
482 | |
483 | bool getBoolValue() const { return !!*this; } |
484 | |
485 | |
486 | |
487 | uint64_t getLimitedValue(uint64_t Limit = UINT64_MAX) const { |
488 | return ugt(Limit) ? Limit : getZExtValue(); |
489 | } |
490 | |
491 | |
492 | |
493 | |
494 | |
495 | |
496 | bool isSplat(unsigned SplatSizeInBits) const; |
497 | |
498 | |
499 | |
500 | bool isMask(unsigned numBits) const { |
501 | assert(numBits != 0 && "numBits must be non-zero"); |
502 | assert(numBits <= BitWidth && "numBits out of range"); |
503 | if (isSingleWord()) |
504 | return U.VAL == (WORDTYPE_MAX >> (APINT_BITS_PER_WORD - numBits)); |
505 | unsigned Ones = countTrailingOnesSlowCase(); |
506 | return (numBits == Ones) && |
507 | ((Ones + countLeadingZerosSlowCase()) == BitWidth); |
508 | } |
509 | |
510 | |
511 | |
512 | |
513 | bool isMask() const { |
514 | if (isSingleWord()) |
515 | return isMask_64(U.VAL); |
516 | unsigned Ones = countTrailingOnesSlowCase(); |
517 | return (Ones > 0) && ((Ones + countLeadingZerosSlowCase()) == BitWidth); |
518 | } |
519 | |
520 | |
521 | |
522 | bool isShiftedMask() const { |
523 | if (isSingleWord()) |
524 | return isShiftedMask_64(U.VAL); |
525 | unsigned Ones = countPopulationSlowCase(); |
526 | unsigned LeadZ = countLeadingZerosSlowCase(); |
527 | return (Ones + LeadZ + countTrailingZeros()) == BitWidth; |
528 | } |
529 | |
530 | |
531 | |
532 | |
533 | |
534 | |
535 | static APInt getMaxValue(unsigned numBits) { |
536 | return getAllOnesValue(numBits); |
537 | } |
538 | |
539 | |
540 | static APInt getSignedMaxValue(unsigned numBits) { |
541 | APInt API = getAllOnesValue(numBits); |
542 | API.clearBit(numBits - 1); |
543 | return API; |
544 | } |
545 | |
546 | |
547 | static APInt getMinValue(unsigned numBits) { return APInt(numBits, 0); } |
548 | |
549 | |
550 | static APInt getSignedMinValue(unsigned numBits) { |
551 | APInt API(numBits, 0); |
552 | API.setBit(numBits - 1); |
553 | return API; |
554 | } |
555 | |
556 | |
557 | |
558 | |
559 | |
560 | static APInt getSignMask(unsigned BitWidth) { |
561 | return getSignedMinValue(BitWidth); |
562 | } |
563 | |
564 | |
565 | |
566 | |
567 | static APInt getAllOnesValue(unsigned numBits) { |
568 | return APInt(numBits, WORDTYPE_MAX, true); |
569 | } |
570 | |
571 | |
572 | |
573 | |
574 | static APInt getNullValue(unsigned numBits) { return APInt(numBits, 0); } |
575 | |
576 | |
577 | |
578 | |
579 | |
580 | |
581 | |
582 | APInt getHiBits(unsigned numBits) const; |
583 | |
584 | |
585 | |
586 | |
587 | |
588 | |
589 | |
590 | APInt getLoBits(unsigned numBits) const; |
591 | |
592 | |
593 | static APInt getOneBitSet(unsigned numBits, unsigned BitNo) { |
594 | APInt Res(numBits, 0); |
595 | Res.setBit(BitNo); |
596 | return Res; |
597 | } |
598 | |
599 | |
600 | |
601 | |
602 | |
603 | |
604 | |
605 | |
606 | |
607 | |
608 | |
609 | |
610 | |
611 | |
612 | static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit) { |
613 | assert(loBit <= hiBit && "loBit greater than hiBit"); |
614 | APInt Res(numBits, 0); |
615 | Res.setBits(loBit, hiBit); |
616 | return Res; |
617 | } |
618 | |
619 | |
620 | |
621 | |
622 | |
623 | |
624 | |
625 | static APInt getBitsSetWithWrap(unsigned numBits, unsigned loBit, |
626 | unsigned hiBit) { |
627 | APInt Res(numBits, 0); |
628 | Res.setBitsWithWrap(loBit, hiBit); |
629 | return Res; |
630 | } |
631 | |
632 | |
633 | |
634 | |
635 | |
636 | |
637 | |
638 | |
639 | |
640 | |
641 | |
642 | |
643 | static APInt getBitsSetFrom(unsigned numBits, unsigned loBit) { |
644 | APInt Res(numBits, 0); |
645 | Res.setBitsFrom(loBit); |
646 | return Res; |
647 | } |
648 | |
649 | |
650 | |
651 | |
652 | |
653 | |
654 | |
655 | static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet) { |
656 | APInt Res(numBits, 0); |
657 | Res.setHighBits(hiBitsSet); |
658 | return Res; |
659 | } |
660 | |
661 | |
662 | |
663 | |
664 | |
665 | |
666 | |
667 | static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet) { |
668 | APInt Res(numBits, 0); |
669 | Res.setLowBits(loBitsSet); |
670 | return Res; |
671 | } |
672 | |
673 | |
674 | static APInt getSplat(unsigned NewLen, const APInt &V); |
675 | |
676 | |
677 | |
678 | static bool isSameValue(const APInt &I1, const APInt &I2) { |
679 | if (I1.getBitWidth() == I2.getBitWidth()) |
680 | return I1 == I2; |
681 | |
682 | if (I1.getBitWidth() > I2.getBitWidth()) |
683 | return I1 == I2.zext(I1.getBitWidth()); |
684 | |
685 | return I1.zext(I2.getBitWidth()) == I2; |
686 | } |
687 | |
688 | |
689 | friend hash_code hash_value(const APInt &Arg); |
690 | |
691 | |
692 | |
693 | |
694 | const uint64_t *getRawData() const { |
695 | if (isSingleWord()) |
696 | return &U.VAL; |
697 | return &U.pVal[0]; |
698 | } |
699 | |
700 | |
701 | |
702 | |
703 | |
704 | |
705 | |
706 | |
707 | |
708 | |
709 | const APInt operator++(int) { |
710 | APInt API(*this); |
711 | ++(*this); |
712 | return API; |
713 | } |
714 | |
715 | |
716 | |
717 | |
718 | APInt &operator++(); |
719 | |
720 | |
721 | |
722 | |
723 | |
724 | |
725 | const APInt operator--(int) { |
726 | APInt API(*this); |
727 | --(*this); |
728 | return API; |
729 | } |
730 | |
731 | |
732 | |
733 | |
734 | APInt &operator--(); |
735 | |
736 | |
737 | |
738 | |
739 | |
740 | |
741 | bool operator!() const { |
742 | if (isSingleWord()) |
743 | return U.VAL == 0; |
744 | return countLeadingZerosSlowCase() == BitWidth; |
745 | } |
746 | |
747 | |
748 | |
749 | |
750 | |
751 | |
752 | |
753 | |
754 | APInt &operator=(const APInt &RHS) { |
755 | |
756 | if (isSingleWord() && RHS.isSingleWord()) { |
757 | U.VAL = RHS.U.VAL; |
758 | BitWidth = RHS.BitWidth; |
759 | return clearUnusedBits(); |
760 | } |
761 | |
762 | AssignSlowCase(RHS); |
763 | return *this; |
764 | } |
765 | |
766 | |
767 | APInt &operator=(APInt &&that) { |
768 | #ifdef EXPENSIVE_CHECKS |
769 | |
770 | if (this == &that) |
771 | return *this; |
772 | #endif |
773 | assert(this != &that && "Self-move not supported"); |
774 | if (!isSingleWord()) |
| |
775 | delete[] U.pVal; |
| |
776 | |
777 | |
778 | |
779 | memcpy(&U, &that.U, sizeof(U)); |
780 | |
781 | BitWidth = that.BitWidth; |
782 | that.BitWidth = 0; |
783 | |
784 | return *this; |
785 | } |
786 | |
787 | |
788 | |
789 | |
790 | |
791 | |
792 | |
793 | |
794 | APInt &operator=(uint64_t RHS) { |
795 | if (isSingleWord()) { |
796 | U.VAL = RHS; |
797 | return clearUnusedBits(); |
798 | } |
799 | U.pVal[0] = RHS; |
800 | memset(U.pVal + 1, 0, (getNumWords() - 1) * APINT_WORD_SIZE); |
801 | return *this; |
802 | } |
803 | |
804 | |
805 | |
806 | |
807 | |
808 | |
809 | |
810 | APInt &operator&=(const APInt &RHS) { |
811 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
812 | if (isSingleWord()) |
813 | U.VAL &= RHS.U.VAL; |
814 | else |
815 | AndAssignSlowCase(RHS); |
816 | return *this; |
817 | } |
818 | |
819 | |
820 | |
821 | |
822 | |
823 | |
824 | APInt &operator&=(uint64_t RHS) { |
825 | if (isSingleWord()) { |
826 | U.VAL &= RHS; |
827 | return *this; |
828 | } |
829 | U.pVal[0] &= RHS; |
830 | memset(U.pVal+1, 0, (getNumWords() - 1) * APINT_WORD_SIZE); |
831 | return *this; |
832 | } |
833 | |
834 | |
835 | |
836 | |
837 | |
838 | |
839 | |
840 | APInt &operator|=(const APInt &RHS) { |
841 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
842 | if (isSingleWord()) |
843 | U.VAL |= RHS.U.VAL; |
844 | else |
845 | OrAssignSlowCase(RHS); |
846 | return *this; |
847 | } |
848 | |
849 | |
850 | |
851 | |
852 | |
853 | |
854 | APInt &operator|=(uint64_t RHS) { |
855 | if (isSingleWord()) { |
856 | U.VAL |= RHS; |
857 | return clearUnusedBits(); |
858 | } |
859 | U.pVal[0] |= RHS; |
860 | return *this; |
861 | } |
862 | |
863 | |
864 | |
865 | |
866 | |
867 | |
868 | |
869 | APInt &operator^=(const APInt &RHS) { |
870 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
871 | if (isSingleWord()) |
872 | U.VAL ^= RHS.U.VAL; |
873 | else |
874 | XorAssignSlowCase(RHS); |
875 | return *this; |
876 | } |
877 | |
878 | |
879 | |
880 | |
881 | |
882 | |
883 | APInt &operator^=(uint64_t RHS) { |
884 | if (isSingleWord()) { |
885 | U.VAL ^= RHS; |
886 | return clearUnusedBits(); |
887 | } |
888 | U.pVal[0] ^= RHS; |
889 | return *this; |
890 | } |
891 | |
892 | |
893 | |
894 | |
895 | |
896 | |
897 | APInt &operator*=(const APInt &RHS); |
898 | APInt &operator*=(uint64_t RHS); |
899 | |
900 | |
901 | |
902 | |
903 | |
904 | |
905 | APInt &operator+=(const APInt &RHS); |
906 | APInt &operator+=(uint64_t RHS); |
907 | |
908 | |
909 | |
910 | |
911 | |
912 | |
913 | APInt &operator-=(const APInt &RHS); |
914 | APInt &operator-=(uint64_t RHS); |
915 | |
916 | |
917 | |
918 | |
919 | |
920 | |
921 | APInt &operator<<=(unsigned ShiftAmt) { |
922 | assert(ShiftAmt <= BitWidth && "Invalid shift amount"); |
923 | if (isSingleWord()) { |
924 | if (ShiftAmt == BitWidth) |
925 | U.VAL = 0; |
926 | else |
927 | U.VAL <<= ShiftAmt; |
928 | return clearUnusedBits(); |
929 | } |
930 | shlSlowCase(ShiftAmt); |
931 | return *this; |
932 | } |
933 | |
934 | |
935 | |
936 | |
937 | |
938 | |
939 | APInt &operator<<=(const APInt &ShiftAmt); |
940 | |
941 | |
942 | |
943 | |
944 | |
945 | |
946 | |
947 | |
948 | APInt operator*(const APInt &RHS) const; |
949 | |
950 | |
951 | |
952 | |
953 | APInt operator<<(unsigned Bits) const { return shl(Bits); } |
954 | |
955 | |
956 | |
957 | |
958 | APInt operator<<(const APInt &Bits) const { return shl(Bits); } |
959 | |
960 | |
961 | |
962 | |
963 | APInt ashr(unsigned ShiftAmt) const { |
964 | APInt R(*this); |
965 | R.ashrInPlace(ShiftAmt); |
966 | return R; |
967 | } |
968 | |
969 | |
970 | void ashrInPlace(unsigned ShiftAmt) { |
971 | assert(ShiftAmt <= BitWidth && "Invalid shift amount"); |
972 | if (isSingleWord()) { |
973 | int64_t SExtVAL = SignExtend64(U.VAL, BitWidth); |
974 | if (ShiftAmt == BitWidth) |
975 | U.VAL = SExtVAL >> (APINT_BITS_PER_WORD - 1); |
976 | else |
977 | U.VAL = SExtVAL >> ShiftAmt; |
978 | clearUnusedBits(); |
979 | return; |
980 | } |
981 | ashrSlowCase(ShiftAmt); |
982 | } |
983 | |
984 | |
985 | |
986 | |
987 | APInt lshr(unsigned shiftAmt) const { |
988 | APInt R(*this); |
989 | R.lshrInPlace(shiftAmt); |
990 | return R; |
991 | } |
992 | |
993 | |
994 | void lshrInPlace(unsigned ShiftAmt) { |
995 | assert(ShiftAmt <= BitWidth && "Invalid shift amount"); |
996 | if (isSingleWord()) { |
997 | if (ShiftAmt == BitWidth) |
998 | U.VAL = 0; |
999 | else |
1000 | U.VAL >>= ShiftAmt; |
1001 | return; |
1002 | } |
1003 | lshrSlowCase(ShiftAmt); |
1004 | } |
1005 | |
1006 | |
1007 | |
1008 | |
1009 | APInt shl(unsigned shiftAmt) const { |
1010 | APInt R(*this); |
1011 | R <<= shiftAmt; |
1012 | return R; |
1013 | } |
1014 | |
1015 | |
1016 | APInt rotl(unsigned rotateAmt) const; |
1017 | |
1018 | |
1019 | APInt rotr(unsigned rotateAmt) const; |
1020 | |
1021 | |
1022 | |
1023 | |
1024 | APInt ashr(const APInt &ShiftAmt) const { |
1025 | APInt R(*this); |
1026 | R.ashrInPlace(ShiftAmt); |
1027 | return R; |
1028 | } |
1029 | |
1030 | |
1031 | void ashrInPlace(const APInt &shiftAmt); |
1032 | |
1033 | |
1034 | |
1035 | |
1036 | APInt lshr(const APInt &ShiftAmt) const { |
1037 | APInt R(*this); |
1038 | R.lshrInPlace(ShiftAmt); |
1039 | return R; |
1040 | } |
1041 | |
1042 | |
1043 | void lshrInPlace(const APInt &ShiftAmt); |
1044 | |
1045 | |
1046 | |
1047 | |
1048 | APInt shl(const APInt &ShiftAmt) const { |
1049 | APInt R(*this); |
1050 | R <<= ShiftAmt; |
1051 | return R; |
1052 | } |
1053 | |
1054 | |
1055 | APInt rotl(const APInt &rotateAmt) const; |
1056 | |
1057 | |
1058 | APInt rotr(const APInt &rotateAmt) const; |
1059 | |
1060 | |
1061 | |
1062 | |
1063 | |
1064 | |
1065 | |
1066 | |
1067 | APInt udiv(const APInt &RHS) const; |
1068 | APInt udiv(uint64_t RHS) const; |
1069 | |
1070 | |
1071 | |
1072 | |
1073 | |
1074 | |
1075 | APInt sdiv(const APInt &RHS) const; |
1076 | APInt sdiv(int64_t RHS) const; |
1077 | |
1078 | |
1079 | |
1080 | |
1081 | |
1082 | |
1083 | |
1084 | |
1085 | |
1086 | |
1087 | APInt urem(const APInt &RHS) const; |
1088 | uint64_t urem(uint64_t RHS) const; |
1089 | |
1090 | |
1091 | |
1092 | |
1093 | APInt srem(const APInt &RHS) const; |
1094 | int64_t srem(int64_t RHS) const; |
1095 | |
1096 | |
1097 | |
1098 | |
1099 | |
1100 | |
1101 | |
1102 | |
1103 | static void udivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, |
1104 | APInt &Remainder); |
1105 | static void udivrem(const APInt &LHS, uint64_t RHS, APInt &Quotient, |
1106 | uint64_t &Remainder); |
1107 | |
1108 | static void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, |
1109 | APInt &Remainder); |
1110 | static void sdivrem(const APInt &LHS, int64_t RHS, APInt &Quotient, |
1111 | int64_t &Remainder); |
1112 | |
1113 | |
1114 | APInt sadd_ov(const APInt &RHS, bool &Overflow) const; |
1115 | APInt uadd_ov(const APInt &RHS, bool &Overflow) const; |
1116 | APInt ssub_ov(const APInt &RHS, bool &Overflow) const; |
1117 | APInt usub_ov(const APInt &RHS, bool &Overflow) const; |
1118 | APInt sdiv_ov(const APInt &RHS, bool &Overflow) const; |
1119 | APInt smul_ov(const APInt &RHS, bool &Overflow) const; |
1120 | APInt umul_ov(const APInt &RHS, bool &Overflow) const; |
1121 | APInt sshl_ov(const APInt &Amt, bool &Overflow) const; |
1122 | APInt ushl_ov(const APInt &Amt, bool &Overflow) const; |
1123 | |
1124 | |
1125 | APInt sadd_sat(const APInt &RHS) const; |
1126 | APInt uadd_sat(const APInt &RHS) const; |
1127 | APInt ssub_sat(const APInt &RHS) const; |
1128 | APInt usub_sat(const APInt &RHS) const; |
1129 | APInt smul_sat(const APInt &RHS) const; |
1130 | APInt umul_sat(const APInt &RHS) const; |
1131 | APInt sshl_sat(const APInt &RHS) const; |
1132 | APInt ushl_sat(const APInt &RHS) const; |
1133 | |
1134 | |
1135 | |
1136 | |
1137 | bool operator[](unsigned bitPosition) const { |
1138 | assert(bitPosition < getBitWidth() && "Bit position out of bounds!"); |
1139 | return (maskBit(bitPosition) & getWord(bitPosition)) != 0; |
1140 | } |
1141 | |
1142 | |
1143 | |
1144 | |
1145 | |
1146 | |
1147 | |
1148 | |
1149 | |
1150 | bool operator==(const APInt &RHS) const { |
1151 | assert(BitWidth == RHS.BitWidth && "Comparison requires equal bit widths"); |
1152 | if (isSingleWord()) |
1153 | return U.VAL == RHS.U.VAL; |
1154 | return EqualSlowCase(RHS); |
1155 | } |
1156 | |
1157 | |
1158 | |
1159 | |
1160 | |
1161 | |
1162 | |
1163 | bool operator==(uint64_t Val) const { |
1164 | return (isSingleWord() || getActiveBits() <= 64) && getZExtValue() == Val; |
1165 | } |
1166 | |
1167 | |
1168 | |
1169 | |
1170 | |
1171 | |
1172 | |
1173 | bool eq(const APInt &RHS) const { return (*this) == RHS; } |
1174 | |
1175 | |
1176 | |
1177 | |
1178 | |
1179 | |
1180 | |
1181 | bool operator!=(const APInt &RHS) const { return !((*this) == RHS); } |
1182 | |
1183 | |
1184 | |
1185 | |
1186 | |
1187 | |
1188 | |
1189 | bool operator!=(uint64_t Val) const { return !((*this) == Val); } |
1190 | |
1191 | |
1192 | |
1193 | |
1194 | |
1195 | |
1196 | |
1197 | bool ne(const APInt &RHS) const { return !((*this) == RHS); } |
1198 | |
1199 | |
1200 | |
1201 | |
1202 | |
1203 | |
1204 | |
1205 | bool ult(const APInt &RHS) const { return compare(RHS) < 0; } |
1206 | |
1207 | |
1208 | |
1209 | |
1210 | |
1211 | |
1212 | |
1213 | bool ult(uint64_t RHS) const { |
1214 | |
1215 | return (isSingleWord() || getActiveBits() <= 64) && getZExtValue() < RHS; |
1216 | } |
1217 | |
1218 | |
1219 | |
1220 | |
1221 | |
1222 | |
1223 | |
1224 | bool slt(const APInt &RHS) const { return compareSigned(RHS) < 0; } |
1225 | |
1226 | |
1227 | |
1228 | |
1229 | |
1230 | |
1231 | |
1232 | bool slt(int64_t RHS) const { |
1233 | return (!isSingleWord() && getMinSignedBits() > 64) ? isNegative() |
1234 | : getSExtValue() < RHS; |
1235 | } |
1236 | |
1237 | |
1238 | |
1239 | |
1240 | |
1241 | |
1242 | |
1243 | bool ule(const APInt &RHS) const { return compare(RHS) <= 0; } |
1244 | |
1245 | |
1246 | |
1247 | |
1248 | |
1249 | |
1250 | |
1251 | bool ule(uint64_t RHS) const { return !ugt(RHS); } |
1252 | |
1253 | |
1254 | |
1255 | |
1256 | |
1257 | |
1258 | |
1259 | bool sle(const APInt &RHS) const { return compareSigned(RHS) <= 0; } |
1260 | |
1261 | |
1262 | |
1263 | |
1264 | |
1265 | |
1266 | |
1267 | bool sle(uint64_t RHS) const { return !sgt(RHS); } |
1268 | |
1269 | |
1270 | |
1271 | |
1272 | |
1273 | |
1274 | |
1275 | bool ugt(const APInt &RHS) const { return !ule(RHS); } |
1276 | |
1277 | |
1278 | |
1279 | |
1280 | |
1281 | |
1282 | |
1283 | bool ugt(uint64_t RHS) const { |
1284 | |
1285 | return (!isSingleWord() && getActiveBits() > 64) || getZExtValue() > RHS; |
1286 | } |
1287 | |
1288 | |
1289 | |
1290 | |
1291 | |
1292 | |
1293 | |
1294 | bool sgt(const APInt &RHS) const { return !sle(RHS); } |
1295 | |
1296 | |
1297 | |
1298 | |
1299 | |
1300 | |
1301 | |
1302 | bool sgt(int64_t RHS) const { |
1303 | return (!isSingleWord() && getMinSignedBits() > 64) ? !isNegative() |
1304 | : getSExtValue() > RHS; |
1305 | } |
1306 | |
1307 | |
1308 | |
1309 | |
1310 | |
1311 | |
1312 | |
1313 | bool uge(const APInt &RHS) const { return !ult(RHS); } |
1314 | |
1315 | |
1316 | |
1317 | |
1318 | |
1319 | |
1320 | |
1321 | bool uge(uint64_t RHS) const { return !ult(RHS); } |
1322 | |
1323 | |
1324 | |
1325 | |
1326 | |
1327 | |
1328 | |
1329 | bool sge(const APInt &RHS) const { return !slt(RHS); } |
1330 | |
1331 | |
1332 | |
1333 | |
1334 | |
1335 | |
1336 | |
1337 | bool sge(int64_t RHS) const { return !slt(RHS); } |
1338 | |
1339 | |
1340 | |
1341 | bool intersects(const APInt &RHS) const { |
1342 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
1343 | if (isSingleWord()) |
1344 | return (U.VAL & RHS.U.VAL) != 0; |
1345 | return intersectsSlowCase(RHS); |
1346 | } |
1347 | |
1348 | |
1349 | bool isSubsetOf(const APInt &RHS) const { |
1350 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
1351 | if (isSingleWord()) |
1352 | return (U.VAL & ~RHS.U.VAL) == 0; |
1353 | return isSubsetOfSlowCase(RHS); |
1354 | } |
1355 | |
1356 | |
1357 | |
1358 | |
1359 | |
1360 | |
1361 | |
1362 | |
1363 | |
1364 | APInt trunc(unsigned width) const; |
1365 | |
1366 | |
1367 | |
1368 | |
1369 | |
1370 | APInt truncUSat(unsigned width) const; |
1371 | |
1372 | |
1373 | |
1374 | |
1375 | |
1376 | |
1377 | APInt truncSSat(unsigned width) const; |
1378 | |
1379 | |
1380 | |
1381 | |
1382 | |
1383 | |
1384 | |
1385 | APInt sext(unsigned width) const; |
1386 | |
1387 | |
1388 | |
1389 | |
1390 | |
1391 | |
1392 | APInt zext(unsigned width) const; |
1393 | |
1394 | |
1395 | |
1396 | |
1397 | |
1398 | APInt sextOrTrunc(unsigned width) const; |
1399 | |
1400 | |
1401 | |
1402 | |
1403 | |
1404 | APInt zextOrTrunc(unsigned width) const; |
1405 | |
1406 | |
1407 | |
1408 | |
1409 | |
1410 | APInt truncOrSelf(unsigned width) const; |
1411 | |
1412 | |
1413 | |
1414 | |
1415 | |
1416 | APInt sextOrSelf(unsigned width) const; |
1417 | |
1418 | |
1419 | |
1420 | |
1421 | |
1422 | APInt zextOrSelf(unsigned width) const; |
1423 | |
1424 | |
1425 | |
1426 | |
1427 | |
1428 | |
1429 | void setAllBits() { |
1430 | if (isSingleWord()) |
1431 | U.VAL = WORDTYPE_MAX; |
1432 | else |
1433 | |
1434 | memset(U.pVal, -1, getNumWords() * APINT_WORD_SIZE); |
1435 | |
1436 | clearUnusedBits(); |
1437 | } |
1438 | |
1439 | |
1440 | |
1441 | |
1442 | void setBit(unsigned BitPosition) { |
1443 | assert(BitPosition < BitWidth && "BitPosition out of range"); |
1444 | WordType Mask = maskBit(BitPosition); |
1445 | if (isSingleWord()) |
1446 | U.VAL |= Mask; |
1447 | else |
1448 | U.pVal[whichWord(BitPosition)] |= Mask; |
1449 | } |
1450 | |
1451 | |
1452 | void setSignBit() { |
1453 | setBit(BitWidth - 1); |
1454 | } |
1455 | |
1456 | |
1457 | void setBitVal(unsigned BitPosition, bool BitValue) { |
1458 | if (BitValue) |
1459 | setBit(BitPosition); |
1460 | else |
1461 | clearBit(BitPosition); |
1462 | } |
1463 | |
1464 | |
1465 | |
1466 | |
1467 | |
1468 | void setBitsWithWrap(unsigned loBit, unsigned hiBit) { |
1469 | assert(hiBit <= BitWidth && "hiBit out of range"); |
1470 | assert(loBit <= BitWidth && "loBit out of range"); |
1471 | if (loBit < hiBit) { |
1472 | setBits(loBit, hiBit); |
1473 | return; |
1474 | } |
1475 | setLowBits(hiBit); |
1476 | setHighBits(BitWidth - loBit); |
1477 | } |
1478 | |
1479 | |
1480 | |
1481 | void setBits(unsigned loBit, unsigned hiBit) { |
1482 | assert(hiBit <= BitWidth && "hiBit out of range"); |
1483 | assert(loBit <= BitWidth && "loBit out of range"); |
1484 | assert(loBit <= hiBit && "loBit greater than hiBit"); |
1485 | if (loBit == hiBit) |
1486 | return; |
1487 | if (loBit < APINT_BITS_PER_WORD && hiBit <= APINT_BITS_PER_WORD) { |
1488 | uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - (hiBit - loBit)); |
1489 | mask <<= loBit; |
1490 | if (isSingleWord()) |
1491 | U.VAL |= mask; |
1492 | else |
1493 | U.pVal[0] |= mask; |
1494 | } else { |
1495 | setBitsSlowCase(loBit, hiBit); |
1496 | } |
1497 | } |
1498 | |
1499 | |
1500 | void setBitsFrom(unsigned loBit) { |
1501 | return setBits(loBit, BitWidth); |
1502 | } |
1503 | |
1504 | |
1505 | void setLowBits(unsigned loBits) { |
1506 | return setBits(0, loBits); |
1507 | } |
1508 | |
1509 | |
1510 | void setHighBits(unsigned hiBits) { |
1511 | return setBits(BitWidth - hiBits, BitWidth); |
1512 | } |
1513 | |
1514 | |
1515 | void clearAllBits() { |
1516 | if (isSingleWord()) |
1517 | U.VAL = 0; |
1518 | else |
1519 | memset(U.pVal, 0, getNumWords() * APINT_WORD_SIZE); |
1520 | } |
1521 | |
1522 | |
1523 | |
1524 | |
1525 | void clearBit(unsigned BitPosition) { |
1526 | assert(BitPosition < BitWidth && "BitPosition out of range"); |
1527 | WordType Mask = ~maskBit(BitPosition); |
1528 | if (isSingleWord()) |
1529 | U.VAL &= Mask; |
1530 | else |
1531 | U.pVal[whichWord(BitPosition)] &= Mask; |
1532 | } |
1533 | |
1534 | |
1535 | void clearLowBits(unsigned loBits) { |
1536 | assert(loBits <= BitWidth && "More bits than bitwidth"); |
1537 | APInt Keep = getHighBitsSet(BitWidth, BitWidth - loBits); |
1538 | *this &= Keep; |
1539 | } |
1540 | |
1541 | |
1542 | void clearSignBit() { |
1543 | clearBit(BitWidth - 1); |
1544 | } |
1545 | |
1546 | |
1547 | void flipAllBits() { |
1548 | if (isSingleWord()) { |
1549 | U.VAL ^= WORDTYPE_MAX; |
1550 | clearUnusedBits(); |
1551 | } else { |
1552 | flipAllBitsSlowCase(); |
1553 | } |
1554 | } |
1555 | |
1556 | |
1557 | |
1558 | |
1559 | |
1560 | void flipBit(unsigned bitPosition); |
1561 | |
1562 | |
1563 | void negate() { |
1564 | flipAllBits(); |
1565 | ++(*this); |
1566 | } |
1567 | |
1568 | |
1569 | void insertBits(const APInt &SubBits, unsigned bitPosition); |
1570 | void insertBits(uint64_t SubBits, unsigned bitPosition, unsigned numBits); |
1571 | |
1572 | |
1573 | APInt extractBits(unsigned numBits, unsigned bitPosition) const; |
1574 | uint64_t extractBitsAsZExtValue(unsigned numBits, unsigned bitPosition) const; |
1575 | |
1576 | |
1577 | |
1578 | |
1579 | |
1580 | |
1581 | unsigned getBitWidth() const { return BitWidth; } |
1582 | |
1583 | |
1584 | |
1585 | |
1586 | |
1587 | |
1588 | unsigned getNumWords() const { return getNumWords(BitWidth); } |
1589 | |
1590 | |
1591 | |
1592 | |
1593 | |
1594 | |
1595 | |
1596 | static unsigned getNumWords(unsigned BitWidth) { |
1597 | return ((uint64_t)BitWidth + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD; |
1598 | } |
1599 | |
1600 | |
1601 | |
1602 | |
1603 | |
1604 | |
1605 | unsigned getActiveBits() const { return BitWidth - countLeadingZeros(); } |
1606 | |
1607 | |
1608 | |
1609 | |
1610 | |
1611 | unsigned getActiveWords() const { |
1612 | unsigned numActiveBits = getActiveBits(); |
1613 | return numActiveBits ? whichWord(numActiveBits - 1) + 1 : 1; |
1614 | } |
1615 | |
1616 | |
1617 | |
1618 | |
1619 | |
1620 | |
1621 | |
1622 | |
1623 | |
1624 | unsigned getMinSignedBits() const { return BitWidth - getNumSignBits() + 1; } |
1625 | |
1626 | |
1627 | |
1628 | |
1629 | |
1630 | |
1631 | uint64_t getZExtValue() const { |
1632 | if (isSingleWord()) |
1633 | return U.VAL; |
1634 | assert(getActiveBits() <= 64 && "Too many bits for uint64_t"); |
1635 | return U.pVal[0]; |
1636 | } |
1637 | |
1638 | |
1639 | |
1640 | |
1641 | |
1642 | |
1643 | int64_t getSExtValue() const { |
1644 | if (isSingleWord()) |
1645 | return SignExtend64(U.VAL, BitWidth); |
1646 | assert(getMinSignedBits() <= 64 && "Too many bits for int64_t"); |
1647 | return int64_t(U.pVal[0]); |
1648 | } |
1649 | |
1650 | |
1651 | |
1652 | |
1653 | |
1654 | static unsigned getBitsNeeded(StringRef str, uint8_t radix); |
1655 | |
1656 | |
1657 | |
1658 | |
1659 | |
1660 | |
1661 | |
1662 | |
1663 | |
1664 | unsigned countLeadingZeros() const { |
1665 | if (isSingleWord()) { |
1666 | unsigned unusedBits = APINT_BITS_PER_WORD - BitWidth; |
1667 | return llvm::countLeadingZeros(U.VAL) - unusedBits; |
1668 | } |
1669 | return countLeadingZerosSlowCase(); |
1670 | } |
1671 | |
1672 | |
1673 | |
1674 | |
1675 | |
1676 | |
1677 | |
1678 | |
1679 | |
1680 | unsigned countLeadingOnes() const { |
1681 | if (isSingleWord()) |
1682 | return llvm::countLeadingOnes(U.VAL << (APINT_BITS_PER_WORD - BitWidth)); |
1683 | return countLeadingOnesSlowCase(); |
1684 | } |
1685 | |
1686 | |
1687 | |
1688 | unsigned getNumSignBits() const { |
1689 | return isNegative() ? countLeadingOnes() : countLeadingZeros(); |
1690 | } |
1691 | |
1692 | |
1693 | |
1694 | |
1695 | |
1696 | |
1697 | |
1698 | |
1699 | |
1700 | unsigned countTrailingZeros() const { |
1701 | if (isSingleWord()) { |
1702 | unsigned TrailingZeros = llvm::countTrailingZeros(U.VAL); |
1703 | return (TrailingZeros > BitWidth ? BitWidth : TrailingZeros); |
1704 | } |
1705 | return countTrailingZerosSlowCase(); |
1706 | } |
1707 | |
1708 | |
1709 | |
1710 | |
1711 | |
1712 | |
1713 | |
1714 | |
1715 | |
1716 | unsigned countTrailingOnes() const { |
1717 | if (isSingleWord()) |
1718 | return llvm::countTrailingOnes(U.VAL); |
1719 | return countTrailingOnesSlowCase(); |
1720 | } |
1721 | |
1722 | |
1723 | |
1724 | |
1725 | |
1726 | |
1727 | |
1728 | unsigned countPopulation() const { |
1729 | if (isSingleWord()) |
1730 | return llvm::countPopulation(U.VAL); |
1731 | return countPopulationSlowCase(); |
1732 | } |
1733 | |
1734 | |
1735 | |
1736 | |
1737 | void print(raw_ostream &OS, bool isSigned) const; |
1738 | |
1739 | |
1740 | |
1741 | void toString(SmallVectorImpl<char> &Str, unsigned Radix, bool Signed, |
1742 | bool formatAsCLiteral = false) const; |
1743 | |
1744 | |
1745 | |
1746 | void toStringUnsigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const { |
1747 | toString(Str, Radix, false, false); |
1748 | } |
1749 | |
1750 | |
1751 | |
1752 | void toStringSigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const { |
1753 | toString(Str, Radix, true, false); |
1754 | } |
1755 | |
1756 | |
1757 | APInt byteSwap() const; |
1758 | |
1759 | |
1760 | |
1761 | APInt reverseBits() const; |
1762 | |
1763 | |
1764 | double roundToDouble(bool isSigned) const; |
1765 | |
1766 | |
1767 | double roundToDouble() const { return roundToDouble(false); } |
1768 | |
1769 | |
1770 | double signedRoundToDouble() const { return roundToDouble(true); } |
1771 | |
1772 | |
1773 | |
1774 | |
1775 | |
1776 | |
1777 | double bitsToDouble() const { |
1778 | return BitsToDouble(getWord(0)); |
1779 | } |
1780 | |
1781 | |
1782 | |
1783 | |
1784 | |
1785 | |
1786 | float bitsToFloat() const { |
1787 | return BitsToFloat(static_cast<uint32_t>(getWord(0))); |
1788 | } |
1789 | |
1790 | |
1791 | |
1792 | |
1793 | |
1794 | static APInt doubleToBits(double V) { |
1795 | return APInt(sizeof(double) * CHAR_BIT, DoubleToBits(V)); |
1796 | } |
1797 | |
1798 | |
1799 | |
1800 | |
1801 | |
1802 | static APInt floatToBits(float V) { |
1803 | return APInt(sizeof(float) * CHAR_BIT, FloatToBits(V)); |
1804 | } |
1805 | |
1806 | |
1807 | |
1808 | |
1809 | |
1810 | |
1811 | unsigned logBase2() const { return getActiveBits() - 1; } |
1812 | |
1813 | |
1814 | unsigned ceilLogBase2() const { |
1815 | APInt temp(*this); |
1816 | --temp; |
1817 | return temp.getActiveBits(); |
1818 | } |
1819 | |
1820 | |
1821 | |
1822 | |
1823 | |
1824 | |
1825 | |
1826 | |
1827 | |
1828 | |
1829 | unsigned nearestLogBase2() const { |
1830 | |
1831 | |
1832 | |
1833 | if (BitWidth == 1) |
1834 | return U.VAL - 1; |
1835 | |
1836 | |
1837 | if (isNullValue()) |
1838 | return UINT32_MAX; |
1839 | |
1840 | |
1841 | |
1842 | |
1843 | |
1844 | |
1845 | unsigned lg = logBase2(); |
1846 | return lg + unsigned((*this)[lg - 1]); |
1847 | } |
1848 | |
1849 | |
1850 | |
1851 | int32_t exactLogBase2() const { |
1852 | if (!isPowerOf2()) |
1853 | return -1; |
1854 | return logBase2(); |
1855 | } |
1856 | |
1857 | |
1858 | APInt sqrt() const; |
1859 | |
1860 | |
1861 | |
1862 | |
1863 | APInt abs() const { |
1864 | if (isNegative()) |
1865 | return -(*this); |
1866 | return *this; |
1867 | } |
1868 | |
1869 | |
1870 | APInt multiplicativeInverse(const APInt &modulo) const; |
1871 | |
1872 | |
1873 | |
1874 | |
1875 | |
1876 | |
1877 | struct ms; |
1878 | ms magic() const; |
1879 | |
1880 | |
1881 | struct mu; |
1882 | mu magicu(unsigned LeadingZeros = 0) const; |
1883 | |
1884 | |
1885 | |
1886 | |
1887 | |
1888 | |
1889 | |
1890 | |
1891 | |
1892 | |
1893 | |
1894 | |
1895 | |
1896 | static void tcSet(WordType *, WordType, unsigned); |
1897 | |
1898 | |
1899 | static void tcAssign(WordType *, const WordType *, unsigned); |
1900 | |
1901 | |
1902 | static bool tcIsZero(const WordType *, unsigned); |
1903 | |
1904 | |
1905 | static int tcExtractBit(const WordType *, unsigned bit); |
1906 | |
1907 | |
1908 | |
1909 | |
1910 | |
1911 | static void tcExtract(WordType *, unsigned dstCount, |
1912 | const WordType *, unsigned srcBits, |
1913 | unsigned srcLSB); |
1914 | |
1915 | |
1916 | static void tcSetBit(WordType *, unsigned bit); |
1917 | |
1918 | |
1919 | static void tcClearBit(WordType *, unsigned bit); |
1920 | |
1921 | |
1922 | |
1923 | static unsigned tcLSB(const WordType *, unsigned n); |
1924 | static unsigned tcMSB(const WordType *parts, unsigned n); |
1925 | |
1926 | |
1927 | static void tcNegate(WordType *, unsigned); |
1928 | |
1929 | |
1930 | static WordType tcAdd(WordType *, const WordType *, |
1931 | WordType carry, unsigned); |
1932 | |
1933 | static WordType tcAddPart(WordType *, WordType, unsigned); |
1934 | |
1935 | |
1936 | static WordType tcSubtract(WordType *, const WordType *, |
1937 | WordType carry, unsigned); |
1938 | |
1939 | static WordType tcSubtractPart(WordType *, WordType, unsigned); |
1940 | |
1941 | |
1942 | |
1943 | |
1944 | |
1945 | |
1946 | |
1947 | |
1948 | |
1949 | |
1950 | |
1951 | static int tcMultiplyPart(WordType *dst, const WordType *src, |
1952 | WordType multiplier, WordType carry, |
1953 | unsigned srcParts, unsigned dstParts, |
1954 | bool add); |
1955 | |
1956 | |
1957 | |
1958 | |
1959 | |
1960 | static int tcMultiply(WordType *, const WordType *, const WordType *, |
1961 | unsigned); |
1962 | |
1963 | |
1964 | |
1965 | static void tcFullMultiply(WordType *, const WordType *, |
1966 | const WordType *, unsigned, unsigned); |
1967 | |
1968 | |
1969 | |
1970 | |
1971 | |
1972 | |
1973 | |
1974 | |
1975 | |
1976 | |
1977 | static int tcDivide(WordType *lhs, const WordType *rhs, |
1978 | WordType *remainder, WordType *scratch, |
1979 | unsigned parts); |
1980 | |
1981 | |
1982 | |
1983 | static void tcShiftLeft(WordType *, unsigned Words, unsigned Count); |
1984 | |
1985 | |
1986 | |
1987 | static void tcShiftRight(WordType *, unsigned Words, unsigned Count); |
1988 | |
1989 | |
1990 | static void tcAnd(WordType *, const WordType *, unsigned); |
1991 | static void tcOr(WordType *, const WordType *, unsigned); |
1992 | static void tcXor(WordType *, const WordType *, unsigned); |
1993 | static void tcComplement(WordType *, unsigned); |
1994 | |
1995 | |
1996 | static int tcCompare(const WordType *, const WordType *, unsigned); |
1997 | |
1998 | |
1999 | static WordType tcIncrement(WordType *dst, unsigned parts) { |
2000 | return tcAddPart(dst, 1, parts); |
2001 | } |
2002 | |
2003 | |
2004 | static WordType tcDecrement(WordType *dst, unsigned parts) { |
2005 | return tcSubtractPart(dst, 1, parts); |
2006 | } |
2007 | |
2008 | |
2009 | static void tcSetLeastSignificantBits(WordType *, unsigned, unsigned bits); |
2010 | |
2011 | |
2012 | void dump() const; |
2013 | |
2014 | |
2015 | }; |
2016 | |
2017 | |
2018 | struct APInt::ms { |
2019 | APInt m; |
2020 | unsigned s; |
2021 | }; |
2022 | |
2023 | |
2024 | struct APInt::mu { |
2025 | APInt m; |
2026 | bool a; |
2027 | unsigned s; |
2028 | }; |
2029 | |
2030 | inline bool operator==(uint64_t V1, const APInt &V2) { return V2 == V1; } |
2031 | |
2032 | inline bool operator!=(uint64_t V1, const APInt &V2) { return V2 != V1; } |
2033 | |
2034 | |
2035 | |
2036 | |
2037 | inline APInt operator~(APInt v) { |
2038 | v.flipAllBits(); |
2039 | return v; |
2040 | } |
2041 | |
2042 | inline APInt operator&(APInt a, const APInt &b) { |
2043 | a &= b; |
2044 | return a; |
2045 | } |
2046 | |
2047 | inline APInt operator&(const APInt &a, APInt &&b) { |
2048 | b &= a; |
2049 | return std::move(b); |
2050 | } |
2051 | |
2052 | inline APInt operator&(APInt a, uint64_t RHS) { |
2053 | a &= RHS; |
2054 | return a; |
2055 | } |
2056 | |
2057 | inline APInt operator&(uint64_t LHS, APInt b) { |
2058 | b &= LHS; |
2059 | return b; |
2060 | } |
2061 | |
2062 | inline APInt operator|(APInt a, const APInt &b) { |
2063 | a |= b; |
2064 | return a; |
2065 | } |
2066 | |
2067 | inline APInt operator|(const APInt &a, APInt &&b) { |
2068 | b |= a; |
2069 | return std::move(b); |
2070 | } |
2071 | |
2072 | inline APInt operator|(APInt a, uint64_t RHS) { |
2073 | a |= RHS; |
2074 | return a; |
2075 | } |
2076 | |
2077 | inline APInt operator|(uint64_t LHS, APInt b) { |
2078 | b |= LHS; |
2079 | return b; |
2080 | } |
2081 | |
2082 | inline APInt operator^(APInt a, const APInt &b) { |
2083 | a ^= b; |
2084 | return a; |
2085 | } |
2086 | |
2087 | inline APInt operator^(const APInt &a, APInt &&b) { |
2088 | b ^= a; |
2089 | return std::move(b); |
2090 | } |
2091 | |
2092 | inline APInt operator^(APInt a, uint64_t RHS) { |
2093 | a ^= RHS; |
2094 | return a; |
2095 | } |
2096 | |
2097 | inline APInt operator^(uint64_t LHS, APInt b) { |
2098 | b ^= LHS; |
2099 | return b; |
2100 | } |
2101 | |
2102 | inline raw_ostream &operator<<(raw_ostream &OS, const APInt &I) { |
2103 | I.print(OS, true); |
2104 | return OS; |
2105 | } |
2106 | |
2107 | inline APInt operator-(APInt v) { |
2108 | v.negate(); |
2109 | return v; |
2110 | } |
2111 | |
2112 | inline APInt operator+(APInt a, const APInt &b) { |
2113 | a += b; |
2114 | return a; |
2115 | } |
2116 | |
2117 | inline APInt operator+(const APInt &a, APInt &&b) { |
2118 | b += a; |
2119 | return std::move(b); |
2120 | } |
2121 | |
2122 | inline APInt operator+(APInt a, uint64_t RHS) { |
2123 | a += RHS; |
2124 | return a; |
2125 | } |
2126 | |
2127 | inline APInt operator+(uint64_t LHS, APInt b) { |
2128 | b += LHS; |
2129 | return b; |
2130 | } |
2131 | |
2132 | inline APInt operator-(APInt a, const APInt &b) { |
2133 | a -= b; |
2134 | return a; |
2135 | } |
2136 | |
2137 | inline APInt operator-(const APInt &a, APInt &&b) { |
2138 | b.negate(); |
2139 | b += a; |
2140 | return std::move(b); |
2141 | } |
2142 | |
2143 | inline APInt operator-(APInt a, uint64_t RHS) { |
2144 | a -= RHS; |
2145 | return a; |
2146 | } |
2147 | |
2148 | inline APInt operator-(uint64_t LHS, APInt b) { |
2149 | b.negate(); |
2150 | b += LHS; |
2151 | return b; |
2152 | } |
2153 | |
2154 | inline APInt operator*(APInt a, uint64_t RHS) { |
2155 | a *= RHS; |
2156 | return a; |
2157 | } |
2158 | |
2159 | inline APInt operator*(uint64_t LHS, APInt b) { |
2160 | b *= LHS; |
2161 | return b; |
2162 | } |
2163 | |
2164 | |
2165 | namespace APIntOps { |
2166 | |
2167 | |
2168 | inline const APInt &smin(const APInt &A, const APInt &B) { |
2169 | return A.slt(B) ? A : B; |
2170 | } |
2171 | |
2172 | |
2173 | inline const APInt &smax(const APInt &A, const APInt &B) { |
2174 | return A.sgt(B) ? A : B; |
2175 | } |
2176 | |
2177 | |
2178 | inline const APInt &umin(const APInt &A, const APInt &B) { |
2179 | return A.ult(B) ? A : B; |
2180 | } |
2181 | |
2182 | |
2183 | inline const APInt &umax(const APInt &A, const APInt &B) { |
2184 | return A.ugt(B) ? A : B; |
2185 | } |
2186 | |
2187 | |
2188 | |
2189 | |
2190 | |
2191 | |
2192 | |
2193 | APInt GreatestCommonDivisor(APInt A, APInt B); |
2194 | |
2195 | |
2196 | |
2197 | |
2198 | inline double RoundAPIntToDouble(const APInt &APIVal) { |
2199 | return APIVal.roundToDouble(); |
2200 | } |
2201 | |
2202 | |
2203 | |
2204 | |
2205 | inline double RoundSignedAPIntToDouble(const APInt &APIVal) { |
2206 | return APIVal.signedRoundToDouble(); |
2207 | } |
2208 | |
2209 | |
2210 | inline float RoundAPIntToFloat(const APInt &APIVal) { |
2211 | return float(RoundAPIntToDouble(APIVal)); |
2212 | } |
2213 | |
2214 | |
2215 | |
2216 | |
2217 | inline float RoundSignedAPIntToFloat(const APInt &APIVal) { |
2218 | return float(APIVal.signedRoundToDouble()); |
2219 | } |
2220 | |
2221 | |
2222 | |
2223 | |
2224 | APInt RoundDoubleToAPInt(double Double, unsigned width); |
2225 | |
2226 | |
2227 | |
2228 | |
2229 | inline APInt RoundFloatToAPInt(float Float, unsigned width) { |
2230 | return RoundDoubleToAPInt(double(Float), width); |
2231 | } |
2232 | |
2233 | |
2234 | APInt RoundingUDiv(const APInt &A, const APInt &B, APInt::Rounding RM); |
2235 | |
2236 | |
2237 | APInt RoundingSDiv(const APInt &A, const APInt &B, APInt::Rounding RM); |
2238 | |
2239 | |
2240 | |
2241 | |
2242 | |
2243 | |
2244 | |
2245 | |
2246 | |
2247 | |
2248 | |
2249 | |
2250 | |
2251 | |
2252 | |
2253 | |
2254 | |
2255 | |
2256 | |
2257 | |
2258 | |
2259 | |
2260 | |
2261 | |
2262 | |
2263 | |
2264 | |
2265 | |
2266 | |
2267 | |
2268 | |
2269 | |
2270 | |
2271 | |
2272 | Optional<APInt> SolveQuadraticEquationWrap(APInt A, APInt B, APInt C, |
2273 | unsigned RangeWidth); |
2274 | |
2275 | |
2276 | |
2277 | Optional<unsigned> GetMostSignificantDifferentBit(const APInt &A, |
2278 | const APInt &B); |
2279 | |
2280 | } |
2281 | |
2282 | |
2283 | |
2284 | hash_code hash_value(const APInt &Arg); |
2285 | |
2286 | |
2287 | |
2288 | void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst, unsigned StoreBytes); |
2289 | |
2290 | |
2291 | |
2292 | void LoadIntFromMemory(APInt &IntVal, const uint8_t *Src, unsigned LoadBytes); |
2293 | |
2294 | |
2295 | template <> struct DenseMapInfo<APInt> { |
2296 | static inline APInt getEmptyKey() { |
2297 | APInt V(nullptr, 0); |
2298 | V.U.VAL = 0; |
2299 | return V; |
2300 | } |
2301 | |
2302 | static inline APInt getTombstoneKey() { |
2303 | APInt V(nullptr, 0); |
2304 | V.U.VAL = 1; |
2305 | return V; |
2306 | } |
2307 | |
2308 | static unsigned getHashValue(const APInt &Key); |
2309 | |
2310 | static bool isEqual(const APInt &LHS, const APInt &RHS) { |
2311 | return LHS.getBitWidth() == RHS.getBitWidth() && LHS == RHS; |
2312 | } |
2313 | }; |
2314 | |
2315 | } |
2316 | |
2317 | #endif |