1 //===-- APInt.cpp - Implement APInt class ---------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements a class to represent arbitrary precision integer 11 // constant values and provide a variety of arithmetic operations on them. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/ADT/APInt.h" 16 #include "llvm/ADT/ArrayRef.h" 17 #include "llvm/ADT/FoldingSet.h" 18 #include "llvm/ADT/Hashing.h" 19 #include "llvm/ADT/SmallString.h" 20 #include "llvm/ADT/StringRef.h" 21 #include "llvm/Support/Debug.h" 22 #include "llvm/Support/ErrorHandling.h" 23 #include "llvm/Support/MathExtras.h" 24 #include "llvm/Support/raw_ostream.h" 25 #include <climits> 26 #include <cmath> 27 #include <cstdlib> 28 #include <cstring> 29 using namespace llvm; 30 31 #define DEBUG_TYPE "apint" 32 33 /// A utility function for allocating memory, checking for allocation failures, 34 /// and ensuring the contents are zeroed. 35 inline static uint64_t* getClearedMemory(unsigned numWords) { 36 uint64_t * result = new uint64_t[numWords]; 37 assert(result && "APInt memory allocation fails!"); 38 memset(result, 0, numWords * sizeof(uint64_t)); 39 return result; 40 } 41 42 /// A utility function for allocating memory and checking for allocation 43 /// failure. The content is not zeroed. 44 inline static uint64_t* getMemory(unsigned numWords) { 45 uint64_t * result = new uint64_t[numWords]; 46 assert(result && "APInt memory allocation fails!"); 47 return result; 48 } 49 50 /// A utility function that converts a character to a digit. 51 inline static unsigned getDigit(char cdigit, uint8_t radix) { 52 unsigned r; 53 54 if (radix == 16 || radix == 36) { 55 r = cdigit - '0'; 56 if (r <= 9) 57 return r; 58 59 r = cdigit - 'A'; 60 if (r <= radix - 11U) 61 return r + 10; 62 63 r = cdigit - 'a'; 64 if (r <= radix - 11U) 65 return r + 10; 66 67 radix = 10; 68 } 69 70 r = cdigit - '0'; 71 if (r < radix) 72 return r; 73 74 return -1U; 75 } 76 77 78 void APInt::initSlowCase(uint64_t val, bool isSigned) { 79 U.pVal = getClearedMemory(getNumWords()); 80 U.pVal[0] = val; 81 if (isSigned && int64_t(val) < 0) 82 for (unsigned i = 1; i < getNumWords(); ++i) 83 U.pVal[i] = WORD_MAX; 84 clearUnusedBits(); 85 } 86 87 void APInt::initSlowCase(const APInt& that) { 88 U.pVal = getMemory(getNumWords()); 89 memcpy(U.pVal, that.U.pVal, getNumWords() * APINT_WORD_SIZE); 90 } 91 92 void APInt::initFromArray(ArrayRef<uint64_t> bigVal) { 93 assert(BitWidth && "Bitwidth too small"); 94 assert(bigVal.data() && "Null pointer detected!"); 95 if (isSingleWord()) 96 U.VAL = bigVal[0]; 97 else { 98 // Get memory, cleared to 0 99 U.pVal = getClearedMemory(getNumWords()); 100 // Calculate the number of words to copy 101 unsigned words = std::min<unsigned>(bigVal.size(), getNumWords()); 102 // Copy the words from bigVal to pVal 103 memcpy(U.pVal, bigVal.data(), words * APINT_WORD_SIZE); 104 } 105 // Make sure unused high bits are cleared 106 clearUnusedBits(); 107 } 108 109 APInt::APInt(unsigned numBits, ArrayRef<uint64_t> bigVal) 110 : BitWidth(numBits) { 111 initFromArray(bigVal); 112 } 113 114 APInt::APInt(unsigned numBits, unsigned numWords, const uint64_t bigVal[]) 115 : BitWidth(numBits) { 116 initFromArray(makeArrayRef(bigVal, numWords)); 117 } 118 119 APInt::APInt(unsigned numbits, StringRef Str, uint8_t radix) 120 : BitWidth(numbits) { 121 assert(BitWidth && "Bitwidth too small"); 122 fromString(numbits, Str, radix); 123 } 124 125 void APInt::reallocate(unsigned NewBitWidth) { 126 // If the number of words is the same we can just change the width and stop. 127 if (getNumWords() == getNumWords(NewBitWidth)) { 128 BitWidth = NewBitWidth; 129 return; 130 } 131 132 // If we have an allocation, delete it. 133 if (!isSingleWord()) 134 delete [] U.pVal; 135 136 // Update BitWidth. 137 BitWidth = NewBitWidth; 138 139 // If we are supposed to have an allocation, create it. 140 if (!isSingleWord()) 141 U.pVal = getMemory(getNumWords()); 142 } 143 144 void APInt::AssignSlowCase(const APInt& RHS) { 145 // Don't do anything for X = X 146 if (this == &RHS) 147 return; 148 149 // Adjust the bit width and handle allocations as necessary. 150 reallocate(RHS.getBitWidth()); 151 152 // Copy the data. 153 if (isSingleWord()) 154 U.VAL = RHS.U.VAL; 155 else 156 memcpy(U.pVal, RHS.U.pVal, getNumWords() * APINT_WORD_SIZE); 157 } 158 159 /// This method 'profiles' an APInt for use with FoldingSet. 160 void APInt::Profile(FoldingSetNodeID& ID) const { 161 ID.AddInteger(BitWidth); 162 163 if (isSingleWord()) { 164 ID.AddInteger(U.VAL); 165 return; 166 } 167 168 unsigned NumWords = getNumWords(); 169 for (unsigned i = 0; i < NumWords; ++i) 170 ID.AddInteger(U.pVal[i]); 171 } 172 173 /// @brief Prefix increment operator. Increments the APInt by one. 174 APInt& APInt::operator++() { 175 if (isSingleWord()) 176 ++U.VAL; 177 else 178 tcIncrement(U.pVal, getNumWords()); 179 return clearUnusedBits(); 180 } 181 182 /// @brief Prefix decrement operator. Decrements the APInt by one. 183 APInt& APInt::operator--() { 184 if (isSingleWord()) 185 --U.VAL; 186 else 187 tcDecrement(U.pVal, getNumWords()); 188 return clearUnusedBits(); 189 } 190 191 /// Adds the RHS APint to this APInt. 192 /// @returns this, after addition of RHS. 193 /// @brief Addition assignment operator. 194 APInt& APInt::operator+=(const APInt& RHS) { 195 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 196 if (isSingleWord()) 197 U.VAL += RHS.U.VAL; 198 else 199 tcAdd(U.pVal, RHS.U.pVal, 0, getNumWords()); 200 return clearUnusedBits(); 201 } 202 203 APInt& APInt::operator+=(uint64_t RHS) { 204 if (isSingleWord()) 205 U.VAL += RHS; 206 else 207 tcAddPart(U.pVal, RHS, getNumWords()); 208 return clearUnusedBits(); 209 } 210 211 /// Subtracts the RHS APInt from this APInt 212 /// @returns this, after subtraction 213 /// @brief Subtraction assignment operator. 214 APInt& APInt::operator-=(const APInt& RHS) { 215 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 216 if (isSingleWord()) 217 U.VAL -= RHS.U.VAL; 218 else 219 tcSubtract(U.pVal, RHS.U.pVal, 0, getNumWords()); 220 return clearUnusedBits(); 221 } 222 223 APInt& APInt::operator-=(uint64_t RHS) { 224 if (isSingleWord()) 225 U.VAL -= RHS; 226 else 227 tcSubtractPart(U.pVal, RHS, getNumWords()); 228 return clearUnusedBits(); 229 } 230 231 APInt APInt::operator*(const APInt& RHS) const { 232 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 233 if (isSingleWord()) 234 return APInt(BitWidth, U.VAL * RHS.U.VAL); 235 236 APInt Result(getMemory(getNumWords()), getBitWidth()); 237 238 tcMultiply(Result.U.pVal, U.pVal, RHS.U.pVal, getNumWords()); 239 240 Result.clearUnusedBits(); 241 return Result; 242 } 243 244 void APInt::AndAssignSlowCase(const APInt& RHS) { 245 tcAnd(U.pVal, RHS.U.pVal, getNumWords()); 246 } 247 248 void APInt::OrAssignSlowCase(const APInt& RHS) { 249 tcOr(U.pVal, RHS.U.pVal, getNumWords()); 250 } 251 252 void APInt::XorAssignSlowCase(const APInt& RHS) { 253 tcXor(U.pVal, RHS.U.pVal, getNumWords()); 254 } 255 256 APInt& APInt::operator*=(const APInt& RHS) { 257 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 258 *this = *this * RHS; 259 return *this; 260 } 261 262 APInt& APInt::operator*=(uint64_t RHS) { 263 if (isSingleWord()) { 264 U.VAL *= RHS; 265 } else { 266 unsigned NumWords = getNumWords(); 267 tcMultiplyPart(U.pVal, U.pVal, RHS, 0, NumWords, NumWords, false); 268 } 269 return clearUnusedBits(); 270 } 271 272 bool APInt::EqualSlowCase(const APInt& RHS) const { 273 return std::equal(U.pVal, U.pVal + getNumWords(), RHS.U.pVal); 274 } 275 276 int APInt::compare(const APInt& RHS) const { 277 assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison"); 278 if (isSingleWord()) 279 return U.VAL < RHS.U.VAL ? -1 : U.VAL > RHS.U.VAL; 280 281 return tcCompare(U.pVal, RHS.U.pVal, getNumWords()); 282 } 283 284 int APInt::compareSigned(const APInt& RHS) const { 285 assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison"); 286 if (isSingleWord()) { 287 int64_t lhsSext = SignExtend64(U.VAL, BitWidth); 288 int64_t rhsSext = SignExtend64(RHS.U.VAL, BitWidth); 289 return lhsSext < rhsSext ? -1 : lhsSext > rhsSext; 290 } 291 292 bool lhsNeg = isNegative(); 293 bool rhsNeg = RHS.isNegative(); 294 295 // If the sign bits don't match, then (LHS < RHS) if LHS is negative 296 if (lhsNeg != rhsNeg) 297 return lhsNeg ? -1 : 1; 298 299 // Otherwise we can just use an unsigned comparison, because even negative 300 // numbers compare correctly this way if both have the same signed-ness. 301 return tcCompare(U.pVal, RHS.U.pVal, getNumWords()); 302 } 303 304 void APInt::setBitsSlowCase(unsigned loBit, unsigned hiBit) { 305 unsigned loWord = whichWord(loBit); 306 unsigned hiWord = whichWord(hiBit); 307 308 // Create an initial mask for the low word with zeros below loBit. 309 uint64_t loMask = WORD_MAX << whichBit(loBit); 310 311 // If hiBit is not aligned, we need a high mask. 312 unsigned hiShiftAmt = whichBit(hiBit); 313 if (hiShiftAmt != 0) { 314 // Create a high mask with zeros above hiBit. 315 uint64_t hiMask = WORD_MAX >> (APINT_BITS_PER_WORD - hiShiftAmt); 316 // If loWord and hiWord are equal, then we combine the masks. Otherwise, 317 // set the bits in hiWord. 318 if (hiWord == loWord) 319 loMask &= hiMask; 320 else 321 U.pVal[hiWord] |= hiMask; 322 } 323 // Apply the mask to the low word. 324 U.pVal[loWord] |= loMask; 325 326 // Fill any words between loWord and hiWord with all ones. 327 for (unsigned word = loWord + 1; word < hiWord; ++word) 328 U.pVal[word] = WORD_MAX; 329 } 330 331 /// @brief Toggle every bit to its opposite value. 332 void APInt::flipAllBitsSlowCase() { 333 tcComplement(U.pVal, getNumWords()); 334 clearUnusedBits(); 335 } 336 337 /// Toggle a given bit to its opposite value whose position is given 338 /// as "bitPosition". 339 /// @brief Toggles a given bit to its opposite value. 340 void APInt::flipBit(unsigned bitPosition) { 341 assert(bitPosition < BitWidth && "Out of the bit-width range!"); 342 if ((*this)[bitPosition]) clearBit(bitPosition); 343 else setBit(bitPosition); 344 } 345 346 void APInt::insertBits(const APInt &subBits, unsigned bitPosition) { 347 unsigned subBitWidth = subBits.getBitWidth(); 348 assert(0 < subBitWidth && (subBitWidth + bitPosition) <= BitWidth && 349 "Illegal bit insertion"); 350 351 // Insertion is a direct copy. 352 if (subBitWidth == BitWidth) { 353 *this = subBits; 354 return; 355 } 356 357 // Single word result can be done as a direct bitmask. 358 if (isSingleWord()) { 359 uint64_t mask = WORD_MAX >> (APINT_BITS_PER_WORD - subBitWidth); 360 U.VAL &= ~(mask << bitPosition); 361 U.VAL |= (subBits.U.VAL << bitPosition); 362 return; 363 } 364 365 unsigned loBit = whichBit(bitPosition); 366 unsigned loWord = whichWord(bitPosition); 367 unsigned hi1Word = whichWord(bitPosition + subBitWidth - 1); 368 369 // Insertion within a single word can be done as a direct bitmask. 370 if (loWord == hi1Word) { 371 uint64_t mask = WORD_MAX >> (APINT_BITS_PER_WORD - subBitWidth); 372 U.pVal[loWord] &= ~(mask << loBit); 373 U.pVal[loWord] |= (subBits.U.VAL << loBit); 374 return; 375 } 376 377 // Insert on word boundaries. 378 if (loBit == 0) { 379 // Direct copy whole words. 380 unsigned numWholeSubWords = subBitWidth / APINT_BITS_PER_WORD; 381 memcpy(U.pVal + loWord, subBits.getRawData(), 382 numWholeSubWords * APINT_WORD_SIZE); 383 384 // Mask+insert remaining bits. 385 unsigned remainingBits = subBitWidth % APINT_BITS_PER_WORD; 386 if (remainingBits != 0) { 387 uint64_t mask = WORD_MAX >> (APINT_BITS_PER_WORD - remainingBits); 388 U.pVal[hi1Word] &= ~mask; 389 U.pVal[hi1Word] |= subBits.getWord(subBitWidth - 1); 390 } 391 return; 392 } 393 394 // General case - set/clear individual bits in dst based on src. 395 // TODO - there is scope for optimization here, but at the moment this code 396 // path is barely used so prefer readability over performance. 397 for (unsigned i = 0; i != subBitWidth; ++i) { 398 if (subBits[i]) 399 setBit(bitPosition + i); 400 else 401 clearBit(bitPosition + i); 402 } 403 } 404 405 APInt APInt::extractBits(unsigned numBits, unsigned bitPosition) const { 406 assert(numBits > 0 && "Can't extract zero bits"); 407 assert(bitPosition < BitWidth && (numBits + bitPosition) <= BitWidth && 408 "Illegal bit extraction"); 409 410 if (isSingleWord()) 411 return APInt(numBits, U.VAL >> bitPosition); 412 413 unsigned loBit = whichBit(bitPosition); 414 unsigned loWord = whichWord(bitPosition); 415 unsigned hiWord = whichWord(bitPosition + numBits - 1); 416 417 // Single word result extracting bits from a single word source. 418 if (loWord == hiWord) 419 return APInt(numBits, U.pVal[loWord] >> loBit); 420 421 // Extracting bits that start on a source word boundary can be done 422 // as a fast memory copy. 423 if (loBit == 0) 424 return APInt(numBits, makeArrayRef(U.pVal + loWord, 1 + hiWord - loWord)); 425 426 // General case - shift + copy source words directly into place. 427 APInt Result(numBits, 0); 428 unsigned NumSrcWords = getNumWords(); 429 unsigned NumDstWords = Result.getNumWords(); 430 431 for (unsigned word = 0; word < NumDstWords; ++word) { 432 uint64_t w0 = U.pVal[loWord + word]; 433 uint64_t w1 = 434 (loWord + word + 1) < NumSrcWords ? U.pVal[loWord + word + 1] : 0; 435 Result.U.pVal[word] = (w0 >> loBit) | (w1 << (APINT_BITS_PER_WORD - loBit)); 436 } 437 438 return Result.clearUnusedBits(); 439 } 440 441 unsigned APInt::getBitsNeeded(StringRef str, uint8_t radix) { 442 assert(!str.empty() && "Invalid string length"); 443 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2 || 444 radix == 36) && 445 "Radix should be 2, 8, 10, 16, or 36!"); 446 447 size_t slen = str.size(); 448 449 // Each computation below needs to know if it's negative. 450 StringRef::iterator p = str.begin(); 451 unsigned isNegative = *p == '-'; 452 if (*p == '-' || *p == '+') { 453 p++; 454 slen--; 455 assert(slen && "String is only a sign, needs a value."); 456 } 457 458 // For radixes of power-of-two values, the bits required is accurately and 459 // easily computed 460 if (radix == 2) 461 return slen + isNegative; 462 if (radix == 8) 463 return slen * 3 + isNegative; 464 if (radix == 16) 465 return slen * 4 + isNegative; 466 467 // FIXME: base 36 468 469 // This is grossly inefficient but accurate. We could probably do something 470 // with a computation of roughly slen*64/20 and then adjust by the value of 471 // the first few digits. But, I'm not sure how accurate that could be. 472 473 // Compute a sufficient number of bits that is always large enough but might 474 // be too large. This avoids the assertion in the constructor. This 475 // calculation doesn't work appropriately for the numbers 0-9, so just use 4 476 // bits in that case. 477 unsigned sufficient 478 = radix == 10? (slen == 1 ? 4 : slen * 64/18) 479 : (slen == 1 ? 7 : slen * 16/3); 480 481 // Convert to the actual binary value. 482 APInt tmp(sufficient, StringRef(p, slen), radix); 483 484 // Compute how many bits are required. If the log is infinite, assume we need 485 // just bit. 486 unsigned log = tmp.logBase2(); 487 if (log == (unsigned)-1) { 488 return isNegative + 1; 489 } else { 490 return isNegative + log + 1; 491 } 492 } 493 494 hash_code llvm::hash_value(const APInt &Arg) { 495 if (Arg.isSingleWord()) 496 return hash_combine(Arg.U.VAL); 497 498 return hash_combine_range(Arg.U.pVal, Arg.U.pVal + Arg.getNumWords()); 499 } 500 501 bool APInt::isSplat(unsigned SplatSizeInBits) const { 502 assert(getBitWidth() % SplatSizeInBits == 0 && 503 "SplatSizeInBits must divide width!"); 504 // We can check that all parts of an integer are equal by making use of a 505 // little trick: rotate and check if it's still the same value. 506 return *this == rotl(SplatSizeInBits); 507 } 508 509 /// This function returns the high "numBits" bits of this APInt. 510 APInt APInt::getHiBits(unsigned numBits) const { 511 return this->lshr(BitWidth - numBits); 512 } 513 514 /// This function returns the low "numBits" bits of this APInt. 515 APInt APInt::getLoBits(unsigned numBits) const { 516 APInt Result(getLowBitsSet(BitWidth, numBits)); 517 Result &= *this; 518 return Result; 519 } 520 521 /// Return a value containing V broadcasted over NewLen bits. 522 APInt APInt::getSplat(unsigned NewLen, const APInt &V) { 523 assert(NewLen >= V.getBitWidth() && "Can't splat to smaller bit width!"); 524 525 APInt Val = V.zextOrSelf(NewLen); 526 for (unsigned I = V.getBitWidth(); I < NewLen; I <<= 1) 527 Val |= Val << I; 528 529 return Val; 530 } 531 532 unsigned APInt::countLeadingZerosSlowCase() const { 533 unsigned Count = 0; 534 for (int i = getNumWords()-1; i >= 0; --i) { 535 uint64_t V = U.pVal[i]; 536 if (V == 0) 537 Count += APINT_BITS_PER_WORD; 538 else { 539 Count += llvm::countLeadingZeros(V); 540 break; 541 } 542 } 543 // Adjust for unused bits in the most significant word (they are zero). 544 unsigned Mod = BitWidth % APINT_BITS_PER_WORD; 545 Count -= Mod > 0 ? APINT_BITS_PER_WORD - Mod : 0; 546 return Count; 547 } 548 549 unsigned APInt::countLeadingOnes() const { 550 if (isSingleWord()) 551 return llvm::countLeadingOnes(U.VAL << (APINT_BITS_PER_WORD - BitWidth)); 552 553 unsigned highWordBits = BitWidth % APINT_BITS_PER_WORD; 554 unsigned shift; 555 if (!highWordBits) { 556 highWordBits = APINT_BITS_PER_WORD; 557 shift = 0; 558 } else { 559 shift = APINT_BITS_PER_WORD - highWordBits; 560 } 561 int i = getNumWords() - 1; 562 unsigned Count = llvm::countLeadingOnes(U.pVal[i] << shift); 563 if (Count == highWordBits) { 564 for (i--; i >= 0; --i) { 565 if (U.pVal[i] == WORD_MAX) 566 Count += APINT_BITS_PER_WORD; 567 else { 568 Count += llvm::countLeadingOnes(U.pVal[i]); 569 break; 570 } 571 } 572 } 573 return Count; 574 } 575 576 unsigned APInt::countTrailingZeros() const { 577 if (isSingleWord()) 578 return std::min(unsigned(llvm::countTrailingZeros(U.VAL)), BitWidth); 579 unsigned Count = 0; 580 unsigned i = 0; 581 for (; i < getNumWords() && U.pVal[i] == 0; ++i) 582 Count += APINT_BITS_PER_WORD; 583 if (i < getNumWords()) 584 Count += llvm::countTrailingZeros(U.pVal[i]); 585 return std::min(Count, BitWidth); 586 } 587 588 unsigned APInt::countTrailingOnesSlowCase() const { 589 unsigned Count = 0; 590 unsigned i = 0; 591 for (; i < getNumWords() && U.pVal[i] == WORD_MAX; ++i) 592 Count += APINT_BITS_PER_WORD; 593 if (i < getNumWords()) 594 Count += llvm::countTrailingOnes(U.pVal[i]); 595 assert(Count <= BitWidth); 596 return Count; 597 } 598 599 unsigned APInt::countPopulationSlowCase() const { 600 unsigned Count = 0; 601 for (unsigned i = 0; i < getNumWords(); ++i) 602 Count += llvm::countPopulation(U.pVal[i]); 603 return Count; 604 } 605 606 bool APInt::intersectsSlowCase(const APInt &RHS) const { 607 for (unsigned i = 0, e = getNumWords(); i != e; ++i) 608 if ((U.pVal[i] & RHS.U.pVal[i]) != 0) 609 return true; 610 611 return false; 612 } 613 614 bool APInt::isSubsetOfSlowCase(const APInt &RHS) const { 615 for (unsigned i = 0, e = getNumWords(); i != e; ++i) 616 if ((U.pVal[i] & ~RHS.U.pVal[i]) != 0) 617 return false; 618 619 return true; 620 } 621 622 APInt APInt::byteSwap() const { 623 assert(BitWidth >= 16 && BitWidth % 16 == 0 && "Cannot byteswap!"); 624 if (BitWidth == 16) 625 return APInt(BitWidth, ByteSwap_16(uint16_t(U.VAL))); 626 if (BitWidth == 32) 627 return APInt(BitWidth, ByteSwap_32(unsigned(U.VAL))); 628 if (BitWidth == 48) { 629 unsigned Tmp1 = unsigned(U.VAL >> 16); 630 Tmp1 = ByteSwap_32(Tmp1); 631 uint16_t Tmp2 = uint16_t(U.VAL); 632 Tmp2 = ByteSwap_16(Tmp2); 633 return APInt(BitWidth, (uint64_t(Tmp2) << 32) | Tmp1); 634 } 635 if (BitWidth == 64) 636 return APInt(BitWidth, ByteSwap_64(U.VAL)); 637 638 APInt Result(getNumWords() * APINT_BITS_PER_WORD, 0); 639 for (unsigned I = 0, N = getNumWords(); I != N; ++I) 640 Result.U.pVal[I] = ByteSwap_64(U.pVal[N - I - 1]); 641 if (Result.BitWidth != BitWidth) { 642 Result.lshrInPlace(Result.BitWidth - BitWidth); 643 Result.BitWidth = BitWidth; 644 } 645 return Result; 646 } 647 648 APInt APInt::reverseBits() const { 649 switch (BitWidth) { 650 case 64: 651 return APInt(BitWidth, llvm::reverseBits<uint64_t>(U.VAL)); 652 case 32: 653 return APInt(BitWidth, llvm::reverseBits<uint32_t>(U.VAL)); 654 case 16: 655 return APInt(BitWidth, llvm::reverseBits<uint16_t>(U.VAL)); 656 case 8: 657 return APInt(BitWidth, llvm::reverseBits<uint8_t>(U.VAL)); 658 default: 659 break; 660 } 661 662 APInt Val(*this); 663 APInt Reversed(BitWidth, 0); 664 unsigned S = BitWidth; 665 666 for (; Val != 0; Val.lshrInPlace(1)) { 667 Reversed <<= 1; 668 Reversed |= Val[0]; 669 --S; 670 } 671 672 Reversed <<= S; 673 return Reversed; 674 } 675 676 APInt llvm::APIntOps::GreatestCommonDivisor(APInt A, APInt B) { 677 // Fast-path a common case. 678 if (A == B) return A; 679 680 // Corner cases: if either operand is zero, the other is the gcd. 681 if (!A) return B; 682 if (!B) return A; 683 684 // Count common powers of 2 and remove all other powers of 2. 685 unsigned Pow2; 686 { 687 unsigned Pow2_A = A.countTrailingZeros(); 688 unsigned Pow2_B = B.countTrailingZeros(); 689 if (Pow2_A > Pow2_B) { 690 A.lshrInPlace(Pow2_A - Pow2_B); 691 Pow2 = Pow2_B; 692 } else if (Pow2_B > Pow2_A) { 693 B.lshrInPlace(Pow2_B - Pow2_A); 694 Pow2 = Pow2_A; 695 } else { 696 Pow2 = Pow2_A; 697 } 698 } 699 700 // Both operands are odd multiples of 2^Pow_2: 701 // 702 // gcd(a, b) = gcd(|a - b| / 2^i, min(a, b)) 703 // 704 // This is a modified version of Stein's algorithm, taking advantage of 705 // efficient countTrailingZeros(). 706 while (A != B) { 707 if (A.ugt(B)) { 708 A -= B; 709 A.lshrInPlace(A.countTrailingZeros() - Pow2); 710 } else { 711 B -= A; 712 B.lshrInPlace(B.countTrailingZeros() - Pow2); 713 } 714 } 715 716 return A; 717 } 718 719 APInt llvm::APIntOps::RoundDoubleToAPInt(double Double, unsigned width) { 720 union { 721 double D; 722 uint64_t I; 723 } T; 724 T.D = Double; 725 726 // Get the sign bit from the highest order bit 727 bool isNeg = T.I >> 63; 728 729 // Get the 11-bit exponent and adjust for the 1023 bit bias 730 int64_t exp = ((T.I >> 52) & 0x7ff) - 1023; 731 732 // If the exponent is negative, the value is < 0 so just return 0. 733 if (exp < 0) 734 return APInt(width, 0u); 735 736 // Extract the mantissa by clearing the top 12 bits (sign + exponent). 737 uint64_t mantissa = (T.I & (~0ULL >> 12)) | 1ULL << 52; 738 739 // If the exponent doesn't shift all bits out of the mantissa 740 if (exp < 52) 741 return isNeg ? -APInt(width, mantissa >> (52 - exp)) : 742 APInt(width, mantissa >> (52 - exp)); 743 744 // If the client didn't provide enough bits for us to shift the mantissa into 745 // then the result is undefined, just return 0 746 if (width <= exp - 52) 747 return APInt(width, 0); 748 749 // Otherwise, we have to shift the mantissa bits up to the right location 750 APInt Tmp(width, mantissa); 751 Tmp <<= (unsigned)exp - 52; 752 return isNeg ? -Tmp : Tmp; 753 } 754 755 /// This function converts this APInt to a double. 756 /// The layout for double is as following (IEEE Standard 754): 757 /// -------------------------------------- 758 /// | Sign Exponent Fraction Bias | 759 /// |-------------------------------------- | 760 /// | 1[63] 11[62-52] 52[51-00] 1023 | 761 /// -------------------------------------- 762 double APInt::roundToDouble(bool isSigned) const { 763 764 // Handle the simple case where the value is contained in one uint64_t. 765 // It is wrong to optimize getWord(0) to VAL; there might be more than one word. 766 if (isSingleWord() || getActiveBits() <= APINT_BITS_PER_WORD) { 767 if (isSigned) { 768 int64_t sext = SignExtend64(getWord(0), BitWidth); 769 return double(sext); 770 } else 771 return double(getWord(0)); 772 } 773 774 // Determine if the value is negative. 775 bool isNeg = isSigned ? (*this)[BitWidth-1] : false; 776 777 // Construct the absolute value if we're negative. 778 APInt Tmp(isNeg ? -(*this) : (*this)); 779 780 // Figure out how many bits we're using. 781 unsigned n = Tmp.getActiveBits(); 782 783 // The exponent (without bias normalization) is just the number of bits 784 // we are using. Note that the sign bit is gone since we constructed the 785 // absolute value. 786 uint64_t exp = n; 787 788 // Return infinity for exponent overflow 789 if (exp > 1023) { 790 if (!isSigned || !isNeg) 791 return std::numeric_limits<double>::infinity(); 792 else 793 return -std::numeric_limits<double>::infinity(); 794 } 795 exp += 1023; // Increment for 1023 bias 796 797 // Number of bits in mantissa is 52. To obtain the mantissa value, we must 798 // extract the high 52 bits from the correct words in pVal. 799 uint64_t mantissa; 800 unsigned hiWord = whichWord(n-1); 801 if (hiWord == 0) { 802 mantissa = Tmp.U.pVal[0]; 803 if (n > 52) 804 mantissa >>= n - 52; // shift down, we want the top 52 bits. 805 } else { 806 assert(hiWord > 0 && "huh?"); 807 uint64_t hibits = Tmp.U.pVal[hiWord] << (52 - n % APINT_BITS_PER_WORD); 808 uint64_t lobits = Tmp.U.pVal[hiWord-1] >> (11 + n % APINT_BITS_PER_WORD); 809 mantissa = hibits | lobits; 810 } 811 812 // The leading bit of mantissa is implicit, so get rid of it. 813 uint64_t sign = isNeg ? (1ULL << (APINT_BITS_PER_WORD - 1)) : 0; 814 union { 815 double D; 816 uint64_t I; 817 } T; 818 T.I = sign | (exp << 52) | mantissa; 819 return T.D; 820 } 821 822 // Truncate to new width. 823 APInt APInt::trunc(unsigned width) const { 824 assert(width < BitWidth && "Invalid APInt Truncate request"); 825 assert(width && "Can't truncate to 0 bits"); 826 827 if (width <= APINT_BITS_PER_WORD) 828 return APInt(width, getRawData()[0]); 829 830 APInt Result(getMemory(getNumWords(width)), width); 831 832 // Copy full words. 833 unsigned i; 834 for (i = 0; i != width / APINT_BITS_PER_WORD; i++) 835 Result.U.pVal[i] = U.pVal[i]; 836 837 // Truncate and copy any partial word. 838 unsigned bits = (0 - width) % APINT_BITS_PER_WORD; 839 if (bits != 0) 840 Result.U.pVal[i] = U.pVal[i] << bits >> bits; 841 842 return Result; 843 } 844 845 // Sign extend to a new width. 846 APInt APInt::sext(unsigned Width) const { 847 assert(Width > BitWidth && "Invalid APInt SignExtend request"); 848 849 if (Width <= APINT_BITS_PER_WORD) 850 return APInt(Width, SignExtend64(U.VAL, BitWidth)); 851 852 APInt Result(getMemory(getNumWords(Width)), Width); 853 854 // Copy words. 855 std::memcpy(Result.U.pVal, getRawData(), getNumWords() * APINT_WORD_SIZE); 856 857 // Sign extend the last word since there may be unused bits in the input. 858 Result.U.pVal[getNumWords() - 1] = 859 SignExtend64(Result.U.pVal[getNumWords() - 1], 860 ((BitWidth - 1) % APINT_BITS_PER_WORD) + 1); 861 862 // Fill with sign bits. 863 std::memset(Result.U.pVal + getNumWords(), isNegative() ? -1 : 0, 864 (Result.getNumWords() - getNumWords()) * APINT_WORD_SIZE); 865 Result.clearUnusedBits(); 866 return Result; 867 } 868 869 // Zero extend to a new width. 870 APInt APInt::zext(unsigned width) const { 871 assert(width > BitWidth && "Invalid APInt ZeroExtend request"); 872 873 if (width <= APINT_BITS_PER_WORD) 874 return APInt(width, U.VAL); 875 876 APInt Result(getMemory(getNumWords(width)), width); 877 878 // Copy words. 879 std::memcpy(Result.U.pVal, getRawData(), getNumWords() * APINT_WORD_SIZE); 880 881 // Zero remaining words. 882 std::memset(Result.U.pVal + getNumWords(), 0, 883 (Result.getNumWords() - getNumWords()) * APINT_WORD_SIZE); 884 885 return Result; 886 } 887 888 APInt APInt::zextOrTrunc(unsigned width) const { 889 if (BitWidth < width) 890 return zext(width); 891 if (BitWidth > width) 892 return trunc(width); 893 return *this; 894 } 895 896 APInt APInt::sextOrTrunc(unsigned width) const { 897 if (BitWidth < width) 898 return sext(width); 899 if (BitWidth > width) 900 return trunc(width); 901 return *this; 902 } 903 904 APInt APInt::zextOrSelf(unsigned width) const { 905 if (BitWidth < width) 906 return zext(width); 907 return *this; 908 } 909 910 APInt APInt::sextOrSelf(unsigned width) const { 911 if (BitWidth < width) 912 return sext(width); 913 return *this; 914 } 915 916 /// Arithmetic right-shift this APInt by shiftAmt. 917 /// @brief Arithmetic right-shift function. 918 void APInt::ashrInPlace(const APInt &shiftAmt) { 919 ashrInPlace((unsigned)shiftAmt.getLimitedValue(BitWidth)); 920 } 921 922 /// Arithmetic right-shift this APInt by shiftAmt. 923 /// @brief Arithmetic right-shift function. 924 void APInt::ashrSlowCase(unsigned ShiftAmt) { 925 // Don't bother performing a no-op shift. 926 if (!ShiftAmt) 927 return; 928 929 // Save the original sign bit for later. 930 bool Negative = isNegative(); 931 932 // WordShift is the inter-part shift; BitShift is is intra-part shift. 933 unsigned WordShift = ShiftAmt / APINT_BITS_PER_WORD; 934 unsigned BitShift = ShiftAmt % APINT_BITS_PER_WORD; 935 936 unsigned WordsToMove = getNumWords() - WordShift; 937 if (WordsToMove != 0) { 938 // Sign extend the last word to fill in the unused bits. 939 U.pVal[getNumWords() - 1] = SignExtend64( 940 U.pVal[getNumWords() - 1], ((BitWidth - 1) % APINT_BITS_PER_WORD) + 1); 941 942 // Fastpath for moving by whole words. 943 if (BitShift == 0) { 944 std::memmove(U.pVal, U.pVal + WordShift, WordsToMove * APINT_WORD_SIZE); 945 } else { 946 // Move the words containing significant bits. 947 for (unsigned i = 0; i != WordsToMove - 1; ++i) 948 U.pVal[i] = (U.pVal[i + WordShift] >> BitShift) | 949 (U.pVal[i + WordShift + 1] << (APINT_BITS_PER_WORD - BitShift)); 950 951 // Handle the last word which has no high bits to copy. 952 U.pVal[WordsToMove - 1] = U.pVal[WordShift + WordsToMove - 1] >> BitShift; 953 // Sign extend one more time. 954 U.pVal[WordsToMove - 1] = 955 SignExtend64(U.pVal[WordsToMove - 1], APINT_BITS_PER_WORD - BitShift); 956 } 957 } 958 959 // Fill in the remainder based on the original sign. 960 std::memset(U.pVal + WordsToMove, Negative ? -1 : 0, 961 WordShift * APINT_WORD_SIZE); 962 clearUnusedBits(); 963 } 964 965 /// Logical right-shift this APInt by shiftAmt. 966 /// @brief Logical right-shift function. 967 void APInt::lshrInPlace(const APInt &shiftAmt) { 968 lshrInPlace((unsigned)shiftAmt.getLimitedValue(BitWidth)); 969 } 970 971 /// Logical right-shift this APInt by shiftAmt. 972 /// @brief Logical right-shift function. 973 void APInt::lshrSlowCase(unsigned ShiftAmt) { 974 tcShiftRight(U.pVal, getNumWords(), ShiftAmt); 975 } 976 977 /// Left-shift this APInt by shiftAmt. 978 /// @brief Left-shift function. 979 APInt &APInt::operator<<=(const APInt &shiftAmt) { 980 // It's undefined behavior in C to shift by BitWidth or greater. 981 *this <<= (unsigned)shiftAmt.getLimitedValue(BitWidth); 982 return *this; 983 } 984 985 void APInt::shlSlowCase(unsigned ShiftAmt) { 986 tcShiftLeft(U.pVal, getNumWords(), ShiftAmt); 987 clearUnusedBits(); 988 } 989 990 // Calculate the rotate amount modulo the bit width. 991 static unsigned rotateModulo(unsigned BitWidth, const APInt &rotateAmt) { 992 unsigned rotBitWidth = rotateAmt.getBitWidth(); 993 APInt rot = rotateAmt; 994 if (rotBitWidth < BitWidth) { 995 // Extend the rotate APInt, so that the urem doesn't divide by 0. 996 // e.g. APInt(1, 32) would give APInt(1, 0). 997 rot = rotateAmt.zext(BitWidth); 998 } 999 rot = rot.urem(APInt(rot.getBitWidth(), BitWidth)); 1000 return rot.getLimitedValue(BitWidth); 1001 } 1002 1003 APInt APInt::rotl(const APInt &rotateAmt) const { 1004 return rotl(rotateModulo(BitWidth, rotateAmt)); 1005 } 1006 1007 APInt APInt::rotl(unsigned rotateAmt) const { 1008 rotateAmt %= BitWidth; 1009 if (rotateAmt == 0) 1010 return *this; 1011 return shl(rotateAmt) | lshr(BitWidth - rotateAmt); 1012 } 1013 1014 APInt APInt::rotr(const APInt &rotateAmt) const { 1015 return rotr(rotateModulo(BitWidth, rotateAmt)); 1016 } 1017 1018 APInt APInt::rotr(unsigned rotateAmt) const { 1019 rotateAmt %= BitWidth; 1020 if (rotateAmt == 0) 1021 return *this; 1022 return lshr(rotateAmt) | shl(BitWidth - rotateAmt); 1023 } 1024 1025 // Square Root - this method computes and returns the square root of "this". 1026 // Three mechanisms are used for computation. For small values (<= 5 bits), 1027 // a table lookup is done. This gets some performance for common cases. For 1028 // values using less than 52 bits, the value is converted to double and then 1029 // the libc sqrt function is called. The result is rounded and then converted 1030 // back to a uint64_t which is then used to construct the result. Finally, 1031 // the Babylonian method for computing square roots is used. 1032 APInt APInt::sqrt() const { 1033 1034 // Determine the magnitude of the value. 1035 unsigned magnitude = getActiveBits(); 1036 1037 // Use a fast table for some small values. This also gets rid of some 1038 // rounding errors in libc sqrt for small values. 1039 if (magnitude <= 5) { 1040 static const uint8_t results[32] = { 1041 /* 0 */ 0, 1042 /* 1- 2 */ 1, 1, 1043 /* 3- 6 */ 2, 2, 2, 2, 1044 /* 7-12 */ 3, 3, 3, 3, 3, 3, 1045 /* 13-20 */ 4, 4, 4, 4, 4, 4, 4, 4, 1046 /* 21-30 */ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1047 /* 31 */ 6 1048 }; 1049 return APInt(BitWidth, results[ (isSingleWord() ? U.VAL : U.pVal[0]) ]); 1050 } 1051 1052 // If the magnitude of the value fits in less than 52 bits (the precision of 1053 // an IEEE double precision floating point value), then we can use the 1054 // libc sqrt function which will probably use a hardware sqrt computation. 1055 // This should be faster than the algorithm below. 1056 if (magnitude < 52) { 1057 return APInt(BitWidth, 1058 uint64_t(::round(::sqrt(double(isSingleWord() ? U.VAL 1059 : U.pVal[0]))))); 1060 } 1061 1062 // Okay, all the short cuts are exhausted. We must compute it. The following 1063 // is a classical Babylonian method for computing the square root. This code 1064 // was adapted to APInt from a wikipedia article on such computations. 1065 // See http://www.wikipedia.org/ and go to the page named 1066 // Calculate_an_integer_square_root. 1067 unsigned nbits = BitWidth, i = 4; 1068 APInt testy(BitWidth, 16); 1069 APInt x_old(BitWidth, 1); 1070 APInt x_new(BitWidth, 0); 1071 APInt two(BitWidth, 2); 1072 1073 // Select a good starting value using binary logarithms. 1074 for (;; i += 2, testy = testy.shl(2)) 1075 if (i >= nbits || this->ule(testy)) { 1076 x_old = x_old.shl(i / 2); 1077 break; 1078 } 1079 1080 // Use the Babylonian method to arrive at the integer square root: 1081 for (;;) { 1082 x_new = (this->udiv(x_old) + x_old).udiv(two); 1083 if (x_old.ule(x_new)) 1084 break; 1085 x_old = x_new; 1086 } 1087 1088 // Make sure we return the closest approximation 1089 // NOTE: The rounding calculation below is correct. It will produce an 1090 // off-by-one discrepancy with results from pari/gp. That discrepancy has been 1091 // determined to be a rounding issue with pari/gp as it begins to use a 1092 // floating point representation after 192 bits. There are no discrepancies 1093 // between this algorithm and pari/gp for bit widths < 192 bits. 1094 APInt square(x_old * x_old); 1095 APInt nextSquare((x_old + 1) * (x_old +1)); 1096 if (this->ult(square)) 1097 return x_old; 1098 assert(this->ule(nextSquare) && "Error in APInt::sqrt computation"); 1099 APInt midpoint((nextSquare - square).udiv(two)); 1100 APInt offset(*this - square); 1101 if (offset.ult(midpoint)) 1102 return x_old; 1103 return x_old + 1; 1104 } 1105 1106 /// Computes the multiplicative inverse of this APInt for a given modulo. The 1107 /// iterative extended Euclidean algorithm is used to solve for this value, 1108 /// however we simplify it to speed up calculating only the inverse, and take 1109 /// advantage of div+rem calculations. We also use some tricks to avoid copying 1110 /// (potentially large) APInts around. 1111 APInt APInt::multiplicativeInverse(const APInt& modulo) const { 1112 assert(ult(modulo) && "This APInt must be smaller than the modulo"); 1113 1114 // Using the properties listed at the following web page (accessed 06/21/08): 1115 // http://www.numbertheory.org/php/euclid.html 1116 // (especially the properties numbered 3, 4 and 9) it can be proved that 1117 // BitWidth bits suffice for all the computations in the algorithm implemented 1118 // below. More precisely, this number of bits suffice if the multiplicative 1119 // inverse exists, but may not suffice for the general extended Euclidean 1120 // algorithm. 1121 1122 APInt r[2] = { modulo, *this }; 1123 APInt t[2] = { APInt(BitWidth, 0), APInt(BitWidth, 1) }; 1124 APInt q(BitWidth, 0); 1125 1126 unsigned i; 1127 for (i = 0; r[i^1] != 0; i ^= 1) { 1128 // An overview of the math without the confusing bit-flipping: 1129 // q = r[i-2] / r[i-1] 1130 // r[i] = r[i-2] % r[i-1] 1131 // t[i] = t[i-2] - t[i-1] * q 1132 udivrem(r[i], r[i^1], q, r[i]); 1133 t[i] -= t[i^1] * q; 1134 } 1135 1136 // If this APInt and the modulo are not coprime, there is no multiplicative 1137 // inverse, so return 0. We check this by looking at the next-to-last 1138 // remainder, which is the gcd(*this,modulo) as calculated by the Euclidean 1139 // algorithm. 1140 if (r[i] != 1) 1141 return APInt(BitWidth, 0); 1142 1143 // The next-to-last t is the multiplicative inverse. However, we are 1144 // interested in a positive inverse. Calculate a positive one from a negative 1145 // one if necessary. A simple addition of the modulo suffices because 1146 // abs(t[i]) is known to be less than *this/2 (see the link above). 1147 if (t[i].isNegative()) 1148 t[i] += modulo; 1149 1150 return std::move(t[i]); 1151 } 1152 1153 /// Calculate the magic numbers required to implement a signed integer division 1154 /// by a constant as a sequence of multiplies, adds and shifts. Requires that 1155 /// the divisor not be 0, 1, or -1. Taken from "Hacker's Delight", Henry S. 1156 /// Warren, Jr., chapter 10. 1157 APInt::ms APInt::magic() const { 1158 const APInt& d = *this; 1159 unsigned p; 1160 APInt ad, anc, delta, q1, r1, q2, r2, t; 1161 APInt signedMin = APInt::getSignedMinValue(d.getBitWidth()); 1162 struct ms mag; 1163 1164 ad = d.abs(); 1165 t = signedMin + (d.lshr(d.getBitWidth() - 1)); 1166 anc = t - 1 - t.urem(ad); // absolute value of nc 1167 p = d.getBitWidth() - 1; // initialize p 1168 q1 = signedMin.udiv(anc); // initialize q1 = 2p/abs(nc) 1169 r1 = signedMin - q1*anc; // initialize r1 = rem(2p,abs(nc)) 1170 q2 = signedMin.udiv(ad); // initialize q2 = 2p/abs(d) 1171 r2 = signedMin - q2*ad; // initialize r2 = rem(2p,abs(d)) 1172 do { 1173 p = p + 1; 1174 q1 = q1<<1; // update q1 = 2p/abs(nc) 1175 r1 = r1<<1; // update r1 = rem(2p/abs(nc)) 1176 if (r1.uge(anc)) { // must be unsigned comparison 1177 q1 = q1 + 1; 1178 r1 = r1 - anc; 1179 } 1180 q2 = q2<<1; // update q2 = 2p/abs(d) 1181 r2 = r2<<1; // update r2 = rem(2p/abs(d)) 1182 if (r2.uge(ad)) { // must be unsigned comparison 1183 q2 = q2 + 1; 1184 r2 = r2 - ad; 1185 } 1186 delta = ad - r2; 1187 } while (q1.ult(delta) || (q1 == delta && r1 == 0)); 1188 1189 mag.m = q2 + 1; 1190 if (d.isNegative()) mag.m = -mag.m; // resulting magic number 1191 mag.s = p - d.getBitWidth(); // resulting shift 1192 return mag; 1193 } 1194 1195 /// Calculate the magic numbers required to implement an unsigned integer 1196 /// division by a constant as a sequence of multiplies, adds and shifts. 1197 /// Requires that the divisor not be 0. Taken from "Hacker's Delight", Henry 1198 /// S. Warren, Jr., chapter 10. 1199 /// LeadingZeros can be used to simplify the calculation if the upper bits 1200 /// of the divided value are known zero. 1201 APInt::mu APInt::magicu(unsigned LeadingZeros) const { 1202 const APInt& d = *this; 1203 unsigned p; 1204 APInt nc, delta, q1, r1, q2, r2; 1205 struct mu magu; 1206 magu.a = 0; // initialize "add" indicator 1207 APInt allOnes = APInt::getAllOnesValue(d.getBitWidth()).lshr(LeadingZeros); 1208 APInt signedMin = APInt::getSignedMinValue(d.getBitWidth()); 1209 APInt signedMax = APInt::getSignedMaxValue(d.getBitWidth()); 1210 1211 nc = allOnes - (allOnes - d).urem(d); 1212 p = d.getBitWidth() - 1; // initialize p 1213 q1 = signedMin.udiv(nc); // initialize q1 = 2p/nc 1214 r1 = signedMin - q1*nc; // initialize r1 = rem(2p,nc) 1215 q2 = signedMax.udiv(d); // initialize q2 = (2p-1)/d 1216 r2 = signedMax - q2*d; // initialize r2 = rem((2p-1),d) 1217 do { 1218 p = p + 1; 1219 if (r1.uge(nc - r1)) { 1220 q1 = q1 + q1 + 1; // update q1 1221 r1 = r1 + r1 - nc; // update r1 1222 } 1223 else { 1224 q1 = q1+q1; // update q1 1225 r1 = r1+r1; // update r1 1226 } 1227 if ((r2 + 1).uge(d - r2)) { 1228 if (q2.uge(signedMax)) magu.a = 1; 1229 q2 = q2+q2 + 1; // update q2 1230 r2 = r2+r2 + 1 - d; // update r2 1231 } 1232 else { 1233 if (q2.uge(signedMin)) magu.a = 1; 1234 q2 = q2+q2; // update q2 1235 r2 = r2+r2 + 1; // update r2 1236 } 1237 delta = d - 1 - r2; 1238 } while (p < d.getBitWidth()*2 && 1239 (q1.ult(delta) || (q1 == delta && r1 == 0))); 1240 magu.m = q2 + 1; // resulting magic number 1241 magu.s = p - d.getBitWidth(); // resulting shift 1242 return magu; 1243 } 1244 1245 /// Implementation of Knuth's Algorithm D (Division of nonnegative integers) 1246 /// from "Art of Computer Programming, Volume 2", section 4.3.1, p. 272. The 1247 /// variables here have the same names as in the algorithm. Comments explain 1248 /// the algorithm and any deviation from it. 1249 static void KnuthDiv(uint32_t *u, uint32_t *v, uint32_t *q, uint32_t* r, 1250 unsigned m, unsigned n) { 1251 assert(u && "Must provide dividend"); 1252 assert(v && "Must provide divisor"); 1253 assert(q && "Must provide quotient"); 1254 assert(u != v && u != q && v != q && "Must use different memory"); 1255 assert(n>1 && "n must be > 1"); 1256 1257 // b denotes the base of the number system. In our case b is 2^32. 1258 const uint64_t b = uint64_t(1) << 32; 1259 1260 DEBUG(dbgs() << "KnuthDiv: m=" << m << " n=" << n << '\n'); 1261 DEBUG(dbgs() << "KnuthDiv: original:"); 1262 DEBUG(for (int i = m+n; i >=0; i--) dbgs() << " " << u[i]); 1263 DEBUG(dbgs() << " by"); 1264 DEBUG(for (int i = n; i >0; i--) dbgs() << " " << v[i-1]); 1265 DEBUG(dbgs() << '\n'); 1266 // D1. [Normalize.] Set d = b / (v[n-1] + 1) and multiply all the digits of 1267 // u and v by d. Note that we have taken Knuth's advice here to use a power 1268 // of 2 value for d such that d * v[n-1] >= b/2 (b is the base). A power of 1269 // 2 allows us to shift instead of multiply and it is easy to determine the 1270 // shift amount from the leading zeros. We are basically normalizing the u 1271 // and v so that its high bits are shifted to the top of v's range without 1272 // overflow. Note that this can require an extra word in u so that u must 1273 // be of length m+n+1. 1274 unsigned shift = countLeadingZeros(v[n-1]); 1275 uint32_t v_carry = 0; 1276 uint32_t u_carry = 0; 1277 if (shift) { 1278 for (unsigned i = 0; i < m+n; ++i) { 1279 uint32_t u_tmp = u[i] >> (32 - shift); 1280 u[i] = (u[i] << shift) | u_carry; 1281 u_carry = u_tmp; 1282 } 1283 for (unsigned i = 0; i < n; ++i) { 1284 uint32_t v_tmp = v[i] >> (32 - shift); 1285 v[i] = (v[i] << shift) | v_carry; 1286 v_carry = v_tmp; 1287 } 1288 } 1289 u[m+n] = u_carry; 1290 1291 DEBUG(dbgs() << "KnuthDiv: normal:"); 1292 DEBUG(for (int i = m+n; i >=0; i--) dbgs() << " " << u[i]); 1293 DEBUG(dbgs() << " by"); 1294 DEBUG(for (int i = n; i >0; i--) dbgs() << " " << v[i-1]); 1295 DEBUG(dbgs() << '\n'); 1296 1297 // D2. [Initialize j.] Set j to m. This is the loop counter over the places. 1298 int j = m; 1299 do { 1300 DEBUG(dbgs() << "KnuthDiv: quotient digit #" << j << '\n'); 1301 // D3. [Calculate q'.]. 1302 // Set qp = (u[j+n]*b + u[j+n-1]) / v[n-1]. (qp=qprime=q') 1303 // Set rp = (u[j+n]*b + u[j+n-1]) % v[n-1]. (rp=rprime=r') 1304 // Now test if qp == b or qp*v[n-2] > b*rp + u[j+n-2]; if so, decrease 1305 // qp by 1, increase rp by v[n-1], and repeat this test if rp < b. The test 1306 // on v[n-2] determines at high speed most of the cases in which the trial 1307 // value qp is one too large, and it eliminates all cases where qp is two 1308 // too large. 1309 uint64_t dividend = Make_64(u[j+n], u[j+n-1]); 1310 DEBUG(dbgs() << "KnuthDiv: dividend == " << dividend << '\n'); 1311 uint64_t qp = dividend / v[n-1]; 1312 uint64_t rp = dividend % v[n-1]; 1313 if (qp == b || qp*v[n-2] > b*rp + u[j+n-2]) { 1314 qp--; 1315 rp += v[n-1]; 1316 if (rp < b && (qp == b || qp*v[n-2] > b*rp + u[j+n-2])) 1317 qp--; 1318 } 1319 DEBUG(dbgs() << "KnuthDiv: qp == " << qp << ", rp == " << rp << '\n'); 1320 1321 // D4. [Multiply and subtract.] Replace (u[j+n]u[j+n-1]...u[j]) with 1322 // (u[j+n]u[j+n-1]..u[j]) - qp * (v[n-1]...v[1]v[0]). This computation 1323 // consists of a simple multiplication by a one-place number, combined with 1324 // a subtraction. 1325 // The digits (u[j+n]...u[j]) should be kept positive; if the result of 1326 // this step is actually negative, (u[j+n]...u[j]) should be left as the 1327 // true value plus b**(n+1), namely as the b's complement of 1328 // the true value, and a "borrow" to the left should be remembered. 1329 int64_t borrow = 0; 1330 for (unsigned i = 0; i < n; ++i) { 1331 uint64_t p = uint64_t(qp) * uint64_t(v[i]); 1332 int64_t subres = int64_t(u[j+i]) - borrow - Lo_32(p); 1333 u[j+i] = Lo_32(subres); 1334 borrow = Hi_32(p) - Hi_32(subres); 1335 DEBUG(dbgs() << "KnuthDiv: u[j+i] = " << u[j+i] 1336 << ", borrow = " << borrow << '\n'); 1337 } 1338 bool isNeg = u[j+n] < borrow; 1339 u[j+n] -= Lo_32(borrow); 1340 1341 DEBUG(dbgs() << "KnuthDiv: after subtraction:"); 1342 DEBUG(for (int i = m+n; i >=0; i--) dbgs() << " " << u[i]); 1343 DEBUG(dbgs() << '\n'); 1344 1345 // D5. [Test remainder.] Set q[j] = qp. If the result of step D4 was 1346 // negative, go to step D6; otherwise go on to step D7. 1347 q[j] = Lo_32(qp); 1348 if (isNeg) { 1349 // D6. [Add back]. The probability that this step is necessary is very 1350 // small, on the order of only 2/b. Make sure that test data accounts for 1351 // this possibility. Decrease q[j] by 1 1352 q[j]--; 1353 // and add (0v[n-1]...v[1]v[0]) to (u[j+n]u[j+n-1]...u[j+1]u[j]). 1354 // A carry will occur to the left of u[j+n], and it should be ignored 1355 // since it cancels with the borrow that occurred in D4. 1356 bool carry = false; 1357 for (unsigned i = 0; i < n; i++) { 1358 uint32_t limit = std::min(u[j+i],v[i]); 1359 u[j+i] += v[i] + carry; 1360 carry = u[j+i] < limit || (carry && u[j+i] == limit); 1361 } 1362 u[j+n] += carry; 1363 } 1364 DEBUG(dbgs() << "KnuthDiv: after correction:"); 1365 DEBUG(for (int i = m+n; i >=0; i--) dbgs() << " " << u[i]); 1366 DEBUG(dbgs() << "\nKnuthDiv: digit result = " << q[j] << '\n'); 1367 1368 // D7. [Loop on j.] Decrease j by one. Now if j >= 0, go back to D3. 1369 } while (--j >= 0); 1370 1371 DEBUG(dbgs() << "KnuthDiv: quotient:"); 1372 DEBUG(for (int i = m; i >=0; i--) dbgs() <<" " << q[i]); 1373 DEBUG(dbgs() << '\n'); 1374 1375 // D8. [Unnormalize]. Now q[...] is the desired quotient, and the desired 1376 // remainder may be obtained by dividing u[...] by d. If r is non-null we 1377 // compute the remainder (urem uses this). 1378 if (r) { 1379 // The value d is expressed by the "shift" value above since we avoided 1380 // multiplication by d by using a shift left. So, all we have to do is 1381 // shift right here. 1382 if (shift) { 1383 uint32_t carry = 0; 1384 DEBUG(dbgs() << "KnuthDiv: remainder:"); 1385 for (int i = n-1; i >= 0; i--) { 1386 r[i] = (u[i] >> shift) | carry; 1387 carry = u[i] << (32 - shift); 1388 DEBUG(dbgs() << " " << r[i]); 1389 } 1390 } else { 1391 for (int i = n-1; i >= 0; i--) { 1392 r[i] = u[i]; 1393 DEBUG(dbgs() << " " << r[i]); 1394 } 1395 } 1396 DEBUG(dbgs() << '\n'); 1397 } 1398 DEBUG(dbgs() << '\n'); 1399 } 1400 1401 void APInt::divide(const WordType *LHS, unsigned lhsWords, const WordType *RHS, 1402 unsigned rhsWords, WordType *Quotient, WordType *Remainder) { 1403 assert(lhsWords >= rhsWords && "Fractional result"); 1404 1405 // First, compose the values into an array of 32-bit words instead of 1406 // 64-bit words. This is a necessity of both the "short division" algorithm 1407 // and the Knuth "classical algorithm" which requires there to be native 1408 // operations for +, -, and * on an m bit value with an m*2 bit result. We 1409 // can't use 64-bit operands here because we don't have native results of 1410 // 128-bits. Furthermore, casting the 64-bit values to 32-bit values won't 1411 // work on large-endian machines. 1412 unsigned n = rhsWords * 2; 1413 unsigned m = (lhsWords * 2) - n; 1414 1415 // Allocate space for the temporary values we need either on the stack, if 1416 // it will fit, or on the heap if it won't. 1417 uint32_t SPACE[128]; 1418 uint32_t *U = nullptr; 1419 uint32_t *V = nullptr; 1420 uint32_t *Q = nullptr; 1421 uint32_t *R = nullptr; 1422 if ((Remainder?4:3)*n+2*m+1 <= 128) { 1423 U = &SPACE[0]; 1424 V = &SPACE[m+n+1]; 1425 Q = &SPACE[(m+n+1) + n]; 1426 if (Remainder) 1427 R = &SPACE[(m+n+1) + n + (m+n)]; 1428 } else { 1429 U = new uint32_t[m + n + 1]; 1430 V = new uint32_t[n]; 1431 Q = new uint32_t[m+n]; 1432 if (Remainder) 1433 R = new uint32_t[n]; 1434 } 1435 1436 // Initialize the dividend 1437 memset(U, 0, (m+n+1)*sizeof(uint32_t)); 1438 for (unsigned i = 0; i < lhsWords; ++i) { 1439 uint64_t tmp = LHS[i]; 1440 U[i * 2] = Lo_32(tmp); 1441 U[i * 2 + 1] = Hi_32(tmp); 1442 } 1443 U[m+n] = 0; // this extra word is for "spill" in the Knuth algorithm. 1444 1445 // Initialize the divisor 1446 memset(V, 0, (n)*sizeof(uint32_t)); 1447 for (unsigned i = 0; i < rhsWords; ++i) { 1448 uint64_t tmp = RHS[i]; 1449 V[i * 2] = Lo_32(tmp); 1450 V[i * 2 + 1] = Hi_32(tmp); 1451 } 1452 1453 // initialize the quotient and remainder 1454 memset(Q, 0, (m+n) * sizeof(uint32_t)); 1455 if (Remainder) 1456 memset(R, 0, n * sizeof(uint32_t)); 1457 1458 // Now, adjust m and n for the Knuth division. n is the number of words in 1459 // the divisor. m is the number of words by which the dividend exceeds the 1460 // divisor (i.e. m+n is the length of the dividend). These sizes must not 1461 // contain any zero words or the Knuth algorithm fails. 1462 for (unsigned i = n; i > 0 && V[i-1] == 0; i--) { 1463 n--; 1464 m++; 1465 } 1466 for (unsigned i = m+n; i > 0 && U[i-1] == 0; i--) 1467 m--; 1468 1469 // If we're left with only a single word for the divisor, Knuth doesn't work 1470 // so we implement the short division algorithm here. This is much simpler 1471 // and faster because we are certain that we can divide a 64-bit quantity 1472 // by a 32-bit quantity at hardware speed and short division is simply a 1473 // series of such operations. This is just like doing short division but we 1474 // are using base 2^32 instead of base 10. 1475 assert(n != 0 && "Divide by zero?"); 1476 if (n == 1) { 1477 uint32_t divisor = V[0]; 1478 uint32_t remainder = 0; 1479 for (int i = m; i >= 0; i--) { 1480 uint64_t partial_dividend = Make_64(remainder, U[i]); 1481 if (partial_dividend == 0) { 1482 Q[i] = 0; 1483 remainder = 0; 1484 } else if (partial_dividend < divisor) { 1485 Q[i] = 0; 1486 remainder = Lo_32(partial_dividend); 1487 } else if (partial_dividend == divisor) { 1488 Q[i] = 1; 1489 remainder = 0; 1490 } else { 1491 Q[i] = Lo_32(partial_dividend / divisor); 1492 remainder = Lo_32(partial_dividend - (Q[i] * divisor)); 1493 } 1494 } 1495 if (R) 1496 R[0] = remainder; 1497 } else { 1498 // Now we're ready to invoke the Knuth classical divide algorithm. In this 1499 // case n > 1. 1500 KnuthDiv(U, V, Q, R, m, n); 1501 } 1502 1503 // If the caller wants the quotient 1504 if (Quotient) { 1505 for (unsigned i = 0; i < lhsWords; ++i) 1506 Quotient[i] = Make_64(Q[i*2+1], Q[i*2]); 1507 } 1508 1509 // If the caller wants the remainder 1510 if (Remainder) { 1511 for (unsigned i = 0; i < rhsWords; ++i) 1512 Remainder[i] = Make_64(R[i*2+1], R[i*2]); 1513 } 1514 1515 // Clean up the memory we allocated. 1516 if (U != &SPACE[0]) { 1517 delete [] U; 1518 delete [] V; 1519 delete [] Q; 1520 delete [] R; 1521 } 1522 } 1523 1524 APInt APInt::udiv(const APInt &RHS) const { 1525 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 1526 1527 // First, deal with the easy case 1528 if (isSingleWord()) { 1529 assert(RHS.U.VAL != 0 && "Divide by zero?"); 1530 return APInt(BitWidth, U.VAL / RHS.U.VAL); 1531 } 1532 1533 // Get some facts about the LHS and RHS number of bits and words 1534 unsigned lhsWords = getNumWords(getActiveBits()); 1535 unsigned rhsBits = RHS.getActiveBits(); 1536 unsigned rhsWords = getNumWords(rhsBits); 1537 assert(rhsWords && "Divided by zero???"); 1538 1539 // Deal with some degenerate cases 1540 if (!lhsWords) 1541 // 0 / X ===> 0 1542 return APInt(BitWidth, 0); 1543 if (rhsBits == 1) 1544 // X / 1 ===> X 1545 return *this; 1546 if (lhsWords < rhsWords || this->ult(RHS)) 1547 // X / Y ===> 0, iff X < Y 1548 return APInt(BitWidth, 0); 1549 if (*this == RHS) 1550 // X / X ===> 1 1551 return APInt(BitWidth, 1); 1552 if (lhsWords == 1) // rhsWords is 1 if lhsWords is 1. 1553 // All high words are zero, just use native divide 1554 return APInt(BitWidth, this->U.pVal[0] / RHS.U.pVal[0]); 1555 1556 // We have to compute it the hard way. Invoke the Knuth divide algorithm. 1557 APInt Quotient(BitWidth, 0); // to hold result. 1558 divide(U.pVal, lhsWords, RHS.U.pVal, rhsWords, Quotient.U.pVal, nullptr); 1559 return Quotient; 1560 } 1561 1562 APInt APInt::udiv(uint64_t RHS) const { 1563 assert(RHS != 0 && "Divide by zero?"); 1564 1565 // First, deal with the easy case 1566 if (isSingleWord()) 1567 return APInt(BitWidth, U.VAL / RHS); 1568 1569 // Get some facts about the LHS words. 1570 unsigned lhsWords = getNumWords(getActiveBits()); 1571 1572 // Deal with some degenerate cases 1573 if (!lhsWords) 1574 // 0 / X ===> 0 1575 return APInt(BitWidth, 0); 1576 if (RHS == 1) 1577 // X / 1 ===> X 1578 return *this; 1579 if (this->ult(RHS)) 1580 // X / Y ===> 0, iff X < Y 1581 return APInt(BitWidth, 0); 1582 if (*this == RHS) 1583 // X / X ===> 1 1584 return APInt(BitWidth, 1); 1585 if (lhsWords == 1) // rhsWords is 1 if lhsWords is 1. 1586 // All high words are zero, just use native divide 1587 return APInt(BitWidth, this->U.pVal[0] / RHS); 1588 1589 // We have to compute it the hard way. Invoke the Knuth divide algorithm. 1590 APInt Quotient(BitWidth, 0); // to hold result. 1591 divide(U.pVal, lhsWords, &RHS, 1, Quotient.U.pVal, nullptr); 1592 return Quotient; 1593 } 1594 1595 APInt APInt::sdiv(const APInt &RHS) const { 1596 if (isNegative()) { 1597 if (RHS.isNegative()) 1598 return (-(*this)).udiv(-RHS); 1599 return -((-(*this)).udiv(RHS)); 1600 } 1601 if (RHS.isNegative()) 1602 return -(this->udiv(-RHS)); 1603 return this->udiv(RHS); 1604 } 1605 1606 APInt APInt::sdiv(int64_t RHS) const { 1607 if (isNegative()) { 1608 if (RHS < 0) 1609 return (-(*this)).udiv(-RHS); 1610 return -((-(*this)).udiv(RHS)); 1611 } 1612 if (RHS < 0) 1613 return -(this->udiv(-RHS)); 1614 return this->udiv(RHS); 1615 } 1616 1617 APInt APInt::urem(const APInt &RHS) const { 1618 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 1619 if (isSingleWord()) { 1620 assert(RHS.U.VAL != 0 && "Remainder by zero?"); 1621 return APInt(BitWidth, U.VAL % RHS.U.VAL); 1622 } 1623 1624 // Get some facts about the LHS 1625 unsigned lhsWords = getNumWords(getActiveBits()); 1626 1627 // Get some facts about the RHS 1628 unsigned rhsBits = RHS.getActiveBits(); 1629 unsigned rhsWords = getNumWords(rhsBits); 1630 assert(rhsWords && "Performing remainder operation by zero ???"); 1631 1632 // Check the degenerate cases 1633 if (lhsWords == 0) 1634 // 0 % Y ===> 0 1635 return APInt(BitWidth, 0); 1636 if (rhsBits == 1) 1637 // X % 1 ===> 0 1638 return APInt(BitWidth, 0); 1639 if (lhsWords < rhsWords || this->ult(RHS)) 1640 // X % Y ===> X, iff X < Y 1641 return *this; 1642 if (*this == RHS) 1643 // X % X == 0; 1644 return APInt(BitWidth, 0); 1645 if (lhsWords == 1) 1646 // All high words are zero, just use native remainder 1647 return APInt(BitWidth, U.pVal[0] % RHS.U.pVal[0]); 1648 1649 // We have to compute it the hard way. Invoke the Knuth divide algorithm. 1650 APInt Remainder(BitWidth, 0); 1651 divide(U.pVal, lhsWords, RHS.U.pVal, rhsWords, nullptr, Remainder.U.pVal); 1652 return Remainder; 1653 } 1654 1655 uint64_t APInt::urem(uint64_t RHS) const { 1656 assert(RHS != 0 && "Remainder by zero?"); 1657 1658 if (isSingleWord()) 1659 return U.VAL % RHS; 1660 1661 // Get some facts about the LHS 1662 unsigned lhsWords = getNumWords(getActiveBits()); 1663 1664 // Check the degenerate cases 1665 if (lhsWords == 0) 1666 // 0 % Y ===> 0 1667 return 0; 1668 if (RHS == 1) 1669 // X % 1 ===> 0 1670 return 0; 1671 if (this->ult(RHS)) 1672 // X % Y ===> X, iff X < Y 1673 return getZExtValue(); 1674 if (*this == RHS) 1675 // X % X == 0; 1676 return 0; 1677 if (lhsWords == 1) 1678 // All high words are zero, just use native remainder 1679 return U.pVal[0] % RHS; 1680 1681 // We have to compute it the hard way. Invoke the Knuth divide algorithm. 1682 uint64_t Remainder; 1683 divide(U.pVal, lhsWords, &RHS, 1, nullptr, &Remainder); 1684 return Remainder; 1685 } 1686 1687 APInt APInt::srem(const APInt &RHS) const { 1688 if (isNegative()) { 1689 if (RHS.isNegative()) 1690 return -((-(*this)).urem(-RHS)); 1691 return -((-(*this)).urem(RHS)); 1692 } 1693 if (RHS.isNegative()) 1694 return this->urem(-RHS); 1695 return this->urem(RHS); 1696 } 1697 1698 int64_t APInt::srem(int64_t RHS) const { 1699 if (isNegative()) { 1700 if (RHS < 0) 1701 return -((-(*this)).urem(-RHS)); 1702 return -((-(*this)).urem(RHS)); 1703 } 1704 if (RHS < 0) 1705 return this->urem(-RHS); 1706 return this->urem(RHS); 1707 } 1708 1709 void APInt::udivrem(const APInt &LHS, const APInt &RHS, 1710 APInt &Quotient, APInt &Remainder) { 1711 assert(LHS.BitWidth == RHS.BitWidth && "Bit widths must be the same"); 1712 unsigned BitWidth = LHS.BitWidth; 1713 1714 // First, deal with the easy case 1715 if (LHS.isSingleWord()) { 1716 assert(RHS.U.VAL != 0 && "Divide by zero?"); 1717 uint64_t QuotVal = LHS.U.VAL / RHS.U.VAL; 1718 uint64_t RemVal = LHS.U.VAL % RHS.U.VAL; 1719 Quotient = APInt(BitWidth, QuotVal); 1720 Remainder = APInt(BitWidth, RemVal); 1721 return; 1722 } 1723 1724 // Get some size facts about the dividend and divisor 1725 unsigned lhsWords = getNumWords(LHS.getActiveBits()); 1726 unsigned rhsBits = RHS.getActiveBits(); 1727 unsigned rhsWords = getNumWords(rhsBits); 1728 assert(rhsWords && "Performing divrem operation by zero ???"); 1729 1730 // Check the degenerate cases 1731 if (lhsWords == 0) { 1732 Quotient = 0; // 0 / Y ===> 0 1733 Remainder = 0; // 0 % Y ===> 0 1734 return; 1735 } 1736 1737 if (rhsBits == 1) { 1738 Quotient = LHS; // X / 1 ===> X 1739 Remainder = 0; // X % 1 ===> 0 1740 } 1741 1742 if (lhsWords < rhsWords || LHS.ult(RHS)) { 1743 Remainder = LHS; // X % Y ===> X, iff X < Y 1744 Quotient = 0; // X / Y ===> 0, iff X < Y 1745 return; 1746 } 1747 1748 if (LHS == RHS) { 1749 Quotient = 1; // X / X ===> 1 1750 Remainder = 0; // X % X ===> 0; 1751 return; 1752 } 1753 1754 // Make sure there is enough space to hold the results. 1755 // NOTE: This assumes that reallocate won't affect any bits if it doesn't 1756 // change the size. This is necessary if Quotient or Remainder is aliased 1757 // with LHS or RHS. 1758 Quotient.reallocate(BitWidth); 1759 Remainder.reallocate(BitWidth); 1760 1761 if (lhsWords == 1) { // rhsWords is 1 if lhsWords is 1. 1762 // There is only one word to consider so use the native versions. 1763 uint64_t lhsValue = LHS.U.pVal[0]; 1764 uint64_t rhsValue = RHS.U.pVal[0]; 1765 Quotient = lhsValue / rhsValue; 1766 Remainder = lhsValue % rhsValue; 1767 return; 1768 } 1769 1770 // Okay, lets do it the long way 1771 divide(LHS.U.pVal, lhsWords, RHS.U.pVal, rhsWords, Quotient.U.pVal, 1772 Remainder.U.pVal); 1773 // Clear the rest of the Quotient and Remainder. 1774 std::memset(Quotient.U.pVal + lhsWords, 0, 1775 (getNumWords(BitWidth) - lhsWords) * APINT_WORD_SIZE); 1776 std::memset(Remainder.U.pVal + rhsWords, 0, 1777 (getNumWords(BitWidth) - rhsWords) * APINT_WORD_SIZE); 1778 } 1779 1780 void APInt::udivrem(const APInt &LHS, uint64_t RHS, APInt &Quotient, 1781 uint64_t &Remainder) { 1782 assert(RHS != 0 && "Divide by zero?"); 1783 unsigned BitWidth = LHS.BitWidth; 1784 1785 // First, deal with the easy case 1786 if (LHS.isSingleWord()) { 1787 uint64_t QuotVal = LHS.U.VAL / RHS; 1788 Remainder = LHS.U.VAL % RHS; 1789 Quotient = APInt(BitWidth, QuotVal); 1790 return; 1791 } 1792 1793 // Get some size facts about the dividend and divisor 1794 unsigned lhsWords = getNumWords(LHS.getActiveBits()); 1795 1796 // Check the degenerate cases 1797 if (lhsWords == 0) { 1798 Quotient = 0; // 0 / Y ===> 0 1799 Remainder = 0; // 0 % Y ===> 0 1800 return; 1801 } 1802 1803 if (RHS == 1) { 1804 Quotient = LHS; // X / 1 ===> X 1805 Remainder = 0; // X % 1 ===> 0 1806 } 1807 1808 if (LHS.ult(RHS)) { 1809 Remainder = LHS.getZExtValue(); // X % Y ===> X, iff X < Y 1810 Quotient = 0; // X / Y ===> 0, iff X < Y 1811 return; 1812 } 1813 1814 if (LHS == RHS) { 1815 Quotient = 1; // X / X ===> 1 1816 Remainder = 0; // X % X ===> 0; 1817 return; 1818 } 1819 1820 // Make sure there is enough space to hold the results. 1821 // NOTE: This assumes that reallocate won't affect any bits if it doesn't 1822 // change the size. This is necessary if Quotient is aliased with LHS. 1823 Quotient.reallocate(BitWidth); 1824 1825 if (lhsWords == 1) { // rhsWords is 1 if lhsWords is 1. 1826 // There is only one word to consider so use the native versions. 1827 uint64_t lhsValue = LHS.U.pVal[0]; 1828 Quotient = lhsValue / RHS; 1829 Remainder = lhsValue % RHS; 1830 return; 1831 } 1832 1833 // Okay, lets do it the long way 1834 divide(LHS.U.pVal, lhsWords, &RHS, 1, Quotient.U.pVal, &Remainder); 1835 // Clear the rest of the Quotient. 1836 std::memset(Quotient.U.pVal + lhsWords, 0, 1837 (getNumWords(BitWidth) - lhsWords) * APINT_WORD_SIZE); 1838 } 1839 1840 void APInt::sdivrem(const APInt &LHS, const APInt &RHS, 1841 APInt &Quotient, APInt &Remainder) { 1842 if (LHS.isNegative()) { 1843 if (RHS.isNegative()) 1844 APInt::udivrem(-LHS, -RHS, Quotient, Remainder); 1845 else { 1846 APInt::udivrem(-LHS, RHS, Quotient, Remainder); 1847 Quotient.negate(); 1848 } 1849 Remainder.negate(); 1850 } else if (RHS.isNegative()) { 1851 APInt::udivrem(LHS, -RHS, Quotient, Remainder); 1852 Quotient.negate(); 1853 } else { 1854 APInt::udivrem(LHS, RHS, Quotient, Remainder); 1855 } 1856 } 1857 1858 void APInt::sdivrem(const APInt &LHS, int64_t RHS, 1859 APInt &Quotient, int64_t &Remainder) { 1860 uint64_t R = Remainder; 1861 if (LHS.isNegative()) { 1862 if (RHS < 0) 1863 APInt::udivrem(-LHS, -RHS, Quotient, R); 1864 else { 1865 APInt::udivrem(-LHS, RHS, Quotient, R); 1866 Quotient.negate(); 1867 } 1868 R = -R; 1869 } else if (RHS < 0) { 1870 APInt::udivrem(LHS, -RHS, Quotient, R); 1871 Quotient.negate(); 1872 } else { 1873 APInt::udivrem(LHS, RHS, Quotient, R); 1874 } 1875 Remainder = R; 1876 } 1877 1878 APInt APInt::sadd_ov(const APInt &RHS, bool &Overflow) const { 1879 APInt Res = *this+RHS; 1880 Overflow = isNonNegative() == RHS.isNonNegative() && 1881 Res.isNonNegative() != isNonNegative(); 1882 return Res; 1883 } 1884 1885 APInt APInt::uadd_ov(const APInt &RHS, bool &Overflow) const { 1886 APInt Res = *this+RHS; 1887 Overflow = Res.ult(RHS); 1888 return Res; 1889 } 1890 1891 APInt APInt::ssub_ov(const APInt &RHS, bool &Overflow) const { 1892 APInt Res = *this - RHS; 1893 Overflow = isNonNegative() != RHS.isNonNegative() && 1894 Res.isNonNegative() != isNonNegative(); 1895 return Res; 1896 } 1897 1898 APInt APInt::usub_ov(const APInt &RHS, bool &Overflow) const { 1899 APInt Res = *this-RHS; 1900 Overflow = Res.ugt(*this); 1901 return Res; 1902 } 1903 1904 APInt APInt::sdiv_ov(const APInt &RHS, bool &Overflow) const { 1905 // MININT/-1 --> overflow. 1906 Overflow = isMinSignedValue() && RHS.isAllOnesValue(); 1907 return sdiv(RHS); 1908 } 1909 1910 APInt APInt::smul_ov(const APInt &RHS, bool &Overflow) const { 1911 APInt Res = *this * RHS; 1912 1913 if (*this != 0 && RHS != 0) 1914 Overflow = Res.sdiv(RHS) != *this || Res.sdiv(*this) != RHS; 1915 else 1916 Overflow = false; 1917 return Res; 1918 } 1919 1920 APInt APInt::umul_ov(const APInt &RHS, bool &Overflow) const { 1921 APInt Res = *this * RHS; 1922 1923 if (*this != 0 && RHS != 0) 1924 Overflow = Res.udiv(RHS) != *this || Res.udiv(*this) != RHS; 1925 else 1926 Overflow = false; 1927 return Res; 1928 } 1929 1930 APInt APInt::sshl_ov(const APInt &ShAmt, bool &Overflow) const { 1931 Overflow = ShAmt.uge(getBitWidth()); 1932 if (Overflow) 1933 return APInt(BitWidth, 0); 1934 1935 if (isNonNegative()) // Don't allow sign change. 1936 Overflow = ShAmt.uge(countLeadingZeros()); 1937 else 1938 Overflow = ShAmt.uge(countLeadingOnes()); 1939 1940 return *this << ShAmt; 1941 } 1942 1943 APInt APInt::ushl_ov(const APInt &ShAmt, bool &Overflow) const { 1944 Overflow = ShAmt.uge(getBitWidth()); 1945 if (Overflow) 1946 return APInt(BitWidth, 0); 1947 1948 Overflow = ShAmt.ugt(countLeadingZeros()); 1949 1950 return *this << ShAmt; 1951 } 1952 1953 1954 1955 1956 void APInt::fromString(unsigned numbits, StringRef str, uint8_t radix) { 1957 // Check our assumptions here 1958 assert(!str.empty() && "Invalid string length"); 1959 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2 || 1960 radix == 36) && 1961 "Radix should be 2, 8, 10, 16, or 36!"); 1962 1963 StringRef::iterator p = str.begin(); 1964 size_t slen = str.size(); 1965 bool isNeg = *p == '-'; 1966 if (*p == '-' || *p == '+') { 1967 p++; 1968 slen--; 1969 assert(slen && "String is only a sign, needs a value."); 1970 } 1971 assert((slen <= numbits || radix != 2) && "Insufficient bit width"); 1972 assert(((slen-1)*3 <= numbits || radix != 8) && "Insufficient bit width"); 1973 assert(((slen-1)*4 <= numbits || radix != 16) && "Insufficient bit width"); 1974 assert((((slen-1)*64)/22 <= numbits || radix != 10) && 1975 "Insufficient bit width"); 1976 1977 // Allocate memory if needed 1978 if (isSingleWord()) 1979 U.VAL = 0; 1980 else 1981 U.pVal = getClearedMemory(getNumWords()); 1982 1983 // Figure out if we can shift instead of multiply 1984 unsigned shift = (radix == 16 ? 4 : radix == 8 ? 3 : radix == 2 ? 1 : 0); 1985 1986 // Enter digit traversal loop 1987 for (StringRef::iterator e = str.end(); p != e; ++p) { 1988 unsigned digit = getDigit(*p, radix); 1989 assert(digit < radix && "Invalid character in digit string"); 1990 1991 // Shift or multiply the value by the radix 1992 if (slen > 1) { 1993 if (shift) 1994 *this <<= shift; 1995 else 1996 *this *= radix; 1997 } 1998 1999 // Add in the digit we just interpreted 2000 *this += digit; 2001 } 2002 // If its negative, put it in two's complement form 2003 if (isNeg) 2004 this->negate(); 2005 } 2006 2007 void APInt::toString(SmallVectorImpl<char> &Str, unsigned Radix, 2008 bool Signed, bool formatAsCLiteral) const { 2009 assert((Radix == 10 || Radix == 8 || Radix == 16 || Radix == 2 || 2010 Radix == 36) && 2011 "Radix should be 2, 8, 10, 16, or 36!"); 2012 2013 const char *Prefix = ""; 2014 if (formatAsCLiteral) { 2015 switch (Radix) { 2016 case 2: 2017 // Binary literals are a non-standard extension added in gcc 4.3: 2018 // http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Binary-constants.html 2019 Prefix = "0b"; 2020 break; 2021 case 8: 2022 Prefix = "0"; 2023 break; 2024 case 10: 2025 break; // No prefix 2026 case 16: 2027 Prefix = "0x"; 2028 break; 2029 default: 2030 llvm_unreachable("Invalid radix!"); 2031 } 2032 } 2033 2034 // First, check for a zero value and just short circuit the logic below. 2035 if (*this == 0) { 2036 while (*Prefix) { 2037 Str.push_back(*Prefix); 2038 ++Prefix; 2039 }; 2040 Str.push_back('0'); 2041 return; 2042 } 2043 2044 static const char Digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 2045 2046 if (isSingleWord()) { 2047 char Buffer[65]; 2048 char *BufPtr = std::end(Buffer); 2049 2050 uint64_t N; 2051 if (!Signed) { 2052 N = getZExtValue(); 2053 } else { 2054 int64_t I = getSExtValue(); 2055 if (I >= 0) { 2056 N = I; 2057 } else { 2058 Str.push_back('-'); 2059 N = -(uint64_t)I; 2060 } 2061 } 2062 2063 while (*Prefix) { 2064 Str.push_back(*Prefix); 2065 ++Prefix; 2066 }; 2067 2068 while (N) { 2069 *--BufPtr = Digits[N % Radix]; 2070 N /= Radix; 2071 } 2072 Str.append(BufPtr, std::end(Buffer)); 2073 return; 2074 } 2075 2076 APInt Tmp(*this); 2077 2078 if (Signed && isNegative()) { 2079 // They want to print the signed version and it is a negative value 2080 // Flip the bits and add one to turn it into the equivalent positive 2081 // value and put a '-' in the result. 2082 Tmp.negate(); 2083 Str.push_back('-'); 2084 } 2085 2086 while (*Prefix) { 2087 Str.push_back(*Prefix); 2088 ++Prefix; 2089 }; 2090 2091 // We insert the digits backward, then reverse them to get the right order. 2092 unsigned StartDig = Str.size(); 2093 2094 // For the 2, 8 and 16 bit cases, we can just shift instead of divide 2095 // because the number of bits per digit (1, 3 and 4 respectively) divides 2096 // equally. We just shift until the value is zero. 2097 if (Radix == 2 || Radix == 8 || Radix == 16) { 2098 // Just shift tmp right for each digit width until it becomes zero 2099 unsigned ShiftAmt = (Radix == 16 ? 4 : (Radix == 8 ? 3 : 1)); 2100 unsigned MaskAmt = Radix - 1; 2101 2102 while (Tmp.getBoolValue()) { 2103 unsigned Digit = unsigned(Tmp.getRawData()[0]) & MaskAmt; 2104 Str.push_back(Digits[Digit]); 2105 Tmp.lshrInPlace(ShiftAmt); 2106 } 2107 } else { 2108 while (Tmp.getBoolValue()) { 2109 uint64_t Digit; 2110 udivrem(Tmp, Radix, Tmp, Digit); 2111 assert(Digit < Radix && "divide failed"); 2112 Str.push_back(Digits[Digit]); 2113 } 2114 } 2115 2116 // Reverse the digits before returning. 2117 std::reverse(Str.begin()+StartDig, Str.end()); 2118 } 2119 2120 /// Returns the APInt as a std::string. Note that this is an inefficient method. 2121 /// It is better to pass in a SmallVector/SmallString to the methods above. 2122 std::string APInt::toString(unsigned Radix = 10, bool Signed = true) const { 2123 SmallString<40> S; 2124 toString(S, Radix, Signed, /* formatAsCLiteral = */false); 2125 return S.str(); 2126 } 2127 2128 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 2129 LLVM_DUMP_METHOD void APInt::dump() const { 2130 SmallString<40> S, U; 2131 this->toStringUnsigned(U); 2132 this->toStringSigned(S); 2133 dbgs() << "APInt(" << BitWidth << "b, " 2134 << U << "u " << S << "s)\n"; 2135 } 2136 #endif 2137 2138 void APInt::print(raw_ostream &OS, bool isSigned) const { 2139 SmallString<40> S; 2140 this->toString(S, 10, isSigned, /* formatAsCLiteral = */false); 2141 OS << S; 2142 } 2143 2144 // This implements a variety of operations on a representation of 2145 // arbitrary precision, two's-complement, bignum integer values. 2146 2147 // Assumed by lowHalf, highHalf, partMSB and partLSB. A fairly safe 2148 // and unrestricting assumption. 2149 static_assert(APInt::APINT_BITS_PER_WORD % 2 == 0, 2150 "Part width must be divisible by 2!"); 2151 2152 /* Some handy functions local to this file. */ 2153 2154 /* Returns the integer part with the least significant BITS set. 2155 BITS cannot be zero. */ 2156 static inline APInt::WordType lowBitMask(unsigned bits) { 2157 assert(bits != 0 && bits <= APInt::APINT_BITS_PER_WORD); 2158 2159 return ~(APInt::WordType) 0 >> (APInt::APINT_BITS_PER_WORD - bits); 2160 } 2161 2162 /* Returns the value of the lower half of PART. */ 2163 static inline APInt::WordType lowHalf(APInt::WordType part) { 2164 return part & lowBitMask(APInt::APINT_BITS_PER_WORD / 2); 2165 } 2166 2167 /* Returns the value of the upper half of PART. */ 2168 static inline APInt::WordType highHalf(APInt::WordType part) { 2169 return part >> (APInt::APINT_BITS_PER_WORD / 2); 2170 } 2171 2172 /* Returns the bit number of the most significant set bit of a part. 2173 If the input number has no bits set -1U is returned. */ 2174 static unsigned partMSB(APInt::WordType value) { 2175 return findLastSet(value, ZB_Max); 2176 } 2177 2178 /* Returns the bit number of the least significant set bit of a 2179 part. If the input number has no bits set -1U is returned. */ 2180 static unsigned partLSB(APInt::WordType value) { 2181 return findFirstSet(value, ZB_Max); 2182 } 2183 2184 /* Sets the least significant part of a bignum to the input value, and 2185 zeroes out higher parts. */ 2186 void APInt::tcSet(WordType *dst, WordType part, unsigned parts) { 2187 assert(parts > 0); 2188 2189 dst[0] = part; 2190 for (unsigned i = 1; i < parts; i++) 2191 dst[i] = 0; 2192 } 2193 2194 /* Assign one bignum to another. */ 2195 void APInt::tcAssign(WordType *dst, const WordType *src, unsigned parts) { 2196 for (unsigned i = 0; i < parts; i++) 2197 dst[i] = src[i]; 2198 } 2199 2200 /* Returns true if a bignum is zero, false otherwise. */ 2201 bool APInt::tcIsZero(const WordType *src, unsigned parts) { 2202 for (unsigned i = 0; i < parts; i++) 2203 if (src[i]) 2204 return false; 2205 2206 return true; 2207 } 2208 2209 /* Extract the given bit of a bignum; returns 0 or 1. */ 2210 int APInt::tcExtractBit(const WordType *parts, unsigned bit) { 2211 return (parts[whichWord(bit)] & maskBit(bit)) != 0; 2212 } 2213 2214 /* Set the given bit of a bignum. */ 2215 void APInt::tcSetBit(WordType *parts, unsigned bit) { 2216 parts[whichWord(bit)] |= maskBit(bit); 2217 } 2218 2219 /* Clears the given bit of a bignum. */ 2220 void APInt::tcClearBit(WordType *parts, unsigned bit) { 2221 parts[whichWord(bit)] &= ~maskBit(bit); 2222 } 2223 2224 /* Returns the bit number of the least significant set bit of a 2225 number. If the input number has no bits set -1U is returned. */ 2226 unsigned APInt::tcLSB(const WordType *parts, unsigned n) { 2227 for (unsigned i = 0; i < n; i++) { 2228 if (parts[i] != 0) { 2229 unsigned lsb = partLSB(parts[i]); 2230 2231 return lsb + i * APINT_BITS_PER_WORD; 2232 } 2233 } 2234 2235 return -1U; 2236 } 2237 2238 /* Returns the bit number of the most significant set bit of a number. 2239 If the input number has no bits set -1U is returned. */ 2240 unsigned APInt::tcMSB(const WordType *parts, unsigned n) { 2241 do { 2242 --n; 2243 2244 if (parts[n] != 0) { 2245 unsigned msb = partMSB(parts[n]); 2246 2247 return msb + n * APINT_BITS_PER_WORD; 2248 } 2249 } while (n); 2250 2251 return -1U; 2252 } 2253 2254 /* Copy the bit vector of width srcBITS from SRC, starting at bit 2255 srcLSB, to DST, of dstCOUNT parts, such that the bit srcLSB becomes 2256 the least significant bit of DST. All high bits above srcBITS in 2257 DST are zero-filled. */ 2258 void 2259 APInt::tcExtract(WordType *dst, unsigned dstCount, const WordType *src, 2260 unsigned srcBits, unsigned srcLSB) { 2261 unsigned dstParts = (srcBits + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD; 2262 assert(dstParts <= dstCount); 2263 2264 unsigned firstSrcPart = srcLSB / APINT_BITS_PER_WORD; 2265 tcAssign (dst, src + firstSrcPart, dstParts); 2266 2267 unsigned shift = srcLSB % APINT_BITS_PER_WORD; 2268 tcShiftRight (dst, dstParts, shift); 2269 2270 /* We now have (dstParts * APINT_BITS_PER_WORD - shift) bits from SRC 2271 in DST. If this is less that srcBits, append the rest, else 2272 clear the high bits. */ 2273 unsigned n = dstParts * APINT_BITS_PER_WORD - shift; 2274 if (n < srcBits) { 2275 WordType mask = lowBitMask (srcBits - n); 2276 dst[dstParts - 1] |= ((src[firstSrcPart + dstParts] & mask) 2277 << n % APINT_BITS_PER_WORD); 2278 } else if (n > srcBits) { 2279 if (srcBits % APINT_BITS_PER_WORD) 2280 dst[dstParts - 1] &= lowBitMask (srcBits % APINT_BITS_PER_WORD); 2281 } 2282 2283 /* Clear high parts. */ 2284 while (dstParts < dstCount) 2285 dst[dstParts++] = 0; 2286 } 2287 2288 /* DST += RHS + C where C is zero or one. Returns the carry flag. */ 2289 APInt::WordType APInt::tcAdd(WordType *dst, const WordType *rhs, 2290 WordType c, unsigned parts) { 2291 assert(c <= 1); 2292 2293 for (unsigned i = 0; i < parts; i++) { 2294 WordType l = dst[i]; 2295 if (c) { 2296 dst[i] += rhs[i] + 1; 2297 c = (dst[i] <= l); 2298 } else { 2299 dst[i] += rhs[i]; 2300 c = (dst[i] < l); 2301 } 2302 } 2303 2304 return c; 2305 } 2306 2307 /// This function adds a single "word" integer, src, to the multiple 2308 /// "word" integer array, dst[]. dst[] is modified to reflect the addition and 2309 /// 1 is returned if there is a carry out, otherwise 0 is returned. 2310 /// @returns the carry of the addition. 2311 APInt::WordType APInt::tcAddPart(WordType *dst, WordType src, 2312 unsigned parts) { 2313 for (unsigned i = 0; i < parts; ++i) { 2314 dst[i] += src; 2315 if (dst[i] >= src) 2316 return 0; // No need to carry so exit early. 2317 src = 1; // Carry one to next digit. 2318 } 2319 2320 return 1; 2321 } 2322 2323 /* DST -= RHS + C where C is zero or one. Returns the carry flag. */ 2324 APInt::WordType APInt::tcSubtract(WordType *dst, const WordType *rhs, 2325 WordType c, unsigned parts) { 2326 assert(c <= 1); 2327 2328 for (unsigned i = 0; i < parts; i++) { 2329 WordType l = dst[i]; 2330 if (c) { 2331 dst[i] -= rhs[i] + 1; 2332 c = (dst[i] >= l); 2333 } else { 2334 dst[i] -= rhs[i]; 2335 c = (dst[i] > l); 2336 } 2337 } 2338 2339 return c; 2340 } 2341 2342 /// This function subtracts a single "word" (64-bit word), src, from 2343 /// the multi-word integer array, dst[], propagating the borrowed 1 value until 2344 /// no further borrowing is needed or it runs out of "words" in dst. The result 2345 /// is 1 if "borrowing" exhausted the digits in dst, or 0 if dst was not 2346 /// exhausted. In other words, if src > dst then this function returns 1, 2347 /// otherwise 0. 2348 /// @returns the borrow out of the subtraction 2349 APInt::WordType APInt::tcSubtractPart(WordType *dst, WordType src, 2350 unsigned parts) { 2351 for (unsigned i = 0; i < parts; ++i) { 2352 WordType Dst = dst[i]; 2353 dst[i] -= src; 2354 if (src <= Dst) 2355 return 0; // No need to borrow so exit early. 2356 src = 1; // We have to "borrow 1" from next "word" 2357 } 2358 2359 return 1; 2360 } 2361 2362 /* Negate a bignum in-place. */ 2363 void APInt::tcNegate(WordType *dst, unsigned parts) { 2364 tcComplement(dst, parts); 2365 tcIncrement(dst, parts); 2366 } 2367 2368 /* DST += SRC * MULTIPLIER + CARRY if add is true 2369 DST = SRC * MULTIPLIER + CARRY if add is false 2370 2371 Requires 0 <= DSTPARTS <= SRCPARTS + 1. If DST overlaps SRC 2372 they must start at the same point, i.e. DST == SRC. 2373 2374 If DSTPARTS == SRCPARTS + 1 no overflow occurs and zero is 2375 returned. Otherwise DST is filled with the least significant 2376 DSTPARTS parts of the result, and if all of the omitted higher 2377 parts were zero return zero, otherwise overflow occurred and 2378 return one. */ 2379 int APInt::tcMultiplyPart(WordType *dst, const WordType *src, 2380 WordType multiplier, WordType carry, 2381 unsigned srcParts, unsigned dstParts, 2382 bool add) { 2383 /* Otherwise our writes of DST kill our later reads of SRC. */ 2384 assert(dst <= src || dst >= src + srcParts); 2385 assert(dstParts <= srcParts + 1); 2386 2387 /* N loops; minimum of dstParts and srcParts. */ 2388 unsigned n = std::min(dstParts, srcParts); 2389 2390 for (unsigned i = 0; i < n; i++) { 2391 WordType low, mid, high, srcPart; 2392 2393 /* [ LOW, HIGH ] = MULTIPLIER * SRC[i] + DST[i] + CARRY. 2394 2395 This cannot overflow, because 2396 2397 (n - 1) * (n - 1) + 2 (n - 1) = (n - 1) * (n + 1) 2398 2399 which is less than n^2. */ 2400 2401 srcPart = src[i]; 2402 2403 if (multiplier == 0 || srcPart == 0) { 2404 low = carry; 2405 high = 0; 2406 } else { 2407 low = lowHalf(srcPart) * lowHalf(multiplier); 2408 high = highHalf(srcPart) * highHalf(multiplier); 2409 2410 mid = lowHalf(srcPart) * highHalf(multiplier); 2411 high += highHalf(mid); 2412 mid <<= APINT_BITS_PER_WORD / 2; 2413 if (low + mid < low) 2414 high++; 2415 low += mid; 2416 2417 mid = highHalf(srcPart) * lowHalf(multiplier); 2418 high += highHalf(mid); 2419 mid <<= APINT_BITS_PER_WORD / 2; 2420 if (low + mid < low) 2421 high++; 2422 low += mid; 2423 2424 /* Now add carry. */ 2425 if (low + carry < low) 2426 high++; 2427 low += carry; 2428 } 2429 2430 if (add) { 2431 /* And now DST[i], and store the new low part there. */ 2432 if (low + dst[i] < low) 2433 high++; 2434 dst[i] += low; 2435 } else 2436 dst[i] = low; 2437 2438 carry = high; 2439 } 2440 2441 if (srcParts < dstParts) { 2442 /* Full multiplication, there is no overflow. */ 2443 assert(srcParts + 1 == dstParts); 2444 dst[srcParts] = carry; 2445 return 0; 2446 } 2447 2448 /* We overflowed if there is carry. */ 2449 if (carry) 2450 return 1; 2451 2452 /* We would overflow if any significant unwritten parts would be 2453 non-zero. This is true if any remaining src parts are non-zero 2454 and the multiplier is non-zero. */ 2455 if (multiplier) 2456 for (unsigned i = dstParts; i < srcParts; i++) 2457 if (src[i]) 2458 return 1; 2459 2460 /* We fitted in the narrow destination. */ 2461 return 0; 2462 } 2463 2464 /* DST = LHS * RHS, where DST has the same width as the operands and 2465 is filled with the least significant parts of the result. Returns 2466 one if overflow occurred, otherwise zero. DST must be disjoint 2467 from both operands. */ 2468 int APInt::tcMultiply(WordType *dst, const WordType *lhs, 2469 const WordType *rhs, unsigned parts) { 2470 assert(dst != lhs && dst != rhs); 2471 2472 int overflow = 0; 2473 tcSet(dst, 0, parts); 2474 2475 for (unsigned i = 0; i < parts; i++) 2476 overflow |= tcMultiplyPart(&dst[i], lhs, rhs[i], 0, parts, 2477 parts - i, true); 2478 2479 return overflow; 2480 } 2481 2482 /// DST = LHS * RHS, where DST has width the sum of the widths of the 2483 /// operands. No overflow occurs. DST must be disjoint from both operands. 2484 void APInt::tcFullMultiply(WordType *dst, const WordType *lhs, 2485 const WordType *rhs, unsigned lhsParts, 2486 unsigned rhsParts) { 2487 /* Put the narrower number on the LHS for less loops below. */ 2488 if (lhsParts > rhsParts) 2489 return tcFullMultiply (dst, rhs, lhs, rhsParts, lhsParts); 2490 2491 assert(dst != lhs && dst != rhs); 2492 2493 tcSet(dst, 0, rhsParts); 2494 2495 for (unsigned i = 0; i < lhsParts; i++) 2496 tcMultiplyPart(&dst[i], rhs, lhs[i], 0, rhsParts, rhsParts + 1, true); 2497 } 2498 2499 /* If RHS is zero LHS and REMAINDER are left unchanged, return one. 2500 Otherwise set LHS to LHS / RHS with the fractional part discarded, 2501 set REMAINDER to the remainder, return zero. i.e. 2502 2503 OLD_LHS = RHS * LHS + REMAINDER 2504 2505 SCRATCH is a bignum of the same size as the operands and result for 2506 use by the routine; its contents need not be initialized and are 2507 destroyed. LHS, REMAINDER and SCRATCH must be distinct. 2508 */ 2509 int APInt::tcDivide(WordType *lhs, const WordType *rhs, 2510 WordType *remainder, WordType *srhs, 2511 unsigned parts) { 2512 assert(lhs != remainder && lhs != srhs && remainder != srhs); 2513 2514 unsigned shiftCount = tcMSB(rhs, parts) + 1; 2515 if (shiftCount == 0) 2516 return true; 2517 2518 shiftCount = parts * APINT_BITS_PER_WORD - shiftCount; 2519 unsigned n = shiftCount / APINT_BITS_PER_WORD; 2520 WordType mask = (WordType) 1 << (shiftCount % APINT_BITS_PER_WORD); 2521 2522 tcAssign(srhs, rhs, parts); 2523 tcShiftLeft(srhs, parts, shiftCount); 2524 tcAssign(remainder, lhs, parts); 2525 tcSet(lhs, 0, parts); 2526 2527 /* Loop, subtracting SRHS if REMAINDER is greater and adding that to 2528 the total. */ 2529 for (;;) { 2530 int compare = tcCompare(remainder, srhs, parts); 2531 if (compare >= 0) { 2532 tcSubtract(remainder, srhs, 0, parts); 2533 lhs[n] |= mask; 2534 } 2535 2536 if (shiftCount == 0) 2537 break; 2538 shiftCount--; 2539 tcShiftRight(srhs, parts, 1); 2540 if ((mask >>= 1) == 0) { 2541 mask = (WordType) 1 << (APINT_BITS_PER_WORD - 1); 2542 n--; 2543 } 2544 } 2545 2546 return false; 2547 } 2548 2549 /// Shift a bignum left Cound bits in-place. Shifted in bits are zero. There are 2550 /// no restrictions on Count. 2551 void APInt::tcShiftLeft(WordType *Dst, unsigned Words, unsigned Count) { 2552 // Don't bother performing a no-op shift. 2553 if (!Count) 2554 return; 2555 2556 // WordShift is the inter-part shift; BitShift is the intra-part shift. 2557 unsigned WordShift = std::min(Count / APINT_BITS_PER_WORD, Words); 2558 unsigned BitShift = Count % APINT_BITS_PER_WORD; 2559 2560 // Fastpath for moving by whole words. 2561 if (BitShift == 0) { 2562 std::memmove(Dst + WordShift, Dst, (Words - WordShift) * APINT_WORD_SIZE); 2563 } else { 2564 while (Words-- > WordShift) { 2565 Dst[Words] = Dst[Words - WordShift] << BitShift; 2566 if (Words > WordShift) 2567 Dst[Words] |= 2568 Dst[Words - WordShift - 1] >> (APINT_BITS_PER_WORD - BitShift); 2569 } 2570 } 2571 2572 // Fill in the remainder with 0s. 2573 std::memset(Dst, 0, WordShift * APINT_WORD_SIZE); 2574 } 2575 2576 /// Shift a bignum right Count bits in-place. Shifted in bits are zero. There 2577 /// are no restrictions on Count. 2578 void APInt::tcShiftRight(WordType *Dst, unsigned Words, unsigned Count) { 2579 // Don't bother performing a no-op shift. 2580 if (!Count) 2581 return; 2582 2583 // WordShift is the inter-part shift; BitShift is the intra-part shift. 2584 unsigned WordShift = std::min(Count / APINT_BITS_PER_WORD, Words); 2585 unsigned BitShift = Count % APINT_BITS_PER_WORD; 2586 2587 unsigned WordsToMove = Words - WordShift; 2588 // Fastpath for moving by whole words. 2589 if (BitShift == 0) { 2590 std::memmove(Dst, Dst + WordShift, WordsToMove * APINT_WORD_SIZE); 2591 } else { 2592 for (unsigned i = 0; i != WordsToMove; ++i) { 2593 Dst[i] = Dst[i + WordShift] >> BitShift; 2594 if (i + 1 != WordsToMove) 2595 Dst[i] |= Dst[i + WordShift + 1] << (APINT_BITS_PER_WORD - BitShift); 2596 } 2597 } 2598 2599 // Fill in the remainder with 0s. 2600 std::memset(Dst + WordsToMove, 0, WordShift * APINT_WORD_SIZE); 2601 } 2602 2603 /* Bitwise and of two bignums. */ 2604 void APInt::tcAnd(WordType *dst, const WordType *rhs, unsigned parts) { 2605 for (unsigned i = 0; i < parts; i++) 2606 dst[i] &= rhs[i]; 2607 } 2608 2609 /* Bitwise inclusive or of two bignums. */ 2610 void APInt::tcOr(WordType *dst, const WordType *rhs, unsigned parts) { 2611 for (unsigned i = 0; i < parts; i++) 2612 dst[i] |= rhs[i]; 2613 } 2614 2615 /* Bitwise exclusive or of two bignums. */ 2616 void APInt::tcXor(WordType *dst, const WordType *rhs, unsigned parts) { 2617 for (unsigned i = 0; i < parts; i++) 2618 dst[i] ^= rhs[i]; 2619 } 2620 2621 /* Complement a bignum in-place. */ 2622 void APInt::tcComplement(WordType *dst, unsigned parts) { 2623 for (unsigned i = 0; i < parts; i++) 2624 dst[i] = ~dst[i]; 2625 } 2626 2627 /* Comparison (unsigned) of two bignums. */ 2628 int APInt::tcCompare(const WordType *lhs, const WordType *rhs, 2629 unsigned parts) { 2630 while (parts) { 2631 parts--; 2632 if (lhs[parts] != rhs[parts]) 2633 return (lhs[parts] > rhs[parts]) ? 1 : -1; 2634 } 2635 2636 return 0; 2637 } 2638 2639 /* Set the least significant BITS bits of a bignum, clear the 2640 rest. */ 2641 void APInt::tcSetLeastSignificantBits(WordType *dst, unsigned parts, 2642 unsigned bits) { 2643 unsigned i = 0; 2644 while (bits > APINT_BITS_PER_WORD) { 2645 dst[i++] = ~(WordType) 0; 2646 bits -= APINT_BITS_PER_WORD; 2647 } 2648 2649 if (bits) 2650 dst[i++] = ~(WordType) 0 >> (APINT_BITS_PER_WORD - bits); 2651 2652 while (i < parts) 2653 dst[i++] = 0; 2654 } 2655