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