Make add_1 exit early if carry is 0.

Fix line breaks and 80 cols violation.
Simplify operator^= since bitwidths must be the same.

llvm-svn: 34388
This commit is contained in:
Reid Spencer 2007-02-18 06:39:42 +00:00
parent 8597d69961
commit ee0a685ea1
1 changed files with 23 additions and 36 deletions

View File

@ -139,13 +139,16 @@ APInt& APInt::operator=(uint64_t RHS) {
/// returns the carry. /// returns the carry.
/// @returns the carry of the addition. /// @returns the carry of the addition.
static uint64_t add_1(uint64_t dest[], uint64_t x[], unsigned len, uint64_t y) { static uint64_t add_1(uint64_t dest[], uint64_t x[], unsigned len, uint64_t y) {
uint64_t carry = y;
for (unsigned i = 0; i < len; ++i) { for (unsigned i = 0; i < len; ++i) {
dest[i] = carry + x[i]; dest[i] = y + x[i];
carry = (dest[i] < carry) ? 1 : 0; if (dest[i] < y)
y = 1;
else {
y = 0;
break;
}
} }
return carry; return y;
} }
/// @brief Prefix increment operator. Increments the APInt by one. /// @brief Prefix increment operator. Increments the APInt by one.
@ -161,20 +164,17 @@ APInt& APInt::operator++() {
/// sub_1 - This function subtracts the integer array x[] by /// sub_1 - This function subtracts the integer array x[] by
/// integer y and returns the borrow-out carry. /// integer y and returns the borrow-out carry.
static uint64_t sub_1(uint64_t x[], unsigned len, uint64_t y) { static uint64_t sub_1(uint64_t x[], unsigned len, uint64_t y) {
uint64_t cy = y;
for (unsigned i = 0; i < len; ++i) { for (unsigned i = 0; i < len; ++i) {
uint64_t X = x[i]; uint64_t X = x[i];
x[i] -= cy; x[i] -= y;
if (cy > X) if (y > X)
cy = 1; y = 1;
else { else {
cy = 0; y = 0;
break; break;
} }
} }
return y;
return cy;
} }
/// @brief Prefix decrement operator. Decrements the APInt by one. /// @brief Prefix decrement operator. Decrements the APInt by one.
@ -188,10 +188,8 @@ APInt& APInt::operator--() {
/// add - This function adds the integer array x[] by integer array /// add - This function adds the integer array x[] by integer array
/// y[] and returns the carry. /// y[] and returns the carry.
static uint64_t add(uint64_t dest[], uint64_t x[], static uint64_t add(uint64_t dest[], uint64_t x[], uint64_t y[], unsigned len) {
uint64_t y[], unsigned len) {
unsigned carry = 0; unsigned carry = 0;
for (unsigned i = 0; i< len; ++i) { for (unsigned i = 0; i< len; ++i) {
carry += x[i]; carry += x[i];
dest[i] = carry + y[i]; dest[i] = carry + y[i];
@ -223,8 +221,7 @@ APInt& APInt::operator+=(const APInt& RHS) {
/// sub - This function subtracts the integer array x[] by /// sub - This function subtracts the integer array x[] by
/// integer array y[], and returns the borrow-out carry. /// integer array y[], and returns the borrow-out carry.
static uint64_t sub(uint64_t dest[], uint64_t x[], static uint64_t sub(uint64_t dest[], uint64_t x[], uint64_t y[], unsigned len) {
uint64_t y[], unsigned len) {
// Carry indicator. // Carry indicator.
uint64_t cy = 0; uint64_t cy = 0;
@ -252,7 +249,8 @@ APInt& APInt::operator-=(const APInt& RHS) {
else { else {
if (RHS.getNumWords() < getNumWords()) { if (RHS.getNumWords() < getNumWords()) {
uint64_t carry = sub(pVal, pVal, RHS.pVal, RHS.getNumWords()); uint64_t carry = sub(pVal, pVal, RHS.pVal, RHS.getNumWords());
sub_1(pVal + RHS.getNumWords(), getNumWords() - RHS.getNumWords(), carry); sub_1(pVal + RHS.getNumWords(), getNumWords() - RHS.getNumWords(),
carry);
} }
else else
sub(pVal, pVal, RHS.pVal, getNumWords()); sub(pVal, pVal, RHS.pVal, getNumWords());
@ -410,23 +408,12 @@ APInt& APInt::operator|=(const APInt& RHS) {
APInt& APInt::operator^=(const APInt& RHS) { APInt& APInt::operator^=(const APInt& RHS) {
assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
if (isSingleWord()) { if (isSingleWord()) {
if (RHS.isSingleWord()) VAL ^= RHS.VAL; VAL ^= RHS.VAL;
else VAL ^= RHS.pVal[0]; return *this;
} else {
if (RHS.isSingleWord()) {
for (unsigned i = 0; i < getNumWords(); ++i)
pVal[i] ^= RHS.VAL;
} else {
unsigned minwords = getNumWords() < RHS.getNumWords() ?
getNumWords() : RHS.getNumWords();
for (unsigned i = 0; i < minwords; ++i)
pVal[i] ^= RHS.pVal[i];
if (getNumWords() > minwords)
for (unsigned i = minwords; i < getNumWords(); ++i)
pVal[i] ^= 0;
}
} }
clearUnusedBits(); unsigned numWords = getNumWords();
for (unsigned i = 0; i < numWords; ++i)
pVal[i] ^= RHS.pVal[i];
return *this; return *this;
} }