comparison lib/Support/APInt.cpp @ 83:60c9769439b8

LLVM 3.7
author Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
date Wed, 18 Feb 2015 14:55:36 +0900
parents 54457678186b
children afa8332a0e37
comparison
equal deleted inserted replaced
78:af83660cff7b 83:60c9769439b8
452 unsigned numWords = getNumWords(); 452 unsigned numWords = getNumWords();
453 uint64_t *val = getMemory(numWords); 453 uint64_t *val = getMemory(numWords);
454 for (unsigned i = 0; i < numWords; ++i) 454 for (unsigned i = 0; i < numWords; ++i)
455 val[i] = pVal[i] ^ RHS.pVal[i]; 455 val[i] = pVal[i] ^ RHS.pVal[i];
456 456
457 APInt Result(val, getBitWidth());
457 // 0^0==1 so clear the high bits in case they got set. 458 // 0^0==1 so clear the high bits in case they got set.
458 return APInt(val, getBitWidth()).clearUnusedBits(); 459 Result.clearUnusedBits();
460 return Result;
459 } 461 }
460 462
461 APInt APInt::operator*(const APInt& RHS) const { 463 APInt APInt::operator*(const APInt& RHS) const {
462 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 464 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
463 if (isSingleWord()) 465 if (isSingleWord())
471 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 473 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
472 if (isSingleWord()) 474 if (isSingleWord())
473 return APInt(BitWidth, VAL + RHS.VAL); 475 return APInt(BitWidth, VAL + RHS.VAL);
474 APInt Result(BitWidth, 0); 476 APInt Result(BitWidth, 0);
475 add(Result.pVal, this->pVal, RHS.pVal, getNumWords()); 477 add(Result.pVal, this->pVal, RHS.pVal, getNumWords());
476 return Result.clearUnusedBits(); 478 Result.clearUnusedBits();
479 return Result;
477 } 480 }
478 481
479 APInt APInt::operator-(const APInt& RHS) const { 482 APInt APInt::operator-(const APInt& RHS) const {
480 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 483 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
481 if (isSingleWord()) 484 if (isSingleWord())
482 return APInt(BitWidth, VAL - RHS.VAL); 485 return APInt(BitWidth, VAL - RHS.VAL);
483 APInt Result(BitWidth, 0); 486 APInt Result(BitWidth, 0);
484 sub(Result.pVal, this->pVal, RHS.pVal, getNumWords()); 487 sub(Result.pVal, this->pVal, RHS.pVal, getNumWords());
485 return Result.clearUnusedBits(); 488 Result.clearUnusedBits();
489 return Result;
486 } 490 }
487 491
488 bool APInt::EqualSlowCase(const APInt& RHS) const { 492 bool APInt::EqualSlowCase(const APInt& RHS) const {
489 // Get some facts about the number of bits used in the two operands. 493 // Get some facts about the number of bits used in the two operands.
490 unsigned n1 = getActiveBits(); 494 unsigned n1 = getActiveBits();
707 return Count; 711 return Count;
708 } 712 }
709 713
710 unsigned APInt::countLeadingOnes() const { 714 unsigned APInt::countLeadingOnes() const {
711 if (isSingleWord()) 715 if (isSingleWord())
712 return CountLeadingOnes_64(VAL << (APINT_BITS_PER_WORD - BitWidth)); 716 return llvm::countLeadingOnes(VAL << (APINT_BITS_PER_WORD - BitWidth));
713 717
714 unsigned highWordBits = BitWidth % APINT_BITS_PER_WORD; 718 unsigned highWordBits = BitWidth % APINT_BITS_PER_WORD;
715 unsigned shift; 719 unsigned shift;
716 if (!highWordBits) { 720 if (!highWordBits) {
717 highWordBits = APINT_BITS_PER_WORD; 721 highWordBits = APINT_BITS_PER_WORD;
718 shift = 0; 722 shift = 0;
719 } else { 723 } else {
720 shift = APINT_BITS_PER_WORD - highWordBits; 724 shift = APINT_BITS_PER_WORD - highWordBits;
721 } 725 }
722 int i = getNumWords() - 1; 726 int i = getNumWords() - 1;
723 unsigned Count = CountLeadingOnes_64(pVal[i] << shift); 727 unsigned Count = llvm::countLeadingOnes(pVal[i] << shift);
724 if (Count == highWordBits) { 728 if (Count == highWordBits) {
725 for (i--; i >= 0; --i) { 729 for (i--; i >= 0; --i) {
726 if (pVal[i] == -1ULL) 730 if (pVal[i] == -1ULL)
727 Count += APINT_BITS_PER_WORD; 731 Count += APINT_BITS_PER_WORD;
728 else { 732 else {
729 Count += CountLeadingOnes_64(pVal[i]); 733 Count += llvm::countLeadingOnes(pVal[i]);
730 break; 734 break;
731 } 735 }
732 } 736 }
733 } 737 }
734 return Count; 738 return Count;
750 unsigned Count = 0; 754 unsigned Count = 0;
751 unsigned i = 0; 755 unsigned i = 0;
752 for (; i < getNumWords() && pVal[i] == -1ULL; ++i) 756 for (; i < getNumWords() && pVal[i] == -1ULL; ++i)
753 Count += APINT_BITS_PER_WORD; 757 Count += APINT_BITS_PER_WORD;
754 if (i < getNumWords()) 758 if (i < getNumWords())
755 Count += CountTrailingOnes_64(pVal[i]); 759 Count += llvm::countTrailingOnes(pVal[i]);
756 return std::min(Count, BitWidth); 760 return std::min(Count, BitWidth);
757 } 761 }
758 762
759 unsigned APInt::countPopulationSlowCase() const { 763 unsigned APInt::countPopulationSlowCase() const {
760 unsigned Count = 0; 764 unsigned Count = 0;
761 for (unsigned i = 0; i < getNumWords(); ++i) 765 for (unsigned i = 0; i < getNumWords(); ++i)
762 Count += CountPopulation_64(pVal[i]); 766 Count += llvm::countPopulation(pVal[i]);
763 return Count; 767 return Count;
764 } 768 }
765 769
766 /// Perform a logical right-shift from Src to Dst, which must be equal or 770 /// Perform a logical right-shift from Src to Dst, which must be equal or
767 /// non-overlapping, of Words words, by Shift, which must be less than 64. 771 /// non-overlapping, of Words words, by Shift, which must be less than 64.
1112 1116
1113 // Remaining words are 0 or -1, just assign them. 1117 // Remaining words are 0 or -1, just assign them.
1114 uint64_t fillValue = (isNegative() ? -1ULL : 0); 1118 uint64_t fillValue = (isNegative() ? -1ULL : 0);
1115 for (unsigned i = breakWord+1; i < getNumWords(); ++i) 1119 for (unsigned i = breakWord+1; i < getNumWords(); ++i)
1116 val[i] = fillValue; 1120 val[i] = fillValue;
1117 return APInt(val, BitWidth).clearUnusedBits(); 1121 APInt Result(val, BitWidth);
1122 Result.clearUnusedBits();
1123 return Result;
1118 } 1124 }
1119 1125
1120 /// Logical right-shift this APInt by shiftAmt. 1126 /// Logical right-shift this APInt by shiftAmt.
1121 /// @brief Logical right-shift function. 1127 /// @brief Logical right-shift function.
1122 APInt APInt::lshr(const APInt &shiftAmt) const { 1128 APInt APInt::lshr(const APInt &shiftAmt) const {
1149 uint64_t * val = new uint64_t[getNumWords()]; 1155 uint64_t * val = new uint64_t[getNumWords()];
1150 1156
1151 // If we are shifting less than a word, compute the shift with a simple carry 1157 // If we are shifting less than a word, compute the shift with a simple carry
1152 if (shiftAmt < APINT_BITS_PER_WORD) { 1158 if (shiftAmt < APINT_BITS_PER_WORD) {
1153 lshrNear(val, pVal, getNumWords(), shiftAmt); 1159 lshrNear(val, pVal, getNumWords(), shiftAmt);
1154 return APInt(val, BitWidth).clearUnusedBits(); 1160 APInt Result(val, BitWidth);
1161 Result.clearUnusedBits();
1162 return Result;
1155 } 1163 }
1156 1164
1157 // Compute some values needed by the remaining shift algorithms 1165 // Compute some values needed by the remaining shift algorithms
1158 unsigned wordShift = shiftAmt % APINT_BITS_PER_WORD; 1166 unsigned wordShift = shiftAmt % APINT_BITS_PER_WORD;
1159 unsigned offset = shiftAmt / APINT_BITS_PER_WORD; 1167 unsigned offset = shiftAmt / APINT_BITS_PER_WORD;
1162 if (wordShift == 0) { 1170 if (wordShift == 0) {
1163 for (unsigned i = 0; i < getNumWords() - offset; ++i) 1171 for (unsigned i = 0; i < getNumWords() - offset; ++i)
1164 val[i] = pVal[i+offset]; 1172 val[i] = pVal[i+offset];
1165 for (unsigned i = getNumWords()-offset; i < getNumWords(); i++) 1173 for (unsigned i = getNumWords()-offset; i < getNumWords(); i++)
1166 val[i] = 0; 1174 val[i] = 0;
1167 return APInt(val,BitWidth).clearUnusedBits(); 1175 APInt Result(val, BitWidth);
1176 Result.clearUnusedBits();
1177 return Result;
1168 } 1178 }
1169 1179
1170 // Shift the low order words 1180 // Shift the low order words
1171 unsigned breakWord = getNumWords() - offset -1; 1181 unsigned breakWord = getNumWords() - offset -1;
1172 for (unsigned i = 0; i < breakWord; ++i) 1182 for (unsigned i = 0; i < breakWord; ++i)
1176 val[breakWord] = pVal[breakWord+offset] >> wordShift; 1186 val[breakWord] = pVal[breakWord+offset] >> wordShift;
1177 1187
1178 // Remaining words are 0 1188 // Remaining words are 0
1179 for (unsigned i = breakWord+1; i < getNumWords(); ++i) 1189 for (unsigned i = breakWord+1; i < getNumWords(); ++i)
1180 val[i] = 0; 1190 val[i] = 0;
1181 return APInt(val, BitWidth).clearUnusedBits(); 1191 APInt Result(val, BitWidth);
1192 Result.clearUnusedBits();
1193 return Result;
1182 } 1194 }
1183 1195
1184 /// Left-shift this APInt by shiftAmt. 1196 /// Left-shift this APInt by shiftAmt.
1185 /// @brief Left-shift function. 1197 /// @brief Left-shift function.
1186 APInt APInt::shl(const APInt &shiftAmt) const { 1198 APInt APInt::shl(const APInt &shiftAmt) const {
1209 uint64_t carry = 0; 1221 uint64_t carry = 0;
1210 for (unsigned i = 0; i < getNumWords(); i++) { 1222 for (unsigned i = 0; i < getNumWords(); i++) {
1211 val[i] = pVal[i] << shiftAmt | carry; 1223 val[i] = pVal[i] << shiftAmt | carry;
1212 carry = pVal[i] >> (APINT_BITS_PER_WORD - shiftAmt); 1224 carry = pVal[i] >> (APINT_BITS_PER_WORD - shiftAmt);
1213 } 1225 }
1214 return APInt(val, BitWidth).clearUnusedBits(); 1226 APInt Result(val, BitWidth);
1227 Result.clearUnusedBits();
1228 return Result;
1215 } 1229 }
1216 1230
1217 // Compute some values needed by the remaining shift algorithms 1231 // Compute some values needed by the remaining shift algorithms
1218 unsigned wordShift = shiftAmt % APINT_BITS_PER_WORD; 1232 unsigned wordShift = shiftAmt % APINT_BITS_PER_WORD;
1219 unsigned offset = shiftAmt / APINT_BITS_PER_WORD; 1233 unsigned offset = shiftAmt / APINT_BITS_PER_WORD;
1222 if (wordShift == 0) { 1236 if (wordShift == 0) {
1223 for (unsigned i = 0; i < offset; i++) 1237 for (unsigned i = 0; i < offset; i++)
1224 val[i] = 0; 1238 val[i] = 0;
1225 for (unsigned i = offset; i < getNumWords(); i++) 1239 for (unsigned i = offset; i < getNumWords(); i++)
1226 val[i] = pVal[i-offset]; 1240 val[i] = pVal[i-offset];
1227 return APInt(val,BitWidth).clearUnusedBits(); 1241 APInt Result(val, BitWidth);
1242 Result.clearUnusedBits();
1243 return Result;
1228 } 1244 }
1229 1245
1230 // Copy whole words from this to Result. 1246 // Copy whole words from this to Result.
1231 unsigned i = getNumWords() - 1; 1247 unsigned i = getNumWords() - 1;
1232 for (; i > offset; --i) 1248 for (; i > offset; --i)
1233 val[i] = pVal[i-offset] << wordShift | 1249 val[i] = pVal[i-offset] << wordShift |
1234 pVal[i-offset-1] >> (APINT_BITS_PER_WORD - wordShift); 1250 pVal[i-offset-1] >> (APINT_BITS_PER_WORD - wordShift);
1235 val[offset] = pVal[0] << wordShift; 1251 val[offset] = pVal[0] << wordShift;
1236 for (i = 0; i < offset; ++i) 1252 for (i = 0; i < offset; ++i)
1237 val[i] = 0; 1253 val[i] = 0;
1238 return APInt(val, BitWidth).clearUnusedBits(); 1254 APInt Result(val, BitWidth);
1255 Result.clearUnusedBits();
1256 return Result;
1239 } 1257 }
1240 1258
1241 APInt APInt::rotl(const APInt &rotateAmt) const { 1259 APInt APInt::rotl(const APInt &rotateAmt) const {
1242 return rotl((unsigned)rotateAmt.getLimitedValue(BitWidth)); 1260 return rotl((unsigned)rotateAmt.getLimitedValue(BitWidth));
1243 } 1261 }
1301 #endif 1319 #endif
1302 } 1320 }
1303 1321
1304 // Okay, all the short cuts are exhausted. We must compute it. The following 1322 // Okay, all the short cuts are exhausted. We must compute it. The following
1305 // is a classical Babylonian method for computing the square root. This code 1323 // is a classical Babylonian method for computing the square root. This code
1306 // was adapted to APINt from a wikipedia article on such computations. 1324 // was adapted to APInt from a wikipedia article on such computations.
1307 // See http://www.wikipedia.org/ and go to the page named 1325 // See http://www.wikipedia.org/ and go to the page named
1308 // Calculate_an_integer_square_root. 1326 // Calculate_an_integer_square_root.
1309 unsigned nbits = BitWidth, i = 4; 1327 unsigned nbits = BitWidth, i = 4;
1310 APInt testy(BitWidth, 16); 1328 APInt testy(BitWidth, 16);
1311 APInt x_old(BitWidth, 1); 1329 APInt x_old(BitWidth, 1);
1936 return this->urem(RHS); 1954 return this->urem(RHS);
1937 } 1955 }
1938 1956
1939 void APInt::udivrem(const APInt &LHS, const APInt &RHS, 1957 void APInt::udivrem(const APInt &LHS, const APInt &RHS,
1940 APInt &Quotient, APInt &Remainder) { 1958 APInt &Quotient, APInt &Remainder) {
1959 assert(LHS.BitWidth == RHS.BitWidth && "Bit widths must be the same");
1960
1961 // First, deal with the easy case
1962 if (LHS.isSingleWord()) {
1963 assert(RHS.VAL != 0 && "Divide by zero?");
1964 uint64_t QuotVal = LHS.VAL / RHS.VAL;
1965 uint64_t RemVal = LHS.VAL % RHS.VAL;
1966 Quotient = APInt(LHS.BitWidth, QuotVal);
1967 Remainder = APInt(LHS.BitWidth, RemVal);
1968 return;
1969 }
1970
1941 // Get some size facts about the dividend and divisor 1971 // Get some size facts about the dividend and divisor
1942 unsigned lhsBits = LHS.getActiveBits(); 1972 unsigned lhsBits = LHS.getActiveBits();
1943 unsigned lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1); 1973 unsigned lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1);
1944 unsigned rhsBits = RHS.getActiveBits(); 1974 unsigned rhsBits = RHS.getActiveBits();
1945 unsigned rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1); 1975 unsigned rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
2044 else 2074 else
2045 Overflow = false; 2075 Overflow = false;
2046 return Res; 2076 return Res;
2047 } 2077 }
2048 2078
2049 APInt APInt::sshl_ov(unsigned ShAmt, bool &Overflow) const { 2079 APInt APInt::sshl_ov(const APInt &ShAmt, bool &Overflow) const {
2050 Overflow = ShAmt >= getBitWidth(); 2080 Overflow = ShAmt.uge(getBitWidth());
2051 if (Overflow) 2081 if (Overflow)
2052 ShAmt = getBitWidth()-1; 2082 return APInt(BitWidth, 0);
2053 2083
2054 if (isNonNegative()) // Don't allow sign change. 2084 if (isNonNegative()) // Don't allow sign change.
2055 Overflow = ShAmt >= countLeadingZeros(); 2085 Overflow = ShAmt.uge(countLeadingZeros());
2056 else 2086 else
2057 Overflow = ShAmt >= countLeadingOnes(); 2087 Overflow = ShAmt.uge(countLeadingOnes());
2058 2088
2089 return *this << ShAmt;
2090 }
2091
2092 APInt APInt::ushl_ov(const APInt &ShAmt, bool &Overflow) const {
2093 Overflow = ShAmt.uge(getBitWidth());
2094 if (Overflow)
2095 return APInt(BitWidth, 0);
2096
2097 Overflow = ShAmt.ugt(countLeadingZeros());
2098
2059 return *this << ShAmt; 2099 return *this << ShAmt;
2060 } 2100 }
2061 2101
2062 2102
2063 2103
2268 // This implements a variety of operations on a representation of 2308 // This implements a variety of operations on a representation of
2269 // arbitrary precision, two's-complement, bignum integer values. 2309 // arbitrary precision, two's-complement, bignum integer values.
2270 2310
2271 // Assumed by lowHalf, highHalf, partMSB and partLSB. A fairly safe 2311 // Assumed by lowHalf, highHalf, partMSB and partLSB. A fairly safe
2272 // and unrestricting assumption. 2312 // and unrestricting assumption.
2273 #define COMPILE_TIME_ASSERT(cond) extern int CTAssert[(cond) ? 1 : -1] 2313 static_assert(integerPartWidth % 2 == 0, "Part width must be divisible by 2!");
2274 COMPILE_TIME_ASSERT(integerPartWidth % 2 == 0);
2275 2314
2276 /* Some handy functions local to this file. */ 2315 /* Some handy functions local to this file. */
2277 namespace { 2316 namespace {
2278 2317
2279 /* Returns the integer part with the least significant BITS set. 2318 /* Returns the integer part with the least significant BITS set.