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