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