APInt.cpp revision 443b570149f5756b298de6b63d13bbbf66b4f6fc
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 integral 11// constant values. 12// 13//===----------------------------------------------------------------------===// 14 15#include "llvm/ADT/APInt.h" 16#include "llvm/DerivedTypes.h" 17#include "llvm/Support/MathExtras.h" 18#include <cstring> 19#include <cstdlib> 20using namespace llvm; 21 22#if 0 23/// lshift - This function shift x[0:len-1] left by shiftAmt bits, and 24/// store the len least significant words of the result in 25/// dest[d_offset:d_offset+len-1]. It returns the bits shifted out from 26/// the most significant digit. 27static uint64_t lshift(uint64_t dest[], unsigned d_offset, 28 uint64_t x[], unsigned len, unsigned shiftAmt) { 29 unsigned count = APINT_BITS_PER_WORD - shiftAmt; 30 int i = len - 1; 31 uint64_t high_word = x[i], retVal = high_word >> count; 32 ++d_offset; 33 while (--i >= 0) { 34 uint64_t low_word = x[i]; 35 dest[d_offset+i] = (high_word << shiftAmt) | (low_word >> count); 36 high_word = low_word; 37 } 38 dest[d_offset+i] = high_word << shiftAmt; 39 return retVal; 40} 41#endif 42 43APInt::APInt(unsigned numBits, uint64_t val) 44 : BitWidth(numBits) { 45 assert(BitWidth >= IntegerType::MIN_INT_BITS && "bitwidth too small"); 46 assert(BitWidth <= IntegerType::MAX_INT_BITS && "bitwidth too large"); 47 if (isSingleWord()) 48 VAL = val & (~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - BitWidth)); 49 else { 50 // Memory allocation and check if successful. 51 assert((pVal = new uint64_t[getNumWords()]) && 52 "APInt memory allocation fails!"); 53 memset(pVal, 0, getNumWords() * 8); 54 pVal[0] = val; 55 } 56} 57 58APInt::APInt(unsigned numBits, unsigned numWords, uint64_t bigVal[]) 59 : BitWidth(numBits) { 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] & (~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - BitWidth)); 65 else { 66 // Memory allocation and check if successful. 67 assert((pVal = new uint64_t[getNumWords()]) && 68 "APInt memory allocation fails!"); 69 // Calculate the actual length of bigVal[]. 70 unsigned maxN = std::max<unsigned>(numWords, getNumWords()); 71 unsigned minN = std::min<unsigned>(numWords, getNumWords()); 72 memcpy(pVal, bigVal, (minN - 1) * 8); 73 pVal[minN-1] = bigVal[minN-1] & 74 (~uint64_t(0ULL) >> 75 (APINT_BITS_PER_WORD - BitWidth % APINT_BITS_PER_WORD)); 76 if (maxN == getNumWords()) 77 memset(pVal+numWords, 0, (getNumWords() - numWords) * 8); 78 } 79} 80 81/// @brief Create a new APInt by translating the char array represented 82/// integer value. 83APInt::APInt(unsigned numbits, const char StrStart[], unsigned slen, 84 uint8_t radix) { 85 fromString(numbits, StrStart, slen, radix); 86} 87 88/// @brief Create a new APInt by translating the string represented 89/// integer value. 90APInt::APInt(unsigned numbits, const std::string& Val, uint8_t radix) { 91 assert(!Val.empty() && "String empty?"); 92 fromString(numbits, Val.c_str(), Val.size(), radix); 93} 94 95APInt::APInt(const APInt& APIVal) 96 : BitWidth(APIVal.BitWidth) { 97 if (isSingleWord()) VAL = APIVal.VAL; 98 else { 99 // Memory allocation and check if successful. 100 assert((pVal = new uint64_t[getNumWords()]) && 101 "APInt memory allocation fails!"); 102 memcpy(pVal, APIVal.pVal, getNumWords() * 8); 103 } 104} 105 106APInt::~APInt() { 107 if (!isSingleWord() && pVal) delete[] pVal; 108} 109 110/// @brief Copy assignment operator. Create a new object from the given 111/// APInt one by initialization. 112APInt& APInt::operator=(const APInt& RHS) { 113 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 114 if (isSingleWord()) 115 VAL = RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0]; 116 else { 117 unsigned minN = std::min(getNumWords(), RHS.getNumWords()); 118 memcpy(pVal, RHS.isSingleWord() ? &RHS.VAL : RHS.pVal, minN * 8); 119 if (getNumWords() != minN) 120 memset(pVal + minN, 0, (getNumWords() - minN) * 8); 121 } 122 return *this; 123} 124 125/// @brief Assignment operator. Assigns a common case integer value to 126/// the APInt. 127APInt& APInt::operator=(uint64_t RHS) { 128 if (isSingleWord()) 129 VAL = RHS; 130 else { 131 pVal[0] = RHS; 132 memset(pVal, 0, (getNumWords() - 1) * 8); 133 } 134 clearUnusedBits(); 135 return *this; 136} 137 138/// add_1 - This function adds the integer array x[] by integer y and 139/// returns the carry. 140/// @returns the carry of the addition. 141static uint64_t add_1(uint64_t dest[], uint64_t x[], unsigned len, uint64_t y) { 142 uint64_t carry = y; 143 144 for (unsigned i = 0; i < len; ++i) { 145 dest[i] = carry + x[i]; 146 carry = (dest[i] < carry) ? 1 : 0; 147 } 148 return carry; 149} 150 151/// @brief Prefix increment operator. Increments the APInt by one. 152APInt& APInt::operator++() { 153 if (isSingleWord()) 154 ++VAL; 155 else 156 add_1(pVal, pVal, getNumWords(), 1); 157 clearUnusedBits(); 158 return *this; 159} 160 161/// sub_1 - This function subtracts the integer array x[] by 162/// integer y and returns the borrow-out carry. 163static uint64_t sub_1(uint64_t x[], unsigned len, uint64_t y) { 164 uint64_t cy = y; 165 166 for (unsigned i = 0; i < len; ++i) { 167 uint64_t X = x[i]; 168 x[i] -= cy; 169 if (cy > X) 170 cy = 1; 171 else { 172 cy = 0; 173 break; 174 } 175 } 176 177 return cy; 178} 179 180/// @brief Prefix decrement operator. Decrements the APInt by one. 181APInt& APInt::operator--() { 182 if (isSingleWord()) --VAL; 183 else 184 sub_1(pVal, getNumWords(), 1); 185 clearUnusedBits(); 186 return *this; 187} 188 189/// add - This function adds the integer array x[] by integer array 190/// y[] and returns the carry. 191static uint64_t add(uint64_t dest[], uint64_t x[], 192 uint64_t y[], unsigned len) { 193 unsigned carry = 0; 194 195 for (unsigned i = 0; i< len; ++i) { 196 carry += x[i]; 197 dest[i] = carry + y[i]; 198 carry = carry < x[i] ? 1 : (dest[i] < carry ? 1 : 0); 199 } 200 return carry; 201} 202 203/// @brief Addition assignment operator. Adds this APInt by the given APInt& 204/// RHS and assigns the result to this APInt. 205APInt& APInt::operator+=(const APInt& RHS) { 206 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 207 if (isSingleWord()) VAL += RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0]; 208 else { 209 if (RHS.isSingleWord()) add_1(pVal, pVal, getNumWords(), RHS.VAL); 210 else { 211 if (getNumWords() <= RHS.getNumWords()) 212 add(pVal, pVal, RHS.pVal, getNumWords()); 213 else { 214 uint64_t carry = add(pVal, pVal, RHS.pVal, RHS.getNumWords()); 215 add_1(pVal + RHS.getNumWords(), pVal + RHS.getNumWords(), 216 getNumWords() - RHS.getNumWords(), carry); 217 } 218 } 219 } 220 clearUnusedBits(); 221 return *this; 222} 223 224/// sub - This function subtracts the integer array x[] by 225/// integer array y[], and returns the borrow-out carry. 226static uint64_t sub(uint64_t dest[], uint64_t x[], 227 uint64_t y[], unsigned len) { 228 // Carry indicator. 229 uint64_t cy = 0; 230 231 for (unsigned i = 0; i < len; ++i) { 232 uint64_t Y = y[i], X = x[i]; 233 Y += cy; 234 235 cy = Y < cy ? 1 : 0; 236 Y = X - Y; 237 cy += Y > X ? 1 : 0; 238 dest[i] = Y; 239 } 240 return cy; 241} 242 243/// @brief Subtraction assignment operator. Subtracts this APInt by the given 244/// APInt &RHS and assigns the result to this APInt. 245APInt& APInt::operator-=(const APInt& RHS) { 246 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 247 if (isSingleWord()) 248 VAL -= RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0]; 249 else { 250 if (RHS.isSingleWord()) 251 sub_1(pVal, getNumWords(), RHS.VAL); 252 else { 253 if (RHS.getNumWords() < getNumWords()) { 254 uint64_t carry = sub(pVal, pVal, RHS.pVal, RHS.getNumWords()); 255 sub_1(pVal + RHS.getNumWords(), getNumWords() - RHS.getNumWords(), carry); 256 } 257 else 258 sub(pVal, pVal, RHS.pVal, getNumWords()); 259 } 260 } 261 clearUnusedBits(); 262 return *this; 263} 264 265/// mul_1 - This function performs the multiplication operation on a 266/// large integer (represented as an integer array) and a uint64_t integer. 267/// @returns the carry of the multiplication. 268static uint64_t mul_1(uint64_t dest[], uint64_t x[], 269 unsigned len, uint64_t y) { 270 // Split y into high 32-bit part and low 32-bit part. 271 uint64_t ly = y & 0xffffffffULL, hy = y >> 32; 272 uint64_t carry = 0, lx, hx; 273 for (unsigned i = 0; i < len; ++i) { 274 lx = x[i] & 0xffffffffULL; 275 hx = x[i] >> 32; 276 // hasCarry - A flag to indicate if has carry. 277 // hasCarry == 0, no carry 278 // hasCarry == 1, has carry 279 // hasCarry == 2, no carry and the calculation result == 0. 280 uint8_t hasCarry = 0; 281 dest[i] = carry + lx * ly; 282 // Determine if the add above introduces carry. 283 hasCarry = (dest[i] < carry) ? 1 : 0; 284 carry = hx * ly + (dest[i] >> 32) + (hasCarry ? (1ULL << 32) : 0); 285 // The upper limit of carry can be (2^32 - 1)(2^32 - 1) + 286 // (2^32 - 1) + 2^32 = 2^64. 287 hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0); 288 289 carry += (lx * hy) & 0xffffffffULL; 290 dest[i] = (carry << 32) | (dest[i] & 0xffffffffULL); 291 carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0) + 292 (carry >> 32) + ((lx * hy) >> 32) + hx * hy; 293 } 294 295 return carry; 296} 297 298/// mul - This function multiplies integer array x[] by integer array y[] and 299/// stores the result into integer array dest[]. 300/// Note the array dest[]'s size should no less than xlen + ylen. 301static void mul(uint64_t dest[], uint64_t x[], unsigned xlen, 302 uint64_t y[], unsigned ylen) { 303 dest[xlen] = mul_1(dest, x, xlen, y[0]); 304 305 for (unsigned i = 1; i < ylen; ++i) { 306 uint64_t ly = y[i] & 0xffffffffULL, hy = y[i] >> 32; 307 uint64_t carry = 0, lx, hx; 308 for (unsigned j = 0; j < xlen; ++j) { 309 lx = x[j] & 0xffffffffULL; 310 hx = x[j] >> 32; 311 // hasCarry - A flag to indicate if has carry. 312 // hasCarry == 0, no carry 313 // hasCarry == 1, has carry 314 // hasCarry == 2, no carry and the calculation result == 0. 315 uint8_t hasCarry = 0; 316 uint64_t resul = carry + lx * ly; 317 hasCarry = (resul < carry) ? 1 : 0; 318 carry = (hasCarry ? (1ULL << 32) : 0) + hx * ly + (resul >> 32); 319 hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0); 320 321 carry += (lx * hy) & 0xffffffffULL; 322 resul = (carry << 32) | (resul & 0xffffffffULL); 323 dest[i+j] += resul; 324 carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0)+ 325 (carry >> 32) + (dest[i+j] < resul ? 1 : 0) + 326 ((lx * hy) >> 32) + hx * hy; 327 } 328 dest[i+xlen] = carry; 329 } 330} 331 332/// @brief Multiplication assignment operator. Multiplies this APInt by the 333/// given APInt& RHS and assigns the result to this APInt. 334APInt& APInt::operator*=(const APInt& RHS) { 335 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 336 if (isSingleWord()) VAL *= RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0]; 337 else { 338 // one-based first non-zero bit position. 339 unsigned first = getActiveBits(); 340 unsigned xlen = !first ? 0 : whichWord(first - 1) + 1; 341 if (!xlen) 342 return *this; 343 else if (RHS.isSingleWord()) 344 mul_1(pVal, pVal, xlen, RHS.VAL); 345 else { 346 first = RHS.getActiveBits(); 347 unsigned ylen = !first ? 0 : whichWord(first - 1) + 1; 348 if (!ylen) { 349 memset(pVal, 0, getNumWords() * 8); 350 return *this; 351 } 352 uint64_t *dest = new uint64_t[xlen+ylen]; 353 assert(dest && "Memory Allocation Failed!"); 354 mul(dest, pVal, xlen, RHS.pVal, ylen); 355 memcpy(pVal, dest, ((xlen + ylen >= getNumWords()) ? 356 getNumWords() : xlen + ylen) * 8); 357 delete[] dest; 358 } 359 } 360 clearUnusedBits(); 361 return *this; 362} 363 364/// @brief Bitwise AND assignment operator. Performs bitwise AND operation on 365/// this APInt and the given APInt& RHS, assigns the result to this APInt. 366APInt& APInt::operator&=(const APInt& RHS) { 367 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 368 if (isSingleWord()) { 369 if (RHS.isSingleWord()) VAL &= RHS.VAL; 370 else VAL &= RHS.pVal[0]; 371 } else { 372 if (RHS.isSingleWord()) { 373 memset(pVal, 0, (getNumWords() - 1) * 8); 374 pVal[0] &= RHS.VAL; 375 } else { 376 unsigned minwords = getNumWords() < RHS.getNumWords() ? 377 getNumWords() : RHS.getNumWords(); 378 for (unsigned i = 0; i < minwords; ++i) 379 pVal[i] &= RHS.pVal[i]; 380 if (getNumWords() > minwords) 381 memset(pVal+minwords, 0, (getNumWords() - minwords) * 8); 382 } 383 } 384 return *this; 385} 386 387/// @brief Bitwise OR assignment operator. Performs bitwise OR operation on 388/// this APInt and the given APInt& RHS, assigns the result to this APInt. 389APInt& APInt::operator|=(const APInt& RHS) { 390 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 391 if (isSingleWord()) { 392 if (RHS.isSingleWord()) VAL |= RHS.VAL; 393 else VAL |= RHS.pVal[0]; 394 } else { 395 if (RHS.isSingleWord()) { 396 pVal[0] |= RHS.VAL; 397 } else { 398 unsigned minwords = getNumWords() < RHS.getNumWords() ? 399 getNumWords() : RHS.getNumWords(); 400 for (unsigned i = 0; i < minwords; ++i) 401 pVal[i] |= RHS.pVal[i]; 402 } 403 } 404 clearUnusedBits(); 405 return *this; 406} 407 408/// @brief Bitwise XOR assignment operator. Performs bitwise XOR operation on 409/// this APInt and the given APInt& RHS, assigns the result to this APInt. 410APInt& APInt::operator^=(const APInt& RHS) { 411 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 412 if (isSingleWord()) { 413 if (RHS.isSingleWord()) VAL ^= RHS.VAL; 414 else VAL ^= RHS.pVal[0]; 415 } else { 416 if (RHS.isSingleWord()) { 417 for (unsigned i = 0; i < getNumWords(); ++i) 418 pVal[i] ^= RHS.VAL; 419 } else { 420 unsigned minwords = getNumWords() < RHS.getNumWords() ? 421 getNumWords() : RHS.getNumWords(); 422 for (unsigned i = 0; i < minwords; ++i) 423 pVal[i] ^= RHS.pVal[i]; 424 if (getNumWords() > minwords) 425 for (unsigned i = minwords; i < getNumWords(); ++i) 426 pVal[i] ^= 0; 427 } 428 } 429 clearUnusedBits(); 430 return *this; 431} 432 433/// @brief Bitwise AND operator. Performs bitwise AND operation on this APInt 434/// and the given APInt& RHS. 435APInt APInt::operator&(const APInt& RHS) const { 436 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 437 APInt API(RHS); 438 return API &= *this; 439} 440 441/// @brief Bitwise OR operator. Performs bitwise OR operation on this APInt 442/// and the given APInt& RHS. 443APInt APInt::operator|(const APInt& RHS) const { 444 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 445 APInt API(RHS); 446 API |= *this; 447 API.clearUnusedBits(); 448 return API; 449} 450 451/// @brief Bitwise XOR operator. Performs bitwise XOR operation on this APInt 452/// and the given APInt& RHS. 453APInt APInt::operator^(const APInt& RHS) const { 454 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 455 APInt API(RHS); 456 API ^= *this; 457 API.clearUnusedBits(); 458 return API; 459} 460 461 462/// @brief Logical negation operator. Performs logical negation operation on 463/// this APInt. 464bool APInt::operator !() const { 465 if (isSingleWord()) 466 return !VAL; 467 else 468 for (unsigned i = 0; i < getNumWords(); ++i) 469 if (pVal[i]) 470 return false; 471 return true; 472} 473 474/// @brief Multiplication operator. Multiplies this APInt by the given APInt& 475/// RHS. 476APInt APInt::operator*(const APInt& RHS) const { 477 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 478 APInt API(RHS); 479 API *= *this; 480 API.clearUnusedBits(); 481 return API; 482} 483 484/// @brief Addition operator. Adds this APInt by the given APInt& RHS. 485APInt APInt::operator+(const APInt& RHS) const { 486 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 487 APInt API(*this); 488 API += RHS; 489 API.clearUnusedBits(); 490 return API; 491} 492 493/// @brief Subtraction operator. Subtracts this APInt by the given APInt& RHS 494APInt APInt::operator-(const APInt& RHS) const { 495 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 496 APInt API(*this); 497 API -= RHS; 498 return API; 499} 500 501/// @brief Array-indexing support. 502bool APInt::operator[](unsigned bitPosition) const { 503 return (maskBit(bitPosition) & (isSingleWord() ? 504 VAL : pVal[whichWord(bitPosition)])) != 0; 505} 506 507/// @brief Equality operator. Compare this APInt with the given APInt& RHS 508/// for the validity of the equality relationship. 509bool APInt::operator==(const APInt& RHS) const { 510 unsigned n1 = getActiveBits(); 511 unsigned n2 = RHS.getActiveBits(); 512 if (n1 != n2) return false; 513 else if (isSingleWord()) 514 return VAL == (RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0]); 515 else { 516 if (n1 <= APINT_BITS_PER_WORD) 517 return pVal[0] == (RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0]); 518 for (int i = whichWord(n1 - 1); i >= 0; --i) 519 if (pVal[i] != RHS.pVal[i]) return false; 520 } 521 return true; 522} 523 524/// @brief Equality operator. Compare this APInt with the given uint64_t value 525/// for the validity of the equality relationship. 526bool APInt::operator==(uint64_t Val) const { 527 if (isSingleWord()) 528 return VAL == Val; 529 else { 530 unsigned n = getActiveBits(); 531 if (n <= APINT_BITS_PER_WORD) 532 return pVal[0] == Val; 533 else 534 return false; 535 } 536} 537 538/// @brief Unsigned less than comparison 539bool APInt::ult(const APInt& RHS) const { 540 assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison"); 541 if (isSingleWord()) 542 return VAL < RHS.VAL; 543 else { 544 unsigned n1 = getActiveBits(); 545 unsigned n2 = RHS.getActiveBits(); 546 if (n1 < n2) 547 return true; 548 else if (n2 < n1) 549 return false; 550 else if (n1 <= APINT_BITS_PER_WORD && n2 <= APINT_BITS_PER_WORD) 551 return pVal[0] < RHS.pVal[0]; 552 for (int i = whichWord(n1 - 1); i >= 0; --i) { 553 if (pVal[i] > RHS.pVal[i]) return false; 554 else if (pVal[i] < RHS.pVal[i]) return true; 555 } 556 } 557 return false; 558} 559 560/// @brief Signed less than comparison 561bool APInt::slt(const APInt& RHS) const { 562 assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison"); 563 if (isSingleWord()) 564 return VAL < RHS.VAL; 565 else { 566 unsigned n1 = getActiveBits(); 567 unsigned n2 = RHS.getActiveBits(); 568 if (n1 < n2) 569 return true; 570 else if (n2 < n1) 571 return false; 572 else if (n1 <= APINT_BITS_PER_WORD && n2 <= APINT_BITS_PER_WORD) 573 return pVal[0] < RHS.pVal[0]; 574 for (int i = whichWord(n1 - 1); i >= 0; --i) { 575 if (pVal[i] > RHS.pVal[i]) return false; 576 else if (pVal[i] < RHS.pVal[i]) return true; 577 } 578 } 579 return false; 580} 581 582/// Set the given bit to 1 whose poition is given as "bitPosition". 583/// @brief Set a given bit to 1. 584APInt& APInt::set(unsigned bitPosition) { 585 if (isSingleWord()) VAL |= maskBit(bitPosition); 586 else pVal[whichWord(bitPosition)] |= maskBit(bitPosition); 587 return *this; 588} 589 590/// @brief Set every bit to 1. 591APInt& APInt::set() { 592 if (isSingleWord()) 593 VAL = ~0ULL >> (APINT_BITS_PER_WORD - BitWidth); 594 else { 595 for (unsigned i = 0; i < getNumWords() - 1; ++i) 596 pVal[i] = -1ULL; 597 pVal[getNumWords() - 1] = ~0ULL >> 598 (APINT_BITS_PER_WORD - BitWidth % APINT_BITS_PER_WORD); 599 } 600 return *this; 601} 602 603/// Set the given bit to 0 whose position is given as "bitPosition". 604/// @brief Set a given bit to 0. 605APInt& APInt::clear(unsigned bitPosition) { 606 if (isSingleWord()) VAL &= ~maskBit(bitPosition); 607 else pVal[whichWord(bitPosition)] &= ~maskBit(bitPosition); 608 return *this; 609} 610 611/// @brief Set every bit to 0. 612APInt& APInt::clear() { 613 if (isSingleWord()) VAL = 0; 614 else 615 memset(pVal, 0, getNumWords() * 8); 616 return *this; 617} 618 619/// @brief Bitwise NOT operator. Performs a bitwise logical NOT operation on 620/// this APInt. 621APInt APInt::operator~() const { 622 APInt API(*this); 623 API.flip(); 624 return API; 625} 626 627/// @brief Toggle every bit to its opposite value. 628APInt& APInt::flip() { 629 if (isSingleWord()) VAL = (~(VAL << 630 (APINT_BITS_PER_WORD - BitWidth))) >> (APINT_BITS_PER_WORD - BitWidth); 631 else { 632 unsigned i = 0; 633 for (; i < getNumWords() - 1; ++i) 634 pVal[i] = ~pVal[i]; 635 unsigned offset = 636 APINT_BITS_PER_WORD - (BitWidth - APINT_BITS_PER_WORD * (i - 1)); 637 pVal[i] = (~(pVal[i] << offset)) >> offset; 638 } 639 return *this; 640} 641 642/// Toggle a given bit to its opposite value whose position is given 643/// as "bitPosition". 644/// @brief Toggles a given bit to its opposite value. 645APInt& APInt::flip(unsigned bitPosition) { 646 assert(bitPosition < BitWidth && "Out of the bit-width range!"); 647 if ((*this)[bitPosition]) clear(bitPosition); 648 else set(bitPosition); 649 return *this; 650} 651 652/// to_string - This function translates the APInt into a string. 653std::string APInt::toString(uint8_t radix, bool wantSigned) const { 654 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) && 655 "Radix should be 2, 8, 10, or 16!"); 656 static const char *digits[] = { 657 "0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F" 658 }; 659 std::string result; 660 unsigned bits_used = getActiveBits(); 661 if (isSingleWord()) { 662 char buf[65]; 663 const char *format = (radix == 10 ? (wantSigned ? "%lld" : "%llu") : 664 (radix == 16 ? "%llX" : (radix == 8 ? "%llo" : 0))); 665 if (format) { 666 if (wantSigned) { 667 int64_t sextVal = (int64_t(VAL) << (APINT_BITS_PER_WORD-BitWidth)) >> 668 (APINT_BITS_PER_WORD-BitWidth); 669 sprintf(buf, format, sextVal); 670 } else 671 sprintf(buf, format, VAL); 672 } else { 673 memset(buf, 0, 65); 674 uint64_t v = VAL; 675 while (bits_used) { 676 unsigned bit = v & 1; 677 bits_used--; 678 buf[bits_used] = digits[bit][0]; 679 v >>=1; 680 } 681 } 682 result = buf; 683 return result; 684 } 685 686 APInt tmp(*this); 687 APInt divisor(tmp.getBitWidth(), radix); 688 APInt zero(tmp.getBitWidth(), 0); 689 size_t insert_at = 0; 690 if (wantSigned && tmp[BitWidth-1]) { 691 // They want to print the signed version and it is a negative value 692 // Flip the bits and add one to turn it into the equivalent positive 693 // value and put a '-' in the result. 694 tmp.flip(); 695 tmp++; 696 result = "-"; 697 insert_at = 1; 698 } 699 if (tmp == 0) 700 result = "0"; 701 else while (tmp.ne(zero)) { 702 APInt APdigit = APIntOps::urem(tmp,divisor); 703 unsigned digit = APdigit.getValue(); 704 assert(digit < radix && "urem failed"); 705 result.insert(insert_at,digits[digit]); 706 tmp = APIntOps::udiv(tmp, divisor); 707 } 708 709 return result; 710} 711 712/// getMaxValue - This function returns the largest value 713/// for an APInt of the specified bit-width and if isSign == true, 714/// it should be largest signed value, otherwise unsigned value. 715APInt APInt::getMaxValue(unsigned numBits, bool isSign) { 716 APInt APIVal(numBits, 0); 717 APIVal.set(); 718 if (isSign) APIVal.clear(numBits - 1); 719 return APIVal; 720} 721 722/// getMinValue - This function returns the smallest value for 723/// an APInt of the given bit-width and if isSign == true, 724/// it should be smallest signed value, otherwise zero. 725APInt APInt::getMinValue(unsigned numBits, bool isSign) { 726 APInt APIVal(numBits, 0); 727 if (isSign) APIVal.set(numBits - 1); 728 return APIVal; 729} 730 731/// getAllOnesValue - This function returns an all-ones value for 732/// an APInt of the specified bit-width. 733APInt APInt::getAllOnesValue(unsigned numBits) { 734 return getMaxValue(numBits, false); 735} 736 737/// getNullValue - This function creates an '0' value for an 738/// APInt of the specified bit-width. 739APInt APInt::getNullValue(unsigned numBits) { 740 return getMinValue(numBits, false); 741} 742 743/// HiBits - This function returns the high "numBits" bits of this APInt. 744APInt APInt::getHiBits(unsigned numBits) const { 745 return APIntOps::lshr(*this, BitWidth - numBits); 746} 747 748/// LoBits - This function returns the low "numBits" bits of this APInt. 749APInt APInt::getLoBits(unsigned numBits) const { 750 return APIntOps::lshr(APIntOps::shl(*this, BitWidth - numBits), 751 BitWidth - numBits); 752} 753 754bool APInt::isPowerOf2() const { 755 return (!!*this) && !(*this & (*this - APInt(BitWidth,1))); 756} 757 758/// countLeadingZeros - This function is a APInt version corresponding to 759/// llvm/include/llvm/Support/MathExtras.h's function 760/// countLeadingZeros_{32, 64}. It performs platform optimal form of counting 761/// the number of zeros from the most significant bit to the first one bit. 762/// @returns numWord() * 64 if the value is zero. 763unsigned APInt::countLeadingZeros() const { 764 if (isSingleWord()) 765 return CountLeadingZeros_64(VAL) - (APINT_BITS_PER_WORD - BitWidth); 766 unsigned Count = 0; 767 for (unsigned i = getNumWords(); i > 0u; --i) { 768 unsigned tmp = CountLeadingZeros_64(pVal[i-1]); 769 Count += tmp; 770 if (tmp != APINT_BITS_PER_WORD) 771 if (i == getNumWords()) 772 Count -= (APINT_BITS_PER_WORD - whichBit(BitWidth)); 773 break; 774 } 775 return Count; 776} 777 778/// countTrailingZeros - This function is a APInt version corresponding to 779/// llvm/include/llvm/Support/MathExtras.h's function 780/// countTrailingZeros_{32, 64}. It performs platform optimal form of counting 781/// the number of zeros from the least significant bit to the first one bit. 782/// @returns numWord() * 64 if the value is zero. 783unsigned APInt::countTrailingZeros() const { 784 if (isSingleWord()) 785 return CountTrailingZeros_64(VAL); 786 APInt Tmp( ~(*this) & ((*this) - APInt(BitWidth,1)) ); 787 return getNumWords() * APINT_BITS_PER_WORD - Tmp.countLeadingZeros(); 788} 789 790/// countPopulation - This function is a APInt version corresponding to 791/// llvm/include/llvm/Support/MathExtras.h's function 792/// countPopulation_{32, 64}. It counts the number of set bits in a value. 793/// @returns 0 if the value is zero. 794unsigned APInt::countPopulation() const { 795 if (isSingleWord()) 796 return CountPopulation_64(VAL); 797 unsigned Count = 0; 798 for (unsigned i = 0; i < getNumWords(); ++i) 799 Count += CountPopulation_64(pVal[i]); 800 return Count; 801} 802 803 804/// byteSwap - This function returns a byte-swapped representation of the 805/// this APInt. 806APInt APInt::byteSwap() const { 807 assert(BitWidth >= 16 && BitWidth % 16 == 0 && "Cannot byteswap!"); 808 if (BitWidth == 16) 809 return APInt(BitWidth, ByteSwap_16(VAL)); 810 else if (BitWidth == 32) 811 return APInt(BitWidth, ByteSwap_32(VAL)); 812 else if (BitWidth == 48) { 813 uint64_t Tmp1 = ((VAL >> 32) << 16) | (VAL & 0xFFFF); 814 Tmp1 = ByteSwap_32(Tmp1); 815 uint64_t Tmp2 = (VAL >> 16) & 0xFFFF; 816 Tmp2 = ByteSwap_16(Tmp2); 817 return 818 APInt(BitWidth, 819 (Tmp1 & 0xff) | ((Tmp1<<16) & 0xffff00000000ULL) | (Tmp2 << 16)); 820 } else if (BitWidth == 64) 821 return APInt(BitWidth, ByteSwap_64(VAL)); 822 else { 823 APInt Result(BitWidth, 0); 824 char *pByte = (char*)Result.pVal; 825 for (unsigned i = 0; i < BitWidth / 8 / 2; ++i) { 826 char Tmp = pByte[i]; 827 pByte[i] = pByte[BitWidth / 8 - 1 - i]; 828 pByte[BitWidth / 8 - i - 1] = Tmp; 829 } 830 return Result; 831 } 832} 833 834/// GreatestCommonDivisor - This function returns the greatest common 835/// divisor of the two APInt values using Enclid's algorithm. 836APInt llvm::APIntOps::GreatestCommonDivisor(const APInt& API1, 837 const APInt& API2) { 838 APInt A = API1, B = API2; 839 while (!!B) { 840 APInt T = B; 841 B = APIntOps::urem(A, B); 842 A = T; 843 } 844 return A; 845} 846 847/// DoubleRoundToAPInt - This function convert a double value to 848/// a APInt value. 849APInt llvm::APIntOps::RoundDoubleToAPInt(double Double) { 850 union { 851 double D; 852 uint64_t I; 853 } T; 854 T.D = Double; 855 bool isNeg = T.I >> 63; 856 int64_t exp = ((T.I >> 52) & 0x7ff) - 1023; 857 if (exp < 0) 858 return APInt(64ull, 0u); 859 uint64_t mantissa = ((T.I << 12) >> 12) | (1ULL << 52); 860 if (exp < 52) 861 return isNeg ? -APInt(64u, mantissa >> (52 - exp)) : 862 APInt(64u, mantissa >> (52 - exp)); 863 APInt Tmp(exp + 1, mantissa); 864 Tmp = Tmp.shl(exp - 52); 865 return isNeg ? -Tmp : Tmp; 866} 867 868/// RoundToDouble - This function convert this APInt to a double. 869/// The layout for double is as following (IEEE Standard 754): 870/// -------------------------------------- 871/// | Sign Exponent Fraction Bias | 872/// |-------------------------------------- | 873/// | 1[63] 11[62-52] 52[51-00] 1023 | 874/// -------------------------------------- 875double APInt::roundToDouble(bool isSigned) const { 876 bool isNeg = isSigned ? (*this)[BitWidth-1] : false; 877 APInt Tmp(isNeg ? -(*this) : (*this)); 878 if (Tmp.isSingleWord()) 879 return isSigned ? double(int64_t(Tmp.VAL)) : double(Tmp.VAL); 880 unsigned n = Tmp.getActiveBits(); 881 if (n <= APINT_BITS_PER_WORD) 882 return isSigned ? double(int64_t(Tmp.pVal[0])) : double(Tmp.pVal[0]); 883 // Exponent when normalized to have decimal point directly after 884 // leading one. This is stored excess 1023 in the exponent bit field. 885 uint64_t exp = n - 1; 886 887 // Gross overflow. 888 assert(exp <= 1023 && "Infinity value!"); 889 890 // Number of bits in mantissa including the leading one 891 // equals to 53. 892 uint64_t mantissa; 893 if (n % APINT_BITS_PER_WORD >= 53) 894 mantissa = Tmp.pVal[whichWord(n - 1)] >> (n % APINT_BITS_PER_WORD - 53); 895 else 896 mantissa = (Tmp.pVal[whichWord(n - 1)] << (53 - n % APINT_BITS_PER_WORD)) | 897 (Tmp.pVal[whichWord(n - 1) - 1] >> 898 (11 + n % APINT_BITS_PER_WORD)); 899 // The leading bit of mantissa is implicit, so get rid of it. 900 mantissa &= ~(1ULL << 52); 901 uint64_t sign = isNeg ? (1ULL << (APINT_BITS_PER_WORD - 1)) : 0; 902 exp += 1023; 903 union { 904 double D; 905 uint64_t I; 906 } T; 907 T.I = sign | (exp << 52) | mantissa; 908 return T.D; 909} 910 911// Truncate to new width. 912void APInt::trunc(unsigned width) { 913 assert(width < BitWidth && "Invalid APInt Truncate request"); 914} 915 916// Sign extend to a new width. 917void APInt::sext(unsigned width) { 918 assert(width > BitWidth && "Invalid APInt SignExtend request"); 919} 920 921// Zero extend to a new width. 922void APInt::zext(unsigned width) { 923 assert(width > BitWidth && "Invalid APInt ZeroExtend request"); 924} 925 926/// Arithmetic right-shift this APInt by shiftAmt. 927/// @brief Arithmetic right-shift function. 928APInt APInt::ashr(unsigned shiftAmt) const { 929 APInt API(*this); 930 if (API.isSingleWord()) 931 API.VAL = 932 (((int64_t(API.VAL) << (APINT_BITS_PER_WORD - API.BitWidth)) >> 933 (APINT_BITS_PER_WORD - API.BitWidth)) >> shiftAmt) & 934 (~uint64_t(0UL) >> (APINT_BITS_PER_WORD - API.BitWidth)); 935 else { 936 if (shiftAmt >= API.BitWidth) { 937 memset(API.pVal, API[API.BitWidth-1] ? 1 : 0, (API.getNumWords()-1) * 8); 938 API.pVal[API.getNumWords() - 1] = 939 ~uint64_t(0UL) >> 940 (APINT_BITS_PER_WORD - API.BitWidth % APINT_BITS_PER_WORD); 941 } else { 942 unsigned i = 0; 943 for (; i < API.BitWidth - shiftAmt; ++i) 944 if (API[i+shiftAmt]) 945 API.set(i); 946 else 947 API.clear(i); 948 for (; i < API.BitWidth; ++i) 949 if (API[API.BitWidth-1]) 950 API.set(i); 951 else API.clear(i); 952 } 953 } 954 return API; 955} 956 957/// Logical right-shift this APInt by shiftAmt. 958/// @brief Logical right-shift function. 959APInt APInt::lshr(unsigned shiftAmt) const { 960 APInt API(*this); 961 if (API.isSingleWord()) 962 API.VAL >>= shiftAmt; 963 else { 964 if (shiftAmt >= API.BitWidth) 965 memset(API.pVal, 0, API.getNumWords() * 8); 966 unsigned i = 0; 967 for (i = 0; i < API.BitWidth - shiftAmt; ++i) 968 if (API[i+shiftAmt]) API.set(i); 969 else API.clear(i); 970 for (; i < API.BitWidth; ++i) 971 API.clear(i); 972 } 973 return API; 974} 975 976/// Left-shift this APInt by shiftAmt. 977/// @brief Left-shift function. 978APInt APInt::shl(unsigned shiftAmt) const { 979 APInt API(*this); 980 if (API.isSingleWord()) 981 API.VAL <<= shiftAmt; 982 else if (shiftAmt >= API.BitWidth) 983 memset(API.pVal, 0, API.getNumWords() * 8); 984 else { 985 if (unsigned offset = shiftAmt / APINT_BITS_PER_WORD) { 986 for (unsigned i = API.getNumWords() - 1; i > offset - 1; --i) 987 API.pVal[i] = API.pVal[i-offset]; 988 memset(API.pVal, 0, offset * 8); 989 } 990 shiftAmt %= APINT_BITS_PER_WORD; 991 unsigned i; 992 for (i = API.getNumWords() - 1; i > 0; --i) 993 API.pVal[i] = (API.pVal[i] << shiftAmt) | 994 (API.pVal[i-1] >> (APINT_BITS_PER_WORD - shiftAmt)); 995 API.pVal[i] <<= shiftAmt; 996 } 997 API.clearUnusedBits(); 998 return API; 999} 1000 1001/// subMul - This function substracts x[len-1:0] * y from 1002/// dest[offset+len-1:offset], and returns the most significant 1003/// word of the product, minus the borrow-out from the subtraction. 1004static unsigned subMul(unsigned dest[], unsigned offset, 1005 unsigned x[], unsigned len, unsigned y) { 1006 uint64_t yl = (uint64_t) y & 0xffffffffL; 1007 unsigned carry = 0; 1008 unsigned j = 0; 1009 do { 1010 uint64_t prod = ((uint64_t) x[j] & 0xffffffffUL) * yl; 1011 unsigned prod_low = (unsigned) prod; 1012 unsigned prod_high = (unsigned) (prod >> 32); 1013 prod_low += carry; 1014 carry = (prod_low < carry ? 1 : 0) + prod_high; 1015 unsigned x_j = dest[offset+j]; 1016 prod_low = x_j - prod_low; 1017 if (prod_low > x_j) ++carry; 1018 dest[offset+j] = prod_low; 1019 } while (++j < len); 1020 return carry; 1021} 1022 1023/// unitDiv - This function divides N by D, 1024/// and returns (remainder << 32) | quotient. 1025/// Assumes (N >> 32) < D. 1026static uint64_t unitDiv(uint64_t N, unsigned D) { 1027 uint64_t q, r; // q: quotient, r: remainder. 1028 uint64_t a1 = N >> 32; // a1: high 32-bit part of N. 1029 uint64_t a0 = N & 0xffffffffL; // a0: low 32-bit part of N 1030 if (a1 < ((D - a1 - (a0 >> 31)) & 0xffffffffL)) { 1031 q = N / D; 1032 r = N % D; 1033 } 1034 else { 1035 // Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d 1036 uint64_t c = N - ((uint64_t) D << 31); 1037 // Divide (c1*2^32 + c0) by d 1038 q = c / D; 1039 r = c % D; 1040 // Add 2^31 to quotient 1041 q += 1 << 31; 1042 } 1043 1044 return (r << 32) | (q & 0xFFFFFFFFl); 1045} 1046 1047/// div - This is basically Knuth's formulation of the classical algorithm. 1048/// Correspondance with Knuth's notation: 1049/// Knuth's u[0:m+n] == zds[nx:0]. 1050/// Knuth's v[1:n] == y[ny-1:0] 1051/// Knuth's n == ny. 1052/// Knuth's m == nx-ny. 1053/// Our nx == Knuth's m+n. 1054/// Could be re-implemented using gmp's mpn_divrem: 1055/// zds[nx] = mpn_divrem (&zds[ny], 0, zds, nx, y, ny). 1056static void div(unsigned zds[], unsigned nx, unsigned y[], unsigned ny) { 1057 unsigned j = nx; 1058 do { // loop over digits of quotient 1059 // Knuth's j == our nx-j. 1060 // Knuth's u[j:j+n] == our zds[j:j-ny]. 1061 unsigned qhat; // treated as unsigned 1062 if (zds[j] == y[ny-1]) 1063 qhat = -1U; // 0xffffffff 1064 else { 1065 uint64_t w = (((uint64_t)(zds[j])) << 32) + 1066 ((uint64_t)zds[j-1] & 0xffffffffL); 1067 qhat = (unsigned) unitDiv(w, y[ny-1]); 1068 } 1069 if (qhat) { 1070 unsigned borrow = subMul(zds, j - ny, y, ny, qhat); 1071 unsigned save = zds[j]; 1072 uint64_t num = ((uint64_t)save&0xffffffffL) - 1073 ((uint64_t)borrow&0xffffffffL); 1074 while (num) { 1075 qhat--; 1076 uint64_t carry = 0; 1077 for (unsigned i = 0; i < ny; i++) { 1078 carry += ((uint64_t) zds[j-ny+i] & 0xffffffffL) 1079 + ((uint64_t) y[i] & 0xffffffffL); 1080 zds[j-ny+i] = (unsigned) carry; 1081 carry >>= 32; 1082 } 1083 zds[j] += carry; 1084 num = carry - 1; 1085 } 1086 } 1087 zds[j] = qhat; 1088 } while (--j >= ny); 1089} 1090 1091/// Unsigned divide this APInt by APInt RHS. 1092/// @brief Unsigned division function for APInt. 1093APInt APInt::udiv(const APInt& RHS) const { 1094 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 1095 1096 // First, deal with the easy case 1097 if (isSingleWord()) { 1098 assert(RHS.VAL != 0 && "Divide by zero?"); 1099 return APInt(BitWidth, VAL / RHS.VAL); 1100 } 1101 1102 // Make a temporary to hold the result 1103 APInt Result(*this); 1104 1105 // Get some facts about the LHS and RHS number of bits and words 1106 unsigned rhsBits = RHS.getActiveBits(); 1107 unsigned rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1); 1108 assert(rhsWords && "Divided by zero???"); 1109 unsigned lhsBits = Result.getActiveBits(); 1110 unsigned lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1); 1111 1112 // Deal with some degenerate cases 1113 if (!lhsWords) 1114 return Result; // 0 / X == 0 1115 else if (lhsWords < rhsWords || Result.ult(RHS)) 1116 // X / Y with X < Y == 0 1117 memset(Result.pVal, 0, Result.getNumWords() * 8); 1118 else if (Result == RHS) { 1119 // X / X == 1 1120 memset(Result.pVal, 0, Result.getNumWords() * 8); 1121 Result.pVal[0] = 1; 1122 } else if (lhsWords == 1) 1123 // All high words are zero, just use native divide 1124 Result.pVal[0] /= RHS.pVal[0]; 1125 else { 1126 // Compute it the hard way .. 1127 APInt X(BitWidth, 0); 1128 APInt Y(BitWidth, 0); 1129 unsigned nshift = 1130 (APINT_BITS_PER_WORD - 1) - ((rhsBits - 1) % APINT_BITS_PER_WORD ); 1131 if (nshift) { 1132 Y = APIntOps::shl(RHS, nshift); 1133 X = APIntOps::shl(Result, nshift); 1134 ++lhsWords; 1135 } 1136 div((unsigned*)X.pVal, lhsWords * 2 - 1, 1137 (unsigned*)(Y.isSingleWord()? &Y.VAL : Y.pVal), rhsWords*2); 1138 memset(Result.pVal, 0, Result.getNumWords() * 8); 1139 memcpy(Result.pVal, X.pVal + rhsWords, (lhsWords - rhsWords) * 8); 1140 } 1141 return Result; 1142} 1143 1144/// Unsigned remainder operation on APInt. 1145/// @brief Function for unsigned remainder operation. 1146APInt APInt::urem(const APInt& RHS) const { 1147 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 1148 if (isSingleWord()) { 1149 assert(RHS.VAL != 0 && "Remainder by zero?"); 1150 return APInt(BitWidth, VAL % RHS.VAL); 1151 } 1152 1153 // Make a temporary to hold the result 1154 APInt Result(*this); 1155 1156 // Get some facts about the RHS 1157 unsigned rhsBits = RHS.getActiveBits(); 1158 unsigned rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1); 1159 assert(rhsWords && "Performing remainder operation by zero ???"); 1160 1161 // Get some facts about the LHS 1162 unsigned lhsBits = Result.getActiveBits(); 1163 unsigned lhsWords = !lhsBits ? 0 : (Result.whichWord(lhsBits - 1) + 1); 1164 1165 // Check the degenerate cases 1166 if (lhsWords == 0) 1167 // 0 % Y == 0 1168 memset(Result.pVal, 0, Result.getNumWords() * 8); 1169 else if (lhsWords < rhsWords || Result.ult(RHS)) 1170 // X % Y == X iff X < Y 1171 return Result; 1172 else if (Result == RHS) 1173 // X % X == 0; 1174 memset(Result.pVal, 0, Result.getNumWords() * 8); 1175 else if (lhsWords == 1) 1176 // All high words are zero, just use native remainder 1177 Result.pVal[0] %= RHS.pVal[0]; 1178 else { 1179 // Do it the hard way 1180 APInt X((lhsWords+1)*APINT_BITS_PER_WORD, 0); 1181 APInt Y(rhsWords*APINT_BITS_PER_WORD, 0); 1182 unsigned nshift = 1183 (APINT_BITS_PER_WORD - 1) - (rhsBits - 1) % APINT_BITS_PER_WORD; 1184 if (nshift) { 1185 APIntOps::shl(Y, nshift); 1186 APIntOps::shl(X, nshift); 1187 } 1188 div((unsigned*)X.pVal, rhsWords*2-1, 1189 (unsigned*)(Y.isSingleWord()? &Y.VAL : Y.pVal), rhsWords*2); 1190 memset(Result.pVal, 0, Result.getNumWords() * 8); 1191 for (unsigned i = 0; i < rhsWords-1; ++i) 1192 Result.pVal[i] = (X.pVal[i] >> nshift) | 1193 (X.pVal[i+1] << (APINT_BITS_PER_WORD - nshift)); 1194 Result.pVal[rhsWords-1] = X.pVal[rhsWords-1] >> nshift; 1195 } 1196 return Result; 1197} 1198 1199/// @brief Converts a char array into an integer. 1200void APInt::fromString(unsigned numbits, const char *StrStart, unsigned slen, 1201 uint8_t radix) { 1202 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) && 1203 "Radix should be 2, 8, 10, or 16!"); 1204 assert(StrStart && "String is null?"); 1205 unsigned size = 0; 1206 // If the radix is a power of 2, read the input 1207 // from most significant to least significant. 1208 if ((radix & (radix - 1)) == 0) { 1209 unsigned nextBitPos = 0, bits_per_digit = radix / 8 + 2; 1210 uint64_t resDigit = 0; 1211 BitWidth = slen * bits_per_digit; 1212 if (getNumWords() > 1) 1213 assert((pVal = new uint64_t[getNumWords()]) && 1214 "APInt memory allocation fails!"); 1215 for (int i = slen - 1; i >= 0; --i) { 1216 uint64_t digit = StrStart[i] - '0'; 1217 resDigit |= digit << nextBitPos; 1218 nextBitPos += bits_per_digit; 1219 if (nextBitPos >= APINT_BITS_PER_WORD) { 1220 if (isSingleWord()) { 1221 VAL = resDigit; 1222 break; 1223 } 1224 pVal[size++] = resDigit; 1225 nextBitPos -= APINT_BITS_PER_WORD; 1226 resDigit = digit >> (bits_per_digit - nextBitPos); 1227 } 1228 } 1229 if (!isSingleWord() && size <= getNumWords()) 1230 pVal[size] = resDigit; 1231 } else { // General case. The radix is not a power of 2. 1232 // For 10-radix, the max value of 64-bit integer is 18446744073709551615, 1233 // and its digits number is 20. 1234 const unsigned chars_per_word = 20; 1235 if (slen < chars_per_word || 1236 (slen == chars_per_word && // In case the value <= 2^64 - 1 1237 strcmp(StrStart, "18446744073709551615") <= 0)) { 1238 BitWidth = APINT_BITS_PER_WORD; 1239 VAL = strtoull(StrStart, 0, 10); 1240 } else { // In case the value > 2^64 - 1 1241 BitWidth = (slen / chars_per_word + 1) * APINT_BITS_PER_WORD; 1242 assert((pVal = new uint64_t[getNumWords()]) && 1243 "APInt memory allocation fails!"); 1244 memset(pVal, 0, getNumWords() * 8); 1245 unsigned str_pos = 0; 1246 while (str_pos < slen) { 1247 unsigned chunk = slen - str_pos; 1248 if (chunk > chars_per_word - 1) 1249 chunk = chars_per_word - 1; 1250 uint64_t resDigit = StrStart[str_pos++] - '0'; 1251 uint64_t big_base = radix; 1252 while (--chunk > 0) { 1253 resDigit = resDigit * radix + StrStart[str_pos++] - '0'; 1254 big_base *= radix; 1255 } 1256 1257 uint64_t carry; 1258 if (!size) 1259 carry = resDigit; 1260 else { 1261 carry = mul_1(pVal, pVal, size, big_base); 1262 carry += add_1(pVal, pVal, size, resDigit); 1263 } 1264 1265 if (carry) pVal[size++] = carry; 1266 } 1267 } 1268 } 1269} 1270 1271