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