APInt.cpp revision 66ed1099ff3591c61e008198bb5a30862e778fc0
1//===-- APInt.cpp - Implement APInt class ---------------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by Sheng Zhou and is distributed under the 6// University of Illinois Open Source 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#define DEBUG_TYPE "apint" 16#include "llvm/ADT/APInt.h" 17#include "llvm/DerivedTypes.h" 18#include "llvm/Support/Debug.h" 19#include "llvm/Support/MathExtras.h" 20#include <cstring> 21#include <cstdlib> 22#ifndef NDEBUG 23#include <iomanip> 24#endif 25 26using namespace llvm; 27 28/// A utility function for allocating memory, checking for allocation failures, 29/// and ensuring the contents are zeroed. 30inline static uint64_t* getClearedMemory(uint32_t numWords) { 31 uint64_t * result = new uint64_t[numWords]; 32 assert(result && "APInt memory allocation fails!"); 33 memset(result, 0, numWords * sizeof(uint64_t)); 34 return result; 35} 36 37/// A utility function for allocating memory and checking for allocation 38/// failure. The content is not zeroed. 39inline static uint64_t* getMemory(uint32_t numWords) { 40 uint64_t * result = new uint64_t[numWords]; 41 assert(result && "APInt memory allocation fails!"); 42 return result; 43} 44 45APInt::APInt(uint32_t numBits, uint64_t val) 46 : BitWidth(numBits), VAL(0) { 47 assert(BitWidth >= IntegerType::MIN_INT_BITS && "bitwidth too small"); 48 assert(BitWidth <= IntegerType::MAX_INT_BITS && "bitwidth too large"); 49 if (isSingleWord()) 50 VAL = val; 51 else { 52 pVal = getClearedMemory(getNumWords()); 53 pVal[0] = val; 54 } 55 clearUnusedBits(); 56} 57 58APInt::APInt(uint32_t numBits, uint32_t numWords, uint64_t bigVal[]) 59 : BitWidth(numBits), VAL(0) { 60 assert(BitWidth >= IntegerType::MIN_INT_BITS && "bitwidth too small"); 61 assert(BitWidth <= IntegerType::MAX_INT_BITS && "bitwidth too large"); 62 assert(bigVal && "Null pointer detected!"); 63 if (isSingleWord()) 64 VAL = bigVal[0]; 65 else { 66 // Get memory, cleared to 0 67 pVal = getClearedMemory(getNumWords()); 68 // Calculate the number of words to copy 69 uint32_t words = std::min<uint32_t>(numWords, getNumWords()); 70 // Copy the words from bigVal to pVal 71 memcpy(pVal, bigVal, words * APINT_WORD_SIZE); 72 } 73 // Make sure unused high bits are cleared 74 clearUnusedBits(); 75} 76 77APInt::APInt(uint32_t numbits, const char StrStart[], uint32_t slen, 78 uint8_t radix) 79 : BitWidth(numbits), VAL(0) { 80 fromString(numbits, StrStart, slen, radix); 81} 82 83APInt::APInt(uint32_t numbits, const std::string& Val, uint8_t radix) 84 : BitWidth(numbits), VAL(0) { 85 assert(!Val.empty() && "String empty?"); 86 fromString(numbits, Val.c_str(), Val.size(), radix); 87} 88 89APInt::APInt(const APInt& that) 90 : BitWidth(that.BitWidth), VAL(0) { 91 if (isSingleWord()) 92 VAL = that.VAL; 93 else { 94 pVal = getMemory(getNumWords()); 95 memcpy(pVal, that.pVal, getNumWords() * APINT_WORD_SIZE); 96 } 97} 98 99APInt::~APInt() { 100 if (!isSingleWord() && pVal) 101 delete [] pVal; 102} 103 104APInt& APInt::operator=(const APInt& RHS) { 105 // Don't do anything for X = X 106 if (this == &RHS) 107 return *this; 108 109 // If the bitwidths are the same, we can avoid mucking with memory 110 if (BitWidth == RHS.getBitWidth()) { 111 if (isSingleWord()) 112 VAL = RHS.VAL; 113 else 114 memcpy(pVal, RHS.pVal, getNumWords() * APINT_WORD_SIZE); 115 return *this; 116 } 117 118 if (isSingleWord()) 119 if (RHS.isSingleWord()) 120 VAL = RHS.VAL; 121 else { 122 VAL = 0; 123 pVal = getMemory(RHS.getNumWords()); 124 memcpy(pVal, RHS.pVal, RHS.getNumWords() * APINT_WORD_SIZE); 125 } 126 else if (getNumWords() == RHS.getNumWords()) 127 memcpy(pVal, RHS.pVal, RHS.getNumWords() * APINT_WORD_SIZE); 128 else if (RHS.isSingleWord()) { 129 delete [] pVal; 130 VAL = RHS.VAL; 131 } else { 132 delete [] pVal; 133 pVal = getMemory(RHS.getNumWords()); 134 memcpy(pVal, RHS.pVal, RHS.getNumWords() * APINT_WORD_SIZE); 135 } 136 BitWidth = RHS.BitWidth; 137 return clearUnusedBits(); 138} 139 140APInt& APInt::operator=(uint64_t RHS) { 141 if (isSingleWord()) 142 VAL = RHS; 143 else { 144 pVal[0] = RHS; 145 memset(pVal+1, 0, (getNumWords() - 1) * APINT_WORD_SIZE); 146 } 147 return clearUnusedBits(); 148} 149 150/// add_1 - This function adds a single "digit" integer, y, to the multiple 151/// "digit" integer array, x[]. x[] is modified to reflect the addition and 152/// 1 is returned if there is a carry out, otherwise 0 is returned. 153/// @returns the carry of the addition. 154static bool add_1(uint64_t dest[], uint64_t x[], uint32_t len, uint64_t y) { 155 for (uint32_t i = 0; i < len; ++i) { 156 dest[i] = y + x[i]; 157 if (dest[i] < y) 158 y = 1; // Carry one to next digit. 159 else { 160 y = 0; // No need to carry so exit early 161 break; 162 } 163 } 164 return y; 165} 166 167/// @brief Prefix increment operator. Increments the APInt by one. 168APInt& APInt::operator++() { 169 if (isSingleWord()) 170 ++VAL; 171 else 172 add_1(pVal, pVal, getNumWords(), 1); 173 return clearUnusedBits(); 174} 175 176/// sub_1 - This function subtracts a single "digit" (64-bit word), y, from 177/// the multi-digit integer array, x[], propagating the borrowed 1 value until 178/// no further borrowing is neeeded or it runs out of "digits" in x. The result 179/// is 1 if "borrowing" exhausted the digits in x, or 0 if x was not exhausted. 180/// In other words, if y > x then this function returns 1, otherwise 0. 181/// @returns the borrow out of the subtraction 182static bool sub_1(uint64_t x[], uint32_t len, uint64_t y) { 183 for (uint32_t i = 0; i < len; ++i) { 184 uint64_t X = x[i]; 185 x[i] -= y; 186 if (y > X) 187 y = 1; // We have to "borrow 1" from next "digit" 188 else { 189 y = 0; // No need to borrow 190 break; // Remaining digits are unchanged so exit early 191 } 192 } 193 return bool(y); 194} 195 196/// @brief Prefix decrement operator. Decrements the APInt by one. 197APInt& APInt::operator--() { 198 if (isSingleWord()) 199 --VAL; 200 else 201 sub_1(pVal, getNumWords(), 1); 202 return clearUnusedBits(); 203} 204 205/// add - This function adds the integer array x to the integer array Y and 206/// places the result in dest. 207/// @returns the carry out from the addition 208/// @brief General addition of 64-bit integer arrays 209static bool add(uint64_t *dest, const uint64_t *x, const uint64_t *y, 210 uint32_t len) { 211 bool carry = false; 212 for (uint32_t i = 0; i< len; ++i) { 213 uint64_t limit = std::min(x[i],y[i]); // must come first in case dest == x 214 dest[i] = x[i] + y[i] + carry; 215 carry = dest[i] < limit || (carry && dest[i] == limit); 216 } 217 return carry; 218} 219 220/// Adds the RHS APint to this APInt. 221/// @returns this, after addition of RHS. 222/// @brief Addition assignment operator. 223APInt& APInt::operator+=(const APInt& RHS) { 224 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 225 if (isSingleWord()) 226 VAL += RHS.VAL; 227 else { 228 add(pVal, pVal, RHS.pVal, getNumWords()); 229 } 230 return clearUnusedBits(); 231} 232 233/// Subtracts the integer array y from the integer array x 234/// @returns returns the borrow out. 235/// @brief Generalized subtraction of 64-bit integer arrays. 236static bool sub(uint64_t *dest, const uint64_t *x, const uint64_t *y, 237 uint32_t len) { 238 bool borrow = false; 239 for (uint32_t i = 0; i < len; ++i) { 240 uint64_t x_tmp = borrow ? x[i] - 1 : x[i]; 241 borrow = y[i] > x_tmp || (borrow && x[i] == 0); 242 dest[i] = x_tmp - y[i]; 243 } 244 return borrow; 245} 246 247/// Subtracts the RHS APInt from this APInt 248/// @returns this, after subtraction 249/// @brief Subtraction assignment operator. 250APInt& APInt::operator-=(const APInt& RHS) { 251 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 252 if (isSingleWord()) 253 VAL -= RHS.VAL; 254 else 255 sub(pVal, pVal, RHS.pVal, getNumWords()); 256 return clearUnusedBits(); 257} 258 259/// Multiplies an integer array, x by a a uint64_t integer and places the result 260/// into dest. 261/// @returns the carry out of the multiplication. 262/// @brief Multiply a multi-digit APInt by a single digit (64-bit) integer. 263static uint64_t mul_1(uint64_t dest[], uint64_t x[], uint32_t len, uint64_t y) { 264 // Split y into high 32-bit part (hy) and low 32-bit part (ly) 265 uint64_t ly = y & 0xffffffffULL, hy = y >> 32; 266 uint64_t carry = 0; 267 268 // For each digit of x. 269 for (uint32_t i = 0; i < len; ++i) { 270 // Split x into high and low words 271 uint64_t lx = x[i] & 0xffffffffULL; 272 uint64_t hx = x[i] >> 32; 273 // hasCarry - A flag to indicate if there is a carry to the next digit. 274 // hasCarry == 0, no carry 275 // hasCarry == 1, has carry 276 // hasCarry == 2, no carry and the calculation result == 0. 277 uint8_t hasCarry = 0; 278 dest[i] = carry + lx * ly; 279 // Determine if the add above introduces carry. 280 hasCarry = (dest[i] < carry) ? 1 : 0; 281 carry = hx * ly + (dest[i] >> 32) + (hasCarry ? (1ULL << 32) : 0); 282 // The upper limit of carry can be (2^32 - 1)(2^32 - 1) + 283 // (2^32 - 1) + 2^32 = 2^64. 284 hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0); 285 286 carry += (lx * hy) & 0xffffffffULL; 287 dest[i] = (carry << 32) | (dest[i] & 0xffffffffULL); 288 carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0) + 289 (carry >> 32) + ((lx * hy) >> 32) + hx * hy; 290 } 291 return carry; 292} 293 294/// Multiplies integer array x by integer array y and stores the result into 295/// the integer array dest. Note that dest's size must be >= xlen + ylen. 296/// @brief Generalized multiplicate of integer arrays. 297static void mul(uint64_t dest[], uint64_t x[], uint32_t xlen, uint64_t y[], 298 uint32_t ylen) { 299 dest[xlen] = mul_1(dest, x, xlen, y[0]); 300 for (uint32_t i = 1; i < ylen; ++i) { 301 uint64_t ly = y[i] & 0xffffffffULL, hy = y[i] >> 32; 302 uint64_t carry = 0, lx = 0, hx = 0; 303 for (uint32_t j = 0; j < xlen; ++j) { 304 lx = x[j] & 0xffffffffULL; 305 hx = x[j] >> 32; 306 // hasCarry - A flag to indicate if has carry. 307 // hasCarry == 0, no carry 308 // hasCarry == 1, has carry 309 // hasCarry == 2, no carry and the calculation result == 0. 310 uint8_t hasCarry = 0; 311 uint64_t resul = carry + lx * ly; 312 hasCarry = (resul < carry) ? 1 : 0; 313 carry = (hasCarry ? (1ULL << 32) : 0) + hx * ly + (resul >> 32); 314 hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0); 315 316 carry += (lx * hy) & 0xffffffffULL; 317 resul = (carry << 32) | (resul & 0xffffffffULL); 318 dest[i+j] += resul; 319 carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0)+ 320 (carry >> 32) + (dest[i+j] < resul ? 1 : 0) + 321 ((lx * hy) >> 32) + hx * hy; 322 } 323 dest[i+xlen] = carry; 324 } 325} 326 327APInt& APInt::operator*=(const APInt& RHS) { 328 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 329 if (isSingleWord()) { 330 VAL *= RHS.VAL; 331 clearUnusedBits(); 332 return *this; 333 } 334 335 // Get some bit facts about LHS and check for zero 336 uint32_t lhsBits = getActiveBits(); 337 uint32_t lhsWords = !lhsBits ? 0 : whichWord(lhsBits - 1) + 1; 338 if (!lhsWords) 339 // 0 * X ===> 0 340 return *this; 341 342 // Get some bit facts about RHS and check for zero 343 uint32_t rhsBits = RHS.getActiveBits(); 344 uint32_t rhsWords = !rhsBits ? 0 : whichWord(rhsBits - 1) + 1; 345 if (!rhsWords) { 346 // X * 0 ===> 0 347 clear(); 348 return *this; 349 } 350 351 // Allocate space for the result 352 uint32_t destWords = rhsWords + lhsWords; 353 uint64_t *dest = getMemory(destWords); 354 355 // Perform the long multiply 356 mul(dest, pVal, lhsWords, RHS.pVal, rhsWords); 357 358 // Copy result back into *this 359 clear(); 360 uint32_t wordsToCopy = destWords >= getNumWords() ? getNumWords() : destWords; 361 memcpy(pVal, dest, wordsToCopy * APINT_WORD_SIZE); 362 363 // delete dest array and return 364 delete[] dest; 365 return *this; 366} 367 368APInt& APInt::operator&=(const APInt& RHS) { 369 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 370 if (isSingleWord()) { 371 VAL &= RHS.VAL; 372 return *this; 373 } 374 uint32_t numWords = getNumWords(); 375 for (uint32_t i = 0; i < numWords; ++i) 376 pVal[i] &= RHS.pVal[i]; 377 return *this; 378} 379 380APInt& APInt::operator|=(const APInt& RHS) { 381 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 382 if (isSingleWord()) { 383 VAL |= RHS.VAL; 384 return *this; 385 } 386 uint32_t numWords = getNumWords(); 387 for (uint32_t i = 0; i < numWords; ++i) 388 pVal[i] |= RHS.pVal[i]; 389 return *this; 390} 391 392APInt& APInt::operator^=(const APInt& RHS) { 393 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 394 if (isSingleWord()) { 395 VAL ^= RHS.VAL; 396 this->clearUnusedBits(); 397 return *this; 398 } 399 uint32_t numWords = getNumWords(); 400 for (uint32_t i = 0; i < numWords; ++i) 401 pVal[i] ^= RHS.pVal[i]; 402 return clearUnusedBits(); 403} 404 405APInt APInt::operator&(const APInt& RHS) const { 406 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 407 if (isSingleWord()) 408 return APInt(getBitWidth(), VAL & RHS.VAL); 409 410 uint32_t numWords = getNumWords(); 411 uint64_t* val = getMemory(numWords); 412 for (uint32_t i = 0; i < numWords; ++i) 413 val[i] = pVal[i] & RHS.pVal[i]; 414 return APInt(val, getBitWidth()); 415} 416 417APInt APInt::operator|(const APInt& RHS) const { 418 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 419 if (isSingleWord()) 420 return APInt(getBitWidth(), VAL | RHS.VAL); 421 422 uint32_t numWords = getNumWords(); 423 uint64_t *val = getMemory(numWords); 424 for (uint32_t i = 0; i < numWords; ++i) 425 val[i] = pVal[i] | RHS.pVal[i]; 426 return APInt(val, getBitWidth()); 427} 428 429APInt APInt::operator^(const APInt& RHS) const { 430 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 431 if (isSingleWord()) 432 return APInt(BitWidth, VAL ^ RHS.VAL); 433 434 uint32_t numWords = getNumWords(); 435 uint64_t *val = getMemory(numWords); 436 for (uint32_t i = 0; i < numWords; ++i) 437 val[i] = pVal[i] ^ RHS.pVal[i]; 438 439 // 0^0==1 so clear the high bits in case they got set. 440 return APInt(val, getBitWidth()).clearUnusedBits(); 441} 442 443bool APInt::operator !() const { 444 if (isSingleWord()) 445 return !VAL; 446 447 for (uint32_t i = 0; i < getNumWords(); ++i) 448 if (pVal[i]) 449 return false; 450 return true; 451} 452 453APInt APInt::operator*(const APInt& RHS) const { 454 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 455 if (isSingleWord()) 456 return APInt(BitWidth, VAL * RHS.VAL); 457 APInt Result(*this); 458 Result *= RHS; 459 return Result.clearUnusedBits(); 460} 461 462APInt APInt::operator+(const APInt& RHS) const { 463 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 464 if (isSingleWord()) 465 return APInt(BitWidth, VAL + RHS.VAL); 466 APInt Result(BitWidth, 0); 467 add(Result.pVal, this->pVal, RHS.pVal, getNumWords()); 468 return Result.clearUnusedBits(); 469} 470 471APInt APInt::operator-(const APInt& RHS) const { 472 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 473 if (isSingleWord()) 474 return APInt(BitWidth, VAL - RHS.VAL); 475 APInt Result(BitWidth, 0); 476 sub(Result.pVal, this->pVal, RHS.pVal, getNumWords()); 477 return Result.clearUnusedBits(); 478} 479 480bool APInt::operator[](uint32_t bitPosition) const { 481 return (maskBit(bitPosition) & 482 (isSingleWord() ? VAL : pVal[whichWord(bitPosition)])) != 0; 483} 484 485bool APInt::operator==(const APInt& RHS) const { 486 assert(BitWidth == RHS.BitWidth && "Comparison requires equal bit widths"); 487 if (isSingleWord()) 488 return VAL == RHS.VAL; 489 490 // Get some facts about the number of bits used in the two operands. 491 uint32_t n1 = getActiveBits(); 492 uint32_t n2 = RHS.getActiveBits(); 493 494 // If the number of bits isn't the same, they aren't equal 495 if (n1 != n2) 496 return false; 497 498 // If the number of bits fits in a word, we only need to compare the low word. 499 if (n1 <= APINT_BITS_PER_WORD) 500 return pVal[0] == RHS.pVal[0]; 501 502 // Otherwise, compare everything 503 for (int i = whichWord(n1 - 1); i >= 0; --i) 504 if (pVal[i] != RHS.pVal[i]) 505 return false; 506 return true; 507} 508 509bool APInt::operator==(uint64_t Val) const { 510 if (isSingleWord()) 511 return VAL == Val; 512 513 uint32_t n = getActiveBits(); 514 if (n <= APINT_BITS_PER_WORD) 515 return pVal[0] == Val; 516 else 517 return false; 518} 519 520bool APInt::ult(const APInt& RHS) const { 521 assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison"); 522 if (isSingleWord()) 523 return VAL < RHS.VAL; 524 525 // Get active bit length of both operands 526 uint32_t n1 = getActiveBits(); 527 uint32_t n2 = RHS.getActiveBits(); 528 529 // If magnitude of LHS is less than RHS, return true. 530 if (n1 < n2) 531 return true; 532 533 // If magnitude of RHS is greather than LHS, return false. 534 if (n2 < n1) 535 return false; 536 537 // If they bot fit in a word, just compare the low order word 538 if (n1 <= APINT_BITS_PER_WORD && n2 <= APINT_BITS_PER_WORD) 539 return pVal[0] < RHS.pVal[0]; 540 541 // Otherwise, compare all words 542 uint32_t topWord = whichWord(std::max(n1,n2)-1); 543 for (int i = topWord; i >= 0; --i) { 544 if (pVal[i] > RHS.pVal[i]) 545 return false; 546 if (pVal[i] < RHS.pVal[i]) 547 return true; 548 } 549 return false; 550} 551 552bool APInt::slt(const APInt& RHS) const { 553 assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison"); 554 if (isSingleWord()) { 555 int64_t lhsSext = (int64_t(VAL) << (64-BitWidth)) >> (64-BitWidth); 556 int64_t rhsSext = (int64_t(RHS.VAL) << (64-BitWidth)) >> (64-BitWidth); 557 return lhsSext < rhsSext; 558 } 559 560 APInt lhs(*this); 561 APInt rhs(RHS); 562 bool lhsNeg = isNegative(); 563 bool rhsNeg = rhs.isNegative(); 564 if (lhsNeg) { 565 // Sign bit is set so perform two's complement to make it positive 566 lhs.flip(); 567 lhs++; 568 } 569 if (rhsNeg) { 570 // Sign bit is set so perform two's complement to make it positive 571 rhs.flip(); 572 rhs++; 573 } 574 575 // Now we have unsigned values to compare so do the comparison if necessary 576 // based on the negativeness of the values. 577 if (lhsNeg) 578 if (rhsNeg) 579 return lhs.ugt(rhs); 580 else 581 return true; 582 else if (rhsNeg) 583 return false; 584 else 585 return lhs.ult(rhs); 586} 587 588APInt& APInt::set(uint32_t bitPosition) { 589 if (isSingleWord()) 590 VAL |= maskBit(bitPosition); 591 else 592 pVal[whichWord(bitPosition)] |= maskBit(bitPosition); 593 return *this; 594} 595 596APInt& APInt::set() { 597 if (isSingleWord()) { 598 VAL = -1ULL; 599 return clearUnusedBits(); 600 } 601 602 // Set all the bits in all the words. 603 for (uint32_t i = 0; i < getNumWords() - 1; ++i) 604 pVal[i] = -1ULL; 605 // Clear the unused ones 606 return clearUnusedBits(); 607} 608 609/// Set the given bit to 0 whose position is given as "bitPosition". 610/// @brief Set a given bit to 0. 611APInt& APInt::clear(uint32_t bitPosition) { 612 if (isSingleWord()) 613 VAL &= ~maskBit(bitPosition); 614 else 615 pVal[whichWord(bitPosition)] &= ~maskBit(bitPosition); 616 return *this; 617} 618 619/// @brief Set every bit to 0. 620APInt& APInt::clear() { 621 if (isSingleWord()) 622 VAL = 0; 623 else 624 memset(pVal, 0, getNumWords() * APINT_WORD_SIZE); 625 return *this; 626} 627 628/// @brief Bitwise NOT operator. Performs a bitwise logical NOT operation on 629/// this APInt. 630APInt APInt::operator~() const { 631 APInt Result(*this); 632 Result.flip(); 633 return Result; 634} 635 636/// @brief Toggle every bit to its opposite value. 637APInt& APInt::flip() { 638 if (isSingleWord()) { 639 VAL ^= -1ULL; 640 return clearUnusedBits(); 641 } 642 for (uint32_t i = 0; i < getNumWords(); ++i) 643 pVal[i] ^= -1ULL; 644 return clearUnusedBits(); 645} 646 647/// Toggle a given bit to its opposite value whose position is given 648/// as "bitPosition". 649/// @brief Toggles a given bit to its opposite value. 650APInt& APInt::flip(uint32_t bitPosition) { 651 assert(bitPosition < BitWidth && "Out of the bit-width range!"); 652 if ((*this)[bitPosition]) clear(bitPosition); 653 else set(bitPosition); 654 return *this; 655} 656 657uint64_t APInt::getHashValue() const { 658 // Put the bit width into the low order bits. 659 uint64_t hash = BitWidth; 660 661 // Add the sum of the words to the hash. 662 if (isSingleWord()) 663 hash += VAL << 6; // clear separation of up to 64 bits 664 else 665 for (uint32_t i = 0; i < getNumWords(); ++i) 666 hash += pVal[i] << 6; // clear sepration of up to 64 bits 667 return hash; 668} 669 670/// HiBits - This function returns the high "numBits" bits of this APInt. 671APInt APInt::getHiBits(uint32_t numBits) const { 672 return APIntOps::lshr(*this, BitWidth - numBits); 673} 674 675/// LoBits - This function returns the low "numBits" bits of this APInt. 676APInt APInt::getLoBits(uint32_t numBits) const { 677 return APIntOps::lshr(APIntOps::shl(*this, BitWidth - numBits), 678 BitWidth - numBits); 679} 680 681bool APInt::isPowerOf2() const { 682 return (!!*this) && !(*this & (*this - APInt(BitWidth,1))); 683} 684 685uint32_t APInt::countLeadingZeros() const { 686 uint32_t Count = 0; 687 if (isSingleWord()) 688 Count = CountLeadingZeros_64(VAL); 689 else { 690 for (uint32_t i = getNumWords(); i > 0u; --i) { 691 if (pVal[i-1] == 0) 692 Count += APINT_BITS_PER_WORD; 693 else { 694 Count += CountLeadingZeros_64(pVal[i-1]); 695 break; 696 } 697 } 698 } 699 uint32_t remainder = BitWidth % APINT_BITS_PER_WORD; 700 if (remainder) 701 Count -= APINT_BITS_PER_WORD - remainder; 702 return Count; 703} 704 705uint32_t APInt::countTrailingZeros() const { 706 if (isSingleWord()) 707 return CountTrailingZeros_64(VAL); 708 uint32_t Count = 0; 709 uint32_t i = 0; 710 for (; i < getNumWords() && pVal[i] == 0; ++i) 711 Count += APINT_BITS_PER_WORD; 712 if (i < getNumWords()) 713 Count += CountTrailingZeros_64(pVal[i]); 714 return Count; 715} 716 717uint32_t APInt::countPopulation() const { 718 if (isSingleWord()) 719 return CountPopulation_64(VAL); 720 uint32_t Count = 0; 721 for (uint32_t i = 0; i < getNumWords(); ++i) 722 Count += CountPopulation_64(pVal[i]); 723 return Count; 724} 725 726APInt APInt::byteSwap() const { 727 assert(BitWidth >= 16 && BitWidth % 16 == 0 && "Cannot byteswap!"); 728 if (BitWidth == 16) 729 return APInt(BitWidth, ByteSwap_16(VAL)); 730 else if (BitWidth == 32) 731 return APInt(BitWidth, ByteSwap_32(VAL)); 732 else if (BitWidth == 48) { 733 uint64_t Tmp1 = ((VAL >> 32) << 16) | (VAL & 0xFFFF); 734 Tmp1 = ByteSwap_32(Tmp1); 735 uint64_t Tmp2 = (VAL >> 16) & 0xFFFF; 736 Tmp2 = ByteSwap_16(Tmp2); 737 return 738 APInt(BitWidth, 739 (Tmp1 & 0xff) | ((Tmp1<<16) & 0xffff00000000ULL) | (Tmp2 << 16)); 740 } else if (BitWidth == 64) 741 return APInt(BitWidth, ByteSwap_64(VAL)); 742 else { 743 APInt Result(BitWidth, 0); 744 char *pByte = (char*)Result.pVal; 745 for (uint32_t i = 0; i < BitWidth / APINT_WORD_SIZE / 2; ++i) { 746 char Tmp = pByte[i]; 747 pByte[i] = pByte[BitWidth / APINT_WORD_SIZE - 1 - i]; 748 pByte[BitWidth / APINT_WORD_SIZE - i - 1] = Tmp; 749 } 750 return Result; 751 } 752} 753 754APInt llvm::APIntOps::GreatestCommonDivisor(const APInt& API1, 755 const APInt& API2) { 756 APInt A = API1, B = API2; 757 while (!!B) { 758 APInt T = B; 759 B = APIntOps::urem(A, B); 760 A = T; 761 } 762 return A; 763} 764 765APInt llvm::APIntOps::RoundDoubleToAPInt(double Double, uint32_t width) { 766 union { 767 double D; 768 uint64_t I; 769 } T; 770 T.D = Double; 771 772 // Get the sign bit from the highest order bit 773 bool isNeg = T.I >> 63; 774 775 // Get the 11-bit exponent and adjust for the 1023 bit bias 776 int64_t exp = ((T.I >> 52) & 0x7ff) - 1023; 777 778 // If the exponent is negative, the value is < 0 so just return 0. 779 if (exp < 0) 780 return APInt(64u, 0u); 781 782 // Extract the mantissa by clearing the top 12 bits (sign + exponent). 783 uint64_t mantissa = (T.I & (~0ULL >> 12)) | 1ULL << 52; 784 785 // If the exponent doesn't shift all bits out of the mantissa 786 if (exp < 52) 787 return isNeg ? -APInt(width, mantissa >> (52 - exp)) : 788 APInt(width, mantissa >> (52 - exp)); 789 790 // If the client didn't provide enough bits for us to shift the mantissa into 791 // then the result is undefined, just return 0 792 if (width <= exp - 52) 793 return APInt(width, 0); 794 795 // Otherwise, we have to shift the mantissa bits up to the right location 796 APInt Tmp(width, mantissa); 797 Tmp = Tmp.shl(exp - 52); 798 return isNeg ? -Tmp : Tmp; 799} 800 801/// RoundToDouble - This function convert this APInt to a double. 802/// The layout for double is as following (IEEE Standard 754): 803/// -------------------------------------- 804/// | Sign Exponent Fraction Bias | 805/// |-------------------------------------- | 806/// | 1[63] 11[62-52] 52[51-00] 1023 | 807/// -------------------------------------- 808double APInt::roundToDouble(bool isSigned) const { 809 810 // Handle the simple case where the value is contained in one uint64_t. 811 if (isSingleWord() || getActiveBits() <= APINT_BITS_PER_WORD) { 812 if (isSigned) { 813 int64_t sext = (int64_t(VAL) << (64-BitWidth)) >> (64-BitWidth); 814 return double(sext); 815 } else 816 return double(VAL); 817 } 818 819 // Determine if the value is negative. 820 bool isNeg = isSigned ? (*this)[BitWidth-1] : false; 821 822 // Construct the absolute value if we're negative. 823 APInt Tmp(isNeg ? -(*this) : (*this)); 824 825 // Figure out how many bits we're using. 826 uint32_t n = Tmp.getActiveBits(); 827 828 // The exponent (without bias normalization) is just the number of bits 829 // we are using. Note that the sign bit is gone since we constructed the 830 // absolute value. 831 uint64_t exp = n; 832 833 // Return infinity for exponent overflow 834 if (exp > 1023) { 835 if (!isSigned || !isNeg) 836 return double(1.0E300 * 1.0E300); // positive infinity 837 else 838 return double(-1.0E300 * 1.0E300); // negative infinity 839 } 840 exp += 1023; // Increment for 1023 bias 841 842 // Number of bits in mantissa is 52. To obtain the mantissa value, we must 843 // extract the high 52 bits from the correct words in pVal. 844 uint64_t mantissa; 845 unsigned hiWord = whichWord(n-1); 846 if (hiWord == 0) { 847 mantissa = Tmp.pVal[0]; 848 if (n > 52) 849 mantissa >>= n - 52; // shift down, we want the top 52 bits. 850 } else { 851 assert(hiWord > 0 && "huh?"); 852 uint64_t hibits = Tmp.pVal[hiWord] << (52 - n % APINT_BITS_PER_WORD); 853 uint64_t lobits = Tmp.pVal[hiWord-1] >> (11 + n % APINT_BITS_PER_WORD); 854 mantissa = hibits | lobits; 855 } 856 857 // The leading bit of mantissa is implicit, so get rid of it. 858 uint64_t sign = isNeg ? (1ULL << (APINT_BITS_PER_WORD - 1)) : 0; 859 union { 860 double D; 861 uint64_t I; 862 } T; 863 T.I = sign | (exp << 52) | mantissa; 864 return T.D; 865} 866 867// Truncate to new width. 868void APInt::trunc(uint32_t width) { 869 assert(width < BitWidth && "Invalid APInt Truncate request"); 870 assert(width >= IntegerType::MIN_INT_BITS && "Can't truncate to 0 bits"); 871 uint32_t wordsBefore = getNumWords(); 872 BitWidth = width; 873 uint32_t wordsAfter = getNumWords(); 874 if (wordsBefore != wordsAfter) { 875 if (wordsAfter == 1) { 876 uint64_t *tmp = pVal; 877 VAL = pVal[0]; 878 delete [] tmp; 879 } else { 880 uint64_t *newVal = getClearedMemory(wordsAfter); 881 for (uint32_t i = 0; i < wordsAfter; ++i) 882 newVal[i] = pVal[i]; 883 delete [] pVal; 884 pVal = newVal; 885 } 886 } 887 clearUnusedBits(); 888} 889 890// Sign extend to a new width. 891void APInt::sext(uint32_t width) { 892 assert(width > BitWidth && "Invalid APInt SignExtend request"); 893 assert(width <= IntegerType::MAX_INT_BITS && "Too many bits"); 894 // If the sign bit isn't set, this is the same as zext. 895 if (!isNegative()) { 896 zext(width); 897 return; 898 } 899 900 // The sign bit is set. First, get some facts 901 uint32_t wordsBefore = getNumWords(); 902 uint32_t wordBits = BitWidth % APINT_BITS_PER_WORD; 903 BitWidth = width; 904 uint32_t wordsAfter = getNumWords(); 905 906 // Mask the high order word appropriately 907 if (wordsBefore == wordsAfter) { 908 uint32_t newWordBits = width % APINT_BITS_PER_WORD; 909 // The extension is contained to the wordsBefore-1th word. 910 uint64_t mask = (~0ULL >> (APINT_BITS_PER_WORD - newWordBits)) << wordBits; 911 if (wordsBefore == 1) 912 VAL |= mask; 913 else 914 pVal[wordsBefore-1] |= mask; 915 clearUnusedBits(); 916 return; 917 } 918 919 uint64_t mask = wordBits == 0 ? 0 : ~0ULL << wordBits; 920 uint64_t *newVal = getMemory(wordsAfter); 921 if (wordsBefore == 1) 922 newVal[0] = VAL | mask; 923 else { 924 for (uint32_t i = 0; i < wordsBefore; ++i) 925 newVal[i] = pVal[i]; 926 newVal[wordsBefore-1] |= mask; 927 } 928 for (uint32_t i = wordsBefore; i < wordsAfter; i++) 929 newVal[i] = -1ULL; 930 if (wordsBefore != 1) 931 delete [] pVal; 932 pVal = newVal; 933 clearUnusedBits(); 934} 935 936// Zero extend to a new width. 937void APInt::zext(uint32_t width) { 938 assert(width > BitWidth && "Invalid APInt ZeroExtend request"); 939 assert(width <= IntegerType::MAX_INT_BITS && "Too many bits"); 940 uint32_t wordsBefore = getNumWords(); 941 BitWidth = width; 942 uint32_t wordsAfter = getNumWords(); 943 if (wordsBefore != wordsAfter) { 944 uint64_t *newVal = getClearedMemory(wordsAfter); 945 if (wordsBefore == 1) 946 newVal[0] = VAL; 947 else 948 for (uint32_t i = 0; i < wordsBefore; ++i) 949 newVal[i] = pVal[i]; 950 if (wordsBefore != 1) 951 delete [] pVal; 952 pVal = newVal; 953 } 954} 955 956/// Arithmetic right-shift this APInt by shiftAmt. 957/// @brief Arithmetic right-shift function. 958APInt APInt::ashr(uint32_t shiftAmt) const { 959 assert(shiftAmt <= BitWidth && "Invalid shift amount"); 960 if (isSingleWord()) { 961 if (shiftAmt == BitWidth) 962 return APInt(BitWidth, 0); // undefined 963 else { 964 uint32_t SignBit = APINT_BITS_PER_WORD - BitWidth; 965 return APInt(BitWidth, 966 (((int64_t(VAL) << SignBit) >> SignBit) >> shiftAmt)); 967 } 968 } 969 970 // If all the bits were shifted out, the result is 0 or -1. This avoids issues 971 // with shifting by the size of the integer type, which produces undefined 972 // results. 973 if (shiftAmt == BitWidth) 974 if (isNegative()) 975 return APInt(BitWidth, -1ULL); 976 else 977 return APInt(BitWidth, 0); 978 979 // Create some space for the result. 980 uint64_t * val = new uint64_t[getNumWords()]; 981 982 // If we are shifting less than a word, compute the shift with a simple carry 983 if (shiftAmt < APINT_BITS_PER_WORD) { 984 uint64_t carry = 0; 985 for (int i = getNumWords()-1; i >= 0; --i) { 986 val[i] = pVal[i] >> shiftAmt | carry; 987 carry = pVal[i] << (APINT_BITS_PER_WORD - shiftAmt); 988 } 989 return APInt(val, BitWidth).clearUnusedBits(); 990 } 991 992 // Compute some values needed by the remaining shift algorithms 993 uint32_t wordShift = shiftAmt % APINT_BITS_PER_WORD; 994 uint32_t offset = shiftAmt / APINT_BITS_PER_WORD; 995 996 // If we are shifting whole words, just move whole words 997 if (wordShift == 0) { 998 for (uint32_t i = 0; i < getNumWords() - offset; ++i) 999 val[i] = pVal[i+offset]; 1000 for (uint32_t i = getNumWords()-offset; i < getNumWords(); i++) 1001 val[i] = (isNegative() ? -1ULL : 0); 1002 return APInt(val,BitWidth).clearUnusedBits(); 1003 } 1004 1005 // Shift the low order words 1006 uint32_t breakWord = getNumWords() - offset -1; 1007 for (uint32_t i = 0; i < breakWord; ++i) 1008 val[i] = pVal[i+offset] >> wordShift | 1009 pVal[i+offset+1] << (APINT_BITS_PER_WORD - wordShift); 1010 // Shift the break word. 1011 uint32_t SignBit = APINT_BITS_PER_WORD - (BitWidth % APINT_BITS_PER_WORD); 1012 val[breakWord] = uint64_t( 1013 (((int64_t(pVal[breakWord+offset]) << SignBit) >> SignBit) >> wordShift)); 1014 1015 // Remaining words are 0 or -1 1016 for (uint32_t i = breakWord+1; i < getNumWords(); ++i) 1017 val[i] = (isNegative() ? -1ULL : 0); 1018 return APInt(val, BitWidth).clearUnusedBits(); 1019} 1020 1021/// Logical right-shift this APInt by shiftAmt. 1022/// @brief Logical right-shift function. 1023APInt APInt::lshr(uint32_t shiftAmt) const { 1024 if (isSingleWord()) 1025 if (shiftAmt == BitWidth) 1026 return APInt(BitWidth, 0); 1027 else 1028 return APInt(BitWidth, this->VAL >> shiftAmt); 1029 1030 // If all the bits were shifted out, the result is 0. This avoids issues 1031 // with shifting by the size of the integer type, which produces undefined 1032 // results. We define these "undefined results" to always be 0. 1033 if (shiftAmt == BitWidth) 1034 return APInt(BitWidth, 0); 1035 1036 // Create some space for the result. 1037 uint64_t * val = new uint64_t[getNumWords()]; 1038 1039 // If we are shifting less than a word, compute the shift with a simple carry 1040 if (shiftAmt < APINT_BITS_PER_WORD) { 1041 uint64_t carry = 0; 1042 for (int i = getNumWords()-1; i >= 0; --i) { 1043 val[i] = pVal[i] >> shiftAmt | carry; 1044 carry = pVal[i] << (APINT_BITS_PER_WORD - shiftAmt); 1045 } 1046 return APInt(val, BitWidth).clearUnusedBits(); 1047 } 1048 1049 // Compute some values needed by the remaining shift algorithms 1050 uint32_t wordShift = shiftAmt % APINT_BITS_PER_WORD; 1051 uint32_t offset = shiftAmt / APINT_BITS_PER_WORD; 1052 1053 // If we are shifting whole words, just move whole words 1054 if (wordShift == 0) { 1055 for (uint32_t i = 0; i < getNumWords() - offset; ++i) 1056 val[i] = pVal[i+offset]; 1057 for (uint32_t i = getNumWords()-offset; i < getNumWords(); i++) 1058 val[i] = 0; 1059 return APInt(val,BitWidth).clearUnusedBits(); 1060 } 1061 1062 // Shift the low order words 1063 uint32_t breakWord = getNumWords() - offset -1; 1064 for (uint32_t i = 0; i < breakWord; ++i) 1065 val[i] = pVal[i+offset] >> wordShift | 1066 pVal[i+offset+1] << (APINT_BITS_PER_WORD - wordShift); 1067 // Shift the break word. 1068 val[breakWord] = pVal[breakWord+offset] >> wordShift; 1069 1070 // Remaining words are 0 1071 for (uint32_t i = breakWord+1; i < getNumWords(); ++i) 1072 val[i] = 0; 1073 return APInt(val, BitWidth).clearUnusedBits(); 1074} 1075 1076/// Left-shift this APInt by shiftAmt. 1077/// @brief Left-shift function. 1078APInt APInt::shl(uint32_t shiftAmt) const { 1079 assert(shiftAmt <= BitWidth && "Invalid shift amount"); 1080 if (isSingleWord()) { 1081 if (shiftAmt == BitWidth) 1082 return APInt(BitWidth, 0); // avoid undefined shift results 1083 return APInt(BitWidth, VAL << shiftAmt); 1084 } 1085 1086 // If all the bits were shifted out, the result is 0. This avoids issues 1087 // with shifting by the size of the integer type, which produces undefined 1088 // results. We define these "undefined results" to always be 0. 1089 if (shiftAmt == BitWidth) 1090 return APInt(BitWidth, 0); 1091 1092 // Create some space for the result. 1093 uint64_t * val = new uint64_t[getNumWords()]; 1094 1095 // If we are shifting less than a word, do it the easy way 1096 if (shiftAmt < APINT_BITS_PER_WORD) { 1097 uint64_t carry = 0; 1098 for (uint32_t i = 0; i < getNumWords(); i++) { 1099 val[i] = pVal[i] << shiftAmt | carry; 1100 carry = pVal[i] >> (APINT_BITS_PER_WORD - shiftAmt); 1101 } 1102 return APInt(val, BitWidth).clearUnusedBits(); 1103 } 1104 1105 // Compute some values needed by the remaining shift algorithms 1106 uint32_t wordShift = shiftAmt % APINT_BITS_PER_WORD; 1107 uint32_t offset = shiftAmt / APINT_BITS_PER_WORD; 1108 1109 // If we are shifting whole words, just move whole words 1110 if (wordShift == 0) { 1111 for (uint32_t i = 0; i < offset; i++) 1112 val[i] = 0; 1113 for (uint32_t i = offset; i < getNumWords(); i++) 1114 val[i] = pVal[i-offset]; 1115 return APInt(val,BitWidth).clearUnusedBits(); 1116 } 1117 1118 // Copy whole words from this to Result. 1119 uint32_t i = getNumWords() - 1; 1120 for (; i > offset; --i) 1121 val[i] = pVal[i-offset] << wordShift | 1122 pVal[i-offset-1] >> (APINT_BITS_PER_WORD - wordShift); 1123 val[offset] = pVal[0] << wordShift; 1124 for (i = 0; i < offset; ++i) 1125 val[i] = 0; 1126 return APInt(val, BitWidth).clearUnusedBits(); 1127} 1128 1129/// Implementation of Knuth's Algorithm D (Division of nonnegative integers) 1130/// from "Art of Computer Programming, Volume 2", section 4.3.1, p. 272. The 1131/// variables here have the same names as in the algorithm. Comments explain 1132/// the algorithm and any deviation from it. 1133static void KnuthDiv(uint32_t *u, uint32_t *v, uint32_t *q, uint32_t* r, 1134 uint32_t m, uint32_t n) { 1135 assert(u && "Must provide dividend"); 1136 assert(v && "Must provide divisor"); 1137 assert(q && "Must provide quotient"); 1138 assert(u != v && u != q && v != q && "Must us different memory"); 1139 assert(n>1 && "n must be > 1"); 1140 1141 // Knuth uses the value b as the base of the number system. In our case b 1142 // is 2^31 so we just set it to -1u. 1143 uint64_t b = uint64_t(1) << 32; 1144 1145 DEBUG(cerr << "KnuthDiv: m=" << m << " n=" << n << '\n'); 1146 DEBUG(cerr << "KnuthDiv: original:"); 1147 DEBUG(for (int i = m+n; i >=0; i--) cerr << " " << std::setbase(16) << u[i]); 1148 DEBUG(cerr << " by"); 1149 DEBUG(for (int i = n; i >0; i--) cerr << " " << std::setbase(16) << v[i-1]); 1150 DEBUG(cerr << '\n'); 1151 // D1. [Normalize.] Set d = b / (v[n-1] + 1) and multiply all the digits of 1152 // u and v by d. Note that we have taken Knuth's advice here to use a power 1153 // of 2 value for d such that d * v[n-1] >= b/2 (b is the base). A power of 1154 // 2 allows us to shift instead of multiply and it is easy to determine the 1155 // shift amount from the leading zeros. We are basically normalizing the u 1156 // and v so that its high bits are shifted to the top of v's range without 1157 // overflow. Note that this can require an extra word in u so that u must 1158 // be of length m+n+1. 1159 uint32_t shift = CountLeadingZeros_32(v[n-1]); 1160 uint32_t v_carry = 0; 1161 uint32_t u_carry = 0; 1162 if (shift) { 1163 for (uint32_t i = 0; i < m+n; ++i) { 1164 uint32_t u_tmp = u[i] >> (32 - shift); 1165 u[i] = (u[i] << shift) | u_carry; 1166 u_carry = u_tmp; 1167 } 1168 for (uint32_t i = 0; i < n; ++i) { 1169 uint32_t v_tmp = v[i] >> (32 - shift); 1170 v[i] = (v[i] << shift) | v_carry; 1171 v_carry = v_tmp; 1172 } 1173 } 1174 u[m+n] = u_carry; 1175 DEBUG(cerr << "KnuthDiv: normal:"); 1176 DEBUG(for (int i = m+n; i >=0; i--) cerr << " " << std::setbase(16) << u[i]); 1177 DEBUG(cerr << " by"); 1178 DEBUG(for (int i = n; i >0; i--) cerr << " " << std::setbase(16) << v[i-1]); 1179 DEBUG(cerr << '\n'); 1180 1181 // D2. [Initialize j.] Set j to m. This is the loop counter over the places. 1182 int j = m; 1183 do { 1184 DEBUG(cerr << "KnuthDiv: quotient digit #" << j << '\n'); 1185 // D3. [Calculate q'.]. 1186 // Set qp = (u[j+n]*b + u[j+n-1]) / v[n-1]. (qp=qprime=q') 1187 // Set rp = (u[j+n]*b + u[j+n-1]) % v[n-1]. (rp=rprime=r') 1188 // Now test if qp == b or qp*v[n-2] > b*rp + u[j+n-2]; if so, decrease 1189 // qp by 1, inrease rp by v[n-1], and repeat this test if rp < b. The test 1190 // on v[n-2] determines at high speed most of the cases in which the trial 1191 // value qp is one too large, and it eliminates all cases where qp is two 1192 // too large. 1193 uint64_t dividend = ((uint64_t(u[j+n]) << 32) + u[j+n-1]); 1194 DEBUG(cerr << "KnuthDiv: dividend == " << dividend << '\n'); 1195 uint64_t qp = dividend / v[n-1]; 1196 uint64_t rp = dividend % v[n-1]; 1197 if (qp == b || qp*v[n-2] > b*rp + u[j+n-2]) { 1198 qp--; 1199 rp += v[n-1]; 1200 if (rp < b && (qp == b || qp*v[n-2] > b*rp + u[j+n-2])) 1201 qp--; 1202 } 1203 DEBUG(cerr << "KnuthDiv: qp == " << qp << ", rp == " << rp << '\n'); 1204 1205 // D4. [Multiply and subtract.] Replace (u[j+n]u[j+n-1]...u[j]) with 1206 // (u[j+n]u[j+n-1]..u[j]) - qp * (v[n-1]...v[1]v[0]). This computation 1207 // consists of a simple multiplication by a one-place number, combined with 1208 // a subtraction. 1209 bool isNeg = false; 1210 for (uint32_t i = 0; i < n; ++i) { 1211 uint64_t u_tmp = uint64_t(u[j+i]) | (uint64_t(u[j+i+1]) << 32); 1212 uint64_t subtrahend = uint64_t(qp) * uint64_t(v[i]); 1213 bool borrow = subtrahend > u_tmp; 1214 DEBUG(cerr << "KnuthDiv: u_tmp == " << u_tmp 1215 << ", subtrahend == " << subtrahend 1216 << ", borrow = " << borrow << '\n'); 1217 1218 uint64_t result = u_tmp - subtrahend; 1219 uint32_t k = j + i; 1220 u[k++] = result & (b-1); // subtract low word 1221 u[k++] = result >> 32; // subtract high word 1222 while (borrow && k <= m+n) { // deal with borrow to the left 1223 borrow = u[k] == 0; 1224 u[k]--; 1225 k++; 1226 } 1227 isNeg |= borrow; 1228 DEBUG(cerr << "KnuthDiv: u[j+i] == " << u[j+i] << ", u[j+i+1] == " << 1229 u[j+i+1] << '\n'); 1230 } 1231 DEBUG(cerr << "KnuthDiv: after subtraction:"); 1232 DEBUG(for (int i = m+n; i >=0; i--) cerr << " " << u[i]); 1233 DEBUG(cerr << '\n'); 1234 // The digits (u[j+n]...u[j]) should be kept positive; if the result of 1235 // this step is actually negative, (u[j+n]...u[j]) should be left as the 1236 // true value plus b**(n+1), namely as the b's complement of 1237 // the true value, and a "borrow" to the left should be remembered. 1238 // 1239 if (isNeg) { 1240 bool carry = true; // true because b's complement is "complement + 1" 1241 for (uint32_t i = 0; i <= m+n; ++i) { 1242 u[i] = ~u[i] + carry; // b's complement 1243 carry = carry && u[i] == 0; 1244 } 1245 } 1246 DEBUG(cerr << "KnuthDiv: after complement:"); 1247 DEBUG(for (int i = m+n; i >=0; i--) cerr << " " << u[i]); 1248 DEBUG(cerr << '\n'); 1249 1250 // D5. [Test remainder.] Set q[j] = qp. If the result of step D4 was 1251 // negative, go to step D6; otherwise go on to step D7. 1252 q[j] = qp; 1253 if (isNeg) { 1254 // D6. [Add back]. The probability that this step is necessary is very 1255 // small, on the order of only 2/b. Make sure that test data accounts for 1256 // this possibility. Decrease q[j] by 1 1257 q[j]--; 1258 // and add (0v[n-1]...v[1]v[0]) to (u[j+n]u[j+n-1]...u[j+1]u[j]). 1259 // A carry will occur to the left of u[j+n], and it should be ignored 1260 // since it cancels with the borrow that occurred in D4. 1261 bool carry = false; 1262 for (uint32_t i = 0; i < n; i++) { 1263 uint32_t limit = std::min(u[j+i],v[i]); 1264 u[j+i] += v[i] + carry; 1265 carry = u[j+i] < limit || (carry && u[j+i] == limit); 1266 } 1267 u[j+n] += carry; 1268 } 1269 DEBUG(cerr << "KnuthDiv: after correction:"); 1270 DEBUG(for (int i = m+n; i >=0; i--) cerr <<" " << u[i]); 1271 DEBUG(cerr << "\nKnuthDiv: digit result = " << q[j] << '\n'); 1272 1273 // D7. [Loop on j.] Decrease j by one. Now if j >= 0, go back to D3. 1274 } while (--j >= 0); 1275 1276 DEBUG(cerr << "KnuthDiv: quotient:"); 1277 DEBUG(for (int i = m; i >=0; i--) cerr <<" " << q[i]); 1278 DEBUG(cerr << '\n'); 1279 1280 // D8. [Unnormalize]. Now q[...] is the desired quotient, and the desired 1281 // remainder may be obtained by dividing u[...] by d. If r is non-null we 1282 // compute the remainder (urem uses this). 1283 if (r) { 1284 // The value d is expressed by the "shift" value above since we avoided 1285 // multiplication by d by using a shift left. So, all we have to do is 1286 // shift right here. In order to mak 1287 if (shift) { 1288 uint32_t carry = 0; 1289 DEBUG(cerr << "KnuthDiv: remainder:"); 1290 for (int i = n-1; i >= 0; i--) { 1291 r[i] = (u[i] >> shift) | carry; 1292 carry = u[i] << (32 - shift); 1293 DEBUG(cerr << " " << r[i]); 1294 } 1295 } else { 1296 for (int i = n-1; i >= 0; i--) { 1297 r[i] = u[i]; 1298 DEBUG(cerr << " " << r[i]); 1299 } 1300 } 1301 DEBUG(cerr << '\n'); 1302 } 1303 DEBUG(cerr << std::setbase(10) << '\n'); 1304} 1305 1306void APInt::divide(const APInt LHS, uint32_t lhsWords, 1307 const APInt &RHS, uint32_t rhsWords, 1308 APInt *Quotient, APInt *Remainder) 1309{ 1310 assert(lhsWords >= rhsWords && "Fractional result"); 1311 1312 // First, compose the values into an array of 32-bit words instead of 1313 // 64-bit words. This is a necessity of both the "short division" algorithm 1314 // and the the Knuth "classical algorithm" which requires there to be native 1315 // operations for +, -, and * on an m bit value with an m*2 bit result. We 1316 // can't use 64-bit operands here because we don't have native results of 1317 // 128-bits. Furthremore, casting the 64-bit values to 32-bit values won't 1318 // work on large-endian machines. 1319 uint64_t mask = ~0ull >> (sizeof(uint32_t)*8); 1320 uint32_t n = rhsWords * 2; 1321 uint32_t m = (lhsWords * 2) - n; 1322 1323 // Allocate space for the temporary values we need either on the stack, if 1324 // it will fit, or on the heap if it won't. 1325 uint32_t SPACE[128]; 1326 uint32_t *U = 0; 1327 uint32_t *V = 0; 1328 uint32_t *Q = 0; 1329 uint32_t *R = 0; 1330 if ((Remainder?4:3)*n+2*m+1 <= 128) { 1331 U = &SPACE[0]; 1332 V = &SPACE[m+n+1]; 1333 Q = &SPACE[(m+n+1) + n]; 1334 if (Remainder) 1335 R = &SPACE[(m+n+1) + n + (m+n)]; 1336 } else { 1337 U = new uint32_t[m + n + 1]; 1338 V = new uint32_t[n]; 1339 Q = new uint32_t[m+n]; 1340 if (Remainder) 1341 R = new uint32_t[n]; 1342 } 1343 1344 // Initialize the dividend 1345 memset(U, 0, (m+n+1)*sizeof(uint32_t)); 1346 for (unsigned i = 0; i < lhsWords; ++i) { 1347 uint64_t tmp = (LHS.getNumWords() == 1 ? LHS.VAL : LHS.pVal[i]); 1348 U[i * 2] = tmp & mask; 1349 U[i * 2 + 1] = tmp >> (sizeof(uint32_t)*8); 1350 } 1351 U[m+n] = 0; // this extra word is for "spill" in the Knuth algorithm. 1352 1353 // Initialize the divisor 1354 memset(V, 0, (n)*sizeof(uint32_t)); 1355 for (unsigned i = 0; i < rhsWords; ++i) { 1356 uint64_t tmp = (RHS.getNumWords() == 1 ? RHS.VAL : RHS.pVal[i]); 1357 V[i * 2] = tmp & mask; 1358 V[i * 2 + 1] = tmp >> (sizeof(uint32_t)*8); 1359 } 1360 1361 // initialize the quotient and remainder 1362 memset(Q, 0, (m+n) * sizeof(uint32_t)); 1363 if (Remainder) 1364 memset(R, 0, n * sizeof(uint32_t)); 1365 1366 // Now, adjust m and n for the Knuth division. n is the number of words in 1367 // the divisor. m is the number of words by which the dividend exceeds the 1368 // divisor (i.e. m+n is the length of the dividend). These sizes must not 1369 // contain any zero words or the Knuth algorithm fails. 1370 for (unsigned i = n; i > 0 && V[i-1] == 0; i--) { 1371 n--; 1372 m++; 1373 } 1374 for (unsigned i = m+n; i > 0 && U[i-1] == 0; i--) 1375 m--; 1376 1377 // If we're left with only a single word for the divisor, Knuth doesn't work 1378 // so we implement the short division algorithm here. This is much simpler 1379 // and faster because we are certain that we can divide a 64-bit quantity 1380 // by a 32-bit quantity at hardware speed and short division is simply a 1381 // series of such operations. This is just like doing short division but we 1382 // are using base 2^32 instead of base 10. 1383 assert(n != 0 && "Divide by zero?"); 1384 if (n == 1) { 1385 uint32_t divisor = V[0]; 1386 uint32_t remainder = 0; 1387 for (int i = m+n-1; i >= 0; i--) { 1388 uint64_t partial_dividend = uint64_t(remainder) << 32 | U[i]; 1389 if (partial_dividend == 0) { 1390 Q[i] = 0; 1391 remainder = 0; 1392 } else if (partial_dividend < divisor) { 1393 Q[i] = 0; 1394 remainder = partial_dividend; 1395 } else if (partial_dividend == divisor) { 1396 Q[i] = 1; 1397 remainder = 0; 1398 } else { 1399 Q[i] = partial_dividend / divisor; 1400 remainder = partial_dividend - (Q[i] * divisor); 1401 } 1402 } 1403 if (R) 1404 R[0] = remainder; 1405 } else { 1406 // Now we're ready to invoke the Knuth classical divide algorithm. In this 1407 // case n > 1. 1408 KnuthDiv(U, V, Q, R, m, n); 1409 } 1410 1411 // If the caller wants the quotient 1412 if (Quotient) { 1413 // Set up the Quotient value's memory. 1414 if (Quotient->BitWidth != LHS.BitWidth) { 1415 if (Quotient->isSingleWord()) 1416 Quotient->VAL = 0; 1417 else 1418 delete [] Quotient->pVal; 1419 Quotient->BitWidth = LHS.BitWidth; 1420 if (!Quotient->isSingleWord()) 1421 Quotient->pVal = getClearedMemory(Quotient->getNumWords()); 1422 } else 1423 Quotient->clear(); 1424 1425 // The quotient is in Q. Reconstitute the quotient into Quotient's low 1426 // order words. 1427 if (lhsWords == 1) { 1428 uint64_t tmp = 1429 uint64_t(Q[0]) | (uint64_t(Q[1]) << (APINT_BITS_PER_WORD / 2)); 1430 if (Quotient->isSingleWord()) 1431 Quotient->VAL = tmp; 1432 else 1433 Quotient->pVal[0] = tmp; 1434 } else { 1435 assert(!Quotient->isSingleWord() && "Quotient APInt not large enough"); 1436 for (unsigned i = 0; i < lhsWords; ++i) 1437 Quotient->pVal[i] = 1438 uint64_t(Q[i*2]) | (uint64_t(Q[i*2+1]) << (APINT_BITS_PER_WORD / 2)); 1439 } 1440 } 1441 1442 // If the caller wants the remainder 1443 if (Remainder) { 1444 // Set up the Remainder value's memory. 1445 if (Remainder->BitWidth != RHS.BitWidth) { 1446 if (Remainder->isSingleWord()) 1447 Remainder->VAL = 0; 1448 else 1449 delete [] Remainder->pVal; 1450 Remainder->BitWidth = RHS.BitWidth; 1451 if (!Remainder->isSingleWord()) 1452 Remainder->pVal = getClearedMemory(Remainder->getNumWords()); 1453 } else 1454 Remainder->clear(); 1455 1456 // The remainder is in R. Reconstitute the remainder into Remainder's low 1457 // order words. 1458 if (rhsWords == 1) { 1459 uint64_t tmp = 1460 uint64_t(R[0]) | (uint64_t(R[1]) << (APINT_BITS_PER_WORD / 2)); 1461 if (Remainder->isSingleWord()) 1462 Remainder->VAL = tmp; 1463 else 1464 Remainder->pVal[0] = tmp; 1465 } else { 1466 assert(!Remainder->isSingleWord() && "Remainder APInt not large enough"); 1467 for (unsigned i = 0; i < rhsWords; ++i) 1468 Remainder->pVal[i] = 1469 uint64_t(R[i*2]) | (uint64_t(R[i*2+1]) << (APINT_BITS_PER_WORD / 2)); 1470 } 1471 } 1472 1473 // Clean up the memory we allocated. 1474 if (U != &SPACE[0]) { 1475 delete [] U; 1476 delete [] V; 1477 delete [] Q; 1478 delete [] R; 1479 } 1480} 1481 1482APInt APInt::udiv(const APInt& RHS) const { 1483 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 1484 1485 // First, deal with the easy case 1486 if (isSingleWord()) { 1487 assert(RHS.VAL != 0 && "Divide by zero?"); 1488 return APInt(BitWidth, VAL / RHS.VAL); 1489 } 1490 1491 // Get some facts about the LHS and RHS number of bits and words 1492 uint32_t rhsBits = RHS.getActiveBits(); 1493 uint32_t rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1); 1494 assert(rhsWords && "Divided by zero???"); 1495 uint32_t lhsBits = this->getActiveBits(); 1496 uint32_t lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1); 1497 1498 // Deal with some degenerate cases 1499 if (!lhsWords) 1500 // 0 / X ===> 0 1501 return APInt(BitWidth, 0); 1502 else if (lhsWords < rhsWords || this->ult(RHS)) { 1503 // X / Y ===> 0, iff X < Y 1504 return APInt(BitWidth, 0); 1505 } else if (*this == RHS) { 1506 // X / X ===> 1 1507 return APInt(BitWidth, 1); 1508 } else if (lhsWords == 1 && rhsWords == 1) { 1509 // All high words are zero, just use native divide 1510 return APInt(BitWidth, this->pVal[0] / RHS.pVal[0]); 1511 } 1512 1513 // We have to compute it the hard way. Invoke the Knuth divide algorithm. 1514 APInt Quotient(1,0); // to hold result. 1515 divide(*this, lhsWords, RHS, rhsWords, &Quotient, 0); 1516 return Quotient; 1517} 1518 1519APInt APInt::urem(const APInt& RHS) const { 1520 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 1521 if (isSingleWord()) { 1522 assert(RHS.VAL != 0 && "Remainder by zero?"); 1523 return APInt(BitWidth, VAL % RHS.VAL); 1524 } 1525 1526 // Get some facts about the LHS 1527 uint32_t lhsBits = getActiveBits(); 1528 uint32_t lhsWords = !lhsBits ? 0 : (whichWord(lhsBits - 1) + 1); 1529 1530 // Get some facts about the RHS 1531 uint32_t rhsBits = RHS.getActiveBits(); 1532 uint32_t rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1); 1533 assert(rhsWords && "Performing remainder operation by zero ???"); 1534 1535 // Check the degenerate cases 1536 if (lhsWords == 0) { 1537 // 0 % Y ===> 0 1538 return APInt(BitWidth, 0); 1539 } else if (lhsWords < rhsWords || this->ult(RHS)) { 1540 // X % Y ===> X, iff X < Y 1541 return *this; 1542 } else if (*this == RHS) { 1543 // X % X == 0; 1544 return APInt(BitWidth, 0); 1545 } else if (lhsWords == 1) { 1546 // All high words are zero, just use native remainder 1547 return APInt(BitWidth, pVal[0] % RHS.pVal[0]); 1548 } 1549 1550 // We have to compute it the hard way. Invoke the Knute divide algorithm. 1551 APInt Remainder(1,0); 1552 divide(*this, lhsWords, RHS, rhsWords, 0, &Remainder); 1553 return Remainder; 1554} 1555 1556void APInt::fromString(uint32_t numbits, const char *str, uint32_t slen, 1557 uint8_t radix) { 1558 // Check our assumptions here 1559 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) && 1560 "Radix should be 2, 8, 10, or 16!"); 1561 assert(str && "String is null?"); 1562 bool isNeg = str[0] == '-'; 1563 if (isNeg) 1564 str++, slen--; 1565 assert(slen <= numbits || radix != 2 && "Insufficient bit width"); 1566 assert(slen*3 <= numbits || radix != 8 && "Insufficient bit width"); 1567 assert(slen*4 <= numbits || radix != 16 && "Insufficient bit width"); 1568 assert((slen*64)/20 <= numbits || radix != 10 && "Insufficient bit width"); 1569 1570 // Allocate memory 1571 if (!isSingleWord()) 1572 pVal = getClearedMemory(getNumWords()); 1573 1574 // Figure out if we can shift instead of multiply 1575 uint32_t shift = (radix == 16 ? 4 : radix == 8 ? 3 : radix == 2 ? 1 : 0); 1576 1577 // Set up an APInt for the digit to add outside the loop so we don't 1578 // constantly construct/destruct it. 1579 APInt apdigit(getBitWidth(), 0); 1580 APInt apradix(getBitWidth(), radix); 1581 1582 // Enter digit traversal loop 1583 for (unsigned i = 0; i < slen; i++) { 1584 // Get a digit 1585 uint32_t digit = 0; 1586 char cdigit = str[i]; 1587 if (isdigit(cdigit)) 1588 digit = cdigit - '0'; 1589 else if (isxdigit(cdigit)) 1590 if (cdigit >= 'a') 1591 digit = cdigit - 'a' + 10; 1592 else if (cdigit >= 'A') 1593 digit = cdigit - 'A' + 10; 1594 else 1595 assert(0 && "huh?"); 1596 else 1597 assert(0 && "Invalid character in digit string"); 1598 1599 // Shift or multiple the value by the radix 1600 if (shift) 1601 this->shl(shift); 1602 else 1603 *this *= apradix; 1604 1605 // Add in the digit we just interpreted 1606 if (apdigit.isSingleWord()) 1607 apdigit.VAL = digit; 1608 else 1609 apdigit.pVal[0] = digit; 1610 *this += apdigit; 1611 } 1612 // If its negative, put it in two's complement form 1613 if (isNeg) { 1614 (*this)--; 1615 this->flip(); 1616 } 1617} 1618 1619std::string APInt::toString(uint8_t radix, bool wantSigned) const { 1620 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) && 1621 "Radix should be 2, 8, 10, or 16!"); 1622 static const char *digits[] = { 1623 "0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F" 1624 }; 1625 std::string result; 1626 uint32_t bits_used = getActiveBits(); 1627 if (isSingleWord()) { 1628 char buf[65]; 1629 const char *format = (radix == 10 ? (wantSigned ? "%lld" : "%llu") : 1630 (radix == 16 ? "%llX" : (radix == 8 ? "%llo" : 0))); 1631 if (format) { 1632 if (wantSigned) { 1633 int64_t sextVal = (int64_t(VAL) << (APINT_BITS_PER_WORD-BitWidth)) >> 1634 (APINT_BITS_PER_WORD-BitWidth); 1635 sprintf(buf, format, sextVal); 1636 } else 1637 sprintf(buf, format, VAL); 1638 } else { 1639 memset(buf, 0, 65); 1640 uint64_t v = VAL; 1641 while (bits_used) { 1642 uint32_t bit = v & 1; 1643 bits_used--; 1644 buf[bits_used] = digits[bit][0]; 1645 v >>=1; 1646 } 1647 } 1648 result = buf; 1649 return result; 1650 } 1651 1652 if (radix != 10) { 1653 uint64_t mask = radix - 1; 1654 uint32_t shift = (radix == 16 ? 4 : radix == 8 ? 3 : 1); 1655 uint32_t nibbles = APINT_BITS_PER_WORD / shift; 1656 for (uint32_t i = 0; i < getNumWords(); ++i) { 1657 uint64_t value = pVal[i]; 1658 for (uint32_t j = 0; j < nibbles; ++j) { 1659 result.insert(0, digits[ value & mask ]); 1660 value >>= shift; 1661 } 1662 } 1663 return result; 1664 } 1665 1666 APInt tmp(*this); 1667 APInt divisor(4, radix); 1668 APInt zero(tmp.getBitWidth(), 0); 1669 size_t insert_at = 0; 1670 if (wantSigned && tmp[BitWidth-1]) { 1671 // They want to print the signed version and it is a negative value 1672 // Flip the bits and add one to turn it into the equivalent positive 1673 // value and put a '-' in the result. 1674 tmp.flip(); 1675 tmp++; 1676 result = "-"; 1677 insert_at = 1; 1678 } 1679 if (tmp == APInt(tmp.getBitWidth(), 0)) 1680 result = "0"; 1681 else while (tmp.ne(zero)) { 1682 APInt APdigit(1,0); 1683 APInt tmp2(tmp.getBitWidth(), 0); 1684 divide(tmp, tmp.getNumWords(), divisor, divisor.getNumWords(), &tmp2, 1685 &APdigit); 1686 uint32_t digit = APdigit.getZExtValue(); 1687 assert(digit < radix && "divide failed"); 1688 result.insert(insert_at,digits[digit]); 1689 tmp = tmp2; 1690 } 1691 1692 return result; 1693} 1694 1695#ifndef NDEBUG 1696void APInt::dump() const 1697{ 1698 cerr << "APInt(" << BitWidth << ")=" << std::setbase(16); 1699 if (isSingleWord()) 1700 cerr << VAL; 1701 else for (unsigned i = getNumWords(); i > 0; i--) { 1702 cerr << pVal[i-1] << " "; 1703 } 1704 cerr << " (" << this->toString(10) << ")\n" << std::setbase(10); 1705} 1706#endif 1707