2007-02-06 11:00:16 +08:00
|
|
|
//===-- APInt.cpp - Implement APInt class ---------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-02-06 11:00:16 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2007-02-26 03:32:03 +08:00
|
|
|
// This file implements a class to represent arbitrary precision integer
|
|
|
|
// constant values and provide a variety of arithmetic operations on them.
|
2007-02-06 11:00:16 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ADT/APInt.h"
|
2016-04-16 15:51:28 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2008-01-19 12:23:33 +08:00
|
|
|
#include "llvm/ADT/FoldingSet.h"
|
2012-03-04 20:02:57 +08:00
|
|
|
#include "llvm/ADT/Hashing.h"
|
2008-08-17 15:19:36 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2012-03-04 20:02:57 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2007-02-24 11:58:46 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-12 04:10:48 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2007-02-06 11:00:16 +08:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2008-08-24 06:23:09 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-08-17 15:19:36 +08:00
|
|
|
#include <cmath>
|
2007-02-06 11:00:16 +08:00
|
|
|
#include <cstdlib>
|
2012-12-04 00:50:05 +08:00
|
|
|
#include <cstring>
|
|
|
|
#include <limits>
|
2007-02-06 11:00:16 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 11:07:47 +08:00
|
|
|
#define DEBUG_TYPE "apint"
|
|
|
|
|
2007-02-26 03:32:03 +08:00
|
|
|
/// A utility function for allocating memory, checking for allocation failures,
|
|
|
|
/// and ensuring the contents are zeroed.
|
2009-01-22 02:09:24 +08:00
|
|
|
inline static uint64_t* getClearedMemory(unsigned numWords) {
|
2007-02-19 02:38:44 +08:00
|
|
|
uint64_t * result = new uint64_t[numWords];
|
|
|
|
assert(result && "APInt memory allocation fails!");
|
|
|
|
memset(result, 0, numWords * sizeof(uint64_t));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-08-21 12:06:45 +08:00
|
|
|
/// A utility function for allocating memory and checking for allocation
|
2007-02-26 03:32:03 +08:00
|
|
|
/// failure. The content is not zeroed.
|
2009-01-22 02:09:24 +08:00
|
|
|
inline static uint64_t* getMemory(unsigned numWords) {
|
2007-02-19 02:38:44 +08:00
|
|
|
uint64_t * result = new uint64_t[numWords];
|
|
|
|
assert(result && "APInt memory allocation fails!");
|
|
|
|
return result;
|
2007-02-06 11:00:16 +08:00
|
|
|
}
|
|
|
|
|
2009-08-21 11:15:28 +08:00
|
|
|
/// A utility function that converts a character to a digit.
|
|
|
|
inline static unsigned getDigit(char cdigit, uint8_t radix) {
|
2009-08-21 14:48:37 +08:00
|
|
|
unsigned r;
|
|
|
|
|
2011-09-14 23:54:46 +08:00
|
|
|
if (radix == 16 || radix == 36) {
|
2009-08-21 14:48:37 +08:00
|
|
|
r = cdigit - '0';
|
|
|
|
if (r <= 9)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = cdigit - 'A';
|
2011-09-21 02:33:29 +08:00
|
|
|
if (r <= radix - 11U)
|
2009-08-21 14:48:37 +08:00
|
|
|
return r + 10;
|
|
|
|
|
|
|
|
r = cdigit - 'a';
|
2011-09-21 02:33:29 +08:00
|
|
|
if (r <= radix - 11U)
|
2009-08-21 14:48:37 +08:00
|
|
|
return r + 10;
|
2011-09-21 02:11:52 +08:00
|
|
|
|
|
|
|
radix = 10;
|
2009-08-21 11:15:28 +08:00
|
|
|
}
|
|
|
|
|
2009-08-21 14:48:37 +08:00
|
|
|
r = cdigit - '0';
|
|
|
|
if (r < radix)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return -1U;
|
2009-08-21 11:15:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-22 02:09:24 +08:00
|
|
|
void APInt::initSlowCase(unsigned numBits, uint64_t val, bool isSigned) {
|
2008-08-21 01:02:31 +08:00
|
|
|
pVal = getClearedMemory(getNumWords());
|
|
|
|
pVal[0] = val;
|
2009-08-21 12:06:45 +08:00
|
|
|
if (isSigned && int64_t(val) < 0)
|
2008-08-21 01:02:31 +08:00
|
|
|
for (unsigned i = 1; i < getNumWords(); ++i)
|
|
|
|
pVal[i] = -1ULL;
|
2007-02-06 14:04:53 +08:00
|
|
|
}
|
|
|
|
|
2008-10-12 06:07:19 +08:00
|
|
|
void APInt::initSlowCase(const APInt& that) {
|
|
|
|
pVal = getMemory(getNumWords());
|
|
|
|
memcpy(pVal, that.pVal, getNumWords() * APINT_WORD_SIZE);
|
|
|
|
}
|
|
|
|
|
2011-07-19 05:45:40 +08:00
|
|
|
void APInt::initFromArray(ArrayRef<uint64_t> bigVal) {
|
2009-08-21 11:15:14 +08:00
|
|
|
assert(BitWidth && "Bitwidth too small");
|
2011-07-19 05:45:40 +08:00
|
|
|
assert(bigVal.data() && "Null pointer detected!");
|
2007-02-06 14:04:53 +08:00
|
|
|
if (isSingleWord())
|
2007-02-24 18:01:42 +08:00
|
|
|
VAL = bigVal[0];
|
2007-02-06 14:04:53 +08:00
|
|
|
else {
|
2007-02-24 18:01:42 +08:00
|
|
|
// Get memory, cleared to 0
|
|
|
|
pVal = getClearedMemory(getNumWords());
|
|
|
|
// Calculate the number of words to copy
|
2011-07-19 05:45:40 +08:00
|
|
|
unsigned words = std::min<unsigned>(bigVal.size(), getNumWords());
|
2007-02-24 18:01:42 +08:00
|
|
|
// Copy the words from bigVal to pVal
|
2011-07-19 05:45:40 +08:00
|
|
|
memcpy(pVal, bigVal.data(), words * APINT_WORD_SIZE);
|
2007-02-06 14:04:53 +08:00
|
|
|
}
|
2007-02-24 18:01:42 +08:00
|
|
|
// Make sure unused high bits are cleared
|
|
|
|
clearUnusedBits();
|
2007-02-06 14:04:53 +08:00
|
|
|
}
|
|
|
|
|
2011-07-19 05:45:40 +08:00
|
|
|
APInt::APInt(unsigned numBits, ArrayRef<uint64_t> bigVal)
|
|
|
|
: BitWidth(numBits), VAL(0) {
|
|
|
|
initFromArray(bigVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
APInt::APInt(unsigned numBits, unsigned numWords, const uint64_t bigVal[])
|
|
|
|
: BitWidth(numBits), VAL(0) {
|
|
|
|
initFromArray(makeArrayRef(bigVal, numWords));
|
|
|
|
}
|
|
|
|
|
2010-07-15 06:38:02 +08:00
|
|
|
APInt::APInt(unsigned numbits, StringRef Str, uint8_t radix)
|
2007-02-21 11:55:44 +08:00
|
|
|
: BitWidth(numbits), VAL(0) {
|
2009-08-21 11:15:14 +08:00
|
|
|
assert(BitWidth && "Bitwidth too small");
|
2009-08-13 10:33:34 +08:00
|
|
|
fromString(numbits, Str, radix);
|
2007-02-07 14:14:53 +08:00
|
|
|
}
|
|
|
|
|
2008-08-21 01:02:31 +08:00
|
|
|
APInt& APInt::AssignSlowCase(const APInt& RHS) {
|
2007-02-27 07:38:21 +08:00
|
|
|
// Don't do anything for X = X
|
|
|
|
if (this == &RHS)
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
if (BitWidth == RHS.getBitWidth()) {
|
2008-08-21 01:02:31 +08:00
|
|
|
// assume same bit-width single-word case is already handled
|
|
|
|
assert(!isSingleWord());
|
|
|
|
memcpy(pVal, RHS.pVal, getNumWords() * APINT_WORD_SIZE);
|
2007-02-27 07:38:21 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2008-08-21 01:02:31 +08:00
|
|
|
if (isSingleWord()) {
|
|
|
|
// assume case where both are single words is already handled
|
|
|
|
assert(!RHS.isSingleWord());
|
|
|
|
VAL = 0;
|
|
|
|
pVal = getMemory(RHS.getNumWords());
|
|
|
|
memcpy(pVal, RHS.pVal, RHS.getNumWords() * APINT_WORD_SIZE);
|
2009-08-21 12:06:45 +08:00
|
|
|
} else if (getNumWords() == RHS.getNumWords())
|
2007-02-27 07:38:21 +08:00
|
|
|
memcpy(pVal, RHS.pVal, RHS.getNumWords() * APINT_WORD_SIZE);
|
|
|
|
else if (RHS.isSingleWord()) {
|
|
|
|
delete [] pVal;
|
2007-02-19 02:38:44 +08:00
|
|
|
VAL = RHS.VAL;
|
2007-02-27 07:38:21 +08:00
|
|
|
} else {
|
|
|
|
delete [] pVal;
|
|
|
|
pVal = getMemory(RHS.getNumWords());
|
|
|
|
memcpy(pVal, RHS.pVal, RHS.getNumWords() * APINT_WORD_SIZE);
|
|
|
|
}
|
|
|
|
BitWidth = RHS.BitWidth;
|
|
|
|
return clearUnusedBits();
|
2007-02-06 11:00:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
APInt& APInt::operator=(uint64_t RHS) {
|
2009-08-21 12:06:45 +08:00
|
|
|
if (isSingleWord())
|
2007-02-17 06:36:51 +08:00
|
|
|
VAL = RHS;
|
2007-02-06 11:00:16 +08:00
|
|
|
else {
|
|
|
|
pVal[0] = RHS;
|
2007-02-19 04:09:41 +08:00
|
|
|
memset(pVal+1, 0, (getNumWords() - 1) * APINT_WORD_SIZE);
|
2007-02-06 11:00:16 +08:00
|
|
|
}
|
2007-02-27 07:38:21 +08:00
|
|
|
return clearUnusedBits();
|
2007-02-06 11:00:16 +08:00
|
|
|
}
|
|
|
|
|
2015-04-06 21:31:39 +08:00
|
|
|
/// This method 'profiles' an APInt for use with FoldingSet.
|
2008-01-19 12:23:33 +08:00
|
|
|
void APInt::Profile(FoldingSetNodeID& ID) const {
|
2008-02-20 04:50:41 +08:00
|
|
|
ID.AddInteger(BitWidth);
|
2009-08-21 12:06:45 +08:00
|
|
|
|
2008-01-19 12:23:33 +08:00
|
|
|
if (isSingleWord()) {
|
|
|
|
ID.AddInteger(VAL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned NumWords = getNumWords();
|
2008-01-19 12:23:33 +08:00
|
|
|
for (unsigned i = 0; i < NumWords; ++i)
|
|
|
|
ID.AddInteger(pVal[i]);
|
|
|
|
}
|
|
|
|
|
2015-04-06 21:31:39 +08:00
|
|
|
/// This function adds a single "digit" integer, y, to the multiple
|
2007-02-19 02:38:44 +08:00
|
|
|
/// "digit" integer array, x[]. x[] is modified to reflect the addition and
|
|
|
|
/// 1 is returned if there is a carry out, otherwise 0 is returned.
|
2007-02-17 11:16:00 +08:00
|
|
|
/// @returns the carry of the addition.
|
2009-01-22 02:09:24 +08:00
|
|
|
static bool add_1(uint64_t dest[], uint64_t x[], unsigned len, uint64_t y) {
|
|
|
|
for (unsigned i = 0; i < len; ++i) {
|
2007-02-18 14:39:42 +08:00
|
|
|
dest[i] = y + x[i];
|
|
|
|
if (dest[i] < y)
|
2007-02-24 18:01:42 +08:00
|
|
|
y = 1; // Carry one to next digit.
|
2007-02-18 14:39:42 +08:00
|
|
|
else {
|
2007-02-24 18:01:42 +08:00
|
|
|
y = 0; // No need to carry so exit early
|
2007-02-18 14:39:42 +08:00
|
|
|
break;
|
|
|
|
}
|
2007-02-17 11:16:00 +08:00
|
|
|
}
|
2007-02-18 14:39:42 +08:00
|
|
|
return y;
|
2007-02-17 11:16:00 +08:00
|
|
|
}
|
|
|
|
|
2007-02-06 11:00:16 +08:00
|
|
|
/// @brief Prefix increment operator. Increments the APInt by one.
|
|
|
|
APInt& APInt::operator++() {
|
2009-08-21 12:06:45 +08:00
|
|
|
if (isSingleWord())
|
2007-02-17 06:36:51 +08:00
|
|
|
++VAL;
|
2007-02-06 11:00:16 +08:00
|
|
|
else
|
2007-02-07 14:14:53 +08:00
|
|
|
add_1(pVal, pVal, getNumWords(), 1);
|
2007-02-26 03:32:03 +08:00
|
|
|
return clearUnusedBits();
|
2007-02-06 11:00:16 +08:00
|
|
|
}
|
|
|
|
|
2015-04-06 21:31:39 +08:00
|
|
|
/// This function subtracts a single "digit" (64-bit word), y, from
|
2009-08-21 12:06:45 +08:00
|
|
|
/// the multi-digit integer array, x[], propagating the borrowed 1 value until
|
2007-02-19 02:38:44 +08:00
|
|
|
/// no further borrowing is neeeded or it runs out of "digits" in x. The result
|
|
|
|
/// is 1 if "borrowing" exhausted the digits in x, or 0 if x was not exhausted.
|
|
|
|
/// In other words, if y > x then this function returns 1, otherwise 0.
|
2007-02-26 03:32:03 +08:00
|
|
|
/// @returns the borrow out of the subtraction
|
2009-01-22 02:09:24 +08:00
|
|
|
static bool sub_1(uint64_t x[], unsigned len, uint64_t y) {
|
|
|
|
for (unsigned i = 0; i < len; ++i) {
|
2007-02-17 11:16:00 +08:00
|
|
|
uint64_t X = x[i];
|
2007-02-18 14:39:42 +08:00
|
|
|
x[i] -= y;
|
2009-08-21 12:06:45 +08:00
|
|
|
if (y > X)
|
2007-02-19 02:38:44 +08:00
|
|
|
y = 1; // We have to "borrow 1" from next "digit"
|
2007-02-17 11:16:00 +08:00
|
|
|
else {
|
2007-02-19 02:38:44 +08:00
|
|
|
y = 0; // No need to borrow
|
|
|
|
break; // Remaining digits are unchanged so exit early
|
2007-02-17 11:16:00 +08:00
|
|
|
}
|
|
|
|
}
|
2007-02-26 03:32:03 +08:00
|
|
|
return bool(y);
|
2007-02-17 11:16:00 +08:00
|
|
|
}
|
|
|
|
|
2007-02-06 11:00:16 +08:00
|
|
|
/// @brief Prefix decrement operator. Decrements the APInt by one.
|
|
|
|
APInt& APInt::operator--() {
|
2009-08-21 12:06:45 +08:00
|
|
|
if (isSingleWord())
|
2007-02-19 02:38:44 +08:00
|
|
|
--VAL;
|
2007-02-06 11:00:16 +08:00
|
|
|
else
|
2007-02-07 14:14:53 +08:00
|
|
|
sub_1(pVal, getNumWords(), 1);
|
2007-02-26 03:32:03 +08:00
|
|
|
return clearUnusedBits();
|
2007-02-06 11:00:16 +08:00
|
|
|
}
|
|
|
|
|
2015-04-06 21:31:39 +08:00
|
|
|
/// This function adds the integer array x to the integer array Y and
|
2009-08-21 12:06:45 +08:00
|
|
|
/// places the result in dest.
|
2007-02-26 03:32:03 +08:00
|
|
|
/// @returns the carry out from the addition
|
|
|
|
/// @brief General addition of 64-bit integer arrays
|
2009-08-21 12:06:45 +08:00
|
|
|
static bool add(uint64_t *dest, const uint64_t *x, const uint64_t *y,
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned len) {
|
2007-02-24 11:58:46 +08:00
|
|
|
bool carry = false;
|
2009-01-22 02:09:24 +08:00
|
|
|
for (unsigned i = 0; i< len; ++i) {
|
2007-02-23 09:57:13 +08:00
|
|
|
uint64_t limit = std::min(x[i],y[i]); // must come first in case dest == x
|
2007-02-21 07:40:25 +08:00
|
|
|
dest[i] = x[i] + y[i] + carry;
|
2007-02-21 13:44:56 +08:00
|
|
|
carry = dest[i] < limit || (carry && dest[i] == limit);
|
2007-02-17 11:16:00 +08:00
|
|
|
}
|
|
|
|
return carry;
|
|
|
|
}
|
|
|
|
|
2007-02-26 03:32:03 +08:00
|
|
|
/// Adds the RHS APint to this APInt.
|
|
|
|
/// @returns this, after addition of RHS.
|
2009-08-21 12:06:45 +08:00
|
|
|
/// @brief Addition assignment operator.
|
2007-02-06 11:00:16 +08:00
|
|
|
APInt& APInt::operator+=(const APInt& RHS) {
|
2007-02-17 08:18:01 +08:00
|
|
|
assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
|
2009-08-21 12:06:45 +08:00
|
|
|
if (isSingleWord())
|
2007-02-21 07:40:25 +08:00
|
|
|
VAL += RHS.VAL;
|
2007-02-06 11:00:16 +08:00
|
|
|
else {
|
2007-02-21 07:40:25 +08:00
|
|
|
add(pVal, pVal, RHS.pVal, getNumWords());
|
2007-02-06 11:00:16 +08:00
|
|
|
}
|
2007-02-26 03:32:03 +08:00
|
|
|
return clearUnusedBits();
|
2007-02-06 11:00:16 +08:00
|
|
|
}
|
|
|
|
|
2009-08-21 12:06:45 +08:00
|
|
|
/// Subtracts the integer array y from the integer array x
|
2007-02-26 03:32:03 +08:00
|
|
|
/// @returns returns the borrow out.
|
|
|
|
/// @brief Generalized subtraction of 64-bit integer arrays.
|
2009-08-21 12:06:45 +08:00
|
|
|
static bool sub(uint64_t *dest, const uint64_t *x, const uint64_t *y,
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned len) {
|
2007-02-21 11:55:44 +08:00
|
|
|
bool borrow = false;
|
2009-01-22 02:09:24 +08:00
|
|
|
for (unsigned i = 0; i < len; ++i) {
|
2007-02-21 11:55:44 +08:00
|
|
|
uint64_t x_tmp = borrow ? x[i] - 1 : x[i];
|
|
|
|
borrow = y[i] > x_tmp || (borrow && x[i] == 0);
|
|
|
|
dest[i] = x_tmp - y[i];
|
2007-02-17 11:16:00 +08:00
|
|
|
}
|
2007-02-21 07:40:25 +08:00
|
|
|
return borrow;
|
2007-02-17 11:16:00 +08:00
|
|
|
}
|
|
|
|
|
2007-02-26 03:32:03 +08:00
|
|
|
/// Subtracts the RHS APInt from this APInt
|
|
|
|
/// @returns this, after subtraction
|
2009-08-21 12:06:45 +08:00
|
|
|
/// @brief Subtraction assignment operator.
|
2007-02-06 11:00:16 +08:00
|
|
|
APInt& APInt::operator-=(const APInt& RHS) {
|
2007-02-17 08:18:01 +08:00
|
|
|
assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
|
2009-08-21 12:06:45 +08:00
|
|
|
if (isSingleWord())
|
2007-02-21 07:40:25 +08:00
|
|
|
VAL -= RHS.VAL;
|
|
|
|
else
|
|
|
|
sub(pVal, pVal, RHS.pVal, getNumWords());
|
2007-02-26 03:32:03 +08:00
|
|
|
return clearUnusedBits();
|
2007-02-06 11:00:16 +08:00
|
|
|
}
|
|
|
|
|
2010-02-11 00:03:48 +08:00
|
|
|
/// Multiplies an integer array, x, by a uint64_t integer and places the result
|
2009-08-21 12:06:45 +08:00
|
|
|
/// into dest.
|
2007-02-26 03:32:03 +08:00
|
|
|
/// @returns the carry out of the multiplication.
|
|
|
|
/// @brief Multiply a multi-digit APInt by a single digit (64-bit) integer.
|
2009-01-22 02:09:24 +08:00
|
|
|
static uint64_t mul_1(uint64_t dest[], uint64_t x[], unsigned len, uint64_t y) {
|
2007-02-24 18:01:42 +08:00
|
|
|
// Split y into high 32-bit part (hy) and low 32-bit part (ly)
|
2007-02-17 11:16:00 +08:00
|
|
|
uint64_t ly = y & 0xffffffffULL, hy = y >> 32;
|
2007-02-26 03:32:03 +08:00
|
|
|
uint64_t carry = 0;
|
|
|
|
|
|
|
|
// For each digit of x.
|
2009-01-22 02:09:24 +08:00
|
|
|
for (unsigned i = 0; i < len; ++i) {
|
2007-02-26 03:32:03 +08:00
|
|
|
// Split x into high and low words
|
|
|
|
uint64_t lx = x[i] & 0xffffffffULL;
|
|
|
|
uint64_t hx = x[i] >> 32;
|
|
|
|
// hasCarry - A flag to indicate if there is a carry to the next digit.
|
2007-02-17 11:16:00 +08:00
|
|
|
// hasCarry == 0, no carry
|
|
|
|
// hasCarry == 1, has carry
|
|
|
|
// hasCarry == 2, no carry and the calculation result == 0.
|
|
|
|
uint8_t hasCarry = 0;
|
|
|
|
dest[i] = carry + lx * ly;
|
|
|
|
// Determine if the add above introduces carry.
|
|
|
|
hasCarry = (dest[i] < carry) ? 1 : 0;
|
|
|
|
carry = hx * ly + (dest[i] >> 32) + (hasCarry ? (1ULL << 32) : 0);
|
2009-08-21 12:06:45 +08:00
|
|
|
// The upper limit of carry can be (2^32 - 1)(2^32 - 1) +
|
2007-02-17 11:16:00 +08:00
|
|
|
// (2^32 - 1) + 2^32 = 2^64.
|
|
|
|
hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0);
|
|
|
|
|
|
|
|
carry += (lx * hy) & 0xffffffffULL;
|
|
|
|
dest[i] = (carry << 32) | (dest[i] & 0xffffffffULL);
|
2009-08-21 12:06:45 +08:00
|
|
|
carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0) +
|
2007-02-17 11:16:00 +08:00
|
|
|
(carry >> 32) + ((lx * hy) >> 32) + hx * hy;
|
|
|
|
}
|
|
|
|
return carry;
|
|
|
|
}
|
|
|
|
|
2009-08-21 12:06:45 +08:00
|
|
|
/// Multiplies integer array x by integer array y and stores the result into
|
2007-02-26 03:32:03 +08:00
|
|
|
/// the integer array dest. Note that dest's size must be >= xlen + ylen.
|
|
|
|
/// @brief Generalized multiplicate of integer arrays.
|
2009-01-22 02:09:24 +08:00
|
|
|
static void mul(uint64_t dest[], uint64_t x[], unsigned xlen, uint64_t y[],
|
|
|
|
unsigned ylen) {
|
2007-02-17 11:16:00 +08:00
|
|
|
dest[xlen] = mul_1(dest, x, xlen, y[0]);
|
2009-01-22 02:09:24 +08:00
|
|
|
for (unsigned i = 1; i < ylen; ++i) {
|
2007-02-17 11:16:00 +08:00
|
|
|
uint64_t ly = y[i] & 0xffffffffULL, hy = y[i] >> 32;
|
2007-02-21 16:21:52 +08:00
|
|
|
uint64_t carry = 0, lx = 0, hx = 0;
|
2009-01-22 02:09:24 +08:00
|
|
|
for (unsigned j = 0; j < xlen; ++j) {
|
2007-02-17 11:16:00 +08:00
|
|
|
lx = x[j] & 0xffffffffULL;
|
|
|
|
hx = x[j] >> 32;
|
|
|
|
// hasCarry - A flag to indicate if has carry.
|
|
|
|
// hasCarry == 0, no carry
|
|
|
|
// hasCarry == 1, has carry
|
|
|
|
// hasCarry == 2, no carry and the calculation result == 0.
|
|
|
|
uint8_t hasCarry = 0;
|
|
|
|
uint64_t resul = carry + lx * ly;
|
|
|
|
hasCarry = (resul < carry) ? 1 : 0;
|
|
|
|
carry = (hasCarry ? (1ULL << 32) : 0) + hx * ly + (resul >> 32);
|
|
|
|
hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0);
|
|
|
|
|
|
|
|
carry += (lx * hy) & 0xffffffffULL;
|
|
|
|
resul = (carry << 32) | (resul & 0xffffffffULL);
|
|
|
|
dest[i+j] += resul;
|
|
|
|
carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0)+
|
2009-08-21 12:06:45 +08:00
|
|
|
(carry >> 32) + (dest[i+j] < resul ? 1 : 0) +
|
2007-02-17 11:16:00 +08:00
|
|
|
((lx * hy) >> 32) + hx * hy;
|
|
|
|
}
|
|
|
|
dest[i+xlen] = carry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-06 11:00:16 +08:00
|
|
|
APInt& APInt::operator*=(const APInt& RHS) {
|
2007-02-17 08:18:01 +08:00
|
|
|
assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
|
2007-02-21 16:21:52 +08:00
|
|
|
if (isSingleWord()) {
|
2007-02-21 04:42:10 +08:00
|
|
|
VAL *= RHS.VAL;
|
2007-02-21 16:21:52 +08:00
|
|
|
clearUnusedBits();
|
|
|
|
return *this;
|
2007-02-06 11:00:16 +08:00
|
|
|
}
|
2007-02-21 16:21:52 +08:00
|
|
|
|
|
|
|
// Get some bit facts about LHS and check for zero
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned lhsBits = getActiveBits();
|
|
|
|
unsigned lhsWords = !lhsBits ? 0 : whichWord(lhsBits - 1) + 1;
|
2009-08-21 12:06:45 +08:00
|
|
|
if (!lhsWords)
|
2007-02-21 16:21:52 +08:00
|
|
|
// 0 * X ===> 0
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
// Get some bit facts about RHS and check for zero
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned rhsBits = RHS.getActiveBits();
|
|
|
|
unsigned rhsWords = !rhsBits ? 0 : whichWord(rhsBits - 1) + 1;
|
2007-02-21 16:21:52 +08:00
|
|
|
if (!rhsWords) {
|
|
|
|
// X * 0 ===> 0
|
2010-12-01 16:53:58 +08:00
|
|
|
clearAllBits();
|
2007-02-21 16:21:52 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate space for the result
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned destWords = rhsWords + lhsWords;
|
2007-02-21 16:21:52 +08:00
|
|
|
uint64_t *dest = getMemory(destWords);
|
|
|
|
|
|
|
|
// Perform the long multiply
|
|
|
|
mul(dest, pVal, lhsWords, RHS.pVal, rhsWords);
|
|
|
|
|
|
|
|
// Copy result back into *this
|
2010-12-01 16:53:58 +08:00
|
|
|
clearAllBits();
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned wordsToCopy = destWords >= getNumWords() ? getNumWords() : destWords;
|
2007-02-21 16:21:52 +08:00
|
|
|
memcpy(pVal, dest, wordsToCopy * APINT_WORD_SIZE);
|
2011-10-08 07:40:49 +08:00
|
|
|
clearUnusedBits();
|
2007-02-21 16:21:52 +08:00
|
|
|
|
|
|
|
// delete dest array and return
|
|
|
|
delete[] dest;
|
2007-02-06 11:00:16 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
APInt& APInt::operator&=(const APInt& RHS) {
|
2007-02-17 08:18:01 +08:00
|
|
|
assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
|
2007-02-06 11:00:16 +08:00
|
|
|
if (isSingleWord()) {
|
2007-02-19 02:38:44 +08:00
|
|
|
VAL &= RHS.VAL;
|
|
|
|
return *this;
|
2007-02-06 11:00:16 +08:00
|
|
|
}
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned numWords = getNumWords();
|
|
|
|
for (unsigned i = 0; i < numWords; ++i)
|
2007-02-19 02:38:44 +08:00
|
|
|
pVal[i] &= RHS.pVal[i];
|
2007-02-06 11:00:16 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
APInt& APInt::operator|=(const APInt& RHS) {
|
2007-02-17 08:18:01 +08:00
|
|
|
assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
|
2007-02-06 11:00:16 +08:00
|
|
|
if (isSingleWord()) {
|
2007-02-19 02:38:44 +08:00
|
|
|
VAL |= RHS.VAL;
|
|
|
|
return *this;
|
2007-02-06 11:00:16 +08:00
|
|
|
}
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned numWords = getNumWords();
|
|
|
|
for (unsigned i = 0; i < numWords; ++i)
|
2007-02-19 02:38:44 +08:00
|
|
|
pVal[i] |= RHS.pVal[i];
|
2007-02-06 11:00:16 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
APInt& APInt::operator^=(const APInt& RHS) {
|
2007-02-17 08:18:01 +08:00
|
|
|
assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
|
2007-02-06 11:00:16 +08:00
|
|
|
if (isSingleWord()) {
|
2007-02-18 14:39:42 +08:00
|
|
|
VAL ^= RHS.VAL;
|
2007-02-21 07:40:25 +08:00
|
|
|
this->clearUnusedBits();
|
2007-02-18 14:39:42 +08:00
|
|
|
return *this;
|
2009-08-21 12:06:45 +08:00
|
|
|
}
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned numWords = getNumWords();
|
|
|
|
for (unsigned i = 0; i < numWords; ++i)
|
2007-02-19 02:38:44 +08:00
|
|
|
pVal[i] ^= RHS.pVal[i];
|
2007-02-26 03:32:03 +08:00
|
|
|
return clearUnusedBits();
|
2007-02-06 11:00:16 +08:00
|
|
|
}
|
|
|
|
|
2008-08-21 01:02:31 +08:00
|
|
|
APInt APInt::AndSlowCase(const APInt& RHS) const {
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned numWords = getNumWords();
|
2007-02-26 03:32:03 +08:00
|
|
|
uint64_t* val = getMemory(numWords);
|
2009-01-22 02:09:24 +08:00
|
|
|
for (unsigned i = 0; i < numWords; ++i)
|
2007-02-26 03:32:03 +08:00
|
|
|
val[i] = pVal[i] & RHS.pVal[i];
|
|
|
|
return APInt(val, getBitWidth());
|
2007-02-06 11:00:16 +08:00
|
|
|
}
|
|
|
|
|
2008-08-21 01:02:31 +08:00
|
|
|
APInt APInt::OrSlowCase(const APInt& RHS) const {
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned numWords = getNumWords();
|
2007-02-26 03:32:03 +08:00
|
|
|
uint64_t *val = getMemory(numWords);
|
2009-01-22 02:09:24 +08:00
|
|
|
for (unsigned i = 0; i < numWords; ++i)
|
2007-02-26 03:32:03 +08:00
|
|
|
val[i] = pVal[i] | RHS.pVal[i];
|
|
|
|
return APInt(val, getBitWidth());
|
2007-02-06 11:00:16 +08:00
|
|
|
}
|
|
|
|
|
2008-08-21 01:02:31 +08:00
|
|
|
APInt APInt::XorSlowCase(const APInt& RHS) const {
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned numWords = getNumWords();
|
2007-02-26 03:32:03 +08:00
|
|
|
uint64_t *val = getMemory(numWords);
|
2009-01-22 02:09:24 +08:00
|
|
|
for (unsigned i = 0; i < numWords; ++i)
|
2007-02-26 03:32:03 +08:00
|
|
|
val[i] = pVal[i] ^ RHS.pVal[i];
|
|
|
|
|
2014-10-10 18:18:12 +08:00
|
|
|
APInt Result(val, getBitWidth());
|
2007-02-26 03:32:03 +08:00
|
|
|
// 0^0==1 so clear the high bits in case they got set.
|
2014-10-10 18:18:12 +08:00
|
|
|
Result.clearUnusedBits();
|
|
|
|
return Result;
|
2007-02-06 11:00:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
APInt APInt::operator*(const APInt& RHS) const {
|
2007-02-17 08:18:01 +08:00
|
|
|
assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
|
2007-02-26 03:32:03 +08:00
|
|
|
if (isSingleWord())
|
2007-02-26 15:44:38 +08:00
|
|
|
return APInt(BitWidth, VAL * RHS.VAL);
|
2007-02-21 04:42:10 +08:00
|
|
|
APInt Result(*this);
|
|
|
|
Result *= RHS;
|
2011-10-08 07:40:49 +08:00
|
|
|
return Result;
|
2007-02-06 11:00:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
APInt APInt::operator+(const APInt& RHS) const {
|
2007-02-17 08:18:01 +08:00
|
|
|
assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
|
2007-02-26 03:32:03 +08:00
|
|
|
if (isSingleWord())
|
2007-02-26 15:44:38 +08:00
|
|
|
return APInt(BitWidth, VAL + RHS.VAL);
|
2007-02-21 07:40:25 +08:00
|
|
|
APInt Result(BitWidth, 0);
|
|
|
|
add(Result.pVal, this->pVal, RHS.pVal, getNumWords());
|
2014-10-10 18:18:12 +08:00
|
|
|
Result.clearUnusedBits();
|
|
|
|
return Result;
|
2007-02-06 11:00:16 +08:00
|
|
|
}
|
|
|
|
|
2016-05-27 11:42:17 +08:00
|
|
|
APInt APInt::operator+(uint64_t RHS) const {
|
|
|
|
if (isSingleWord())
|
|
|
|
return APInt(BitWidth, VAL + RHS);
|
|
|
|
APInt Result(*this);
|
|
|
|
add_1(Result.pVal, Result.pVal, getNumWords(), RHS);
|
|
|
|
Result.clearUnusedBits();
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2007-02-06 11:00:16 +08:00
|
|
|
APInt APInt::operator-(const APInt& RHS) const {
|
2007-02-17 08:18:01 +08:00
|
|
|
assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
|
2007-02-26 03:32:03 +08:00
|
|
|
if (isSingleWord())
|
2007-02-26 15:44:38 +08:00
|
|
|
return APInt(BitWidth, VAL - RHS.VAL);
|
2007-02-21 07:40:25 +08:00
|
|
|
APInt Result(BitWidth, 0);
|
|
|
|
sub(Result.pVal, this->pVal, RHS.pVal, getNumWords());
|
2014-10-10 18:18:12 +08:00
|
|
|
Result.clearUnusedBits();
|
|
|
|
return Result;
|
2007-02-06 11:00:16 +08:00
|
|
|
}
|
|
|
|
|
2016-05-27 11:42:17 +08:00
|
|
|
APInt APInt::operator-(uint64_t RHS) const {
|
|
|
|
if (isSingleWord())
|
|
|
|
return APInt(BitWidth, VAL - RHS);
|
|
|
|
APInt Result(*this);
|
|
|
|
sub_1(Result.pVal, getNumWords(), RHS);
|
|
|
|
Result.clearUnusedBits();
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2008-08-21 01:02:31 +08:00
|
|
|
bool APInt::EqualSlowCase(const APInt& RHS) const {
|
2016-02-16 04:06:19 +08:00
|
|
|
return std::equal(pVal, pVal + getNumWords(), RHS.pVal);
|
2007-02-06 11:00:16 +08:00
|
|
|
}
|
|
|
|
|
2008-08-21 01:02:31 +08:00
|
|
|
bool APInt::EqualSlowCase(uint64_t Val) const {
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned n = getActiveBits();
|
2007-02-21 07:40:25 +08:00
|
|
|
if (n <= APINT_BITS_PER_WORD)
|
|
|
|
return pVal[0] == Val;
|
|
|
|
else
|
|
|
|
return false;
|
2007-02-06 11:00:16 +08:00
|
|
|
}
|
|
|
|
|
2007-02-17 06:36:51 +08:00
|
|
|
bool APInt::ult(const APInt& RHS) const {
|
|
|
|
assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison");
|
|
|
|
if (isSingleWord())
|
|
|
|
return VAL < RHS.VAL;
|
2007-02-26 03:32:03 +08:00
|
|
|
|
|
|
|
// Get active bit length of both operands
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned n1 = getActiveBits();
|
|
|
|
unsigned n2 = RHS.getActiveBits();
|
2007-02-26 03:32:03 +08:00
|
|
|
|
|
|
|
// If magnitude of LHS is less than RHS, return true.
|
|
|
|
if (n1 < n2)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// If magnitude of RHS is greather than LHS, return false.
|
|
|
|
if (n2 < n1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If they bot fit in a word, just compare the low order word
|
|
|
|
if (n1 <= APINT_BITS_PER_WORD && n2 <= APINT_BITS_PER_WORD)
|
|
|
|
return pVal[0] < RHS.pVal[0];
|
|
|
|
|
|
|
|
// Otherwise, compare all words
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned topWord = whichWord(std::max(n1,n2)-1);
|
2007-02-28 02:23:40 +08:00
|
|
|
for (int i = topWord; i >= 0; --i) {
|
2009-08-21 12:06:45 +08:00
|
|
|
if (pVal[i] > RHS.pVal[i])
|
2007-02-17 06:36:51 +08:00
|
|
|
return false;
|
2009-08-21 12:06:45 +08:00
|
|
|
if (pVal[i] < RHS.pVal[i])
|
2007-02-26 03:32:03 +08:00
|
|
|
return true;
|
2007-02-06 11:00:16 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-02-17 06:36:51 +08:00
|
|
|
bool APInt::slt(const APInt& RHS) const {
|
|
|
|
assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison");
|
2007-02-19 04:09:41 +08:00
|
|
|
if (isSingleWord()) {
|
|
|
|
int64_t lhsSext = (int64_t(VAL) << (64-BitWidth)) >> (64-BitWidth);
|
|
|
|
int64_t rhsSext = (int64_t(RHS.VAL) << (64-BitWidth)) >> (64-BitWidth);
|
|
|
|
return lhsSext < rhsSext;
|
2007-02-17 06:36:51 +08:00
|
|
|
}
|
2007-02-19 04:09:41 +08:00
|
|
|
|
2007-02-28 02:23:40 +08:00
|
|
|
bool lhsNeg = isNegative();
|
2016-05-27 01:40:07 +08:00
|
|
|
bool rhsNeg = RHS.isNegative();
|
2007-02-26 03:32:03 +08:00
|
|
|
|
2016-05-27 01:40:07 +08:00
|
|
|
// If the sign bits don't match, then (LHS < RHS) if LHS is negative
|
|
|
|
if (lhsNeg != rhsNeg)
|
|
|
|
return lhsNeg;
|
|
|
|
|
|
|
|
// Otherwise we can just use an unsigned comparision, because even negative
|
|
|
|
// numbers compare correctly this way if both have the same signed-ness.
|
|
|
|
return ult(RHS);
|
2007-02-06 11:00:16 +08:00
|
|
|
}
|
|
|
|
|
2010-12-01 16:53:58 +08:00
|
|
|
void APInt::setBit(unsigned bitPosition) {
|
2009-08-21 12:06:45 +08:00
|
|
|
if (isSingleWord())
|
2007-02-26 03:32:03 +08:00
|
|
|
VAL |= maskBit(bitPosition);
|
2009-08-21 12:06:45 +08:00
|
|
|
else
|
2007-02-26 03:32:03 +08:00
|
|
|
pVal[whichWord(bitPosition)] |= maskBit(bitPosition);
|
2007-02-06 11:00:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the given bit to 0 whose position is given as "bitPosition".
|
|
|
|
/// @brief Set a given bit to 0.
|
2010-12-01 16:53:58 +08:00
|
|
|
void APInt::clearBit(unsigned bitPosition) {
|
2009-08-21 12:06:45 +08:00
|
|
|
if (isSingleWord())
|
2007-02-19 02:38:44 +08:00
|
|
|
VAL &= ~maskBit(bitPosition);
|
2009-08-21 12:06:45 +08:00
|
|
|
else
|
2007-02-19 02:38:44 +08:00
|
|
|
pVal[whichWord(bitPosition)] &= ~maskBit(bitPosition);
|
2007-02-06 11:00:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Toggle every bit to its opposite value.
|
|
|
|
|
2009-08-21 12:06:45 +08:00
|
|
|
/// Toggle a given bit to its opposite value whose position is given
|
2007-02-06 11:00:16 +08:00
|
|
|
/// as "bitPosition".
|
|
|
|
/// @brief Toggles a given bit to its opposite value.
|
2010-12-01 16:53:58 +08:00
|
|
|
void APInt::flipBit(unsigned bitPosition) {
|
2007-02-17 06:36:51 +08:00
|
|
|
assert(bitPosition < BitWidth && "Out of the bit-width range!");
|
2010-12-01 16:53:58 +08:00
|
|
|
if ((*this)[bitPosition]) clearBit(bitPosition);
|
|
|
|
else setBit(bitPosition);
|
2007-02-06 11:00:16 +08:00
|
|
|
}
|
|
|
|
|
2010-07-15 06:38:02 +08:00
|
|
|
unsigned APInt::getBitsNeeded(StringRef str, uint8_t radix) {
|
2009-08-13 10:33:34 +08:00
|
|
|
assert(!str.empty() && "Invalid string length");
|
2011-09-14 23:54:46 +08:00
|
|
|
assert((radix == 10 || radix == 8 || radix == 16 || radix == 2 ||
|
|
|
|
radix == 36) &&
|
|
|
|
"Radix should be 2, 8, 10, 16, or 36!");
|
2009-08-13 10:33:34 +08:00
|
|
|
|
|
|
|
size_t slen = str.size();
|
2007-04-14 03:19:07 +08:00
|
|
|
|
2009-08-21 12:10:31 +08:00
|
|
|
// Each computation below needs to know if it's negative.
|
2009-08-21 11:15:14 +08:00
|
|
|
StringRef::iterator p = str.begin();
|
2009-08-21 12:10:31 +08:00
|
|
|
unsigned isNegative = *p == '-';
|
2009-08-21 11:15:14 +08:00
|
|
|
if (*p == '-' || *p == '+') {
|
|
|
|
p++;
|
2007-04-14 03:19:07 +08:00
|
|
|
slen--;
|
2009-08-21 12:10:31 +08:00
|
|
|
assert(slen && "String is only a sign, needs a value.");
|
2007-04-14 03:19:07 +08:00
|
|
|
}
|
2009-08-21 12:10:31 +08:00
|
|
|
|
2007-04-14 03:19:07 +08:00
|
|
|
// For radixes of power-of-two values, the bits required is accurately and
|
|
|
|
// easily computed
|
|
|
|
if (radix == 2)
|
|
|
|
return slen + isNegative;
|
|
|
|
if (radix == 8)
|
|
|
|
return slen * 3 + isNegative;
|
|
|
|
if (radix == 16)
|
|
|
|
return slen * 4 + isNegative;
|
|
|
|
|
2011-09-14 23:54:46 +08:00
|
|
|
// FIXME: base 36
|
|
|
|
|
2007-04-14 03:19:07 +08:00
|
|
|
// This is grossly inefficient but accurate. We could probably do something
|
|
|
|
// with a computation of roughly slen*64/20 and then adjust by the value of
|
|
|
|
// the first few digits. But, I'm not sure how accurate that could be.
|
|
|
|
|
|
|
|
// Compute a sufficient number of bits that is always large enough but might
|
2009-08-21 11:15:28 +08:00
|
|
|
// be too large. This avoids the assertion in the constructor. This
|
|
|
|
// calculation doesn't work appropriately for the numbers 0-9, so just use 4
|
|
|
|
// bits in that case.
|
2011-09-14 23:54:46 +08:00
|
|
|
unsigned sufficient
|
|
|
|
= radix == 10? (slen == 1 ? 4 : slen * 64/18)
|
|
|
|
: (slen == 1 ? 7 : slen * 16/3);
|
2007-04-14 03:19:07 +08:00
|
|
|
|
|
|
|
// Convert to the actual binary value.
|
2009-08-21 11:15:14 +08:00
|
|
|
APInt tmp(sufficient, StringRef(p, slen), radix);
|
2007-04-14 03:19:07 +08:00
|
|
|
|
2009-08-21 11:15:28 +08:00
|
|
|
// Compute how many bits are required. If the log is infinite, assume we need
|
|
|
|
// just bit.
|
|
|
|
unsigned log = tmp.logBase2();
|
|
|
|
if (log == (unsigned)-1) {
|
|
|
|
return isNegative + 1;
|
|
|
|
} else {
|
|
|
|
return isNegative + log + 1;
|
|
|
|
}
|
2007-04-14 03:19:07 +08:00
|
|
|
}
|
|
|
|
|
2012-03-04 20:02:57 +08:00
|
|
|
hash_code llvm::hash_value(const APInt &Arg) {
|
|
|
|
if (Arg.isSingleWord())
|
|
|
|
return hash_combine(Arg.VAL);
|
2009-03-14 05:51:13 +08:00
|
|
|
|
2012-03-04 20:02:57 +08:00
|
|
|
return hash_combine_range(Arg.pVal, Arg.pVal + Arg.getNumWords());
|
2007-02-27 05:02:27 +08:00
|
|
|
}
|
|
|
|
|
2015-03-26 00:49:59 +08:00
|
|
|
bool APInt::isSplat(unsigned SplatSizeInBits) const {
|
|
|
|
assert(getBitWidth() % SplatSizeInBits == 0 &&
|
|
|
|
"SplatSizeInBits must divide width!");
|
|
|
|
// We can check that all parts of an integer are equal by making use of a
|
|
|
|
// little trick: rotate and check if it's still the same value.
|
|
|
|
return *this == rotl(SplatSizeInBits);
|
|
|
|
}
|
|
|
|
|
2015-04-06 21:31:39 +08:00
|
|
|
/// This function returns the high "numBits" bits of this APInt.
|
2009-01-22 02:09:24 +08:00
|
|
|
APInt APInt::getHiBits(unsigned numBits) const {
|
2007-02-17 06:36:51 +08:00
|
|
|
return APIntOps::lshr(*this, BitWidth - numBits);
|
2007-02-06 11:00:16 +08:00
|
|
|
}
|
|
|
|
|
2015-04-06 21:31:39 +08:00
|
|
|
/// This function returns the low "numBits" bits of this APInt.
|
2009-01-22 02:09:24 +08:00
|
|
|
APInt APInt::getLoBits(unsigned numBits) const {
|
2009-08-21 12:06:45 +08:00
|
|
|
return APIntOps::lshr(APIntOps::shl(*this, BitWidth - numBits),
|
2007-02-17 06:36:51 +08:00
|
|
|
BitWidth - numBits);
|
2007-02-06 11:00:16 +08:00
|
|
|
}
|
|
|
|
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned APInt::countLeadingZerosSlowCase() const {
|
2016-02-16 04:06:22 +08:00
|
|
|
unsigned Count = 0;
|
|
|
|
for (int i = getNumWords()-1; i >= 0; --i) {
|
|
|
|
integerPart V = pVal[i];
|
|
|
|
if (V == 0)
|
2008-08-21 01:02:31 +08:00
|
|
|
Count += APINT_BITS_PER_WORD;
|
|
|
|
else {
|
2016-02-16 04:06:22 +08:00
|
|
|
Count += llvm::countLeadingZeros(V);
|
2008-08-21 01:02:31 +08:00
|
|
|
break;
|
2007-02-21 08:29:48 +08:00
|
|
|
}
|
2007-02-06 11:00:16 +08:00
|
|
|
}
|
2016-02-16 04:06:22 +08:00
|
|
|
// Adjust for unused bits in the most significant word (they are zero).
|
|
|
|
unsigned Mod = BitWidth % APINT_BITS_PER_WORD;
|
|
|
|
Count -= Mod > 0 ? APINT_BITS_PER_WORD - Mod : 0;
|
2010-02-03 11:42:44 +08:00
|
|
|
return Count;
|
2007-02-06 11:00:16 +08:00
|
|
|
}
|
|
|
|
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned APInt::countLeadingOnes() const {
|
2007-02-28 05:59:26 +08:00
|
|
|
if (isSingleWord())
|
2015-02-12 23:35:40 +08:00
|
|
|
return llvm::countLeadingOnes(VAL << (APINT_BITS_PER_WORD - BitWidth));
|
2007-02-28 05:59:26 +08:00
|
|
|
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned highWordBits = BitWidth % APINT_BITS_PER_WORD;
|
2009-01-28 02:06:03 +08:00
|
|
|
unsigned shift;
|
|
|
|
if (!highWordBits) {
|
|
|
|
highWordBits = APINT_BITS_PER_WORD;
|
|
|
|
shift = 0;
|
|
|
|
} else {
|
|
|
|
shift = APINT_BITS_PER_WORD - highWordBits;
|
|
|
|
}
|
2007-02-28 05:59:26 +08:00
|
|
|
int i = getNumWords() - 1;
|
2015-02-12 23:35:40 +08:00
|
|
|
unsigned Count = llvm::countLeadingOnes(pVal[i] << shift);
|
2007-02-28 05:59:26 +08:00
|
|
|
if (Count == highWordBits) {
|
|
|
|
for (i--; i >= 0; --i) {
|
|
|
|
if (pVal[i] == -1ULL)
|
|
|
|
Count += APINT_BITS_PER_WORD;
|
|
|
|
else {
|
2015-02-12 23:35:40 +08:00
|
|
|
Count += llvm::countLeadingOnes(pVal[i]);
|
2007-02-28 05:59:26 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Count;
|
|
|
|
}
|
|
|
|
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned APInt::countTrailingZeros() const {
|
2007-02-06 11:00:16 +08:00
|
|
|
if (isSingleWord())
|
2013-05-25 06:23:49 +08:00
|
|
|
return std::min(unsigned(llvm::countTrailingZeros(VAL)), BitWidth);
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned Count = 0;
|
|
|
|
unsigned i = 0;
|
2007-02-26 15:44:38 +08:00
|
|
|
for (; i < getNumWords() && pVal[i] == 0; ++i)
|
|
|
|
Count += APINT_BITS_PER_WORD;
|
|
|
|
if (i < getNumWords())
|
2013-05-25 06:23:49 +08:00
|
|
|
Count += llvm::countTrailingZeros(pVal[i]);
|
2007-11-24 06:36:25 +08:00
|
|
|
return std::min(Count, BitWidth);
|
2007-02-06 11:00:16 +08:00
|
|
|
}
|
|
|
|
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned APInt::countTrailingOnesSlowCase() const {
|
|
|
|
unsigned Count = 0;
|
|
|
|
unsigned i = 0;
|
2008-02-15 06:38:45 +08:00
|
|
|
for (; i < getNumWords() && pVal[i] == -1ULL; ++i)
|
2008-02-14 05:11:05 +08:00
|
|
|
Count += APINT_BITS_PER_WORD;
|
|
|
|
if (i < getNumWords())
|
2015-02-12 23:35:40 +08:00
|
|
|
Count += llvm::countTrailingOnes(pVal[i]);
|
2008-02-14 05:11:05 +08:00
|
|
|
return std::min(Count, BitWidth);
|
|
|
|
}
|
|
|
|
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned APInt::countPopulationSlowCase() const {
|
|
|
|
unsigned Count = 0;
|
|
|
|
for (unsigned i = 0; i < getNumWords(); ++i)
|
2015-02-12 23:35:40 +08:00
|
|
|
Count += llvm::countPopulation(pVal[i]);
|
2007-02-06 11:00:16 +08:00
|
|
|
return Count;
|
|
|
|
}
|
|
|
|
|
2011-11-24 05:33:37 +08:00
|
|
|
/// Perform a logical right-shift from Src to Dst, which must be equal or
|
|
|
|
/// non-overlapping, of Words words, by Shift, which must be less than 64.
|
|
|
|
static void lshrNear(uint64_t *Dst, uint64_t *Src, unsigned Words,
|
|
|
|
unsigned Shift) {
|
|
|
|
uint64_t Carry = 0;
|
|
|
|
for (int I = Words - 1; I >= 0; --I) {
|
|
|
|
uint64_t Tmp = Src[I];
|
|
|
|
Dst[I] = (Tmp >> Shift) | Carry;
|
|
|
|
Carry = Tmp << (64 - Shift);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-17 06:36:51 +08:00
|
|
|
APInt APInt::byteSwap() const {
|
|
|
|
assert(BitWidth >= 16 && BitWidth % 16 == 0 && "Cannot byteswap!");
|
|
|
|
if (BitWidth == 16)
|
2007-03-21 04:42:36 +08:00
|
|
|
return APInt(BitWidth, ByteSwap_16(uint16_t(VAL)));
|
2011-11-24 05:33:37 +08:00
|
|
|
if (BitWidth == 32)
|
2009-01-22 02:09:24 +08:00
|
|
|
return APInt(BitWidth, ByteSwap_32(unsigned(VAL)));
|
2011-11-24 05:33:37 +08:00
|
|
|
if (BitWidth == 48) {
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned Tmp1 = unsigned(VAL >> 16);
|
2007-02-15 14:36:31 +08:00
|
|
|
Tmp1 = ByteSwap_32(Tmp1);
|
2007-03-21 04:42:36 +08:00
|
|
|
uint16_t Tmp2 = uint16_t(VAL);
|
2007-02-15 14:36:31 +08:00
|
|
|
Tmp2 = ByteSwap_16(Tmp2);
|
2007-03-21 04:42:36 +08:00
|
|
|
return APInt(BitWidth, (uint64_t(Tmp2) << 32) | Tmp1);
|
2011-11-24 05:33:37 +08:00
|
|
|
}
|
|
|
|
if (BitWidth == 64)
|
2007-02-17 08:18:01 +08:00
|
|
|
return APInt(BitWidth, ByteSwap_64(VAL));
|
2011-11-24 05:33:37 +08:00
|
|
|
|
|
|
|
APInt Result(getNumWords() * APINT_BITS_PER_WORD, 0);
|
|
|
|
for (unsigned I = 0, N = getNumWords(); I != N; ++I)
|
|
|
|
Result.pVal[I] = ByteSwap_64(pVal[N - I - 1]);
|
|
|
|
if (Result.BitWidth != BitWidth) {
|
|
|
|
lshrNear(Result.pVal, Result.pVal, getNumWords(),
|
|
|
|
Result.BitWidth - BitWidth);
|
|
|
|
Result.BitWidth = BitWidth;
|
2007-02-15 14:36:31 +08:00
|
|
|
}
|
2011-11-24 05:33:37 +08:00
|
|
|
return Result;
|
2007-02-06 11:00:16 +08:00
|
|
|
}
|
|
|
|
|
2016-03-21 23:00:35 +08:00
|
|
|
APInt APInt::reverseBits() const {
|
|
|
|
switch (BitWidth) {
|
|
|
|
case 64:
|
|
|
|
return APInt(BitWidth, llvm::reverseBits<uint64_t>(VAL));
|
|
|
|
case 32:
|
|
|
|
return APInt(BitWidth, llvm::reverseBits<uint32_t>(VAL));
|
|
|
|
case 16:
|
|
|
|
return APInt(BitWidth, llvm::reverseBits<uint16_t>(VAL));
|
|
|
|
case 8:
|
|
|
|
return APInt(BitWidth, llvm::reverseBits<uint8_t>(VAL));
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
APInt Val(*this);
|
|
|
|
APInt Reversed(*this);
|
|
|
|
int S = BitWidth - 1;
|
|
|
|
|
|
|
|
const APInt One(BitWidth, 1);
|
|
|
|
|
|
|
|
for ((Val = Val.lshr(1)); Val != 0; (Val = Val.lshr(1))) {
|
|
|
|
Reversed <<= 1;
|
|
|
|
Reversed |= (Val & One);
|
|
|
|
--S;
|
|
|
|
}
|
|
|
|
|
|
|
|
Reversed <<= S;
|
|
|
|
return Reversed;
|
|
|
|
}
|
|
|
|
|
2009-08-21 12:06:45 +08:00
|
|
|
APInt llvm::APIntOps::GreatestCommonDivisor(const APInt& API1,
|
2007-02-08 22:35:19 +08:00
|
|
|
const APInt& API2) {
|
2007-02-06 11:00:16 +08:00
|
|
|
APInt A = API1, B = API2;
|
|
|
|
while (!!B) {
|
|
|
|
APInt T = B;
|
2007-02-17 06:36:51 +08:00
|
|
|
B = APIntOps::urem(A, B);
|
2007-02-06 11:00:16 +08:00
|
|
|
A = T;
|
|
|
|
}
|
|
|
|
return A;
|
|
|
|
}
|
2007-02-06 13:38:37 +08:00
|
|
|
|
2009-01-22 02:09:24 +08:00
|
|
|
APInt llvm::APIntOps::RoundDoubleToAPInt(double Double, unsigned width) {
|
2007-02-13 04:02:55 +08:00
|
|
|
union {
|
|
|
|
double D;
|
|
|
|
uint64_t I;
|
|
|
|
} T;
|
|
|
|
T.D = Double;
|
2007-02-27 09:28:10 +08:00
|
|
|
|
|
|
|
// Get the sign bit from the highest order bit
|
2007-02-13 04:02:55 +08:00
|
|
|
bool isNeg = T.I >> 63;
|
2007-02-27 09:28:10 +08:00
|
|
|
|
|
|
|
// Get the 11-bit exponent and adjust for the 1023 bit bias
|
2007-02-13 04:02:55 +08:00
|
|
|
int64_t exp = ((T.I >> 52) & 0x7ff) - 1023;
|
2007-02-27 09:28:10 +08:00
|
|
|
|
|
|
|
// If the exponent is negative, the value is < 0 so just return 0.
|
2007-02-13 04:02:55 +08:00
|
|
|
if (exp < 0)
|
2007-02-28 09:30:08 +08:00
|
|
|
return APInt(width, 0u);
|
2007-02-27 09:28:10 +08:00
|
|
|
|
|
|
|
// Extract the mantissa by clearing the top 12 bits (sign + exponent).
|
|
|
|
uint64_t mantissa = (T.I & (~0ULL >> 12)) | 1ULL << 52;
|
|
|
|
|
|
|
|
// If the exponent doesn't shift all bits out of the mantissa
|
2007-02-13 04:02:55 +08:00
|
|
|
if (exp < 52)
|
2009-08-21 12:06:45 +08:00
|
|
|
return isNeg ? -APInt(width, mantissa >> (52 - exp)) :
|
2007-02-28 02:23:40 +08:00
|
|
|
APInt(width, mantissa >> (52 - exp));
|
|
|
|
|
|
|
|
// If the client didn't provide enough bits for us to shift the mantissa into
|
|
|
|
// then the result is undefined, just return 0
|
|
|
|
if (width <= exp - 52)
|
|
|
|
return APInt(width, 0);
|
2007-02-27 09:28:10 +08:00
|
|
|
|
|
|
|
// Otherwise, we have to shift the mantissa bits up to the right location
|
2007-02-28 02:23:40 +08:00
|
|
|
APInt Tmp(width, mantissa);
|
2009-01-22 02:09:24 +08:00
|
|
|
Tmp = Tmp.shl((unsigned)exp - 52);
|
2007-02-13 04:02:55 +08:00
|
|
|
return isNeg ? -Tmp : Tmp;
|
|
|
|
}
|
|
|
|
|
2015-04-06 21:31:39 +08:00
|
|
|
/// This function converts this APInt to a double.
|
2007-02-13 04:02:55 +08:00
|
|
|
/// The layout for double is as following (IEEE Standard 754):
|
|
|
|
/// --------------------------------------
|
|
|
|
/// | Sign Exponent Fraction Bias |
|
|
|
|
/// |-------------------------------------- |
|
|
|
|
/// | 1[63] 11[62-52] 52[51-00] 1023 |
|
2009-08-21 12:06:45 +08:00
|
|
|
/// --------------------------------------
|
2007-02-17 06:36:51 +08:00
|
|
|
double APInt::roundToDouble(bool isSigned) const {
|
2007-02-20 16:51:03 +08:00
|
|
|
|
|
|
|
// Handle the simple case where the value is contained in one uint64_t.
|
2009-08-13 02:04:11 +08:00
|
|
|
// It is wrong to optimize getWord(0) to VAL; there might be more than one word.
|
2007-02-19 04:09:41 +08:00
|
|
|
if (isSingleWord() || getActiveBits() <= APINT_BITS_PER_WORD) {
|
|
|
|
if (isSigned) {
|
2009-08-13 01:42:34 +08:00
|
|
|
int64_t sext = (int64_t(getWord(0)) << (64-BitWidth)) >> (64-BitWidth);
|
2007-02-19 04:09:41 +08:00
|
|
|
return double(sext);
|
|
|
|
} else
|
2009-08-13 01:42:34 +08:00
|
|
|
return double(getWord(0));
|
2007-02-19 04:09:41 +08:00
|
|
|
}
|
|
|
|
|
2007-02-20 16:51:03 +08:00
|
|
|
// Determine if the value is negative.
|
2007-02-17 06:36:51 +08:00
|
|
|
bool isNeg = isSigned ? (*this)[BitWidth-1] : false;
|
2007-02-20 16:51:03 +08:00
|
|
|
|
|
|
|
// Construct the absolute value if we're negative.
|
2007-02-13 04:02:55 +08:00
|
|
|
APInt Tmp(isNeg ? -(*this) : (*this));
|
2007-02-20 16:51:03 +08:00
|
|
|
|
|
|
|
// Figure out how many bits we're using.
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned n = Tmp.getActiveBits();
|
2007-02-13 04:02:55 +08:00
|
|
|
|
2007-02-20 16:51:03 +08:00
|
|
|
// The exponent (without bias normalization) is just the number of bits
|
|
|
|
// we are using. Note that the sign bit is gone since we constructed the
|
|
|
|
// absolute value.
|
|
|
|
uint64_t exp = n;
|
|
|
|
|
|
|
|
// Return infinity for exponent overflow
|
|
|
|
if (exp > 1023) {
|
|
|
|
if (!isSigned || !isNeg)
|
2007-03-21 04:42:36 +08:00
|
|
|
return std::numeric_limits<double>::infinity();
|
2009-08-21 12:06:45 +08:00
|
|
|
else
|
2007-03-21 04:42:36 +08:00
|
|
|
return -std::numeric_limits<double>::infinity();
|
2007-02-20 16:51:03 +08:00
|
|
|
}
|
|
|
|
exp += 1023; // Increment for 1023 bias
|
2007-02-13 04:02:55 +08:00
|
|
|
|
2007-02-20 16:51:03 +08:00
|
|
|
// Number of bits in mantissa is 52. To obtain the mantissa value, we must
|
|
|
|
// extract the high 52 bits from the correct words in pVal.
|
2007-02-13 04:02:55 +08:00
|
|
|
uint64_t mantissa;
|
2007-02-20 16:51:03 +08:00
|
|
|
unsigned hiWord = whichWord(n-1);
|
|
|
|
if (hiWord == 0) {
|
|
|
|
mantissa = Tmp.pVal[0];
|
|
|
|
if (n > 52)
|
|
|
|
mantissa >>= n - 52; // shift down, we want the top 52 bits.
|
|
|
|
} else {
|
|
|
|
assert(hiWord > 0 && "huh?");
|
|
|
|
uint64_t hibits = Tmp.pVal[hiWord] << (52 - n % APINT_BITS_PER_WORD);
|
|
|
|
uint64_t lobits = Tmp.pVal[hiWord-1] >> (11 + n % APINT_BITS_PER_WORD);
|
|
|
|
mantissa = hibits | lobits;
|
|
|
|
}
|
|
|
|
|
2007-02-13 04:02:55 +08:00
|
|
|
// The leading bit of mantissa is implicit, so get rid of it.
|
2007-02-18 08:44:22 +08:00
|
|
|
uint64_t sign = isNeg ? (1ULL << (APINT_BITS_PER_WORD - 1)) : 0;
|
2007-02-13 04:02:55 +08:00
|
|
|
union {
|
|
|
|
double D;
|
|
|
|
uint64_t I;
|
|
|
|
} T;
|
|
|
|
T.I = sign | (exp << 52) | mantissa;
|
|
|
|
return T.D;
|
|
|
|
}
|
|
|
|
|
2007-02-17 06:36:51 +08:00
|
|
|
// Truncate to new width.
|
2010-12-07 16:25:19 +08:00
|
|
|
APInt APInt::trunc(unsigned width) const {
|
2007-02-17 06:36:51 +08:00
|
|
|
assert(width < BitWidth && "Invalid APInt Truncate request");
|
2008-08-21 01:02:31 +08:00
|
|
|
assert(width && "Can't truncate to 0 bits");
|
2010-12-07 16:25:19 +08:00
|
|
|
|
|
|
|
if (width <= APINT_BITS_PER_WORD)
|
|
|
|
return APInt(width, getRawData()[0]);
|
|
|
|
|
|
|
|
APInt Result(getMemory(getNumWords(width)), width);
|
|
|
|
|
|
|
|
// Copy full words.
|
|
|
|
unsigned i;
|
|
|
|
for (i = 0; i != width / APINT_BITS_PER_WORD; i++)
|
|
|
|
Result.pVal[i] = pVal[i];
|
|
|
|
|
|
|
|
// Truncate and copy any partial word.
|
|
|
|
unsigned bits = (0 - width) % APINT_BITS_PER_WORD;
|
|
|
|
if (bits != 0)
|
|
|
|
Result.pVal[i] = pVal[i] << bits >> bits;
|
|
|
|
|
|
|
|
return Result;
|
2007-02-17 06:36:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sign extend to a new width.
|
2010-12-07 16:25:19 +08:00
|
|
|
APInt APInt::sext(unsigned width) const {
|
2007-02-17 06:36:51 +08:00
|
|
|
assert(width > BitWidth && "Invalid APInt SignExtend request");
|
2010-12-07 16:25:19 +08:00
|
|
|
|
|
|
|
if (width <= APINT_BITS_PER_WORD) {
|
|
|
|
uint64_t val = VAL << (APINT_BITS_PER_WORD - BitWidth);
|
|
|
|
val = (int64_t)val >> (width - BitWidth);
|
|
|
|
return APInt(width, val >> (APINT_BITS_PER_WORD - width));
|
2007-02-26 07:44:53 +08:00
|
|
|
}
|
|
|
|
|
2010-12-07 16:25:19 +08:00
|
|
|
APInt Result(getMemory(getNumWords(width)), width);
|
|
|
|
|
|
|
|
// Copy full words.
|
|
|
|
unsigned i;
|
|
|
|
uint64_t word = 0;
|
|
|
|
for (i = 0; i != BitWidth / APINT_BITS_PER_WORD; i++) {
|
|
|
|
word = getRawData()[i];
|
|
|
|
Result.pVal[i] = word;
|
2007-02-26 07:44:53 +08:00
|
|
|
}
|
|
|
|
|
2010-12-07 16:25:19 +08:00
|
|
|
// Read and sign-extend any partial word.
|
|
|
|
unsigned bits = (0 - BitWidth) % APINT_BITS_PER_WORD;
|
|
|
|
if (bits != 0)
|
|
|
|
word = (int64_t)getRawData()[i] << bits >> bits;
|
|
|
|
else
|
|
|
|
word = (int64_t)word >> (APINT_BITS_PER_WORD - 1);
|
|
|
|
|
|
|
|
// Write remaining full words.
|
|
|
|
for (; i != width / APINT_BITS_PER_WORD; i++) {
|
|
|
|
Result.pVal[i] = word;
|
|
|
|
word = (int64_t)word >> (APINT_BITS_PER_WORD - 1);
|
2007-02-26 07:44:53 +08:00
|
|
|
}
|
2010-12-07 16:25:19 +08:00
|
|
|
|
|
|
|
// Write any partial word.
|
|
|
|
bits = (0 - width) % APINT_BITS_PER_WORD;
|
|
|
|
if (bits != 0)
|
|
|
|
Result.pVal[i] = word << bits >> bits;
|
|
|
|
|
|
|
|
return Result;
|
2007-02-17 06:36:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Zero extend to a new width.
|
2010-12-07 16:25:19 +08:00
|
|
|
APInt APInt::zext(unsigned width) const {
|
2007-02-17 06:36:51 +08:00
|
|
|
assert(width > BitWidth && "Invalid APInt ZeroExtend request");
|
2010-12-07 16:25:19 +08:00
|
|
|
|
|
|
|
if (width <= APINT_BITS_PER_WORD)
|
|
|
|
return APInt(width, VAL);
|
|
|
|
|
|
|
|
APInt Result(getMemory(getNumWords(width)), width);
|
|
|
|
|
|
|
|
// Copy words.
|
|
|
|
unsigned i;
|
|
|
|
for (i = 0; i != getNumWords(); i++)
|
|
|
|
Result.pVal[i] = getRawData()[i];
|
|
|
|
|
|
|
|
// Zero remaining words.
|
|
|
|
memset(&Result.pVal[i], 0, (Result.getNumWords() - i) * APINT_WORD_SIZE);
|
|
|
|
|
|
|
|
return Result;
|
2007-02-17 06:36:51 +08:00
|
|
|
}
|
|
|
|
|
2010-12-07 16:25:19 +08:00
|
|
|
APInt APInt::zextOrTrunc(unsigned width) const {
|
2007-03-02 01:15:32 +08:00
|
|
|
if (BitWidth < width)
|
|
|
|
return zext(width);
|
|
|
|
if (BitWidth > width)
|
|
|
|
return trunc(width);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2010-12-07 16:25:19 +08:00
|
|
|
APInt APInt::sextOrTrunc(unsigned width) const {
|
2007-03-02 01:15:32 +08:00
|
|
|
if (BitWidth < width)
|
|
|
|
return sext(width);
|
|
|
|
if (BitWidth > width)
|
|
|
|
return trunc(width);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-01-28 07:33:07 +08:00
|
|
|
APInt APInt::zextOrSelf(unsigned width) const {
|
|
|
|
if (BitWidth < width)
|
|
|
|
return zext(width);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
APInt APInt::sextOrSelf(unsigned width) const {
|
|
|
|
if (BitWidth < width)
|
|
|
|
return sext(width);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2008-02-29 09:40:47 +08:00
|
|
|
/// Arithmetic right-shift this APInt by shiftAmt.
|
|
|
|
/// @brief Arithmetic right-shift function.
|
|
|
|
APInt APInt::ashr(const APInt &shiftAmt) const {
|
2009-01-22 02:09:24 +08:00
|
|
|
return ashr((unsigned)shiftAmt.getLimitedValue(BitWidth));
|
2008-02-29 09:40:47 +08:00
|
|
|
}
|
|
|
|
|
2007-02-09 15:48:24 +08:00
|
|
|
/// Arithmetic right-shift this APInt by shiftAmt.
|
2007-02-08 22:35:19 +08:00
|
|
|
/// @brief Arithmetic right-shift function.
|
2009-01-22 02:09:24 +08:00
|
|
|
APInt APInt::ashr(unsigned shiftAmt) const {
|
2007-02-26 15:44:38 +08:00
|
|
|
assert(shiftAmt <= BitWidth && "Invalid shift amount");
|
2007-03-03 06:39:11 +08:00
|
|
|
// Handle a degenerate case
|
|
|
|
if (shiftAmt == 0)
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
// Handle single word shifts with built-in ashr
|
2007-02-25 09:56:07 +08:00
|
|
|
if (isSingleWord()) {
|
|
|
|
if (shiftAmt == BitWidth)
|
2007-02-26 15:44:38 +08:00
|
|
|
return APInt(BitWidth, 0); // undefined
|
|
|
|
else {
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned SignBit = APINT_BITS_PER_WORD - BitWidth;
|
2009-08-21 12:06:45 +08:00
|
|
|
return APInt(BitWidth,
|
2007-02-26 15:44:38 +08:00
|
|
|
(((int64_t(VAL) << SignBit) >> SignBit) >> shiftAmt));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-03 06:39:11 +08:00
|
|
|
// If all the bits were shifted out, the result is, technically, undefined.
|
|
|
|
// We return -1 if it was negative, 0 otherwise. We check this early to avoid
|
|
|
|
// issues in the algorithm below.
|
2007-05-04 02:15:36 +08:00
|
|
|
if (shiftAmt == BitWidth) {
|
2007-02-26 15:44:38 +08:00
|
|
|
if (isNegative())
|
2008-06-05 21:27:38 +08:00
|
|
|
return APInt(BitWidth, -1ULL, true);
|
2007-02-25 09:56:07 +08:00
|
|
|
else
|
2007-02-26 15:44:38 +08:00
|
|
|
return APInt(BitWidth, 0);
|
2007-05-04 02:15:36 +08:00
|
|
|
}
|
2007-02-26 15:44:38 +08:00
|
|
|
|
|
|
|
// Create some space for the result.
|
|
|
|
uint64_t * val = new uint64_t[getNumWords()];
|
|
|
|
|
2007-03-03 06:39:11 +08:00
|
|
|
// Compute some values needed by the following shift algorithms
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned wordShift = shiftAmt % APINT_BITS_PER_WORD; // bits to shift per word
|
|
|
|
unsigned offset = shiftAmt / APINT_BITS_PER_WORD; // word offset for shift
|
|
|
|
unsigned breakWord = getNumWords() - 1 - offset; // last word affected
|
|
|
|
unsigned bitsInWord = whichBit(BitWidth); // how many bits in last word?
|
2007-03-03 06:39:11 +08:00
|
|
|
if (bitsInWord == 0)
|
|
|
|
bitsInWord = APINT_BITS_PER_WORD;
|
2007-02-26 03:32:03 +08:00
|
|
|
|
2007-02-26 15:44:38 +08:00
|
|
|
// If we are shifting whole words, just move whole words
|
|
|
|
if (wordShift == 0) {
|
2007-03-03 06:39:11 +08:00
|
|
|
// Move the words containing significant bits
|
2009-01-22 02:09:24 +08:00
|
|
|
for (unsigned i = 0; i <= breakWord; ++i)
|
2007-03-03 06:39:11 +08:00
|
|
|
val[i] = pVal[i+offset]; // move whole word
|
|
|
|
|
|
|
|
// Adjust the top significant word for sign bit fill, if negative
|
|
|
|
if (isNegative())
|
|
|
|
if (bitsInWord < APINT_BITS_PER_WORD)
|
|
|
|
val[breakWord] |= ~0ULL << bitsInWord; // set high bits
|
|
|
|
} else {
|
2009-08-21 12:06:45 +08:00
|
|
|
// Shift the low order words
|
2009-01-22 02:09:24 +08:00
|
|
|
for (unsigned i = 0; i < breakWord; ++i) {
|
2007-03-03 06:39:11 +08:00
|
|
|
// This combines the shifted corresponding word with the low bits from
|
|
|
|
// the next word (shifted into this word's high bits).
|
2009-08-21 12:06:45 +08:00
|
|
|
val[i] = (pVal[i+offset] >> wordShift) |
|
2007-03-03 06:39:11 +08:00
|
|
|
(pVal[i+offset+1] << (APINT_BITS_PER_WORD - wordShift));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shift the break word. In this case there are no bits from the next word
|
|
|
|
// to include in this word.
|
|
|
|
val[breakWord] = pVal[breakWord+offset] >> wordShift;
|
|
|
|
|
2014-01-25 01:20:08 +08:00
|
|
|
// Deal with sign extension in the break word, and possibly the word before
|
2007-03-03 06:39:11 +08:00
|
|
|
// it.
|
2007-05-04 02:15:36 +08:00
|
|
|
if (isNegative()) {
|
2007-03-03 06:39:11 +08:00
|
|
|
if (wordShift > bitsInWord) {
|
|
|
|
if (breakWord > 0)
|
2009-08-21 12:06:45 +08:00
|
|
|
val[breakWord-1] |=
|
2007-03-03 06:39:11 +08:00
|
|
|
~0ULL << (APINT_BITS_PER_WORD - (wordShift - bitsInWord));
|
|
|
|
val[breakWord] |= ~0ULL;
|
2009-08-21 12:06:45 +08:00
|
|
|
} else
|
2007-03-03 06:39:11 +08:00
|
|
|
val[breakWord] |= (~0ULL << (bitsInWord - wordShift));
|
2007-05-04 02:15:36 +08:00
|
|
|
}
|
2007-02-26 15:44:38 +08:00
|
|
|
}
|
|
|
|
|
2007-03-03 06:39:11 +08:00
|
|
|
// Remaining words are 0 or -1, just assign them.
|
|
|
|
uint64_t fillValue = (isNegative() ? -1ULL : 0);
|
2009-01-22 02:09:24 +08:00
|
|
|
for (unsigned i = breakWord+1; i < getNumWords(); ++i)
|
2007-03-03 06:39:11 +08:00
|
|
|
val[i] = fillValue;
|
2014-10-10 18:18:12 +08:00
|
|
|
APInt Result(val, BitWidth);
|
|
|
|
Result.clearUnusedBits();
|
|
|
|
return Result;
|
2007-02-08 22:35:19 +08:00
|
|
|
}
|
|
|
|
|
2008-02-29 09:40:47 +08:00
|
|
|
/// Logical right-shift this APInt by shiftAmt.
|
|
|
|
/// @brief Logical right-shift function.
|
|
|
|
APInt APInt::lshr(const APInt &shiftAmt) const {
|
2009-01-22 02:09:24 +08:00
|
|
|
return lshr((unsigned)shiftAmt.getLimitedValue(BitWidth));
|
2008-02-29 09:40:47 +08:00
|
|
|
}
|
|
|
|
|
2007-02-09 15:48:24 +08:00
|
|
|
/// Logical right-shift this APInt by shiftAmt.
|
2007-02-08 22:35:19 +08:00
|
|
|
/// @brief Logical right-shift function.
|
2009-01-22 02:09:24 +08:00
|
|
|
APInt APInt::lshr(unsigned shiftAmt) const {
|
2007-05-04 02:15:36 +08:00
|
|
|
if (isSingleWord()) {
|
2012-02-25 03:06:15 +08:00
|
|
|
if (shiftAmt >= BitWidth)
|
2007-02-25 09:56:07 +08:00
|
|
|
return APInt(BitWidth, 0);
|
2009-08-21 12:06:45 +08:00
|
|
|
else
|
2007-02-25 09:56:07 +08:00
|
|
|
return APInt(BitWidth, this->VAL >> shiftAmt);
|
2007-05-04 02:15:36 +08:00
|
|
|
}
|
2007-02-25 09:56:07 +08:00
|
|
|
|
2007-02-26 09:19:48 +08:00
|
|
|
// If all the bits were shifted out, the result is 0. This avoids issues
|
|
|
|
// with shifting by the size of the integer type, which produces undefined
|
|
|
|
// results. We define these "undefined results" to always be 0.
|
2012-06-09 02:04:52 +08:00
|
|
|
if (shiftAmt >= BitWidth)
|
2007-02-26 09:19:48 +08:00
|
|
|
return APInt(BitWidth, 0);
|
|
|
|
|
2007-05-17 14:26:29 +08:00
|
|
|
// If none of the bits are shifted out, the result is *this. This avoids
|
2009-08-21 12:06:45 +08:00
|
|
|
// issues with shifting by the size of the integer type, which produces
|
2007-05-17 14:26:29 +08:00
|
|
|
// undefined results in the code below. This is also an optimization.
|
|
|
|
if (shiftAmt == 0)
|
|
|
|
return *this;
|
|
|
|
|
2007-02-26 09:19:48 +08:00
|
|
|
// Create some space for the result.
|
|
|
|
uint64_t * val = new uint64_t[getNumWords()];
|
|
|
|
|
|
|
|
// If we are shifting less than a word, compute the shift with a simple carry
|
|
|
|
if (shiftAmt < APINT_BITS_PER_WORD) {
|
2011-11-24 05:33:37 +08:00
|
|
|
lshrNear(val, pVal, getNumWords(), shiftAmt);
|
2014-10-10 18:18:12 +08:00
|
|
|
APInt Result(val, BitWidth);
|
|
|
|
Result.clearUnusedBits();
|
|
|
|
return Result;
|
2007-02-26 03:32:03 +08:00
|
|
|
}
|
|
|
|
|
2007-02-26 09:19:48 +08:00
|
|
|
// Compute some values needed by the remaining shift algorithms
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned wordShift = shiftAmt % APINT_BITS_PER_WORD;
|
|
|
|
unsigned offset = shiftAmt / APINT_BITS_PER_WORD;
|
2007-02-26 09:19:48 +08:00
|
|
|
|
|
|
|
// If we are shifting whole words, just move whole words
|
|
|
|
if (wordShift == 0) {
|
2009-01-22 02:09:24 +08:00
|
|
|
for (unsigned i = 0; i < getNumWords() - offset; ++i)
|
2007-02-26 09:19:48 +08:00
|
|
|
val[i] = pVal[i+offset];
|
2009-01-22 02:09:24 +08:00
|
|
|
for (unsigned i = getNumWords()-offset; i < getNumWords(); i++)
|
2007-02-26 09:19:48 +08:00
|
|
|
val[i] = 0;
|
2014-10-10 18:18:12 +08:00
|
|
|
APInt Result(val, BitWidth);
|
|
|
|
Result.clearUnusedBits();
|
|
|
|
return Result;
|
2007-02-26 09:19:48 +08:00
|
|
|
}
|
|
|
|
|
2009-08-21 12:06:45 +08:00
|
|
|
// Shift the low order words
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned breakWord = getNumWords() - offset -1;
|
|
|
|
for (unsigned i = 0; i < breakWord; ++i)
|
2007-03-01 13:39:56 +08:00
|
|
|
val[i] = (pVal[i+offset] >> wordShift) |
|
|
|
|
(pVal[i+offset+1] << (APINT_BITS_PER_WORD - wordShift));
|
2007-02-26 09:19:48 +08:00
|
|
|
// Shift the break word.
|
|
|
|
val[breakWord] = pVal[breakWord+offset] >> wordShift;
|
|
|
|
|
|
|
|
// Remaining words are 0
|
2009-01-22 02:09:24 +08:00
|
|
|
for (unsigned i = breakWord+1; i < getNumWords(); ++i)
|
2007-02-26 09:19:48 +08:00
|
|
|
val[i] = 0;
|
2014-10-10 18:18:12 +08:00
|
|
|
APInt Result(val, BitWidth);
|
|
|
|
Result.clearUnusedBits();
|
|
|
|
return Result;
|
2007-02-08 22:35:19 +08:00
|
|
|
}
|
|
|
|
|
2008-02-29 09:40:47 +08:00
|
|
|
/// Left-shift this APInt by shiftAmt.
|
|
|
|
/// @brief Left-shift function.
|
|
|
|
APInt APInt::shl(const APInt &shiftAmt) const {
|
2009-01-20 01:42:33 +08:00
|
|
|
// It's undefined behavior in C to shift by BitWidth or greater.
|
2009-01-22 02:09:24 +08:00
|
|
|
return shl((unsigned)shiftAmt.getLimitedValue(BitWidth));
|
2008-02-29 09:40:47 +08:00
|
|
|
}
|
|
|
|
|
2009-01-22 02:09:24 +08:00
|
|
|
APInt APInt::shlSlowCase(unsigned shiftAmt) const {
|
2007-02-25 08:56:44 +08:00
|
|
|
// If all the bits were shifted out, the result is 0. This avoids issues
|
|
|
|
// with shifting by the size of the integer type, which produces undefined
|
|
|
|
// results. We define these "undefined results" to always be 0.
|
|
|
|
if (shiftAmt == BitWidth)
|
|
|
|
return APInt(BitWidth, 0);
|
|
|
|
|
2007-05-13 02:01:57 +08:00
|
|
|
// If none of the bits are shifted out, the result is *this. This avoids a
|
|
|
|
// lshr by the words size in the loop below which can produce incorrect
|
|
|
|
// results. It also avoids the expensive computation below for a common case.
|
|
|
|
if (shiftAmt == 0)
|
|
|
|
return *this;
|
|
|
|
|
2007-02-25 08:56:44 +08:00
|
|
|
// Create some space for the result.
|
|
|
|
uint64_t * val = new uint64_t[getNumWords()];
|
|
|
|
|
|
|
|
// If we are shifting less than a word, do it the easy way
|
|
|
|
if (shiftAmt < APINT_BITS_PER_WORD) {
|
|
|
|
uint64_t carry = 0;
|
2009-01-22 02:09:24 +08:00
|
|
|
for (unsigned i = 0; i < getNumWords(); i++) {
|
2007-02-25 08:56:44 +08:00
|
|
|
val[i] = pVal[i] << shiftAmt | carry;
|
|
|
|
carry = pVal[i] >> (APINT_BITS_PER_WORD - shiftAmt);
|
|
|
|
}
|
2014-10-10 18:18:12 +08:00
|
|
|
APInt Result(val, BitWidth);
|
|
|
|
Result.clearUnusedBits();
|
|
|
|
return Result;
|
2007-02-25 08:56:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compute some values needed by the remaining shift algorithms
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned wordShift = shiftAmt % APINT_BITS_PER_WORD;
|
|
|
|
unsigned offset = shiftAmt / APINT_BITS_PER_WORD;
|
2007-02-25 08:56:44 +08:00
|
|
|
|
|
|
|
// If we are shifting whole words, just move whole words
|
|
|
|
if (wordShift == 0) {
|
2009-01-22 02:09:24 +08:00
|
|
|
for (unsigned i = 0; i < offset; i++)
|
2007-02-25 08:56:44 +08:00
|
|
|
val[i] = 0;
|
2009-01-22 02:09:24 +08:00
|
|
|
for (unsigned i = offset; i < getNumWords(); i++)
|
2007-02-25 08:56:44 +08:00
|
|
|
val[i] = pVal[i-offset];
|
2014-10-10 18:18:12 +08:00
|
|
|
APInt Result(val, BitWidth);
|
|
|
|
Result.clearUnusedBits();
|
|
|
|
return Result;
|
2007-02-25 08:56:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy whole words from this to Result.
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned i = getNumWords() - 1;
|
2007-02-25 08:56:44 +08:00
|
|
|
for (; i > offset; --i)
|
|
|
|
val[i] = pVal[i-offset] << wordShift |
|
|
|
|
pVal[i-offset-1] >> (APINT_BITS_PER_WORD - wordShift);
|
2007-02-25 09:08:58 +08:00
|
|
|
val[offset] = pVal[0] << wordShift;
|
2007-02-25 08:56:44 +08:00
|
|
|
for (i = 0; i < offset; ++i)
|
|
|
|
val[i] = 0;
|
2014-10-10 18:18:12 +08:00
|
|
|
APInt Result(val, BitWidth);
|
|
|
|
Result.clearUnusedBits();
|
|
|
|
return Result;
|
2007-02-08 22:35:19 +08:00
|
|
|
}
|
|
|
|
|
2008-02-29 09:40:47 +08:00
|
|
|
APInt APInt::rotl(const APInt &rotateAmt) const {
|
2009-01-22 02:09:24 +08:00
|
|
|
return rotl((unsigned)rotateAmt.getLimitedValue(BitWidth));
|
2008-02-29 09:40:47 +08:00
|
|
|
}
|
|
|
|
|
2009-01-22 02:09:24 +08:00
|
|
|
APInt APInt::rotl(unsigned rotateAmt) const {
|
2011-12-22 11:15:35 +08:00
|
|
|
rotateAmt %= BitWidth;
|
2007-05-14 08:15:28 +08:00
|
|
|
if (rotateAmt == 0)
|
|
|
|
return *this;
|
2011-12-22 11:15:35 +08:00
|
|
|
return shl(rotateAmt) | lshr(BitWidth - rotateAmt);
|
2007-05-14 07:44:59 +08:00
|
|
|
}
|
|
|
|
|
2008-02-29 09:40:47 +08:00
|
|
|
APInt APInt::rotr(const APInt &rotateAmt) const {
|
2009-01-22 02:09:24 +08:00
|
|
|
return rotr((unsigned)rotateAmt.getLimitedValue(BitWidth));
|
2008-02-29 09:40:47 +08:00
|
|
|
}
|
|
|
|
|
2009-01-22 02:09:24 +08:00
|
|
|
APInt APInt::rotr(unsigned rotateAmt) const {
|
2011-12-22 11:15:35 +08:00
|
|
|
rotateAmt %= BitWidth;
|
2007-05-14 08:15:28 +08:00
|
|
|
if (rotateAmt == 0)
|
|
|
|
return *this;
|
2011-12-22 11:15:35 +08:00
|
|
|
return lshr(rotateAmt) | shl(BitWidth - rotateAmt);
|
2007-05-14 07:44:59 +08:00
|
|
|
}
|
2007-03-01 13:39:56 +08:00
|
|
|
|
|
|
|
// Square Root - this method computes and returns the square root of "this".
|
|
|
|
// Three mechanisms are used for computation. For small values (<= 5 bits),
|
|
|
|
// a table lookup is done. This gets some performance for common cases. For
|
|
|
|
// values using less than 52 bits, the value is converted to double and then
|
|
|
|
// the libc sqrt function is called. The result is rounded and then converted
|
|
|
|
// back to a uint64_t which is then used to construct the result. Finally,
|
2009-08-21 12:06:45 +08:00
|
|
|
// the Babylonian method for computing square roots is used.
|
2007-03-01 13:39:56 +08:00
|
|
|
APInt APInt::sqrt() const {
|
|
|
|
|
|
|
|
// Determine the magnitude of the value.
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned magnitude = getActiveBits();
|
2007-03-01 13:39:56 +08:00
|
|
|
|
|
|
|
// Use a fast table for some small values. This also gets rid of some
|
|
|
|
// rounding errors in libc sqrt for small values.
|
|
|
|
if (magnitude <= 5) {
|
2007-03-02 01:47:31 +08:00
|
|
|
static const uint8_t results[32] = {
|
2007-03-01 14:23:32 +08:00
|
|
|
/* 0 */ 0,
|
|
|
|
/* 1- 2 */ 1, 1,
|
2009-08-21 12:06:45 +08:00
|
|
|
/* 3- 6 */ 2, 2, 2, 2,
|
2007-03-01 14:23:32 +08:00
|
|
|
/* 7-12 */ 3, 3, 3, 3, 3, 3,
|
|
|
|
/* 13-20 */ 4, 4, 4, 4, 4, 4, 4, 4,
|
|
|
|
/* 21-30 */ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
|
|
|
|
/* 31 */ 6
|
|
|
|
};
|
|
|
|
return APInt(BitWidth, results[ (isSingleWord() ? VAL : pVal[0]) ]);
|
2007-03-01 13:39:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the magnitude of the value fits in less than 52 bits (the precision of
|
|
|
|
// an IEEE double precision floating point value), then we can use the
|
|
|
|
// libc sqrt function which will probably use a hardware sqrt computation.
|
|
|
|
// This should be faster than the algorithm below.
|
2007-03-05 08:00:42 +08:00
|
|
|
if (magnitude < 52) {
|
2009-08-21 12:06:45 +08:00
|
|
|
return APInt(BitWidth,
|
2010-05-16 01:11:55 +08:00
|
|
|
uint64_t(::round(::sqrt(double(isSingleWord()?VAL:pVal[0])))));
|
2007-03-05 08:00:42 +08:00
|
|
|
}
|
2007-03-01 13:39:56 +08:00
|
|
|
|
|
|
|
// Okay, all the short cuts are exhausted. We must compute it. The following
|
|
|
|
// is a classical Babylonian method for computing the square root. This code
|
2014-09-11 23:41:01 +08:00
|
|
|
// was adapted to APInt from a wikipedia article on such computations.
|
2007-03-01 13:39:56 +08:00
|
|
|
// See http://www.wikipedia.org/ and go to the page named
|
2009-08-21 12:06:45 +08:00
|
|
|
// Calculate_an_integer_square_root.
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned nbits = BitWidth, i = 4;
|
2007-03-01 13:39:56 +08:00
|
|
|
APInt testy(BitWidth, 16);
|
|
|
|
APInt x_old(BitWidth, 1);
|
|
|
|
APInt x_new(BitWidth, 0);
|
|
|
|
APInt two(BitWidth, 2);
|
|
|
|
|
|
|
|
// Select a good starting value using binary logarithms.
|
2009-08-21 12:06:45 +08:00
|
|
|
for (;; i += 2, testy = testy.shl(2))
|
2007-03-01 13:39:56 +08:00
|
|
|
if (i >= nbits || this->ule(testy)) {
|
|
|
|
x_old = x_old.shl(i / 2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-08-21 12:06:45 +08:00
|
|
|
// Use the Babylonian method to arrive at the integer square root:
|
2007-03-01 13:39:56 +08:00
|
|
|
for (;;) {
|
|
|
|
x_new = (this->udiv(x_old) + x_old).udiv(two);
|
|
|
|
if (x_old.ule(x_new))
|
|
|
|
break;
|
|
|
|
x_old = x_new;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we return the closest approximation
|
2009-08-21 12:06:45 +08:00
|
|
|
// NOTE: The rounding calculation below is correct. It will produce an
|
2007-03-02 12:21:55 +08:00
|
|
|
// off-by-one discrepancy with results from pari/gp. That discrepancy has been
|
2009-08-21 12:06:45 +08:00
|
|
|
// determined to be a rounding issue with pari/gp as it begins to use a
|
2007-03-02 12:21:55 +08:00
|
|
|
// floating point representation after 192 bits. There are no discrepancies
|
|
|
|
// between this algorithm and pari/gp for bit widths < 192 bits.
|
2007-03-01 13:39:56 +08:00
|
|
|
APInt square(x_old * x_old);
|
|
|
|
APInt nextSquare((x_old + 1) * (x_old +1));
|
|
|
|
if (this->ult(square))
|
|
|
|
return x_old;
|
2011-12-02 04:58:30 +08:00
|
|
|
assert(this->ule(nextSquare) && "Error in APInt::sqrt computation");
|
|
|
|
APInt midpoint((nextSquare - square).udiv(two));
|
|
|
|
APInt offset(*this - square);
|
|
|
|
if (offset.ult(midpoint))
|
|
|
|
return x_old;
|
2007-03-01 13:39:56 +08:00
|
|
|
return x_old + 1;
|
|
|
|
}
|
|
|
|
|
2008-06-24 03:39:50 +08:00
|
|
|
/// Computes the multiplicative inverse of this APInt for a given modulo. The
|
|
|
|
/// iterative extended Euclidean algorithm is used to solve for this value,
|
|
|
|
/// however we simplify it to speed up calculating only the inverse, and take
|
|
|
|
/// advantage of div+rem calculations. We also use some tricks to avoid copying
|
|
|
|
/// (potentially large) APInts around.
|
|
|
|
APInt APInt::multiplicativeInverse(const APInt& modulo) const {
|
|
|
|
assert(ult(modulo) && "This APInt must be smaller than the modulo");
|
|
|
|
|
|
|
|
// Using the properties listed at the following web page (accessed 06/21/08):
|
|
|
|
// http://www.numbertheory.org/php/euclid.html
|
|
|
|
// (especially the properties numbered 3, 4 and 9) it can be proved that
|
|
|
|
// BitWidth bits suffice for all the computations in the algorithm implemented
|
|
|
|
// below. More precisely, this number of bits suffice if the multiplicative
|
|
|
|
// inverse exists, but may not suffice for the general extended Euclidean
|
|
|
|
// algorithm.
|
|
|
|
|
|
|
|
APInt r[2] = { modulo, *this };
|
|
|
|
APInt t[2] = { APInt(BitWidth, 0), APInt(BitWidth, 1) };
|
|
|
|
APInt q(BitWidth, 0);
|
2009-08-21 12:06:45 +08:00
|
|
|
|
2008-06-24 03:39:50 +08:00
|
|
|
unsigned i;
|
|
|
|
for (i = 0; r[i^1] != 0; i ^= 1) {
|
|
|
|
// An overview of the math without the confusing bit-flipping:
|
|
|
|
// q = r[i-2] / r[i-1]
|
|
|
|
// r[i] = r[i-2] % r[i-1]
|
|
|
|
// t[i] = t[i-2] - t[i-1] * q
|
|
|
|
udivrem(r[i], r[i^1], q, r[i]);
|
|
|
|
t[i] -= t[i^1] * q;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this APInt and the modulo are not coprime, there is no multiplicative
|
|
|
|
// inverse, so return 0. We check this by looking at the next-to-last
|
|
|
|
// remainder, which is the gcd(*this,modulo) as calculated by the Euclidean
|
|
|
|
// algorithm.
|
|
|
|
if (r[i] != 1)
|
|
|
|
return APInt(BitWidth, 0);
|
|
|
|
|
|
|
|
// The next-to-last t is the multiplicative inverse. However, we are
|
|
|
|
// interested in a positive inverse. Calcuate a positive one from a negative
|
|
|
|
// one if necessary. A simple addition of the modulo suffices because
|
2008-07-20 23:55:14 +08:00
|
|
|
// abs(t[i]) is known to be less than *this/2 (see the link above).
|
2008-06-24 03:39:50 +08:00
|
|
|
return t[i].isNegative() ? t[i] + modulo : t[i];
|
|
|
|
}
|
|
|
|
|
2009-04-30 18:15:35 +08:00
|
|
|
/// Calculate the magic numbers required to implement a signed integer division
|
|
|
|
/// by a constant as a sequence of multiplies, adds and shifts. Requires that
|
|
|
|
/// the divisor not be 0, 1, or -1. Taken from "Hacker's Delight", Henry S.
|
|
|
|
/// Warren, Jr., chapter 10.
|
|
|
|
APInt::ms APInt::magic() const {
|
|
|
|
const APInt& d = *this;
|
|
|
|
unsigned p;
|
|
|
|
APInt ad, anc, delta, q1, r1, q2, r2, t;
|
|
|
|
APInt signedMin = APInt::getSignedMinValue(d.getBitWidth());
|
|
|
|
struct ms mag;
|
2009-08-21 12:06:45 +08:00
|
|
|
|
2009-04-30 18:15:35 +08:00
|
|
|
ad = d.abs();
|
|
|
|
t = signedMin + (d.lshr(d.getBitWidth() - 1));
|
|
|
|
anc = t - 1 - t.urem(ad); // absolute value of nc
|
|
|
|
p = d.getBitWidth() - 1; // initialize p
|
|
|
|
q1 = signedMin.udiv(anc); // initialize q1 = 2p/abs(nc)
|
|
|
|
r1 = signedMin - q1*anc; // initialize r1 = rem(2p,abs(nc))
|
|
|
|
q2 = signedMin.udiv(ad); // initialize q2 = 2p/abs(d)
|
|
|
|
r2 = signedMin - q2*ad; // initialize r2 = rem(2p,abs(d))
|
|
|
|
do {
|
|
|
|
p = p + 1;
|
|
|
|
q1 = q1<<1; // update q1 = 2p/abs(nc)
|
|
|
|
r1 = r1<<1; // update r1 = rem(2p/abs(nc))
|
|
|
|
if (r1.uge(anc)) { // must be unsigned comparison
|
|
|
|
q1 = q1 + 1;
|
|
|
|
r1 = r1 - anc;
|
|
|
|
}
|
|
|
|
q2 = q2<<1; // update q2 = 2p/abs(d)
|
|
|
|
r2 = r2<<1; // update r2 = rem(2p/abs(d))
|
|
|
|
if (r2.uge(ad)) { // must be unsigned comparison
|
|
|
|
q2 = q2 + 1;
|
|
|
|
r2 = r2 - ad;
|
|
|
|
}
|
|
|
|
delta = ad - r2;
|
2011-02-21 08:22:02 +08:00
|
|
|
} while (q1.ult(delta) || (q1 == delta && r1 == 0));
|
2009-08-21 12:06:45 +08:00
|
|
|
|
2009-04-30 18:15:35 +08:00
|
|
|
mag.m = q2 + 1;
|
|
|
|
if (d.isNegative()) mag.m = -mag.m; // resulting magic number
|
|
|
|
mag.s = p - d.getBitWidth(); // resulting shift
|
|
|
|
return mag;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Calculate the magic numbers required to implement an unsigned integer
|
|
|
|
/// division by a constant as a sequence of multiplies, adds and shifts.
|
|
|
|
/// Requires that the divisor not be 0. Taken from "Hacker's Delight", Henry
|
|
|
|
/// S. Warren, Jr., chapter 10.
|
2011-03-18 04:39:06 +08:00
|
|
|
/// LeadingZeros can be used to simplify the calculation if the upper bits
|
2011-04-15 13:18:47 +08:00
|
|
|
/// of the divided value are known zero.
|
2011-03-18 04:39:06 +08:00
|
|
|
APInt::mu APInt::magicu(unsigned LeadingZeros) const {
|
2009-04-30 18:15:35 +08:00
|
|
|
const APInt& d = *this;
|
|
|
|
unsigned p;
|
|
|
|
APInt nc, delta, q1, r1, q2, r2;
|
|
|
|
struct mu magu;
|
|
|
|
magu.a = 0; // initialize "add" indicator
|
2011-03-18 04:39:06 +08:00
|
|
|
APInt allOnes = APInt::getAllOnesValue(d.getBitWidth()).lshr(LeadingZeros);
|
2009-04-30 18:15:35 +08:00
|
|
|
APInt signedMin = APInt::getSignedMinValue(d.getBitWidth());
|
|
|
|
APInt signedMax = APInt::getSignedMaxValue(d.getBitWidth());
|
|
|
|
|
2012-07-12 02:31:59 +08:00
|
|
|
nc = allOnes - (allOnes - d).urem(d);
|
2009-04-30 18:15:35 +08:00
|
|
|
p = d.getBitWidth() - 1; // initialize p
|
|
|
|
q1 = signedMin.udiv(nc); // initialize q1 = 2p/nc
|
|
|
|
r1 = signedMin - q1*nc; // initialize r1 = rem(2p,nc)
|
|
|
|
q2 = signedMax.udiv(d); // initialize q2 = (2p-1)/d
|
|
|
|
r2 = signedMax - q2*d; // initialize r2 = rem((2p-1),d)
|
|
|
|
do {
|
|
|
|
p = p + 1;
|
|
|
|
if (r1.uge(nc - r1)) {
|
|
|
|
q1 = q1 + q1 + 1; // update q1
|
|
|
|
r1 = r1 + r1 - nc; // update r1
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
q1 = q1+q1; // update q1
|
|
|
|
r1 = r1+r1; // update r1
|
|
|
|
}
|
|
|
|
if ((r2 + 1).uge(d - r2)) {
|
|
|
|
if (q2.uge(signedMax)) magu.a = 1;
|
|
|
|
q2 = q2+q2 + 1; // update q2
|
|
|
|
r2 = r2+r2 + 1 - d; // update r2
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (q2.uge(signedMin)) magu.a = 1;
|
|
|
|
q2 = q2+q2; // update q2
|
|
|
|
r2 = r2+r2 + 1; // update r2
|
|
|
|
}
|
|
|
|
delta = d - 1 - r2;
|
|
|
|
} while (p < d.getBitWidth()*2 &&
|
|
|
|
(q1.ult(delta) || (q1 == delta && r1 == 0)));
|
|
|
|
magu.m = q2 + 1; // resulting magic number
|
|
|
|
magu.s = p - d.getBitWidth(); // resulting shift
|
|
|
|
return magu;
|
|
|
|
}
|
|
|
|
|
2007-02-20 16:51:03 +08:00
|
|
|
/// Implementation of Knuth's Algorithm D (Division of nonnegative integers)
|
|
|
|
/// from "Art of Computer Programming, Volume 2", section 4.3.1, p. 272. The
|
|
|
|
/// variables here have the same names as in the algorithm. Comments explain
|
|
|
|
/// the algorithm and any deviation from it.
|
2009-01-22 02:09:24 +08:00
|
|
|
static void KnuthDiv(unsigned *u, unsigned *v, unsigned *q, unsigned* r,
|
|
|
|
unsigned m, unsigned n) {
|
2007-02-20 16:51:03 +08:00
|
|
|
assert(u && "Must provide dividend");
|
|
|
|
assert(v && "Must provide divisor");
|
|
|
|
assert(q && "Must provide quotient");
|
2015-03-27 03:45:19 +08:00
|
|
|
assert(u != v && u != q && v != q && "Must use different memory");
|
2007-02-20 16:51:03 +08:00
|
|
|
assert(n>1 && "n must be > 1");
|
|
|
|
|
2015-03-27 03:45:19 +08:00
|
|
|
// b denotes the base of the number system. In our case b is 2^32.
|
|
|
|
LLVM_CONSTEXPR uint64_t b = uint64_t(1) << 32;
|
2007-02-20 16:51:03 +08:00
|
|
|
|
2010-01-05 09:28:52 +08:00
|
|
|
DEBUG(dbgs() << "KnuthDiv: m=" << m << " n=" << n << '\n');
|
|
|
|
DEBUG(dbgs() << "KnuthDiv: original:");
|
|
|
|
DEBUG(for (int i = m+n; i >=0; i--) dbgs() << " " << u[i]);
|
|
|
|
DEBUG(dbgs() << " by");
|
|
|
|
DEBUG(for (int i = n; i >0; i--) dbgs() << " " << v[i-1]);
|
|
|
|
DEBUG(dbgs() << '\n');
|
2009-08-21 12:06:45 +08:00
|
|
|
// D1. [Normalize.] Set d = b / (v[n-1] + 1) and multiply all the digits of
|
|
|
|
// u and v by d. Note that we have taken Knuth's advice here to use a power
|
|
|
|
// of 2 value for d such that d * v[n-1] >= b/2 (b is the base). A power of
|
|
|
|
// 2 allows us to shift instead of multiply and it is easy to determine the
|
2007-02-20 16:51:03 +08:00
|
|
|
// shift amount from the leading zeros. We are basically normalizing the u
|
|
|
|
// and v so that its high bits are shifted to the top of v's range without
|
|
|
|
// overflow. Note that this can require an extra word in u so that u must
|
|
|
|
// be of length m+n+1.
|
2013-05-25 06:23:49 +08:00
|
|
|
unsigned shift = countLeadingZeros(v[n-1]);
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned v_carry = 0;
|
|
|
|
unsigned u_carry = 0;
|
2007-02-20 16:51:03 +08:00
|
|
|
if (shift) {
|
2009-01-22 02:09:24 +08:00
|
|
|
for (unsigned i = 0; i < m+n; ++i) {
|
|
|
|
unsigned u_tmp = u[i] >> (32 - shift);
|
2007-02-20 16:51:03 +08:00
|
|
|
u[i] = (u[i] << shift) | u_carry;
|
|
|
|
u_carry = u_tmp;
|
2007-02-17 11:16:00 +08:00
|
|
|
}
|
2009-01-22 02:09:24 +08:00
|
|
|
for (unsigned i = 0; i < n; ++i) {
|
|
|
|
unsigned v_tmp = v[i] >> (32 - shift);
|
2007-02-20 16:51:03 +08:00
|
|
|
v[i] = (v[i] << shift) | v_carry;
|
|
|
|
v_carry = v_tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
u[m+n] = u_carry;
|
2015-03-27 03:45:19 +08:00
|
|
|
|
2010-01-05 09:28:52 +08:00
|
|
|
DEBUG(dbgs() << "KnuthDiv: normal:");
|
|
|
|
DEBUG(for (int i = m+n; i >=0; i--) dbgs() << " " << u[i]);
|
|
|
|
DEBUG(dbgs() << " by");
|
|
|
|
DEBUG(for (int i = n; i >0; i--) dbgs() << " " << v[i-1]);
|
|
|
|
DEBUG(dbgs() << '\n');
|
2007-02-20 16:51:03 +08:00
|
|
|
|
|
|
|
// D2. [Initialize j.] Set j to m. This is the loop counter over the places.
|
|
|
|
int j = m;
|
|
|
|
do {
|
2010-01-05 09:28:52 +08:00
|
|
|
DEBUG(dbgs() << "KnuthDiv: quotient digit #" << j << '\n');
|
2009-08-21 12:06:45 +08:00
|
|
|
// D3. [Calculate q'.].
|
2007-02-20 16:51:03 +08:00
|
|
|
// Set qp = (u[j+n]*b + u[j+n-1]) / v[n-1]. (qp=qprime=q')
|
|
|
|
// Set rp = (u[j+n]*b + u[j+n-1]) % v[n-1]. (rp=rprime=r')
|
|
|
|
// Now test if qp == b or qp*v[n-2] > b*rp + u[j+n-2]; if so, decrease
|
|
|
|
// qp by 1, inrease rp by v[n-1], and repeat this test if rp < b. The test
|
|
|
|
// on v[n-2] determines at high speed most of the cases in which the trial
|
2009-08-21 12:06:45 +08:00
|
|
|
// value qp is one too large, and it eliminates all cases where qp is two
|
|
|
|
// too large.
|
2007-02-23 09:57:13 +08:00
|
|
|
uint64_t dividend = ((uint64_t(u[j+n]) << 32) + u[j+n-1]);
|
2010-01-05 09:28:52 +08:00
|
|
|
DEBUG(dbgs() << "KnuthDiv: dividend == " << dividend << '\n');
|
2007-02-23 09:57:13 +08:00
|
|
|
uint64_t qp = dividend / v[n-1];
|
|
|
|
uint64_t rp = dividend % v[n-1];
|
2007-02-20 16:51:03 +08:00
|
|
|
if (qp == b || qp*v[n-2] > b*rp + u[j+n-2]) {
|
|
|
|
qp--;
|
|
|
|
rp += v[n-1];
|
2007-02-24 18:01:42 +08:00
|
|
|
if (rp < b && (qp == b || qp*v[n-2] > b*rp + u[j+n-2]))
|
2007-02-24 11:58:46 +08:00
|
|
|
qp--;
|
2007-02-23 09:57:13 +08:00
|
|
|
}
|
2010-01-05 09:28:52 +08:00
|
|
|
DEBUG(dbgs() << "KnuthDiv: qp == " << qp << ", rp == " << rp << '\n');
|
2007-02-20 16:51:03 +08:00
|
|
|
|
2007-02-23 09:57:13 +08:00
|
|
|
// D4. [Multiply and subtract.] Replace (u[j+n]u[j+n-1]...u[j]) with
|
|
|
|
// (u[j+n]u[j+n-1]..u[j]) - qp * (v[n-1]...v[1]v[0]). This computation
|
|
|
|
// consists of a simple multiplication by a one-place number, combined with
|
2009-08-21 12:06:45 +08:00
|
|
|
// a subtraction.
|
2015-03-27 03:45:19 +08:00
|
|
|
// The digits (u[j+n]...u[j]) should be kept positive; if the result of
|
|
|
|
// this step is actually negative, (u[j+n]...u[j]) should be left as the
|
|
|
|
// true value plus b**(n+1), namely as the b's complement of
|
|
|
|
// the true value, and a "borrow" to the left should be remembered.
|
2015-04-24 15:38:39 +08:00
|
|
|
int64_t borrow = 0;
|
2009-01-22 02:09:24 +08:00
|
|
|
for (unsigned i = 0; i < n; ++i) {
|
2015-04-24 15:38:39 +08:00
|
|
|
uint64_t p = uint64_t(qp) * uint64_t(v[i]);
|
|
|
|
int64_t subres = int64_t(u[j+i]) - borrow - (unsigned)p;
|
|
|
|
u[j+i] = (unsigned)subres;
|
|
|
|
borrow = (p >> 32) - (subres >> 32);
|
|
|
|
DEBUG(dbgs() << "KnuthDiv: u[j+i] = " << u[j+i]
|
2009-07-13 13:27:30 +08:00
|
|
|
<< ", borrow = " << borrow << '\n');
|
2007-02-24 11:58:46 +08:00
|
|
|
}
|
2015-04-24 15:38:39 +08:00
|
|
|
bool isNeg = u[j+n] < borrow;
|
|
|
|
u[j+n] -= (unsigned)borrow;
|
|
|
|
|
2010-01-05 09:28:52 +08:00
|
|
|
DEBUG(dbgs() << "KnuthDiv: after subtraction:");
|
|
|
|
DEBUG(for (int i = m+n; i >=0; i--) dbgs() << " " << u[i]);
|
|
|
|
DEBUG(dbgs() << '\n');
|
2007-02-20 16:51:03 +08:00
|
|
|
|
2009-08-21 12:06:45 +08:00
|
|
|
// D5. [Test remainder.] Set q[j] = qp. If the result of step D4 was
|
2007-02-20 16:51:03 +08:00
|
|
|
// negative, go to step D6; otherwise go on to step D7.
|
2009-01-22 02:09:24 +08:00
|
|
|
q[j] = (unsigned)qp;
|
2007-02-26 15:44:38 +08:00
|
|
|
if (isNeg) {
|
2009-08-21 12:06:45 +08:00
|
|
|
// D6. [Add back]. The probability that this step is necessary is very
|
2007-02-20 16:51:03 +08:00
|
|
|
// small, on the order of only 2/b. Make sure that test data accounts for
|
2009-08-21 12:06:45 +08:00
|
|
|
// this possibility. Decrease q[j] by 1
|
2007-02-23 09:57:13 +08:00
|
|
|
q[j]--;
|
2009-08-21 12:06:45 +08:00
|
|
|
// and add (0v[n-1]...v[1]v[0]) to (u[j+n]u[j+n-1]...u[j+1]u[j]).
|
|
|
|
// A carry will occur to the left of u[j+n], and it should be ignored
|
2007-02-23 09:57:13 +08:00
|
|
|
// since it cancels with the borrow that occurred in D4.
|
|
|
|
bool carry = false;
|
2009-01-22 02:09:24 +08:00
|
|
|
for (unsigned i = 0; i < n; i++) {
|
|
|
|
unsigned limit = std::min(u[j+i],v[i]);
|
2007-02-20 16:51:03 +08:00
|
|
|
u[j+i] += v[i] + carry;
|
2007-02-24 11:58:46 +08:00
|
|
|
carry = u[j+i] < limit || (carry && u[j+i] == limit);
|
2007-02-17 11:16:00 +08:00
|
|
|
}
|
2007-02-24 11:58:46 +08:00
|
|
|
u[j+n] += carry;
|
2007-02-17 11:16:00 +08:00
|
|
|
}
|
2010-01-05 09:28:52 +08:00
|
|
|
DEBUG(dbgs() << "KnuthDiv: after correction:");
|
2015-03-27 03:45:19 +08:00
|
|
|
DEBUG(for (int i = m+n; i >=0; i--) dbgs() << " " << u[i]);
|
2010-01-05 09:28:52 +08:00
|
|
|
DEBUG(dbgs() << "\nKnuthDiv: digit result = " << q[j] << '\n');
|
2007-02-20 16:51:03 +08:00
|
|
|
|
2007-02-23 09:57:13 +08:00
|
|
|
// D7. [Loop on j.] Decrease j by one. Now if j >= 0, go back to D3.
|
|
|
|
} while (--j >= 0);
|
2007-02-20 16:51:03 +08:00
|
|
|
|
2010-01-05 09:28:52 +08:00
|
|
|
DEBUG(dbgs() << "KnuthDiv: quotient:");
|
|
|
|
DEBUG(for (int i = m; i >=0; i--) dbgs() <<" " << q[i]);
|
|
|
|
DEBUG(dbgs() << '\n');
|
2007-02-24 11:58:46 +08:00
|
|
|
|
2007-02-20 16:51:03 +08:00
|
|
|
// D8. [Unnormalize]. Now q[...] is the desired quotient, and the desired
|
|
|
|
// remainder may be obtained by dividing u[...] by d. If r is non-null we
|
|
|
|
// compute the remainder (urem uses this).
|
|
|
|
if (r) {
|
|
|
|
// The value d is expressed by the "shift" value above since we avoided
|
|
|
|
// multiplication by d by using a shift left. So, all we have to do is
|
|
|
|
// shift right here. In order to mak
|
2007-02-25 04:38:01 +08:00
|
|
|
if (shift) {
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned carry = 0;
|
2010-01-05 09:28:52 +08:00
|
|
|
DEBUG(dbgs() << "KnuthDiv: remainder:");
|
2007-02-25 04:38:01 +08:00
|
|
|
for (int i = n-1; i >= 0; i--) {
|
|
|
|
r[i] = (u[i] >> shift) | carry;
|
|
|
|
carry = u[i] << (32 - shift);
|
2010-01-05 09:28:52 +08:00
|
|
|
DEBUG(dbgs() << " " << r[i]);
|
2007-02-25 04:38:01 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int i = n-1; i >= 0; i--) {
|
|
|
|
r[i] = u[i];
|
2010-01-05 09:28:52 +08:00
|
|
|
DEBUG(dbgs() << " " << r[i]);
|
2007-02-25 04:38:01 +08:00
|
|
|
}
|
2007-02-20 16:51:03 +08:00
|
|
|
}
|
2010-01-05 09:28:52 +08:00
|
|
|
DEBUG(dbgs() << '\n');
|
2007-02-20 16:51:03 +08:00
|
|
|
}
|
2010-01-05 09:28:52 +08:00
|
|
|
DEBUG(dbgs() << '\n');
|
2007-02-20 16:51:03 +08:00
|
|
|
}
|
|
|
|
|
2009-01-22 02:09:24 +08:00
|
|
|
void APInt::divide(const APInt LHS, unsigned lhsWords,
|
|
|
|
const APInt &RHS, unsigned rhsWords,
|
2007-02-20 16:51:03 +08:00
|
|
|
APInt *Quotient, APInt *Remainder)
|
|
|
|
{
|
|
|
|
assert(lhsWords >= rhsWords && "Fractional result");
|
|
|
|
|
2009-08-21 12:06:45 +08:00
|
|
|
// First, compose the values into an array of 32-bit words instead of
|
2007-02-20 16:51:03 +08:00
|
|
|
// 64-bit words. This is a necessity of both the "short division" algorithm
|
2010-02-11 00:03:48 +08:00
|
|
|
// and the Knuth "classical algorithm" which requires there to be native
|
2009-08-21 12:06:45 +08:00
|
|
|
// operations for +, -, and * on an m bit value with an m*2 bit result. We
|
|
|
|
// can't use 64-bit operands here because we don't have native results of
|
|
|
|
// 128-bits. Furthermore, casting the 64-bit values to 32-bit values won't
|
2007-02-20 16:51:03 +08:00
|
|
|
// work on large-endian machines.
|
2009-04-02 02:45:54 +08:00
|
|
|
uint64_t mask = ~0ull >> (sizeof(unsigned)*CHAR_BIT);
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned n = rhsWords * 2;
|
|
|
|
unsigned m = (lhsWords * 2) - n;
|
2007-02-25 09:56:07 +08:00
|
|
|
|
|
|
|
// Allocate space for the temporary values we need either on the stack, if
|
|
|
|
// it will fit, or on the heap if it won't.
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned SPACE[128];
|
2014-04-07 12:17:22 +08:00
|
|
|
unsigned *U = nullptr;
|
|
|
|
unsigned *V = nullptr;
|
|
|
|
unsigned *Q = nullptr;
|
|
|
|
unsigned *R = nullptr;
|
2007-02-25 09:56:07 +08:00
|
|
|
if ((Remainder?4:3)*n+2*m+1 <= 128) {
|
|
|
|
U = &SPACE[0];
|
|
|
|
V = &SPACE[m+n+1];
|
|
|
|
Q = &SPACE[(m+n+1) + n];
|
|
|
|
if (Remainder)
|
|
|
|
R = &SPACE[(m+n+1) + n + (m+n)];
|
|
|
|
} else {
|
2009-01-22 02:09:24 +08:00
|
|
|
U = new unsigned[m + n + 1];
|
|
|
|
V = new unsigned[n];
|
|
|
|
Q = new unsigned[m+n];
|
2007-02-25 09:56:07 +08:00
|
|
|
if (Remainder)
|
2009-01-22 02:09:24 +08:00
|
|
|
R = new unsigned[n];
|
2007-02-25 09:56:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the dividend
|
2009-01-22 02:09:24 +08:00
|
|
|
memset(U, 0, (m+n+1)*sizeof(unsigned));
|
2007-02-20 16:51:03 +08:00
|
|
|
for (unsigned i = 0; i < lhsWords; ++i) {
|
2007-02-22 08:58:45 +08:00
|
|
|
uint64_t tmp = (LHS.getNumWords() == 1 ? LHS.VAL : LHS.pVal[i]);
|
2009-01-22 02:09:24 +08:00
|
|
|
U[i * 2] = (unsigned)(tmp & mask);
|
2009-04-02 02:45:54 +08:00
|
|
|
U[i * 2 + 1] = (unsigned)(tmp >> (sizeof(unsigned)*CHAR_BIT));
|
2007-02-20 16:51:03 +08:00
|
|
|
}
|
|
|
|
U[m+n] = 0; // this extra word is for "spill" in the Knuth algorithm.
|
|
|
|
|
2007-02-25 09:56:07 +08:00
|
|
|
// Initialize the divisor
|
2009-01-22 02:09:24 +08:00
|
|
|
memset(V, 0, (n)*sizeof(unsigned));
|
2007-02-20 16:51:03 +08:00
|
|
|
for (unsigned i = 0; i < rhsWords; ++i) {
|
2007-02-22 08:58:45 +08:00
|
|
|
uint64_t tmp = (RHS.getNumWords() == 1 ? RHS.VAL : RHS.pVal[i]);
|
2009-01-22 02:09:24 +08:00
|
|
|
V[i * 2] = (unsigned)(tmp & mask);
|
2009-04-02 02:45:54 +08:00
|
|
|
V[i * 2 + 1] = (unsigned)(tmp >> (sizeof(unsigned)*CHAR_BIT));
|
2007-02-20 16:51:03 +08:00
|
|
|
}
|
|
|
|
|
2007-02-25 09:56:07 +08:00
|
|
|
// initialize the quotient and remainder
|
2009-01-22 02:09:24 +08:00
|
|
|
memset(Q, 0, (m+n) * sizeof(unsigned));
|
2007-02-25 09:56:07 +08:00
|
|
|
if (Remainder)
|
2009-01-22 02:09:24 +08:00
|
|
|
memset(R, 0, n * sizeof(unsigned));
|
2007-02-20 16:51:03 +08:00
|
|
|
|
2009-08-21 12:06:45 +08:00
|
|
|
// Now, adjust m and n for the Knuth division. n is the number of words in
|
2007-02-20 16:51:03 +08:00
|
|
|
// the divisor. m is the number of words by which the dividend exceeds the
|
2009-08-21 12:06:45 +08:00
|
|
|
// divisor (i.e. m+n is the length of the dividend). These sizes must not
|
2007-02-20 16:51:03 +08:00
|
|
|
// contain any zero words or the Knuth algorithm fails.
|
|
|
|
for (unsigned i = n; i > 0 && V[i-1] == 0; i--) {
|
|
|
|
n--;
|
|
|
|
m++;
|
|
|
|
}
|
|
|
|
for (unsigned i = m+n; i > 0 && U[i-1] == 0; i--)
|
|
|
|
m--;
|
|
|
|
|
|
|
|
// If we're left with only a single word for the divisor, Knuth doesn't work
|
|
|
|
// so we implement the short division algorithm here. This is much simpler
|
|
|
|
// and faster because we are certain that we can divide a 64-bit quantity
|
|
|
|
// by a 32-bit quantity at hardware speed and short division is simply a
|
|
|
|
// series of such operations. This is just like doing short division but we
|
|
|
|
// are using base 2^32 instead of base 10.
|
|
|
|
assert(n != 0 && "Divide by zero?");
|
|
|
|
if (n == 1) {
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned divisor = V[0];
|
|
|
|
unsigned remainder = 0;
|
2007-02-20 16:51:03 +08:00
|
|
|
for (int i = m+n-1; i >= 0; i--) {
|
|
|
|
uint64_t partial_dividend = uint64_t(remainder) << 32 | U[i];
|
|
|
|
if (partial_dividend == 0) {
|
|
|
|
Q[i] = 0;
|
|
|
|
remainder = 0;
|
|
|
|
} else if (partial_dividend < divisor) {
|
|
|
|
Q[i] = 0;
|
2009-01-22 02:09:24 +08:00
|
|
|
remainder = (unsigned)partial_dividend;
|
2007-02-20 16:51:03 +08:00
|
|
|
} else if (partial_dividend == divisor) {
|
|
|
|
Q[i] = 1;
|
|
|
|
remainder = 0;
|
|
|
|
} else {
|
2009-01-22 02:09:24 +08:00
|
|
|
Q[i] = (unsigned)(partial_dividend / divisor);
|
|
|
|
remainder = (unsigned)(partial_dividend - (Q[i] * divisor));
|
2007-02-20 16:51:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (R)
|
|
|
|
R[0] = remainder;
|
|
|
|
} else {
|
|
|
|
// Now we're ready to invoke the Knuth classical divide algorithm. In this
|
|
|
|
// case n > 1.
|
|
|
|
KnuthDiv(U, V, Q, R, m, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the caller wants the quotient
|
|
|
|
if (Quotient) {
|
|
|
|
// Set up the Quotient value's memory.
|
|
|
|
if (Quotient->BitWidth != LHS.BitWidth) {
|
|
|
|
if (Quotient->isSingleWord())
|
|
|
|
Quotient->VAL = 0;
|
|
|
|
else
|
2007-02-27 07:38:21 +08:00
|
|
|
delete [] Quotient->pVal;
|
2007-02-20 16:51:03 +08:00
|
|
|
Quotient->BitWidth = LHS.BitWidth;
|
|
|
|
if (!Quotient->isSingleWord())
|
2007-02-21 16:21:52 +08:00
|
|
|
Quotient->pVal = getClearedMemory(Quotient->getNumWords());
|
2007-02-20 16:51:03 +08:00
|
|
|
} else
|
2010-12-01 16:53:58 +08:00
|
|
|
Quotient->clearAllBits();
|
2007-02-20 16:51:03 +08:00
|
|
|
|
2009-08-21 12:06:45 +08:00
|
|
|
// The quotient is in Q. Reconstitute the quotient into Quotient's low
|
2007-02-20 16:51:03 +08:00
|
|
|
// order words.
|
2015-03-27 03:45:19 +08:00
|
|
|
// This case is currently dead as all users of divide() handle trivial cases
|
|
|
|
// earlier.
|
2007-02-20 16:51:03 +08:00
|
|
|
if (lhsWords == 1) {
|
2009-08-21 12:06:45 +08:00
|
|
|
uint64_t tmp =
|
2007-02-20 16:51:03 +08:00
|
|
|
uint64_t(Q[0]) | (uint64_t(Q[1]) << (APINT_BITS_PER_WORD / 2));
|
|
|
|
if (Quotient->isSingleWord())
|
|
|
|
Quotient->VAL = tmp;
|
|
|
|
else
|
|
|
|
Quotient->pVal[0] = tmp;
|
|
|
|
} else {
|
|
|
|
assert(!Quotient->isSingleWord() && "Quotient APInt not large enough");
|
|
|
|
for (unsigned i = 0; i < lhsWords; ++i)
|
2009-08-21 12:06:45 +08:00
|
|
|
Quotient->pVal[i] =
|
2007-02-20 16:51:03 +08:00
|
|
|
uint64_t(Q[i*2]) | (uint64_t(Q[i*2+1]) << (APINT_BITS_PER_WORD / 2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the caller wants the remainder
|
|
|
|
if (Remainder) {
|
|
|
|
// Set up the Remainder value's memory.
|
|
|
|
if (Remainder->BitWidth != RHS.BitWidth) {
|
|
|
|
if (Remainder->isSingleWord())
|
|
|
|
Remainder->VAL = 0;
|
|
|
|
else
|
2007-02-27 07:38:21 +08:00
|
|
|
delete [] Remainder->pVal;
|
2007-02-20 16:51:03 +08:00
|
|
|
Remainder->BitWidth = RHS.BitWidth;
|
|
|
|
if (!Remainder->isSingleWord())
|
2007-02-21 16:21:52 +08:00
|
|
|
Remainder->pVal = getClearedMemory(Remainder->getNumWords());
|
2007-02-20 16:51:03 +08:00
|
|
|
} else
|
2010-12-01 16:53:58 +08:00
|
|
|
Remainder->clearAllBits();
|
2007-02-20 16:51:03 +08:00
|
|
|
|
|
|
|
// The remainder is in R. Reconstitute the remainder into Remainder's low
|
|
|
|
// order words.
|
|
|
|
if (rhsWords == 1) {
|
2009-08-21 12:06:45 +08:00
|
|
|
uint64_t tmp =
|
2007-02-20 16:51:03 +08:00
|
|
|
uint64_t(R[0]) | (uint64_t(R[1]) << (APINT_BITS_PER_WORD / 2));
|
|
|
|
if (Remainder->isSingleWord())
|
|
|
|
Remainder->VAL = tmp;
|
|
|
|
else
|
|
|
|
Remainder->pVal[0] = tmp;
|
|
|
|
} else {
|
|
|
|
assert(!Remainder->isSingleWord() && "Remainder APInt not large enough");
|
|
|
|
for (unsigned i = 0; i < rhsWords; ++i)
|
2009-08-21 12:06:45 +08:00
|
|
|
Remainder->pVal[i] =
|
2007-02-20 16:51:03 +08:00
|
|
|
uint64_t(R[i*2]) | (uint64_t(R[i*2+1]) << (APINT_BITS_PER_WORD / 2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up the memory we allocated.
|
2007-02-25 09:56:07 +08:00
|
|
|
if (U != &SPACE[0]) {
|
|
|
|
delete [] U;
|
|
|
|
delete [] V;
|
|
|
|
delete [] Q;
|
|
|
|
delete [] R;
|
|
|
|
}
|
2007-02-17 11:16:00 +08:00
|
|
|
}
|
|
|
|
|
2007-02-17 06:36:51 +08:00
|
|
|
APInt APInt::udiv(const APInt& RHS) const {
|
2007-02-17 08:18:01 +08:00
|
|
|
assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
|
2007-02-17 10:07:07 +08:00
|
|
|
|
|
|
|
// First, deal with the easy case
|
|
|
|
if (isSingleWord()) {
|
|
|
|
assert(RHS.VAL != 0 && "Divide by zero?");
|
|
|
|
return APInt(BitWidth, VAL / RHS.VAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get some facts about the LHS and RHS number of bits and words
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned rhsBits = RHS.getActiveBits();
|
|
|
|
unsigned rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
|
2007-02-17 10:07:07 +08:00
|
|
|
assert(rhsWords && "Divided by zero???");
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned lhsBits = this->getActiveBits();
|
|
|
|
unsigned lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1);
|
2007-02-17 10:07:07 +08:00
|
|
|
|
|
|
|
// Deal with some degenerate cases
|
2009-08-21 12:06:45 +08:00
|
|
|
if (!lhsWords)
|
2007-02-21 16:21:52 +08:00
|
|
|
// 0 / X ===> 0
|
2009-08-21 12:06:45 +08:00
|
|
|
return APInt(BitWidth, 0);
|
2007-02-21 16:21:52 +08:00
|
|
|
else if (lhsWords < rhsWords || this->ult(RHS)) {
|
2012-09-27 18:14:43 +08:00
|
|
|
// X / Y ===> 0, iff X < Y
|
2007-02-21 16:21:52 +08:00
|
|
|
return APInt(BitWidth, 0);
|
|
|
|
} else if (*this == RHS) {
|
|
|
|
// X / X ===> 1
|
|
|
|
return APInt(BitWidth, 1);
|
2007-02-20 16:51:03 +08:00
|
|
|
} else if (lhsWords == 1 && rhsWords == 1) {
|
2007-02-17 10:07:07 +08:00
|
|
|
// All high words are zero, just use native divide
|
2007-02-21 16:21:52 +08:00
|
|
|
return APInt(BitWidth, this->pVal[0] / RHS.pVal[0]);
|
2007-02-08 22:35:19 +08:00
|
|
|
}
|
2007-02-20 16:51:03 +08:00
|
|
|
|
|
|
|
// We have to compute it the hard way. Invoke the Knuth divide algorithm.
|
|
|
|
APInt Quotient(1,0); // to hold result.
|
2014-04-07 12:17:22 +08:00
|
|
|
divide(*this, lhsWords, RHS, rhsWords, &Quotient, nullptr);
|
2007-02-20 16:51:03 +08:00
|
|
|
return Quotient;
|
2007-02-08 22:35:19 +08:00
|
|
|
}
|
|
|
|
|
2013-02-20 08:17:42 +08:00
|
|
|
APInt APInt::sdiv(const APInt &RHS) const {
|
|
|
|
if (isNegative()) {
|
|
|
|
if (RHS.isNegative())
|
|
|
|
return (-(*this)).udiv(-RHS);
|
|
|
|
return -((-(*this)).udiv(RHS));
|
|
|
|
}
|
|
|
|
if (RHS.isNegative())
|
|
|
|
return -(this->udiv(-RHS));
|
|
|
|
return this->udiv(RHS);
|
|
|
|
}
|
|
|
|
|
2007-02-17 06:36:51 +08:00
|
|
|
APInt APInt::urem(const APInt& RHS) const {
|
2007-02-17 08:18:01 +08:00
|
|
|
assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
|
2007-02-17 10:07:07 +08:00
|
|
|
if (isSingleWord()) {
|
|
|
|
assert(RHS.VAL != 0 && "Remainder by zero?");
|
|
|
|
return APInt(BitWidth, VAL % RHS.VAL);
|
|
|
|
}
|
|
|
|
|
2007-02-21 16:21:52 +08:00
|
|
|
// Get some facts about the LHS
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned lhsBits = getActiveBits();
|
|
|
|
unsigned lhsWords = !lhsBits ? 0 : (whichWord(lhsBits - 1) + 1);
|
2007-02-17 10:07:07 +08:00
|
|
|
|
|
|
|
// Get some facts about the RHS
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned rhsBits = RHS.getActiveBits();
|
|
|
|
unsigned rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
|
2007-02-17 10:07:07 +08:00
|
|
|
assert(rhsWords && "Performing remainder operation by zero ???");
|
|
|
|
|
|
|
|
// Check the degenerate cases
|
2007-02-20 16:51:03 +08:00
|
|
|
if (lhsWords == 0) {
|
2007-02-21 16:21:52 +08:00
|
|
|
// 0 % Y ===> 0
|
|
|
|
return APInt(BitWidth, 0);
|
|
|
|
} else if (lhsWords < rhsWords || this->ult(RHS)) {
|
2012-09-27 18:14:43 +08:00
|
|
|
// X % Y ===> X, iff X < Y
|
2007-02-21 16:21:52 +08:00
|
|
|
return *this;
|
|
|
|
} else if (*this == RHS) {
|
2007-02-17 10:07:07 +08:00
|
|
|
// X % X == 0;
|
2007-02-21 16:21:52 +08:00
|
|
|
return APInt(BitWidth, 0);
|
2007-02-20 16:51:03 +08:00
|
|
|
} else if (lhsWords == 1) {
|
2007-02-17 10:07:07 +08:00
|
|
|
// All high words are zero, just use native remainder
|
2007-02-21 16:21:52 +08:00
|
|
|
return APInt(BitWidth, pVal[0] % RHS.pVal[0]);
|
2007-02-08 22:35:19 +08:00
|
|
|
}
|
2007-02-20 16:51:03 +08:00
|
|
|
|
2007-05-14 07:44:59 +08:00
|
|
|
// We have to compute it the hard way. Invoke the Knuth divide algorithm.
|
2007-02-20 16:51:03 +08:00
|
|
|
APInt Remainder(1,0);
|
2014-04-07 12:17:22 +08:00
|
|
|
divide(*this, lhsWords, RHS, rhsWords, nullptr, &Remainder);
|
2007-02-20 16:51:03 +08:00
|
|
|
return Remainder;
|
2007-02-08 22:35:19 +08:00
|
|
|
}
|
2007-02-17 11:16:00 +08:00
|
|
|
|
2013-02-20 08:17:42 +08:00
|
|
|
APInt APInt::srem(const APInt &RHS) const {
|
|
|
|
if (isNegative()) {
|
|
|
|
if (RHS.isNegative())
|
|
|
|
return -((-(*this)).urem(-RHS));
|
|
|
|
return -((-(*this)).urem(RHS));
|
|
|
|
}
|
|
|
|
if (RHS.isNegative())
|
|
|
|
return this->urem(-RHS);
|
|
|
|
return this->urem(RHS);
|
|
|
|
}
|
|
|
|
|
2009-08-21 12:06:45 +08:00
|
|
|
void APInt::udivrem(const APInt &LHS, const APInt &RHS,
|
2007-05-14 07:44:59 +08:00
|
|
|
APInt &Quotient, APInt &Remainder) {
|
2014-12-14 17:41:56 +08:00
|
|
|
assert(LHS.BitWidth == RHS.BitWidth && "Bit widths must be the same");
|
|
|
|
|
|
|
|
// First, deal with the easy case
|
|
|
|
if (LHS.isSingleWord()) {
|
|
|
|
assert(RHS.VAL != 0 && "Divide by zero?");
|
|
|
|
uint64_t QuotVal = LHS.VAL / RHS.VAL;
|
|
|
|
uint64_t RemVal = LHS.VAL % RHS.VAL;
|
|
|
|
Quotient = APInt(LHS.BitWidth, QuotVal);
|
|
|
|
Remainder = APInt(LHS.BitWidth, RemVal);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-05-14 07:44:59 +08:00
|
|
|
// Get some size facts about the dividend and divisor
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned lhsBits = LHS.getActiveBits();
|
|
|
|
unsigned lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1);
|
|
|
|
unsigned rhsBits = RHS.getActiveBits();
|
|
|
|
unsigned rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
|
2007-05-14 07:44:59 +08:00
|
|
|
|
|
|
|
// Check the degenerate cases
|
2009-08-21 12:06:45 +08:00
|
|
|
if (lhsWords == 0) {
|
2007-05-14 07:44:59 +08:00
|
|
|
Quotient = 0; // 0 / Y ===> 0
|
|
|
|
Remainder = 0; // 0 % Y ===> 0
|
|
|
|
return;
|
2009-08-21 12:06:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lhsWords < rhsWords || LHS.ult(RHS)) {
|
2012-09-27 18:14:43 +08:00
|
|
|
Remainder = LHS; // X % Y ===> X, iff X < Y
|
|
|
|
Quotient = 0; // X / Y ===> 0, iff X < Y
|
2007-05-14 07:44:59 +08:00
|
|
|
return;
|
2009-08-21 12:06:45 +08:00
|
|
|
}
|
|
|
|
|
2007-05-14 07:44:59 +08:00
|
|
|
if (LHS == RHS) {
|
|
|
|
Quotient = 1; // X / X ===> 1
|
|
|
|
Remainder = 0; // X % X ===> 0;
|
|
|
|
return;
|
2009-08-21 12:06:45 +08:00
|
|
|
}
|
|
|
|
|
2007-05-14 07:44:59 +08:00
|
|
|
if (lhsWords == 1 && rhsWords == 1) {
|
|
|
|
// There is only one word to consider so use the native versions.
|
2008-06-24 03:39:50 +08:00
|
|
|
uint64_t lhsValue = LHS.isSingleWord() ? LHS.VAL : LHS.pVal[0];
|
|
|
|
uint64_t rhsValue = RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0];
|
|
|
|
Quotient = APInt(LHS.getBitWidth(), lhsValue / rhsValue);
|
|
|
|
Remainder = APInt(LHS.getBitWidth(), lhsValue % rhsValue);
|
2007-05-14 07:44:59 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Okay, lets do it the long way
|
|
|
|
divide(LHS, lhsWords, RHS, rhsWords, &Quotient, &Remainder);
|
|
|
|
}
|
|
|
|
|
2013-02-20 08:17:42 +08:00
|
|
|
void APInt::sdivrem(const APInt &LHS, const APInt &RHS,
|
|
|
|
APInt &Quotient, APInt &Remainder) {
|
|
|
|
if (LHS.isNegative()) {
|
|
|
|
if (RHS.isNegative())
|
|
|
|
APInt::udivrem(-LHS, -RHS, Quotient, Remainder);
|
|
|
|
else {
|
|
|
|
APInt::udivrem(-LHS, RHS, Quotient, Remainder);
|
|
|
|
Quotient = -Quotient;
|
|
|
|
}
|
|
|
|
Remainder = -Remainder;
|
|
|
|
} else if (RHS.isNegative()) {
|
|
|
|
APInt::udivrem(LHS, -RHS, Quotient, Remainder);
|
|
|
|
Quotient = -Quotient;
|
|
|
|
} else {
|
|
|
|
APInt::udivrem(LHS, RHS, Quotient, Remainder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-14 07:54:10 +08:00
|
|
|
APInt APInt::sadd_ov(const APInt &RHS, bool &Overflow) const {
|
2010-10-14 07:46:33 +08:00
|
|
|
APInt Res = *this+RHS;
|
|
|
|
Overflow = isNonNegative() == RHS.isNonNegative() &&
|
|
|
|
Res.isNonNegative() != isNonNegative();
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2010-10-14 08:05:07 +08:00
|
|
|
APInt APInt::uadd_ov(const APInt &RHS, bool &Overflow) const {
|
|
|
|
APInt Res = *this+RHS;
|
|
|
|
Overflow = Res.ult(RHS);
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2010-10-14 07:54:10 +08:00
|
|
|
APInt APInt::ssub_ov(const APInt &RHS, bool &Overflow) const {
|
2010-10-14 07:46:33 +08:00
|
|
|
APInt Res = *this - RHS;
|
|
|
|
Overflow = isNonNegative() != RHS.isNonNegative() &&
|
|
|
|
Res.isNonNegative() != isNonNegative();
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2010-10-14 08:05:07 +08:00
|
|
|
APInt APInt::usub_ov(const APInt &RHS, bool &Overflow) const {
|
2010-10-14 08:30:00 +08:00
|
|
|
APInt Res = *this-RHS;
|
|
|
|
Overflow = Res.ugt(*this);
|
2010-10-14 08:05:07 +08:00
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2010-10-14 07:54:10 +08:00
|
|
|
APInt APInt::sdiv_ov(const APInt &RHS, bool &Overflow) const {
|
2010-10-14 07:46:33 +08:00
|
|
|
// MININT/-1 --> overflow.
|
|
|
|
Overflow = isMinSignedValue() && RHS.isAllOnesValue();
|
|
|
|
return sdiv(RHS);
|
|
|
|
}
|
|
|
|
|
2010-10-14 07:54:10 +08:00
|
|
|
APInt APInt::smul_ov(const APInt &RHS, bool &Overflow) const {
|
2010-10-14 07:46:33 +08:00
|
|
|
APInt Res = *this * RHS;
|
|
|
|
|
|
|
|
if (*this != 0 && RHS != 0)
|
|
|
|
Overflow = Res.sdiv(RHS) != *this || Res.sdiv(*this) != RHS;
|
|
|
|
else
|
|
|
|
Overflow = false;
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2011-03-27 22:26:13 +08:00
|
|
|
APInt APInt::umul_ov(const APInt &RHS, bool &Overflow) const {
|
|
|
|
APInt Res = *this * RHS;
|
|
|
|
|
|
|
|
if (*this != 0 && RHS != 0)
|
|
|
|
Overflow = Res.udiv(RHS) != *this || Res.udiv(*this) != RHS;
|
|
|
|
else
|
|
|
|
Overflow = false;
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2014-10-14 05:48:30 +08:00
|
|
|
APInt APInt::sshl_ov(const APInt &ShAmt, bool &Overflow) const {
|
|
|
|
Overflow = ShAmt.uge(getBitWidth());
|
2010-10-14 07:46:33 +08:00
|
|
|
if (Overflow)
|
2014-10-14 05:48:30 +08:00
|
|
|
return APInt(BitWidth, 0);
|
2010-10-14 07:46:33 +08:00
|
|
|
|
|
|
|
if (isNonNegative()) // Don't allow sign change.
|
2014-10-14 05:48:30 +08:00
|
|
|
Overflow = ShAmt.uge(countLeadingZeros());
|
2010-10-14 07:46:33 +08:00
|
|
|
else
|
2014-10-14 05:48:30 +08:00
|
|
|
Overflow = ShAmt.uge(countLeadingOnes());
|
2010-10-14 07:46:33 +08:00
|
|
|
|
|
|
|
return *this << ShAmt;
|
|
|
|
}
|
|
|
|
|
2014-10-14 05:48:30 +08:00
|
|
|
APInt APInt::ushl_ov(const APInt &ShAmt, bool &Overflow) const {
|
|
|
|
Overflow = ShAmt.uge(getBitWidth());
|
|
|
|
if (Overflow)
|
|
|
|
return APInt(BitWidth, 0);
|
|
|
|
|
|
|
|
Overflow = ShAmt.ugt(countLeadingZeros());
|
|
|
|
|
|
|
|
return *this << ShAmt;
|
|
|
|
}
|
|
|
|
|
2010-10-14 07:46:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2010-07-15 06:38:02 +08:00
|
|
|
void APInt::fromString(unsigned numbits, StringRef str, uint8_t radix) {
|
2007-02-21 11:55:44 +08:00
|
|
|
// Check our assumptions here
|
2009-08-21 11:15:14 +08:00
|
|
|
assert(!str.empty() && "Invalid string length");
|
2011-09-14 23:54:46 +08:00
|
|
|
assert((radix == 10 || radix == 8 || radix == 16 || radix == 2 ||
|
|
|
|
radix == 36) &&
|
|
|
|
"Radix should be 2, 8, 10, 16, or 36!");
|
2009-08-21 11:15:14 +08:00
|
|
|
|
2009-08-13 10:33:34 +08:00
|
|
|
StringRef::iterator p = str.begin();
|
|
|
|
size_t slen = str.size();
|
|
|
|
bool isNeg = *p == '-';
|
2009-08-21 11:15:14 +08:00
|
|
|
if (*p == '-' || *p == '+') {
|
2009-08-13 10:33:34 +08:00
|
|
|
p++;
|
|
|
|
slen--;
|
2009-08-21 12:10:31 +08:00
|
|
|
assert(slen && "String is only a sign, needs a value.");
|
2009-08-13 10:33:34 +08:00
|
|
|
}
|
2007-05-04 02:15:36 +08:00
|
|
|
assert((slen <= numbits || radix != 2) && "Insufficient bit width");
|
2009-04-26 02:34:04 +08:00
|
|
|
assert(((slen-1)*3 <= numbits || radix != 8) && "Insufficient bit width");
|
|
|
|
assert(((slen-1)*4 <= numbits || radix != 16) && "Insufficient bit width");
|
2010-03-25 03:38:02 +08:00
|
|
|
assert((((slen-1)*64)/22 <= numbits || radix != 10) &&
|
|
|
|
"Insufficient bit width");
|
2007-02-21 11:55:44 +08:00
|
|
|
|
|
|
|
// Allocate memory
|
|
|
|
if (!isSingleWord())
|
|
|
|
pVal = getClearedMemory(getNumWords());
|
|
|
|
|
|
|
|
// Figure out if we can shift instead of multiply
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned shift = (radix == 16 ? 4 : radix == 8 ? 3 : radix == 2 ? 1 : 0);
|
2007-02-21 11:55:44 +08:00
|
|
|
|
|
|
|
// Set up an APInt for the digit to add outside the loop so we don't
|
|
|
|
// constantly construct/destruct it.
|
|
|
|
APInt apdigit(getBitWidth(), 0);
|
|
|
|
APInt apradix(getBitWidth(), radix);
|
|
|
|
|
|
|
|
// Enter digit traversal loop
|
2009-08-13 10:33:34 +08:00
|
|
|
for (StringRef::iterator e = str.end(); p != e; ++p) {
|
2009-08-21 11:15:28 +08:00
|
|
|
unsigned digit = getDigit(*p, radix);
|
2009-08-21 14:48:37 +08:00
|
|
|
assert(digit < radix && "Invalid character in digit string");
|
2007-02-21 11:55:44 +08:00
|
|
|
|
2007-05-17 03:18:22 +08:00
|
|
|
// Shift or multiply the value by the radix
|
2009-04-26 02:34:04 +08:00
|
|
|
if (slen > 1) {
|
|
|
|
if (shift)
|
|
|
|
*this <<= shift;
|
|
|
|
else
|
|
|
|
*this *= apradix;
|
|
|
|
}
|
2007-02-21 11:55:44 +08:00
|
|
|
|
|
|
|
// Add in the digit we just interpreted
|
2007-02-25 04:19:37 +08:00
|
|
|
if (apdigit.isSingleWord())
|
|
|
|
apdigit.VAL = digit;
|
|
|
|
else
|
|
|
|
apdigit.pVal[0] = digit;
|
2007-02-21 11:55:44 +08:00
|
|
|
*this += apdigit;
|
2007-02-17 11:16:00 +08:00
|
|
|
}
|
2007-02-26 07:44:53 +08:00
|
|
|
// If its negative, put it in two's complement form
|
2007-02-26 15:44:38 +08:00
|
|
|
if (isNeg) {
|
2013-03-21 07:56:19 +08:00
|
|
|
--(*this);
|
2010-12-01 16:53:58 +08:00
|
|
|
this->flipAllBits();
|
2007-02-26 07:44:53 +08:00
|
|
|
}
|
2007-02-17 11:16:00 +08:00
|
|
|
}
|
2007-02-20 16:51:03 +08:00
|
|
|
|
2008-08-17 15:19:36 +08:00
|
|
|
void APInt::toString(SmallVectorImpl<char> &Str, unsigned Radix,
|
2011-06-15 08:51:55 +08:00
|
|
|
bool Signed, bool formatAsCLiteral) const {
|
2011-09-14 23:54:46 +08:00
|
|
|
assert((Radix == 10 || Radix == 8 || Radix == 16 || Radix == 2 ||
|
|
|
|
Radix == 36) &&
|
2011-12-17 04:36:31 +08:00
|
|
|
"Radix should be 2, 8, 10, 16, or 36!");
|
2009-08-21 12:06:45 +08:00
|
|
|
|
2011-06-15 08:51:55 +08:00
|
|
|
const char *Prefix = "";
|
|
|
|
if (formatAsCLiteral) {
|
|
|
|
switch (Radix) {
|
|
|
|
case 2:
|
|
|
|
// Binary literals are a non-standard extension added in gcc 4.3:
|
|
|
|
// http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Binary-constants.html
|
|
|
|
Prefix = "0b";
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
Prefix = "0";
|
|
|
|
break;
|
2011-12-17 04:36:31 +08:00
|
|
|
case 10:
|
|
|
|
break; // No prefix
|
2011-06-15 08:51:55 +08:00
|
|
|
case 16:
|
|
|
|
Prefix = "0x";
|
|
|
|
break;
|
2011-12-17 04:36:31 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("Invalid radix!");
|
2011-06-15 08:51:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-17 15:19:36 +08:00
|
|
|
// First, check for a zero value and just short circuit the logic below.
|
|
|
|
if (*this == 0) {
|
2011-06-15 08:51:55 +08:00
|
|
|
while (*Prefix) {
|
|
|
|
Str.push_back(*Prefix);
|
|
|
|
++Prefix;
|
|
|
|
};
|
2008-08-17 15:19:36 +08:00
|
|
|
Str.push_back('0');
|
|
|
|
return;
|
|
|
|
}
|
2009-08-21 12:06:45 +08:00
|
|
|
|
2011-09-14 23:54:46 +08:00
|
|
|
static const char Digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
2009-08-21 12:06:45 +08:00
|
|
|
|
2007-02-20 16:51:03 +08:00
|
|
|
if (isSingleWord()) {
|
2008-08-17 15:19:36 +08:00
|
|
|
char Buffer[65];
|
|
|
|
char *BufPtr = Buffer+65;
|
2009-08-21 12:06:45 +08:00
|
|
|
|
2008-08-17 15:19:36 +08:00
|
|
|
uint64_t N;
|
2010-08-18 08:33:47 +08:00
|
|
|
if (!Signed) {
|
|
|
|
N = getZExtValue();
|
|
|
|
} else {
|
2008-08-17 15:19:36 +08:00
|
|
|
int64_t I = getSExtValue();
|
2010-08-18 08:33:47 +08:00
|
|
|
if (I >= 0) {
|
|
|
|
N = I;
|
|
|
|
} else {
|
2008-08-17 15:19:36 +08:00
|
|
|
Str.push_back('-');
|
2010-08-18 08:33:47 +08:00
|
|
|
N = -(uint64_t)I;
|
2007-02-20 16:51:03 +08:00
|
|
|
}
|
|
|
|
}
|
2009-08-21 12:06:45 +08:00
|
|
|
|
2011-06-15 08:51:55 +08:00
|
|
|
while (*Prefix) {
|
|
|
|
Str.push_back(*Prefix);
|
|
|
|
++Prefix;
|
|
|
|
};
|
|
|
|
|
2008-08-17 15:19:36 +08:00
|
|
|
while (N) {
|
|
|
|
*--BufPtr = Digits[N % Radix];
|
|
|
|
N /= Radix;
|
2007-02-20 16:51:03 +08:00
|
|
|
}
|
2008-08-17 15:19:36 +08:00
|
|
|
Str.append(BufPtr, Buffer+65);
|
|
|
|
return;
|
2007-02-20 16:51:03 +08:00
|
|
|
}
|
|
|
|
|
2008-08-17 15:19:36 +08:00
|
|
|
APInt Tmp(*this);
|
2009-08-21 12:06:45 +08:00
|
|
|
|
2008-08-17 15:19:36 +08:00
|
|
|
if (Signed && isNegative()) {
|
2007-02-20 16:51:03 +08:00
|
|
|
// They want to print the signed version and it is a negative value
|
|
|
|
// Flip the bits and add one to turn it into the equivalent positive
|
|
|
|
// value and put a '-' in the result.
|
2010-12-01 16:53:58 +08:00
|
|
|
Tmp.flipAllBits();
|
2013-03-21 07:56:19 +08:00
|
|
|
++Tmp;
|
2008-08-17 15:19:36 +08:00
|
|
|
Str.push_back('-');
|
2007-02-20 16:51:03 +08:00
|
|
|
}
|
2009-08-21 12:06:45 +08:00
|
|
|
|
2011-06-15 08:51:55 +08:00
|
|
|
while (*Prefix) {
|
|
|
|
Str.push_back(*Prefix);
|
|
|
|
++Prefix;
|
|
|
|
};
|
|
|
|
|
2008-08-17 15:19:36 +08:00
|
|
|
// We insert the digits backward, then reverse them to get the right order.
|
|
|
|
unsigned StartDig = Str.size();
|
2009-08-21 12:06:45 +08:00
|
|
|
|
|
|
|
// For the 2, 8 and 16 bit cases, we can just shift instead of divide
|
|
|
|
// because the number of bits per digit (1, 3 and 4 respectively) divides
|
2008-08-17 15:19:36 +08:00
|
|
|
// equaly. We just shift until the value is zero.
|
2011-09-14 23:54:46 +08:00
|
|
|
if (Radix == 2 || Radix == 8 || Radix == 16) {
|
2008-08-17 15:19:36 +08:00
|
|
|
// Just shift tmp right for each digit width until it becomes zero
|
|
|
|
unsigned ShiftAmt = (Radix == 16 ? 4 : (Radix == 8 ? 3 : 1));
|
|
|
|
unsigned MaskAmt = Radix - 1;
|
2009-08-21 12:06:45 +08:00
|
|
|
|
2008-08-17 15:19:36 +08:00
|
|
|
while (Tmp != 0) {
|
|
|
|
unsigned Digit = unsigned(Tmp.getRawData()[0]) & MaskAmt;
|
|
|
|
Str.push_back(Digits[Digit]);
|
|
|
|
Tmp = Tmp.lshr(ShiftAmt);
|
|
|
|
}
|
|
|
|
} else {
|
2011-09-14 23:54:46 +08:00
|
|
|
APInt divisor(Radix == 10? 4 : 8, Radix);
|
2008-08-17 15:19:36 +08:00
|
|
|
while (Tmp != 0) {
|
|
|
|
APInt APdigit(1, 0);
|
|
|
|
APInt tmp2(Tmp.getBitWidth(), 0);
|
2009-08-21 12:06:45 +08:00
|
|
|
divide(Tmp, Tmp.getNumWords(), divisor, divisor.getNumWords(), &tmp2,
|
2008-08-17 15:19:36 +08:00
|
|
|
&APdigit);
|
2009-01-22 02:09:24 +08:00
|
|
|
unsigned Digit = (unsigned)APdigit.getZExtValue();
|
2008-08-17 15:19:36 +08:00
|
|
|
assert(Digit < Radix && "divide failed");
|
|
|
|
Str.push_back(Digits[Digit]);
|
|
|
|
Tmp = tmp2;
|
|
|
|
}
|
|
|
|
}
|
2009-08-21 12:06:45 +08:00
|
|
|
|
2008-08-17 15:19:36 +08:00
|
|
|
// Reverse the digits before returning.
|
|
|
|
std::reverse(Str.begin()+StartDig, Str.end());
|
|
|
|
}
|
2007-02-20 16:51:03 +08:00
|
|
|
|
2015-04-06 21:31:39 +08:00
|
|
|
/// Returns the APInt as a std::string. Note that this is an inefficient method.
|
|
|
|
/// It is better to pass in a SmallVector/SmallString to the methods above.
|
2008-08-17 15:19:36 +08:00
|
|
|
std::string APInt::toString(unsigned Radix = 10, bool Signed = true) const {
|
|
|
|
SmallString<40> S;
|
2011-06-15 08:51:55 +08:00
|
|
|
toString(S, Radix, Signed, /* formatAsCLiteral = */false);
|
2009-08-20 04:07:03 +08:00
|
|
|
return S.str();
|
2007-02-20 16:51:03 +08:00
|
|
|
}
|
|
|
|
|
2008-08-17 15:19:36 +08:00
|
|
|
|
2016-01-30 04:50:44 +08:00
|
|
|
LLVM_DUMP_METHOD void APInt::dump() const {
|
2008-08-17 15:19:36 +08:00
|
|
|
SmallString<40> S, U;
|
|
|
|
this->toStringUnsigned(U);
|
|
|
|
this->toStringSigned(S);
|
2010-01-05 09:28:52 +08:00
|
|
|
dbgs() << "APInt(" << BitWidth << "b, "
|
2015-03-10 15:33:23 +08:00
|
|
|
<< U << "u " << S << "s)";
|
2008-08-17 15:19:36 +08:00
|
|
|
}
|
|
|
|
|
2008-08-24 06:23:09 +08:00
|
|
|
void APInt::print(raw_ostream &OS, bool isSigned) const {
|
2008-08-17 15:19:36 +08:00
|
|
|
SmallString<40> S;
|
2011-06-15 08:51:55 +08:00
|
|
|
this->toString(S, 10, isSigned, /* formatAsCLiteral = */false);
|
2015-03-18 18:17:07 +08:00
|
|
|
OS << S;
|
2007-02-21 11:55:44 +08:00
|
|
|
}
|
2007-08-16 23:56:55 +08:00
|
|
|
|
|
|
|
// This implements a variety of operations on a representation of
|
|
|
|
// arbitrary precision, two's-complement, bignum integer values.
|
|
|
|
|
2009-08-24 07:11:28 +08:00
|
|
|
// Assumed by lowHalf, highHalf, partMSB and partLSB. A fairly safe
|
|
|
|
// and unrestricting assumption.
|
2014-10-13 01:56:40 +08:00
|
|
|
static_assert(integerPartWidth % 2 == 0, "Part width must be divisible by 2!");
|
2007-08-16 23:56:55 +08:00
|
|
|
|
|
|
|
/* Some handy functions local to this file. */
|
|
|
|
namespace {
|
|
|
|
|
2007-08-21 06:49:32 +08:00
|
|
|
/* Returns the integer part with the least significant BITS set.
|
|
|
|
BITS cannot be zero. */
|
2008-04-11 05:11:47 +08:00
|
|
|
static inline integerPart
|
2007-08-21 06:49:32 +08:00
|
|
|
lowBitMask(unsigned int bits)
|
|
|
|
{
|
2010-03-25 03:38:02 +08:00
|
|
|
assert(bits != 0 && bits <= integerPartWidth);
|
2007-08-21 06:49:32 +08:00
|
|
|
|
|
|
|
return ~(integerPart) 0 >> (integerPartWidth - bits);
|
|
|
|
}
|
|
|
|
|
2007-10-06 08:43:45 +08:00
|
|
|
/* Returns the value of the lower half of PART. */
|
2008-04-11 05:11:47 +08:00
|
|
|
static inline integerPart
|
2007-08-21 06:49:32 +08:00
|
|
|
lowHalf(integerPart part)
|
|
|
|
{
|
|
|
|
return part & lowBitMask(integerPartWidth / 2);
|
|
|
|
}
|
|
|
|
|
2007-10-06 08:43:45 +08:00
|
|
|
/* Returns the value of the upper half of PART. */
|
2008-04-11 05:11:47 +08:00
|
|
|
static inline integerPart
|
2007-08-21 06:49:32 +08:00
|
|
|
highHalf(integerPart part)
|
|
|
|
{
|
|
|
|
return part >> (integerPartWidth / 2);
|
|
|
|
}
|
|
|
|
|
2007-10-06 08:43:45 +08:00
|
|
|
/* Returns the bit number of the most significant set bit of a part.
|
|
|
|
If the input number has no bits set -1U is returned. */
|
2008-04-11 05:11:47 +08:00
|
|
|
static unsigned int
|
2007-08-21 06:49:32 +08:00
|
|
|
partMSB(integerPart value)
|
2007-08-16 23:56:55 +08:00
|
|
|
{
|
2013-06-01 19:26:39 +08:00
|
|
|
return findLastSet(value, ZB_Max);
|
2007-08-16 23:56:55 +08:00
|
|
|
}
|
|
|
|
|
2007-10-06 08:43:45 +08:00
|
|
|
/* Returns the bit number of the least significant set bit of a
|
|
|
|
part. If the input number has no bits set -1U is returned. */
|
2008-04-11 05:11:47 +08:00
|
|
|
static unsigned int
|
2007-08-16 23:56:55 +08:00
|
|
|
partLSB(integerPart value)
|
|
|
|
{
|
2013-06-01 19:26:39 +08:00
|
|
|
return findFirstSet(value, ZB_Max);
|
2007-08-16 23:56:55 +08:00
|
|
|
}
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2007-08-16 23:56:55 +08:00
|
|
|
|
|
|
|
/* Sets the least significant part of a bignum to the input value, and
|
|
|
|
zeroes out higher parts. */
|
|
|
|
void
|
|
|
|
APInt::tcSet(integerPart *dst, integerPart part, unsigned int parts)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
2010-03-25 03:38:02 +08:00
|
|
|
assert(parts > 0);
|
2007-10-08 21:47:12 +08:00
|
|
|
|
2007-08-16 23:56:55 +08:00
|
|
|
dst[0] = part;
|
2010-03-25 03:38:02 +08:00
|
|
|
for (i = 1; i < parts; i++)
|
2007-08-16 23:56:55 +08:00
|
|
|
dst[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Assign one bignum to another. */
|
|
|
|
void
|
|
|
|
APInt::tcAssign(integerPart *dst, const integerPart *src, unsigned int parts)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
2010-03-25 03:38:02 +08:00
|
|
|
for (i = 0; i < parts; i++)
|
2007-08-16 23:56:55 +08:00
|
|
|
dst[i] = src[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns true if a bignum is zero, false otherwise. */
|
|
|
|
bool
|
|
|
|
APInt::tcIsZero(const integerPart *src, unsigned int parts)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
2010-03-25 03:38:02 +08:00
|
|
|
for (i = 0; i < parts; i++)
|
2007-08-16 23:56:55 +08:00
|
|
|
if (src[i])
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extract the given bit of a bignum; returns 0 or 1. */
|
|
|
|
int
|
|
|
|
APInt::tcExtractBit(const integerPart *parts, unsigned int bit)
|
|
|
|
{
|
2010-03-25 03:38:02 +08:00
|
|
|
return (parts[bit / integerPartWidth] &
|
|
|
|
((integerPart) 1 << bit % integerPartWidth)) != 0;
|
2007-08-16 23:56:55 +08:00
|
|
|
}
|
|
|
|
|
2010-02-28 10:51:25 +08:00
|
|
|
/* Set the given bit of a bignum. */
|
2007-08-16 23:56:55 +08:00
|
|
|
void
|
|
|
|
APInt::tcSetBit(integerPart *parts, unsigned int bit)
|
|
|
|
{
|
|
|
|
parts[bit / integerPartWidth] |= (integerPart) 1 << (bit % integerPartWidth);
|
|
|
|
}
|
|
|
|
|
2010-02-28 10:51:25 +08:00
|
|
|
/* Clears the given bit of a bignum. */
|
|
|
|
void
|
|
|
|
APInt::tcClearBit(integerPart *parts, unsigned int bit)
|
|
|
|
{
|
|
|
|
parts[bit / integerPartWidth] &=
|
|
|
|
~((integerPart) 1 << (bit % integerPartWidth));
|
|
|
|
}
|
|
|
|
|
2007-10-06 08:43:45 +08:00
|
|
|
/* Returns the bit number of the least significant set bit of a
|
|
|
|
number. If the input number has no bits set -1U is returned. */
|
2007-08-16 23:56:55 +08:00
|
|
|
unsigned int
|
|
|
|
APInt::tcLSB(const integerPart *parts, unsigned int n)
|
|
|
|
{
|
|
|
|
unsigned int i, lsb;
|
|
|
|
|
2010-03-25 03:38:02 +08:00
|
|
|
for (i = 0; i < n; i++) {
|
2007-08-16 23:56:55 +08:00
|
|
|
if (parts[i] != 0) {
|
|
|
|
lsb = partLSB(parts[i]);
|
|
|
|
|
|
|
|
return lsb + i * integerPartWidth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1U;
|
|
|
|
}
|
|
|
|
|
2007-10-06 08:43:45 +08:00
|
|
|
/* Returns the bit number of the most significant set bit of a number.
|
|
|
|
If the input number has no bits set -1U is returned. */
|
2007-08-16 23:56:55 +08:00
|
|
|
unsigned int
|
|
|
|
APInt::tcMSB(const integerPart *parts, unsigned int n)
|
|
|
|
{
|
|
|
|
unsigned int msb;
|
|
|
|
|
|
|
|
do {
|
2010-03-25 03:38:02 +08:00
|
|
|
--n;
|
2007-08-16 23:56:55 +08:00
|
|
|
|
2010-03-25 03:38:02 +08:00
|
|
|
if (parts[n] != 0) {
|
|
|
|
msb = partMSB(parts[n]);
|
2007-08-16 23:56:55 +08:00
|
|
|
|
2010-03-25 03:38:02 +08:00
|
|
|
return msb + n * integerPartWidth;
|
|
|
|
}
|
2007-08-16 23:56:55 +08:00
|
|
|
} while (n);
|
|
|
|
|
|
|
|
return -1U;
|
|
|
|
}
|
|
|
|
|
2007-10-08 21:47:12 +08:00
|
|
|
/* Copy the bit vector of width srcBITS from SRC, starting at bit
|
|
|
|
srcLSB, to DST, of dstCOUNT parts, such that the bit srcLSB becomes
|
|
|
|
the least significant bit of DST. All high bits above srcBITS in
|
|
|
|
DST are zero-filled. */
|
|
|
|
void
|
2009-05-22 07:47:47 +08:00
|
|
|
APInt::tcExtract(integerPart *dst, unsigned int dstCount,const integerPart *src,
|
2007-10-08 21:47:12 +08:00
|
|
|
unsigned int srcBits, unsigned int srcLSB)
|
|
|
|
{
|
|
|
|
unsigned int firstSrcPart, dstParts, shift, n;
|
|
|
|
|
|
|
|
dstParts = (srcBits + integerPartWidth - 1) / integerPartWidth;
|
2010-03-25 03:38:02 +08:00
|
|
|
assert(dstParts <= dstCount);
|
2007-10-08 21:47:12 +08:00
|
|
|
|
|
|
|
firstSrcPart = srcLSB / integerPartWidth;
|
|
|
|
tcAssign (dst, src + firstSrcPart, dstParts);
|
|
|
|
|
|
|
|
shift = srcLSB % integerPartWidth;
|
|
|
|
tcShiftRight (dst, dstParts, shift);
|
|
|
|
|
|
|
|
/* We now have (dstParts * integerPartWidth - shift) bits from SRC
|
|
|
|
in DST. If this is less that srcBits, append the rest, else
|
|
|
|
clear the high bits. */
|
|
|
|
n = dstParts * integerPartWidth - shift;
|
|
|
|
if (n < srcBits) {
|
|
|
|
integerPart mask = lowBitMask (srcBits - n);
|
|
|
|
dst[dstParts - 1] |= ((src[firstSrcPart + dstParts] & mask)
|
|
|
|
<< n % integerPartWidth);
|
|
|
|
} else if (n > srcBits) {
|
2007-10-12 23:31:31 +08:00
|
|
|
if (srcBits % integerPartWidth)
|
|
|
|
dst[dstParts - 1] &= lowBitMask (srcBits % integerPartWidth);
|
2007-10-08 21:47:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear high parts. */
|
|
|
|
while (dstParts < dstCount)
|
|
|
|
dst[dstParts++] = 0;
|
|
|
|
}
|
|
|
|
|
2007-08-16 23:56:55 +08:00
|
|
|
/* DST += RHS + C where C is zero or one. Returns the carry flag. */
|
|
|
|
integerPart
|
|
|
|
APInt::tcAdd(integerPart *dst, const integerPart *rhs,
|
|
|
|
integerPart c, unsigned int parts)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
assert(c <= 1);
|
|
|
|
|
2010-03-25 03:38:02 +08:00
|
|
|
for (i = 0; i < parts; i++) {
|
2007-08-16 23:56:55 +08:00
|
|
|
integerPart l;
|
|
|
|
|
|
|
|
l = dst[i];
|
|
|
|
if (c) {
|
|
|
|
dst[i] += rhs[i] + 1;
|
|
|
|
c = (dst[i] <= l);
|
|
|
|
} else {
|
|
|
|
dst[i] += rhs[i];
|
|
|
|
c = (dst[i] < l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* DST -= RHS + C where C is zero or one. Returns the carry flag. */
|
|
|
|
integerPart
|
|
|
|
APInt::tcSubtract(integerPart *dst, const integerPart *rhs,
|
|
|
|
integerPart c, unsigned int parts)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
assert(c <= 1);
|
|
|
|
|
2010-03-25 03:38:02 +08:00
|
|
|
for (i = 0; i < parts; i++) {
|
2007-08-16 23:56:55 +08:00
|
|
|
integerPart l;
|
|
|
|
|
|
|
|
l = dst[i];
|
|
|
|
if (c) {
|
|
|
|
dst[i] -= rhs[i] + 1;
|
|
|
|
c = (dst[i] >= l);
|
|
|
|
} else {
|
|
|
|
dst[i] -= rhs[i];
|
|
|
|
c = (dst[i] > l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Negate a bignum in-place. */
|
|
|
|
void
|
|
|
|
APInt::tcNegate(integerPart *dst, unsigned int parts)
|
|
|
|
{
|
|
|
|
tcComplement(dst, parts);
|
|
|
|
tcIncrement(dst, parts);
|
|
|
|
}
|
|
|
|
|
2007-10-06 08:43:45 +08:00
|
|
|
/* DST += SRC * MULTIPLIER + CARRY if add is true
|
|
|
|
DST = SRC * MULTIPLIER + CARRY if add is false
|
2007-08-16 23:56:55 +08:00
|
|
|
|
|
|
|
Requires 0 <= DSTPARTS <= SRCPARTS + 1. If DST overlaps SRC
|
|
|
|
they must start at the same point, i.e. DST == SRC.
|
|
|
|
|
|
|
|
If DSTPARTS == SRCPARTS + 1 no overflow occurs and zero is
|
|
|
|
returned. Otherwise DST is filled with the least significant
|
|
|
|
DSTPARTS parts of the result, and if all of the omitted higher
|
|
|
|
parts were zero return zero, otherwise overflow occurred and
|
|
|
|
return one. */
|
|
|
|
int
|
|
|
|
APInt::tcMultiplyPart(integerPart *dst, const integerPart *src,
|
|
|
|
integerPart multiplier, integerPart carry,
|
|
|
|
unsigned int srcParts, unsigned int dstParts,
|
|
|
|
bool add)
|
|
|
|
{
|
|
|
|
unsigned int i, n;
|
|
|
|
|
|
|
|
/* Otherwise our writes of DST kill our later reads of SRC. */
|
|
|
|
assert(dst <= src || dst >= src + srcParts);
|
|
|
|
assert(dstParts <= srcParts + 1);
|
|
|
|
|
|
|
|
/* N loops; minimum of dstParts and srcParts. */
|
|
|
|
n = dstParts < srcParts ? dstParts: srcParts;
|
|
|
|
|
2010-03-25 03:38:02 +08:00
|
|
|
for (i = 0; i < n; i++) {
|
2007-08-16 23:56:55 +08:00
|
|
|
integerPart low, mid, high, srcPart;
|
|
|
|
|
|
|
|
/* [ LOW, HIGH ] = MULTIPLIER * SRC[i] + DST[i] + CARRY.
|
|
|
|
|
|
|
|
This cannot overflow, because
|
|
|
|
|
|
|
|
(n - 1) * (n - 1) + 2 (n - 1) = (n - 1) * (n + 1)
|
|
|
|
|
|
|
|
which is less than n^2. */
|
|
|
|
|
|
|
|
srcPart = src[i];
|
|
|
|
|
|
|
|
if (multiplier == 0 || srcPart == 0) {
|
|
|
|
low = carry;
|
|
|
|
high = 0;
|
|
|
|
} else {
|
|
|
|
low = lowHalf(srcPart) * lowHalf(multiplier);
|
|
|
|
high = highHalf(srcPart) * highHalf(multiplier);
|
|
|
|
|
|
|
|
mid = lowHalf(srcPart) * highHalf(multiplier);
|
|
|
|
high += highHalf(mid);
|
|
|
|
mid <<= integerPartWidth / 2;
|
|
|
|
if (low + mid < low)
|
|
|
|
high++;
|
|
|
|
low += mid;
|
|
|
|
|
|
|
|
mid = highHalf(srcPart) * lowHalf(multiplier);
|
|
|
|
high += highHalf(mid);
|
|
|
|
mid <<= integerPartWidth / 2;
|
|
|
|
if (low + mid < low)
|
|
|
|
high++;
|
|
|
|
low += mid;
|
|
|
|
|
|
|
|
/* Now add carry. */
|
|
|
|
if (low + carry < low)
|
|
|
|
high++;
|
|
|
|
low += carry;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (add) {
|
|
|
|
/* And now DST[i], and store the new low part there. */
|
|
|
|
if (low + dst[i] < low)
|
|
|
|
high++;
|
|
|
|
dst[i] += low;
|
|
|
|
} else
|
|
|
|
dst[i] = low;
|
|
|
|
|
|
|
|
carry = high;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i < dstParts) {
|
|
|
|
/* Full multiplication, there is no overflow. */
|
|
|
|
assert(i + 1 == dstParts);
|
|
|
|
dst[i] = carry;
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
/* We overflowed if there is carry. */
|
|
|
|
if (carry)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* We would overflow if any significant unwritten parts would be
|
|
|
|
non-zero. This is true if any remaining src parts are non-zero
|
|
|
|
and the multiplier is non-zero. */
|
|
|
|
if (multiplier)
|
2010-03-25 03:38:02 +08:00
|
|
|
for (; i < srcParts; i++)
|
2007-08-16 23:56:55 +08:00
|
|
|
if (src[i])
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* We fitted in the narrow destination. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* DST = LHS * RHS, where DST has the same width as the operands and
|
|
|
|
is filled with the least significant parts of the result. Returns
|
|
|
|
one if overflow occurred, otherwise zero. DST must be disjoint
|
|
|
|
from both operands. */
|
|
|
|
int
|
|
|
|
APInt::tcMultiply(integerPart *dst, const integerPart *lhs,
|
|
|
|
const integerPart *rhs, unsigned int parts)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
int overflow;
|
|
|
|
|
|
|
|
assert(dst != lhs && dst != rhs);
|
|
|
|
|
|
|
|
overflow = 0;
|
|
|
|
tcSet(dst, 0, parts);
|
|
|
|
|
2010-03-25 03:38:02 +08:00
|
|
|
for (i = 0; i < parts; i++)
|
2007-08-16 23:56:55 +08:00
|
|
|
overflow |= tcMultiplyPart(&dst[i], lhs, rhs[i], 0, parts,
|
|
|
|
parts - i, true);
|
|
|
|
|
|
|
|
return overflow;
|
|
|
|
}
|
|
|
|
|
2007-10-06 08:24:48 +08:00
|
|
|
/* DST = LHS * RHS, where DST has width the sum of the widths of the
|
|
|
|
operands. No overflow occurs. DST must be disjoint from both
|
|
|
|
operands. Returns the number of parts required to hold the
|
|
|
|
result. */
|
|
|
|
unsigned int
|
2007-08-16 23:56:55 +08:00
|
|
|
APInt::tcFullMultiply(integerPart *dst, const integerPart *lhs,
|
2007-10-06 08:24:48 +08:00
|
|
|
const integerPart *rhs, unsigned int lhsParts,
|
|
|
|
unsigned int rhsParts)
|
2007-08-16 23:56:55 +08:00
|
|
|
{
|
2007-10-06 08:24:48 +08:00
|
|
|
/* Put the narrower number on the LHS for less loops below. */
|
|
|
|
if (lhsParts > rhsParts) {
|
|
|
|
return tcFullMultiply (dst, rhs, lhs, rhsParts, lhsParts);
|
|
|
|
} else {
|
|
|
|
unsigned int n;
|
2007-08-16 23:56:55 +08:00
|
|
|
|
2007-10-06 08:24:48 +08:00
|
|
|
assert(dst != lhs && dst != rhs);
|
2007-08-16 23:56:55 +08:00
|
|
|
|
2007-10-06 08:24:48 +08:00
|
|
|
tcSet(dst, 0, rhsParts);
|
2007-08-16 23:56:55 +08:00
|
|
|
|
2010-03-25 03:38:02 +08:00
|
|
|
for (n = 0; n < lhsParts; n++)
|
2007-10-06 08:24:48 +08:00
|
|
|
tcMultiplyPart(&dst[n], rhs, lhs[n], 0, rhsParts, rhsParts + 1, true);
|
|
|
|
|
|
|
|
n = lhsParts + rhsParts;
|
2007-08-16 23:56:55 +08:00
|
|
|
|
2007-10-06 08:24:48 +08:00
|
|
|
return n - (dst[n - 1] == 0);
|
|
|
|
}
|
2007-08-16 23:56:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If RHS is zero LHS and REMAINDER are left unchanged, return one.
|
|
|
|
Otherwise set LHS to LHS / RHS with the fractional part discarded,
|
|
|
|
set REMAINDER to the remainder, return zero. i.e.
|
|
|
|
|
|
|
|
OLD_LHS = RHS * LHS + REMAINDER
|
|
|
|
|
|
|
|
SCRATCH is a bignum of the same size as the operands and result for
|
|
|
|
use by the routine; its contents need not be initialized and are
|
|
|
|
destroyed. LHS, REMAINDER and SCRATCH must be distinct.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
APInt::tcDivide(integerPart *lhs, const integerPart *rhs,
|
|
|
|
integerPart *remainder, integerPart *srhs,
|
|
|
|
unsigned int parts)
|
|
|
|
{
|
|
|
|
unsigned int n, shiftCount;
|
|
|
|
integerPart mask;
|
|
|
|
|
|
|
|
assert(lhs != remainder && lhs != srhs && remainder != srhs);
|
|
|
|
|
2007-08-21 06:49:32 +08:00
|
|
|
shiftCount = tcMSB(rhs, parts) + 1;
|
|
|
|
if (shiftCount == 0)
|
2007-08-16 23:56:55 +08:00
|
|
|
return true;
|
|
|
|
|
2007-08-21 06:49:32 +08:00
|
|
|
shiftCount = parts * integerPartWidth - shiftCount;
|
2007-08-16 23:56:55 +08:00
|
|
|
n = shiftCount / integerPartWidth;
|
|
|
|
mask = (integerPart) 1 << (shiftCount % integerPartWidth);
|
|
|
|
|
|
|
|
tcAssign(srhs, rhs, parts);
|
|
|
|
tcShiftLeft(srhs, parts, shiftCount);
|
|
|
|
tcAssign(remainder, lhs, parts);
|
|
|
|
tcSet(lhs, 0, parts);
|
|
|
|
|
|
|
|
/* Loop, subtracting SRHS if REMAINDER is greater and adding that to
|
|
|
|
the total. */
|
2010-03-25 03:38:02 +08:00
|
|
|
for (;;) {
|
2007-08-16 23:56:55 +08:00
|
|
|
int compare;
|
|
|
|
|
|
|
|
compare = tcCompare(remainder, srhs, parts);
|
|
|
|
if (compare >= 0) {
|
|
|
|
tcSubtract(remainder, srhs, 0, parts);
|
|
|
|
lhs[n] |= mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shiftCount == 0)
|
|
|
|
break;
|
|
|
|
shiftCount--;
|
|
|
|
tcShiftRight(srhs, parts, 1);
|
2016-02-19 06:09:30 +08:00
|
|
|
if ((mask >>= 1) == 0) {
|
|
|
|
mask = (integerPart) 1 << (integerPartWidth - 1);
|
|
|
|
n--;
|
|
|
|
}
|
2007-08-16 23:56:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Shift a bignum left COUNT bits in-place. Shifted in bits are zero.
|
|
|
|
There are no restrictions on COUNT. */
|
|
|
|
void
|
|
|
|
APInt::tcShiftLeft(integerPart *dst, unsigned int parts, unsigned int count)
|
|
|
|
{
|
2007-10-08 21:47:12 +08:00
|
|
|
if (count) {
|
|
|
|
unsigned int jump, shift;
|
2007-08-16 23:56:55 +08:00
|
|
|
|
2007-10-08 21:47:12 +08:00
|
|
|
/* Jump is the inter-part jump; shift is is intra-part shift. */
|
|
|
|
jump = count / integerPartWidth;
|
|
|
|
shift = count % integerPartWidth;
|
2007-08-16 23:56:55 +08:00
|
|
|
|
2007-10-08 21:47:12 +08:00
|
|
|
while (parts > jump) {
|
|
|
|
integerPart part;
|
2007-08-16 23:56:55 +08:00
|
|
|
|
2007-10-08 21:47:12 +08:00
|
|
|
parts--;
|
2007-08-16 23:56:55 +08:00
|
|
|
|
2007-10-08 21:47:12 +08:00
|
|
|
/* dst[i] comes from the two parts src[i - jump] and, if we have
|
|
|
|
an intra-part shift, src[i - jump - 1]. */
|
|
|
|
part = dst[parts - jump];
|
|
|
|
if (shift) {
|
|
|
|
part <<= shift;
|
2007-08-16 23:56:55 +08:00
|
|
|
if (parts >= jump + 1)
|
|
|
|
part |= dst[parts - jump - 1] >> (integerPartWidth - shift);
|
|
|
|
}
|
|
|
|
|
2007-10-08 21:47:12 +08:00
|
|
|
dst[parts] = part;
|
|
|
|
}
|
2007-08-16 23:56:55 +08:00
|
|
|
|
2007-10-08 21:47:12 +08:00
|
|
|
while (parts > 0)
|
|
|
|
dst[--parts] = 0;
|
|
|
|
}
|
2007-08-16 23:56:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Shift a bignum right COUNT bits in-place. Shifted in bits are
|
|
|
|
zero. There are no restrictions on COUNT. */
|
|
|
|
void
|
|
|
|
APInt::tcShiftRight(integerPart *dst, unsigned int parts, unsigned int count)
|
|
|
|
{
|
2007-10-08 21:47:12 +08:00
|
|
|
if (count) {
|
|
|
|
unsigned int i, jump, shift;
|
2007-08-16 23:56:55 +08:00
|
|
|
|
2007-10-08 21:47:12 +08:00
|
|
|
/* Jump is the inter-part jump; shift is is intra-part shift. */
|
|
|
|
jump = count / integerPartWidth;
|
|
|
|
shift = count % integerPartWidth;
|
2007-08-16 23:56:55 +08:00
|
|
|
|
2007-10-08 21:47:12 +08:00
|
|
|
/* Perform the shift. This leaves the most significant COUNT bits
|
|
|
|
of the result at zero. */
|
2010-03-25 03:38:02 +08:00
|
|
|
for (i = 0; i < parts; i++) {
|
2007-10-08 21:47:12 +08:00
|
|
|
integerPart part;
|
2007-08-16 23:56:55 +08:00
|
|
|
|
2007-10-08 21:47:12 +08:00
|
|
|
if (i + jump >= parts) {
|
|
|
|
part = 0;
|
|
|
|
} else {
|
|
|
|
part = dst[i + jump];
|
|
|
|
if (shift) {
|
|
|
|
part >>= shift;
|
|
|
|
if (i + jump + 1 < parts)
|
|
|
|
part |= dst[i + jump + 1] << (integerPartWidth - shift);
|
|
|
|
}
|
2007-08-16 23:56:55 +08:00
|
|
|
}
|
|
|
|
|
2007-10-08 21:47:12 +08:00
|
|
|
dst[i] = part;
|
|
|
|
}
|
2007-08-16 23:56:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Bitwise and of two bignums. */
|
|
|
|
void
|
|
|
|
APInt::tcAnd(integerPart *dst, const integerPart *rhs, unsigned int parts)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
2010-03-25 03:38:02 +08:00
|
|
|
for (i = 0; i < parts; i++)
|
2007-08-16 23:56:55 +08:00
|
|
|
dst[i] &= rhs[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Bitwise inclusive or of two bignums. */
|
|
|
|
void
|
|
|
|
APInt::tcOr(integerPart *dst, const integerPart *rhs, unsigned int parts)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
2010-03-25 03:38:02 +08:00
|
|
|
for (i = 0; i < parts; i++)
|
2007-08-16 23:56:55 +08:00
|
|
|
dst[i] |= rhs[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Bitwise exclusive or of two bignums. */
|
|
|
|
void
|
|
|
|
APInt::tcXor(integerPart *dst, const integerPart *rhs, unsigned int parts)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
2010-03-25 03:38:02 +08:00
|
|
|
for (i = 0; i < parts; i++)
|
2007-08-16 23:56:55 +08:00
|
|
|
dst[i] ^= rhs[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Complement a bignum in-place. */
|
|
|
|
void
|
|
|
|
APInt::tcComplement(integerPart *dst, unsigned int parts)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
2010-03-25 03:38:02 +08:00
|
|
|
for (i = 0; i < parts; i++)
|
2007-08-16 23:56:55 +08:00
|
|
|
dst[i] = ~dst[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Comparison (unsigned) of two bignums. */
|
|
|
|
int
|
|
|
|
APInt::tcCompare(const integerPart *lhs, const integerPart *rhs,
|
|
|
|
unsigned int parts)
|
|
|
|
{
|
|
|
|
while (parts) {
|
|
|
|
parts--;
|
|
|
|
if (lhs[parts] == rhs[parts])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (lhs[parts] > rhs[parts])
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Increment a bignum in-place, return the carry flag. */
|
|
|
|
integerPart
|
|
|
|
APInt::tcIncrement(integerPart *dst, unsigned int parts)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
2010-03-25 03:38:02 +08:00
|
|
|
for (i = 0; i < parts; i++)
|
2007-08-16 23:56:55 +08:00
|
|
|
if (++dst[i] != 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
return i == parts;
|
|
|
|
}
|
|
|
|
|
2013-05-29 03:50:20 +08:00
|
|
|
/* Decrement a bignum in-place, return the borrow flag. */
|
|
|
|
integerPart
|
|
|
|
APInt::tcDecrement(integerPart *dst, unsigned int parts) {
|
|
|
|
for (unsigned int i = 0; i < parts; i++) {
|
|
|
|
// If the current word is non-zero, then the decrement has no effect on the
|
|
|
|
// higher-order words of the integer and no borrow can occur. Exit early.
|
|
|
|
if (dst[i]--)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// If every word was zero, then there is a borrow.
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-16 23:56:55 +08:00
|
|
|
/* Set the least significant BITS bits of a bignum, clear the
|
|
|
|
rest. */
|
|
|
|
void
|
|
|
|
APInt::tcSetLeastSignificantBits(integerPart *dst, unsigned int parts,
|
|
|
|
unsigned int bits)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while (bits > integerPartWidth) {
|
|
|
|
dst[i++] = ~(integerPart) 0;
|
|
|
|
bits -= integerPartWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bits)
|
|
|
|
dst[i++] = ~(integerPart) 0 >> (integerPartWidth - bits);
|
|
|
|
|
|
|
|
while (i < parts)
|
|
|
|
dst[i++] = 0;
|
|
|
|
}
|