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