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 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 10 | // This file implements a class to represent arbitrary precision integer |
| 11 | // constant values and provide a variety of arithmetic operations on them. |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 15 | #define DEBUG_TYPE "apint" |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/APInt.h" |
Ted Kremenek | e420deb | 2008-01-19 04:23:33 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/FoldingSet.h" |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Debug.h" |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 19 | #include "llvm/Support/MathExtras.h" |
Jeff Cohen | ca5183d | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 20 | #include <math.h> |
Jeff Cohen | 09dfd8e | 2007-03-20 20:42:36 +0000 | [diff] [blame] | 21 | #include <limits> |
Zhou Sheng | a3832fd | 2007-02-07 06:14:53 +0000 | [diff] [blame] | 22 | #include <cstring> |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 23 | #include <cstdlib> |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 24 | #include <iomanip> |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 25 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Reid Spencer | 9af1887 | 2007-12-11 06:53:58 +0000 | [diff] [blame] | 28 | /// This enumeration just provides for internal constants used in this |
| 29 | /// translation unit. |
| 30 | enum { |
| 31 | MIN_INT_BITS = 1, ///< Minimum number of bits that can be specified |
| 32 | ///< Note that this must remain synchronized with IntegerType::MIN_INT_BITS |
| 33 | MAX_INT_BITS = (1<<23)-1 ///< Maximum number of bits that can be specified |
| 34 | ///< Note that this must remain synchronized with IntegerType::MAX_INT_BITS |
| 35 | }; |
| 36 | |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 37 | /// A utility function for allocating memory, checking for allocation failures, |
| 38 | /// and ensuring the contents are zeroed. |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 39 | inline static uint64_t* getClearedMemory(uint32_t numWords) { |
| 40 | uint64_t * result = new uint64_t[numWords]; |
| 41 | assert(result && "APInt memory allocation fails!"); |
| 42 | memset(result, 0, numWords * sizeof(uint64_t)); |
| 43 | return result; |
Zhou Sheng | 353815d | 2007-02-06 06:04:53 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 46 | /// A utility function for allocating memory and checking for allocation |
| 47 | /// failure. The content is not zeroed. |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 48 | inline static uint64_t* getMemory(uint32_t numWords) { |
| 49 | uint64_t * result = new uint64_t[numWords]; |
| 50 | assert(result && "APInt memory allocation fails!"); |
| 51 | return result; |
| 52 | } |
| 53 | |
Reid Spencer | adf2a20 | 2007-03-19 21:19:02 +0000 | [diff] [blame] | 54 | APInt::APInt(uint32_t numBits, uint64_t val, bool isSigned) |
Reid Spencer | 3a34137 | 2007-03-19 20:37:47 +0000 | [diff] [blame] | 55 | : BitWidth(numBits), VAL(0) { |
Reid Spencer | 9af1887 | 2007-12-11 06:53:58 +0000 | [diff] [blame] | 56 | assert(BitWidth >= MIN_INT_BITS && "bitwidth too small"); |
| 57 | assert(BitWidth <= MAX_INT_BITS && "bitwidth too large"); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 58 | if (isSingleWord()) |
| 59 | VAL = val; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 60 | else { |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 61 | pVal = getClearedMemory(getNumWords()); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 62 | pVal[0] = val; |
Reid Spencer | 3a34137 | 2007-03-19 20:37:47 +0000 | [diff] [blame] | 63 | if (isSigned && int64_t(val) < 0) |
| 64 | for (unsigned i = 1; i < getNumWords(); ++i) |
| 65 | pVal[i] = -1ULL; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 66 | } |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 67 | clearUnusedBits(); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Dale Johannesen | 910993e | 2007-09-21 22:09:37 +0000 | [diff] [blame] | 70 | APInt::APInt(uint32_t numBits, uint32_t numWords, const uint64_t bigVal[]) |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 71 | : BitWidth(numBits), VAL(0) { |
Reid Spencer | 9af1887 | 2007-12-11 06:53:58 +0000 | [diff] [blame] | 72 | assert(BitWidth >= MIN_INT_BITS && "bitwidth too small"); |
| 73 | assert(BitWidth <= MAX_INT_BITS && "bitwidth too large"); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 74 | assert(bigVal && "Null pointer detected!"); |
| 75 | if (isSingleWord()) |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 76 | VAL = bigVal[0]; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 77 | else { |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 78 | // Get memory, cleared to 0 |
| 79 | pVal = getClearedMemory(getNumWords()); |
| 80 | // Calculate the number of words to copy |
| 81 | uint32_t words = std::min<uint32_t>(numWords, getNumWords()); |
| 82 | // Copy the words from bigVal to pVal |
| 83 | memcpy(pVal, bigVal, words * APINT_WORD_SIZE); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 84 | } |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 85 | // Make sure unused high bits are cleared |
| 86 | clearUnusedBits(); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 89 | APInt::APInt(uint32_t numbits, const char StrStart[], uint32_t slen, |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 90 | uint8_t radix) |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 91 | : BitWidth(numbits), VAL(0) { |
Reid Spencer | 9af1887 | 2007-12-11 06:53:58 +0000 | [diff] [blame] | 92 | assert(BitWidth >= MIN_INT_BITS && "bitwidth too small"); |
| 93 | assert(BitWidth <= MAX_INT_BITS && "bitwidth too large"); |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 94 | fromString(numbits, StrStart, slen, radix); |
Zhou Sheng | a3832fd | 2007-02-07 06:14:53 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 97 | APInt::APInt(uint32_t numbits, const std::string& Val, uint8_t radix) |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 98 | : BitWidth(numbits), VAL(0) { |
Reid Spencer | 9af1887 | 2007-12-11 06:53:58 +0000 | [diff] [blame] | 99 | assert(BitWidth >= MIN_INT_BITS && "bitwidth too small"); |
| 100 | assert(BitWidth <= MAX_INT_BITS && "bitwidth too large"); |
Zhou Sheng | a3832fd | 2007-02-07 06:14:53 +0000 | [diff] [blame] | 101 | assert(!Val.empty() && "String empty?"); |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 102 | fromString(numbits, Val.c_str(), Val.size(), radix); |
Zhou Sheng | a3832fd | 2007-02-07 06:14:53 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Reid Spencer | 54362ca | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 105 | APInt::APInt(const APInt& that) |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 106 | : BitWidth(that.BitWidth), VAL(0) { |
Reid Spencer | 9af1887 | 2007-12-11 06:53:58 +0000 | [diff] [blame] | 107 | assert(BitWidth >= MIN_INT_BITS && "bitwidth too small"); |
| 108 | assert(BitWidth <= MAX_INT_BITS && "bitwidth too large"); |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 109 | if (isSingleWord()) |
Reid Spencer | 54362ca | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 110 | VAL = that.VAL; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 111 | else { |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 112 | pVal = getMemory(getNumWords()); |
Reid Spencer | 54362ca | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 113 | memcpy(pVal, that.pVal, getNumWords() * APINT_WORD_SIZE); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
| 117 | APInt::~APInt() { |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 118 | if (!isSingleWord() && pVal) |
Reid Spencer | 9ac4411 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 119 | delete [] pVal; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 122 | APInt& APInt::operator=(const APInt& RHS) { |
Reid Spencer | 9ac4411 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 123 | // Don't do anything for X = X |
| 124 | if (this == &RHS) |
| 125 | return *this; |
| 126 | |
| 127 | // If the bitwidths are the same, we can avoid mucking with memory |
| 128 | if (BitWidth == RHS.getBitWidth()) { |
| 129 | if (isSingleWord()) |
| 130 | VAL = RHS.VAL; |
| 131 | else |
| 132 | memcpy(pVal, RHS.pVal, getNumWords() * APINT_WORD_SIZE); |
| 133 | return *this; |
| 134 | } |
| 135 | |
| 136 | if (isSingleWord()) |
| 137 | if (RHS.isSingleWord()) |
| 138 | VAL = RHS.VAL; |
| 139 | else { |
| 140 | VAL = 0; |
| 141 | pVal = getMemory(RHS.getNumWords()); |
| 142 | memcpy(pVal, RHS.pVal, RHS.getNumWords() * APINT_WORD_SIZE); |
| 143 | } |
| 144 | else if (getNumWords() == RHS.getNumWords()) |
| 145 | memcpy(pVal, RHS.pVal, RHS.getNumWords() * APINT_WORD_SIZE); |
| 146 | else if (RHS.isSingleWord()) { |
| 147 | delete [] pVal; |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 148 | VAL = RHS.VAL; |
Reid Spencer | 9ac4411 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 149 | } else { |
| 150 | delete [] pVal; |
| 151 | pVal = getMemory(RHS.getNumWords()); |
| 152 | memcpy(pVal, RHS.pVal, RHS.getNumWords() * APINT_WORD_SIZE); |
| 153 | } |
| 154 | BitWidth = RHS.BitWidth; |
| 155 | return clearUnusedBits(); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 158 | APInt& APInt::operator=(uint64_t RHS) { |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 159 | if (isSingleWord()) |
| 160 | VAL = RHS; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 161 | else { |
| 162 | pVal[0] = RHS; |
Reid Spencer | a58f058 | 2007-02-18 20:09:41 +0000 | [diff] [blame] | 163 | memset(pVal+1, 0, (getNumWords() - 1) * APINT_WORD_SIZE); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 164 | } |
Reid Spencer | 9ac4411 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 165 | return clearUnusedBits(); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Ted Kremenek | e420deb | 2008-01-19 04:23:33 +0000 | [diff] [blame] | 168 | /// Profile - This method 'profiles' an APInt for use with FoldingSet. |
| 169 | void APInt::Profile(FoldingSetNodeID& ID) const { |
| 170 | if (isSingleWord()) { |
| 171 | ID.AddInteger(VAL); |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | uint32_t NumWords = getNumWords(); |
| 176 | for (unsigned i = 0; i < NumWords; ++i) |
| 177 | ID.AddInteger(pVal[i]); |
| 178 | } |
| 179 | |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 180 | /// add_1 - This function adds a single "digit" integer, y, to the multiple |
| 181 | /// "digit" integer array, x[]. x[] is modified to reflect the addition and |
| 182 | /// 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] | 183 | /// @returns the carry of the addition. |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 184 | static bool 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] | 185 | for (uint32_t i = 0; i < len; ++i) { |
Reid Spencer | f2c521c | 2007-02-18 06:39:42 +0000 | [diff] [blame] | 186 | dest[i] = y + x[i]; |
| 187 | if (dest[i] < y) |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 188 | y = 1; // Carry one to next digit. |
Reid Spencer | f2c521c | 2007-02-18 06:39:42 +0000 | [diff] [blame] | 189 | else { |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 190 | y = 0; // No need to carry so exit early |
Reid Spencer | f2c521c | 2007-02-18 06:39:42 +0000 | [diff] [blame] | 191 | break; |
| 192 | } |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 193 | } |
Reid Spencer | f2c521c | 2007-02-18 06:39:42 +0000 | [diff] [blame] | 194 | return y; |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 197 | /// @brief Prefix increment operator. Increments the APInt by one. |
| 198 | APInt& APInt::operator++() { |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 199 | if (isSingleWord()) |
| 200 | ++VAL; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 201 | else |
Zhou Sheng | a3832fd | 2007-02-07 06:14:53 +0000 | [diff] [blame] | 202 | add_1(pVal, pVal, getNumWords(), 1); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 203 | return clearUnusedBits(); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 206 | /// sub_1 - This function subtracts a single "digit" (64-bit word), y, from |
| 207 | /// the multi-digit integer array, x[], propagating the borrowed 1 value until |
| 208 | /// no further borrowing is neeeded or it runs out of "digits" in x. The result |
| 209 | /// is 1 if "borrowing" exhausted the digits in x, or 0 if x was not exhausted. |
| 210 | /// In other words, if y > x then this function returns 1, otherwise 0. |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 211 | /// @returns the borrow out of the subtraction |
| 212 | static bool sub_1(uint64_t x[], uint32_t len, uint64_t y) { |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 213 | for (uint32_t i = 0; i < len; ++i) { |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 214 | uint64_t X = x[i]; |
Reid Spencer | f2c521c | 2007-02-18 06:39:42 +0000 | [diff] [blame] | 215 | x[i] -= y; |
| 216 | if (y > X) |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 217 | y = 1; // We have to "borrow 1" from next "digit" |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 218 | else { |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 219 | y = 0; // No need to borrow |
| 220 | break; // Remaining digits are unchanged so exit early |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 221 | } |
| 222 | } |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 223 | return bool(y); |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 226 | /// @brief Prefix decrement operator. Decrements the APInt by one. |
| 227 | APInt& APInt::operator--() { |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 228 | if (isSingleWord()) |
| 229 | --VAL; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 230 | else |
Zhou Sheng | a3832fd | 2007-02-07 06:14:53 +0000 | [diff] [blame] | 231 | sub_1(pVal, getNumWords(), 1); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 232 | return clearUnusedBits(); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 235 | /// add - This function adds the integer array x to the integer array Y and |
| 236 | /// places the result in dest. |
| 237 | /// @returns the carry out from the addition |
| 238 | /// @brief General addition of 64-bit integer arrays |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 239 | static bool add(uint64_t *dest, const uint64_t *x, const uint64_t *y, |
| 240 | uint32_t len) { |
| 241 | bool carry = false; |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 242 | for (uint32_t i = 0; i< len; ++i) { |
Reid Spencer | 9290463 | 2007-02-23 01:57:13 +0000 | [diff] [blame] | 243 | 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] | 244 | dest[i] = x[i] + y[i] + carry; |
Reid Spencer | 60c0a6a | 2007-02-21 05:44:56 +0000 | [diff] [blame] | 245 | carry = dest[i] < limit || (carry && dest[i] == limit); |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 246 | } |
| 247 | return carry; |
| 248 | } |
| 249 | |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 250 | /// Adds the RHS APint to this APInt. |
| 251 | /// @returns this, after addition of RHS. |
| 252 | /// @brief Addition assignment operator. |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 253 | APInt& APInt::operator+=(const APInt& RHS) { |
Reid Spencer | cd6f2bf | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 254 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Reid Spencer | 54362ca | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 255 | if (isSingleWord()) |
| 256 | VAL += RHS.VAL; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 257 | else { |
Reid Spencer | 54362ca | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 258 | add(pVal, pVal, RHS.pVal, getNumWords()); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 259 | } |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 260 | return clearUnusedBits(); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 263 | /// Subtracts the integer array y from the integer array x |
| 264 | /// @returns returns the borrow out. |
| 265 | /// @brief Generalized subtraction of 64-bit integer arrays. |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 266 | static bool sub(uint64_t *dest, const uint64_t *x, const uint64_t *y, |
| 267 | uint32_t len) { |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 268 | bool borrow = false; |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 269 | for (uint32_t i = 0; i < len; ++i) { |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 270 | uint64_t x_tmp = borrow ? x[i] - 1 : x[i]; |
| 271 | borrow = y[i] > x_tmp || (borrow && x[i] == 0); |
| 272 | dest[i] = x_tmp - y[i]; |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 273 | } |
Reid Spencer | 54362ca | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 274 | return borrow; |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 277 | /// Subtracts the RHS APInt from this APInt |
| 278 | /// @returns this, after subtraction |
| 279 | /// @brief Subtraction assignment operator. |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 280 | APInt& APInt::operator-=(const APInt& RHS) { |
Reid Spencer | cd6f2bf | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 281 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 282 | if (isSingleWord()) |
Reid Spencer | 54362ca | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 283 | VAL -= RHS.VAL; |
| 284 | else |
| 285 | sub(pVal, pVal, RHS.pVal, getNumWords()); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 286 | return clearUnusedBits(); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 289 | /// Multiplies an integer array, x by a a uint64_t integer and places the result |
| 290 | /// into dest. |
| 291 | /// @returns the carry out of the multiplication. |
| 292 | /// @brief Multiply a multi-digit APInt by a single digit (64-bit) integer. |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 293 | static uint64_t mul_1(uint64_t dest[], uint64_t x[], uint32_t len, uint64_t y) { |
| 294 | // 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] | 295 | uint64_t ly = y & 0xffffffffULL, hy = y >> 32; |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 296 | uint64_t carry = 0; |
| 297 | |
| 298 | // For each digit of x. |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 299 | for (uint32_t i = 0; i < len; ++i) { |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 300 | // Split x into high and low words |
| 301 | uint64_t lx = x[i] & 0xffffffffULL; |
| 302 | uint64_t hx = x[i] >> 32; |
| 303 | // hasCarry - A flag to indicate if there is a carry to the next digit. |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 304 | // hasCarry == 0, no carry |
| 305 | // hasCarry == 1, has carry |
| 306 | // hasCarry == 2, no carry and the calculation result == 0. |
| 307 | uint8_t hasCarry = 0; |
| 308 | dest[i] = carry + lx * ly; |
| 309 | // Determine if the add above introduces carry. |
| 310 | hasCarry = (dest[i] < carry) ? 1 : 0; |
| 311 | carry = hx * ly + (dest[i] >> 32) + (hasCarry ? (1ULL << 32) : 0); |
| 312 | // The upper limit of carry can be (2^32 - 1)(2^32 - 1) + |
| 313 | // (2^32 - 1) + 2^32 = 2^64. |
| 314 | hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0); |
| 315 | |
| 316 | carry += (lx * hy) & 0xffffffffULL; |
| 317 | dest[i] = (carry << 32) | (dest[i] & 0xffffffffULL); |
| 318 | carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0) + |
| 319 | (carry >> 32) + ((lx * hy) >> 32) + hx * hy; |
| 320 | } |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 321 | return carry; |
| 322 | } |
| 323 | |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 324 | /// Multiplies integer array x by integer array y and stores the result into |
| 325 | /// the integer array dest. Note that dest's size must be >= xlen + ylen. |
| 326 | /// @brief Generalized multiplicate of integer arrays. |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 327 | static void mul(uint64_t dest[], uint64_t x[], uint32_t xlen, uint64_t y[], |
| 328 | uint32_t ylen) { |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 329 | dest[xlen] = mul_1(dest, x, xlen, y[0]); |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 330 | for (uint32_t i = 1; i < ylen; ++i) { |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 331 | uint64_t ly = y[i] & 0xffffffffULL, hy = y[i] >> 32; |
Reid Spencer | e0cdd33 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 332 | uint64_t carry = 0, lx = 0, hx = 0; |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 333 | for (uint32_t j = 0; j < xlen; ++j) { |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 334 | lx = x[j] & 0xffffffffULL; |
| 335 | hx = x[j] >> 32; |
| 336 | // hasCarry - A flag to indicate if has carry. |
| 337 | // hasCarry == 0, no carry |
| 338 | // hasCarry == 1, has carry |
| 339 | // hasCarry == 2, no carry and the calculation result == 0. |
| 340 | uint8_t hasCarry = 0; |
| 341 | uint64_t resul = carry + lx * ly; |
| 342 | hasCarry = (resul < carry) ? 1 : 0; |
| 343 | carry = (hasCarry ? (1ULL << 32) : 0) + hx * ly + (resul >> 32); |
| 344 | hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0); |
| 345 | |
| 346 | carry += (lx * hy) & 0xffffffffULL; |
| 347 | resul = (carry << 32) | (resul & 0xffffffffULL); |
| 348 | dest[i+j] += resul; |
| 349 | carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0)+ |
| 350 | (carry >> 32) + (dest[i+j] < resul ? 1 : 0) + |
| 351 | ((lx * hy) >> 32) + hx * hy; |
| 352 | } |
| 353 | dest[i+xlen] = carry; |
| 354 | } |
| 355 | } |
| 356 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 357 | APInt& APInt::operator*=(const APInt& RHS) { |
Reid Spencer | cd6f2bf | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 358 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Reid Spencer | e0cdd33 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 359 | if (isSingleWord()) { |
Reid Spencer | 61eb180 | 2007-02-20 20:42:10 +0000 | [diff] [blame] | 360 | VAL *= RHS.VAL; |
Reid Spencer | e0cdd33 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 361 | clearUnusedBits(); |
| 362 | return *this; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 363 | } |
Reid Spencer | e0cdd33 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 364 | |
| 365 | // Get some bit facts about LHS and check for zero |
| 366 | uint32_t lhsBits = getActiveBits(); |
| 367 | uint32_t lhsWords = !lhsBits ? 0 : whichWord(lhsBits - 1) + 1; |
| 368 | if (!lhsWords) |
| 369 | // 0 * X ===> 0 |
| 370 | return *this; |
| 371 | |
| 372 | // Get some bit facts about RHS and check for zero |
| 373 | uint32_t rhsBits = RHS.getActiveBits(); |
| 374 | uint32_t rhsWords = !rhsBits ? 0 : whichWord(rhsBits - 1) + 1; |
| 375 | if (!rhsWords) { |
| 376 | // X * 0 ===> 0 |
| 377 | clear(); |
| 378 | return *this; |
| 379 | } |
| 380 | |
| 381 | // Allocate space for the result |
| 382 | uint32_t destWords = rhsWords + lhsWords; |
| 383 | uint64_t *dest = getMemory(destWords); |
| 384 | |
| 385 | // Perform the long multiply |
| 386 | mul(dest, pVal, lhsWords, RHS.pVal, rhsWords); |
| 387 | |
| 388 | // Copy result back into *this |
| 389 | clear(); |
| 390 | uint32_t wordsToCopy = destWords >= getNumWords() ? getNumWords() : destWords; |
| 391 | memcpy(pVal, dest, wordsToCopy * APINT_WORD_SIZE); |
| 392 | |
| 393 | // delete dest array and return |
| 394 | delete[] dest; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 395 | return *this; |
| 396 | } |
| 397 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 398 | APInt& APInt::operator&=(const APInt& RHS) { |
Reid Spencer | cd6f2bf | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 399 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 400 | if (isSingleWord()) { |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 401 | VAL &= RHS.VAL; |
| 402 | return *this; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 403 | } |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 404 | uint32_t numWords = getNumWords(); |
| 405 | for (uint32_t i = 0; i < numWords; ++i) |
| 406 | pVal[i] &= RHS.pVal[i]; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 407 | return *this; |
| 408 | } |
| 409 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 410 | APInt& APInt::operator|=(const APInt& RHS) { |
Reid Spencer | cd6f2bf | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 411 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 412 | if (isSingleWord()) { |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 413 | VAL |= RHS.VAL; |
| 414 | return *this; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 415 | } |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 416 | uint32_t numWords = getNumWords(); |
| 417 | for (uint32_t i = 0; i < numWords; ++i) |
| 418 | pVal[i] |= RHS.pVal[i]; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 419 | return *this; |
| 420 | } |
| 421 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 422 | APInt& APInt::operator^=(const APInt& RHS) { |
Reid Spencer | cd6f2bf | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 423 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 424 | if (isSingleWord()) { |
Reid Spencer | f2c521c | 2007-02-18 06:39:42 +0000 | [diff] [blame] | 425 | VAL ^= RHS.VAL; |
Reid Spencer | 54362ca | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 426 | this->clearUnusedBits(); |
Reid Spencer | f2c521c | 2007-02-18 06:39:42 +0000 | [diff] [blame] | 427 | return *this; |
| 428 | } |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 429 | uint32_t numWords = getNumWords(); |
| 430 | for (uint32_t i = 0; i < numWords; ++i) |
| 431 | pVal[i] ^= RHS.pVal[i]; |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 432 | return clearUnusedBits(); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 435 | APInt APInt::operator&(const APInt& RHS) const { |
Reid Spencer | cd6f2bf | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 436 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 437 | if (isSingleWord()) |
| 438 | return APInt(getBitWidth(), VAL & RHS.VAL); |
| 439 | |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 440 | uint32_t numWords = getNumWords(); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 441 | uint64_t* val = getMemory(numWords); |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 442 | for (uint32_t i = 0; i < numWords; ++i) |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 443 | val[i] = pVal[i] & RHS.pVal[i]; |
| 444 | return APInt(val, getBitWidth()); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 447 | APInt APInt::operator|(const APInt& RHS) const { |
Reid Spencer | cd6f2bf | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 448 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 449 | if (isSingleWord()) |
| 450 | return APInt(getBitWidth(), VAL | RHS.VAL); |
Reid Spencer | 54362ca | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 451 | |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 452 | uint32_t numWords = getNumWords(); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 453 | uint64_t *val = getMemory(numWords); |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 454 | for (uint32_t i = 0; i < numWords; ++i) |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 455 | val[i] = pVal[i] | RHS.pVal[i]; |
| 456 | return APInt(val, getBitWidth()); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 459 | APInt APInt::operator^(const APInt& RHS) const { |
Reid Spencer | cd6f2bf | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 460 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 461 | if (isSingleWord()) |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 462 | return APInt(BitWidth, VAL ^ RHS.VAL); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 463 | |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 464 | uint32_t numWords = getNumWords(); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 465 | uint64_t *val = getMemory(numWords); |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 466 | for (uint32_t i = 0; i < numWords; ++i) |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 467 | val[i] = pVal[i] ^ RHS.pVal[i]; |
| 468 | |
| 469 | // 0^0==1 so clear the high bits in case they got set. |
| 470 | return APInt(val, getBitWidth()).clearUnusedBits(); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 473 | bool APInt::operator !() const { |
| 474 | if (isSingleWord()) |
| 475 | return !VAL; |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 476 | |
| 477 | for (uint32_t i = 0; i < getNumWords(); ++i) |
| 478 | if (pVal[i]) |
| 479 | return false; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 480 | return true; |
| 481 | } |
| 482 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 483 | APInt APInt::operator*(const APInt& RHS) const { |
Reid Spencer | cd6f2bf | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 484 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 485 | if (isSingleWord()) |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 486 | return APInt(BitWidth, VAL * RHS.VAL); |
Reid Spencer | 61eb180 | 2007-02-20 20:42:10 +0000 | [diff] [blame] | 487 | APInt Result(*this); |
| 488 | Result *= RHS; |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 489 | return Result.clearUnusedBits(); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 492 | APInt APInt::operator+(const APInt& RHS) const { |
Reid Spencer | cd6f2bf | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 493 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 494 | if (isSingleWord()) |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 495 | return APInt(BitWidth, VAL + RHS.VAL); |
Reid Spencer | 54362ca | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 496 | APInt Result(BitWidth, 0); |
| 497 | add(Result.pVal, this->pVal, RHS.pVal, getNumWords()); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 498 | return Result.clearUnusedBits(); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 501 | APInt APInt::operator-(const APInt& RHS) const { |
Reid Spencer | cd6f2bf | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 502 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 503 | if (isSingleWord()) |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 504 | return APInt(BitWidth, VAL - RHS.VAL); |
Reid Spencer | 54362ca | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 505 | APInt Result(BitWidth, 0); |
| 506 | sub(Result.pVal, this->pVal, RHS.pVal, getNumWords()); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 507 | return Result.clearUnusedBits(); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 510 | bool APInt::operator[](uint32_t bitPosition) const { |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 511 | return (maskBit(bitPosition) & |
| 512 | (isSingleWord() ? VAL : pVal[whichWord(bitPosition)])) != 0; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 515 | bool APInt::operator==(const APInt& RHS) const { |
Reid Spencer | 9ac4411 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 516 | assert(BitWidth == RHS.BitWidth && "Comparison requires equal bit widths"); |
Reid Spencer | 54362ca | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 517 | if (isSingleWord()) |
| 518 | return VAL == RHS.VAL; |
| 519 | |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 520 | // Get some facts about the number of bits used in the two operands. |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 521 | uint32_t n1 = getActiveBits(); |
| 522 | uint32_t n2 = RHS.getActiveBits(); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 523 | |
| 524 | // If the number of bits isn't the same, they aren't equal |
Reid Spencer | 54362ca | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 525 | if (n1 != n2) |
| 526 | return false; |
| 527 | |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 528 | // If the number of bits fits in a word, we only need to compare the low word. |
Reid Spencer | 54362ca | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 529 | if (n1 <= APINT_BITS_PER_WORD) |
| 530 | return pVal[0] == RHS.pVal[0]; |
| 531 | |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 532 | // Otherwise, compare everything |
Reid Spencer | 54362ca | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 533 | for (int i = whichWord(n1 - 1); i >= 0; --i) |
| 534 | if (pVal[i] != RHS.pVal[i]) |
| 535 | return false; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 536 | return true; |
| 537 | } |
| 538 | |
Zhou Sheng | a3832fd | 2007-02-07 06:14:53 +0000 | [diff] [blame] | 539 | bool APInt::operator==(uint64_t Val) const { |
| 540 | if (isSingleWord()) |
| 541 | return VAL == Val; |
Reid Spencer | 54362ca | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 542 | |
| 543 | uint32_t n = getActiveBits(); |
| 544 | if (n <= APINT_BITS_PER_WORD) |
| 545 | return pVal[0] == Val; |
| 546 | else |
| 547 | return false; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 550 | bool APInt::ult(const APInt& RHS) const { |
| 551 | assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison"); |
| 552 | if (isSingleWord()) |
| 553 | return VAL < RHS.VAL; |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 554 | |
| 555 | // Get active bit length of both operands |
| 556 | uint32_t n1 = getActiveBits(); |
| 557 | uint32_t n2 = RHS.getActiveBits(); |
| 558 | |
| 559 | // If magnitude of LHS is less than RHS, return true. |
| 560 | if (n1 < n2) |
| 561 | return true; |
| 562 | |
| 563 | // If magnitude of RHS is greather than LHS, return false. |
| 564 | if (n2 < n1) |
| 565 | return false; |
| 566 | |
| 567 | // If they bot fit in a word, just compare the low order word |
| 568 | if (n1 <= APINT_BITS_PER_WORD && n2 <= APINT_BITS_PER_WORD) |
| 569 | return pVal[0] < RHS.pVal[0]; |
| 570 | |
| 571 | // Otherwise, compare all words |
Reid Spencer | 1fa111e | 2007-02-27 18:23:40 +0000 | [diff] [blame] | 572 | uint32_t topWord = whichWord(std::max(n1,n2)-1); |
| 573 | for (int i = topWord; i >= 0; --i) { |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 574 | if (pVal[i] > RHS.pVal[i]) |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 575 | return false; |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 576 | if (pVal[i] < RHS.pVal[i]) |
| 577 | return true; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 578 | } |
| 579 | return false; |
| 580 | } |
| 581 | |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 582 | bool APInt::slt(const APInt& RHS) const { |
| 583 | assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison"); |
Reid Spencer | a58f058 | 2007-02-18 20:09:41 +0000 | [diff] [blame] | 584 | if (isSingleWord()) { |
| 585 | int64_t lhsSext = (int64_t(VAL) << (64-BitWidth)) >> (64-BitWidth); |
| 586 | int64_t rhsSext = (int64_t(RHS.VAL) << (64-BitWidth)) >> (64-BitWidth); |
| 587 | return lhsSext < rhsSext; |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 588 | } |
Reid Spencer | a58f058 | 2007-02-18 20:09:41 +0000 | [diff] [blame] | 589 | |
| 590 | APInt lhs(*this); |
Reid Spencer | 1fa111e | 2007-02-27 18:23:40 +0000 | [diff] [blame] | 591 | APInt rhs(RHS); |
| 592 | bool lhsNeg = isNegative(); |
| 593 | bool rhsNeg = rhs.isNegative(); |
| 594 | if (lhsNeg) { |
| 595 | // Sign bit is set so perform two's complement to make it positive |
Reid Spencer | a58f058 | 2007-02-18 20:09:41 +0000 | [diff] [blame] | 596 | lhs.flip(); |
| 597 | lhs++; |
| 598 | } |
Reid Spencer | 1fa111e | 2007-02-27 18:23:40 +0000 | [diff] [blame] | 599 | if (rhsNeg) { |
| 600 | // Sign bit is set so perform two's complement to make it positive |
Reid Spencer | a58f058 | 2007-02-18 20:09:41 +0000 | [diff] [blame] | 601 | rhs.flip(); |
| 602 | rhs++; |
| 603 | } |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 604 | |
| 605 | // Now we have unsigned values to compare so do the comparison if necessary |
| 606 | // based on the negativeness of the values. |
Reid Spencer | 1fa111e | 2007-02-27 18:23:40 +0000 | [diff] [blame] | 607 | if (lhsNeg) |
| 608 | if (rhsNeg) |
| 609 | return lhs.ugt(rhs); |
Reid Spencer | a58f058 | 2007-02-18 20:09:41 +0000 | [diff] [blame] | 610 | else |
| 611 | return true; |
Reid Spencer | 1fa111e | 2007-02-27 18:23:40 +0000 | [diff] [blame] | 612 | else if (rhsNeg) |
Reid Spencer | a58f058 | 2007-02-18 20:09:41 +0000 | [diff] [blame] | 613 | return false; |
| 614 | else |
| 615 | return lhs.ult(rhs); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 618 | APInt& APInt::set(uint32_t bitPosition) { |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 619 | if (isSingleWord()) |
| 620 | VAL |= maskBit(bitPosition); |
| 621 | else |
| 622 | pVal[whichWord(bitPosition)] |= maskBit(bitPosition); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 623 | return *this; |
| 624 | } |
| 625 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 626 | APInt& APInt::set() { |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 627 | if (isSingleWord()) { |
| 628 | VAL = -1ULL; |
| 629 | return clearUnusedBits(); |
Zhou Sheng | b04973e | 2007-02-15 06:36:31 +0000 | [diff] [blame] | 630 | } |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 631 | |
| 632 | // Set all the bits in all the words. |
Zhou Sheng | 6dbe233 | 2007-03-21 04:34:37 +0000 | [diff] [blame] | 633 | for (uint32_t i = 0; i < getNumWords(); ++i) |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 634 | pVal[i] = -1ULL; |
| 635 | // Clear the unused ones |
| 636 | return clearUnusedBits(); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | /// Set the given bit to 0 whose position is given as "bitPosition". |
| 640 | /// @brief Set a given bit to 0. |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 641 | APInt& APInt::clear(uint32_t bitPosition) { |
| 642 | if (isSingleWord()) |
| 643 | VAL &= ~maskBit(bitPosition); |
| 644 | else |
| 645 | pVal[whichWord(bitPosition)] &= ~maskBit(bitPosition); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 646 | return *this; |
| 647 | } |
| 648 | |
| 649 | /// @brief Set every bit to 0. |
| 650 | APInt& APInt::clear() { |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 651 | if (isSingleWord()) |
| 652 | VAL = 0; |
Zhou Sheng | a3832fd | 2007-02-07 06:14:53 +0000 | [diff] [blame] | 653 | else |
Reid Spencer | a58f058 | 2007-02-18 20:09:41 +0000 | [diff] [blame] | 654 | memset(pVal, 0, getNumWords() * APINT_WORD_SIZE); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 655 | return *this; |
| 656 | } |
| 657 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 658 | /// @brief Bitwise NOT operator. Performs a bitwise logical NOT operation on |
| 659 | /// this APInt. |
| 660 | APInt APInt::operator~() const { |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 661 | APInt Result(*this); |
| 662 | Result.flip(); |
| 663 | return Result; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | /// @brief Toggle every bit to its opposite value. |
| 667 | APInt& APInt::flip() { |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 668 | if (isSingleWord()) { |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 669 | VAL ^= -1ULL; |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 670 | return clearUnusedBits(); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 671 | } |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 672 | for (uint32_t i = 0; i < getNumWords(); ++i) |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 673 | pVal[i] ^= -1ULL; |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 674 | return clearUnusedBits(); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | /// Toggle a given bit to its opposite value whose position is given |
| 678 | /// as "bitPosition". |
| 679 | /// @brief Toggles a given bit to its opposite value. |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 680 | APInt& APInt::flip(uint32_t bitPosition) { |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 681 | assert(bitPosition < BitWidth && "Out of the bit-width range!"); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 682 | if ((*this)[bitPosition]) clear(bitPosition); |
| 683 | else set(bitPosition); |
| 684 | return *this; |
| 685 | } |
| 686 | |
Reid Spencer | 57ae4f5 | 2007-04-13 19:19:07 +0000 | [diff] [blame] | 687 | uint32_t APInt::getBitsNeeded(const char* str, uint32_t slen, uint8_t radix) { |
| 688 | assert(str != 0 && "Invalid value string"); |
| 689 | assert(slen > 0 && "Invalid string length"); |
| 690 | |
| 691 | // Each computation below needs to know if its negative |
| 692 | uint32_t isNegative = str[0] == '-'; |
| 693 | if (isNegative) { |
| 694 | slen--; |
| 695 | str++; |
| 696 | } |
| 697 | // For radixes of power-of-two values, the bits required is accurately and |
| 698 | // easily computed |
| 699 | if (radix == 2) |
| 700 | return slen + isNegative; |
| 701 | if (radix == 8) |
| 702 | return slen * 3 + isNegative; |
| 703 | if (radix == 16) |
| 704 | return slen * 4 + isNegative; |
| 705 | |
| 706 | // Otherwise it must be radix == 10, the hard case |
| 707 | assert(radix == 10 && "Invalid radix"); |
| 708 | |
| 709 | // This is grossly inefficient but accurate. We could probably do something |
| 710 | // with a computation of roughly slen*64/20 and then adjust by the value of |
| 711 | // the first few digits. But, I'm not sure how accurate that could be. |
| 712 | |
| 713 | // Compute a sufficient number of bits that is always large enough but might |
| 714 | // be too large. This avoids the assertion in the constructor. |
| 715 | uint32_t sufficient = slen*64/18; |
| 716 | |
| 717 | // Convert to the actual binary value. |
| 718 | APInt tmp(sufficient, str, slen, radix); |
| 719 | |
| 720 | // Compute how many bits are required. |
Reid Spencer | 0468ab3 | 2007-04-14 00:00:10 +0000 | [diff] [blame] | 721 | return isNegative + tmp.logBase2() + 1; |
Reid Spencer | 57ae4f5 | 2007-04-13 19:19:07 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Reid Spencer | 794f472 | 2007-02-26 21:02:27 +0000 | [diff] [blame] | 724 | uint64_t APInt::getHashValue() const { |
Reid Spencer | 9ac4411 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 725 | // Put the bit width into the low order bits. |
| 726 | uint64_t hash = BitWidth; |
Reid Spencer | 794f472 | 2007-02-26 21:02:27 +0000 | [diff] [blame] | 727 | |
| 728 | // Add the sum of the words to the hash. |
| 729 | if (isSingleWord()) |
Reid Spencer | 9ac4411 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 730 | hash += VAL << 6; // clear separation of up to 64 bits |
Reid Spencer | 794f472 | 2007-02-26 21:02:27 +0000 | [diff] [blame] | 731 | else |
| 732 | for (uint32_t i = 0; i < getNumWords(); ++i) |
Reid Spencer | 9ac4411 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 733 | hash += pVal[i] << 6; // clear sepration of up to 64 bits |
Reid Spencer | 794f472 | 2007-02-26 21:02:27 +0000 | [diff] [blame] | 734 | return hash; |
| 735 | } |
| 736 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 737 | /// HiBits - This function returns the high "numBits" bits of this APInt. |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 738 | APInt APInt::getHiBits(uint32_t numBits) const { |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 739 | return APIntOps::lshr(*this, BitWidth - numBits); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | /// LoBits - This function returns the low "numBits" bits of this APInt. |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 743 | APInt APInt::getLoBits(uint32_t numBits) const { |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 744 | return APIntOps::lshr(APIntOps::shl(*this, BitWidth - numBits), |
| 745 | BitWidth - numBits); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 748 | bool APInt::isPowerOf2() const { |
| 749 | return (!!*this) && !(*this & (*this - APInt(BitWidth,1))); |
| 750 | } |
| 751 | |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 752 | uint32_t APInt::countLeadingZeros() const { |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 753 | uint32_t Count = 0; |
Reid Spencer | e549c49 | 2007-02-21 00:29:48 +0000 | [diff] [blame] | 754 | if (isSingleWord()) |
| 755 | Count = CountLeadingZeros_64(VAL); |
| 756 | else { |
| 757 | for (uint32_t i = getNumWords(); i > 0u; --i) { |
| 758 | if (pVal[i-1] == 0) |
| 759 | Count += APINT_BITS_PER_WORD; |
| 760 | else { |
| 761 | Count += CountLeadingZeros_64(pVal[i-1]); |
| 762 | break; |
| 763 | } |
| 764 | } |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 765 | } |
Reid Spencer | ab2b2c8 | 2007-02-22 00:22:00 +0000 | [diff] [blame] | 766 | uint32_t remainder = BitWidth % APINT_BITS_PER_WORD; |
| 767 | if (remainder) |
| 768 | Count -= APINT_BITS_PER_WORD - remainder; |
Chris Lattner | 9e513ac | 2007-11-23 22:42:31 +0000 | [diff] [blame] | 769 | return std::min(Count, BitWidth); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Reid Spencer | 681dcd1 | 2007-02-27 21:59:26 +0000 | [diff] [blame] | 772 | static uint32_t countLeadingOnes_64(uint64_t V, uint32_t skip) { |
| 773 | uint32_t Count = 0; |
| 774 | if (skip) |
| 775 | V <<= skip; |
| 776 | while (V && (V & (1ULL << 63))) { |
| 777 | Count++; |
| 778 | V <<= 1; |
| 779 | } |
| 780 | return Count; |
| 781 | } |
| 782 | |
| 783 | uint32_t APInt::countLeadingOnes() const { |
| 784 | if (isSingleWord()) |
| 785 | return countLeadingOnes_64(VAL, APINT_BITS_PER_WORD - BitWidth); |
| 786 | |
| 787 | uint32_t highWordBits = BitWidth % APINT_BITS_PER_WORD; |
| 788 | uint32_t shift = (highWordBits == 0 ? 0 : APINT_BITS_PER_WORD - highWordBits); |
| 789 | int i = getNumWords() - 1; |
| 790 | uint32_t Count = countLeadingOnes_64(pVal[i], shift); |
| 791 | if (Count == highWordBits) { |
| 792 | for (i--; i >= 0; --i) { |
| 793 | if (pVal[i] == -1ULL) |
| 794 | Count += APINT_BITS_PER_WORD; |
| 795 | else { |
| 796 | Count += countLeadingOnes_64(pVal[i], 0); |
| 797 | break; |
| 798 | } |
| 799 | } |
| 800 | } |
| 801 | return Count; |
| 802 | } |
| 803 | |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 804 | uint32_t APInt::countTrailingZeros() const { |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 805 | if (isSingleWord()) |
Anton Korobeynikov | 97d3726 | 2007-12-24 11:16:47 +0000 | [diff] [blame] | 806 | return std::min(uint32_t(CountTrailingZeros_64(VAL)), BitWidth); |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 807 | uint32_t Count = 0; |
| 808 | uint32_t i = 0; |
| 809 | for (; i < getNumWords() && pVal[i] == 0; ++i) |
| 810 | Count += APINT_BITS_PER_WORD; |
| 811 | if (i < getNumWords()) |
| 812 | Count += CountTrailingZeros_64(pVal[i]); |
Chris Lattner | 5e55712 | 2007-11-23 22:36:25 +0000 | [diff] [blame] | 813 | return std::min(Count, BitWidth); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Dan Gohman | 42dd77f | 2008-02-13 21:11:05 +0000 | [diff] [blame^] | 816 | uint32_t APInt::countTrailingOnes() const { |
| 817 | if (isSingleWord()) |
| 818 | return std::min(uint32_t(CountTrailingOnes_64(VAL)), BitWidth); |
| 819 | uint32_t Count = 0; |
| 820 | uint32_t i = 0; |
| 821 | for (; i < getNumWords() && pVal[i] == -1; ++i) |
| 822 | Count += APINT_BITS_PER_WORD; |
| 823 | if (i < getNumWords()) |
| 824 | Count += CountTrailingOnes_64(pVal[i]); |
| 825 | return std::min(Count, BitWidth); |
| 826 | } |
| 827 | |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 828 | uint32_t APInt::countPopulation() const { |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 829 | if (isSingleWord()) |
| 830 | return CountPopulation_64(VAL); |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 831 | uint32_t Count = 0; |
| 832 | for (uint32_t i = 0; i < getNumWords(); ++i) |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 833 | Count += CountPopulation_64(pVal[i]); |
| 834 | return Count; |
| 835 | } |
| 836 | |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 837 | APInt APInt::byteSwap() const { |
| 838 | assert(BitWidth >= 16 && BitWidth % 16 == 0 && "Cannot byteswap!"); |
| 839 | if (BitWidth == 16) |
Jeff Cohen | 09dfd8e | 2007-03-20 20:42:36 +0000 | [diff] [blame] | 840 | return APInt(BitWidth, ByteSwap_16(uint16_t(VAL))); |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 841 | else if (BitWidth == 32) |
Jeff Cohen | 09dfd8e | 2007-03-20 20:42:36 +0000 | [diff] [blame] | 842 | return APInt(BitWidth, ByteSwap_32(uint32_t(VAL))); |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 843 | else if (BitWidth == 48) { |
Jeff Cohen | 09dfd8e | 2007-03-20 20:42:36 +0000 | [diff] [blame] | 844 | uint32_t Tmp1 = uint32_t(VAL >> 16); |
Zhou Sheng | b04973e | 2007-02-15 06:36:31 +0000 | [diff] [blame] | 845 | Tmp1 = ByteSwap_32(Tmp1); |
Jeff Cohen | 09dfd8e | 2007-03-20 20:42:36 +0000 | [diff] [blame] | 846 | uint16_t Tmp2 = uint16_t(VAL); |
Zhou Sheng | b04973e | 2007-02-15 06:36:31 +0000 | [diff] [blame] | 847 | Tmp2 = ByteSwap_16(Tmp2); |
Jeff Cohen | 09dfd8e | 2007-03-20 20:42:36 +0000 | [diff] [blame] | 848 | return APInt(BitWidth, (uint64_t(Tmp2) << 32) | Tmp1); |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 849 | } else if (BitWidth == 64) |
Reid Spencer | cd6f2bf | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 850 | return APInt(BitWidth, ByteSwap_64(VAL)); |
Zhou Sheng | b04973e | 2007-02-15 06:36:31 +0000 | [diff] [blame] | 851 | else { |
Reid Spencer | cd6f2bf | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 852 | APInt Result(BitWidth, 0); |
Zhou Sheng | b04973e | 2007-02-15 06:36:31 +0000 | [diff] [blame] | 853 | char *pByte = (char*)Result.pVal; |
Reid Spencer | a58f058 | 2007-02-18 20:09:41 +0000 | [diff] [blame] | 854 | for (uint32_t i = 0; i < BitWidth / APINT_WORD_SIZE / 2; ++i) { |
Zhou Sheng | b04973e | 2007-02-15 06:36:31 +0000 | [diff] [blame] | 855 | char Tmp = pByte[i]; |
Reid Spencer | a58f058 | 2007-02-18 20:09:41 +0000 | [diff] [blame] | 856 | pByte[i] = pByte[BitWidth / APINT_WORD_SIZE - 1 - i]; |
| 857 | pByte[BitWidth / APINT_WORD_SIZE - i - 1] = Tmp; |
Zhou Sheng | b04973e | 2007-02-15 06:36:31 +0000 | [diff] [blame] | 858 | } |
| 859 | return Result; |
| 860 | } |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Zhou Sheng | 0b706b1 | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 863 | APInt llvm::APIntOps::GreatestCommonDivisor(const APInt& API1, |
| 864 | const APInt& API2) { |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 865 | APInt A = API1, B = API2; |
| 866 | while (!!B) { |
| 867 | APInt T = B; |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 868 | B = APIntOps::urem(A, B); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 869 | A = T; |
| 870 | } |
| 871 | return A; |
| 872 | } |
Chris Lattner | 6ad4c14 | 2007-02-06 05:38:37 +0000 | [diff] [blame] | 873 | |
Reid Spencer | 1fa111e | 2007-02-27 18:23:40 +0000 | [diff] [blame] | 874 | APInt llvm::APIntOps::RoundDoubleToAPInt(double Double, uint32_t width) { |
Zhou Sheng | d93f00c | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 875 | union { |
| 876 | double D; |
| 877 | uint64_t I; |
| 878 | } T; |
| 879 | T.D = Double; |
Reid Spencer | 30f44f3 | 2007-02-27 01:28:10 +0000 | [diff] [blame] | 880 | |
| 881 | // Get the sign bit from the highest order bit |
Zhou Sheng | d93f00c | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 882 | bool isNeg = T.I >> 63; |
Reid Spencer | 30f44f3 | 2007-02-27 01:28:10 +0000 | [diff] [blame] | 883 | |
| 884 | // Get the 11-bit exponent and adjust for the 1023 bit bias |
Zhou Sheng | d93f00c | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 885 | int64_t exp = ((T.I >> 52) & 0x7ff) - 1023; |
Reid Spencer | 30f44f3 | 2007-02-27 01:28:10 +0000 | [diff] [blame] | 886 | |
| 887 | // If the exponent is negative, the value is < 0 so just return 0. |
Zhou Sheng | d93f00c | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 888 | if (exp < 0) |
Reid Spencer | ff60576 | 2007-02-28 01:30:08 +0000 | [diff] [blame] | 889 | return APInt(width, 0u); |
Reid Spencer | 30f44f3 | 2007-02-27 01:28:10 +0000 | [diff] [blame] | 890 | |
| 891 | // Extract the mantissa by clearing the top 12 bits (sign + exponent). |
| 892 | uint64_t mantissa = (T.I & (~0ULL >> 12)) | 1ULL << 52; |
| 893 | |
| 894 | // If the exponent doesn't shift all bits out of the mantissa |
Zhou Sheng | d93f00c | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 895 | if (exp < 52) |
Reid Spencer | 1fa111e | 2007-02-27 18:23:40 +0000 | [diff] [blame] | 896 | return isNeg ? -APInt(width, mantissa >> (52 - exp)) : |
| 897 | APInt(width, mantissa >> (52 - exp)); |
| 898 | |
| 899 | // If the client didn't provide enough bits for us to shift the mantissa into |
| 900 | // then the result is undefined, just return 0 |
| 901 | if (width <= exp - 52) |
| 902 | return APInt(width, 0); |
Reid Spencer | 30f44f3 | 2007-02-27 01:28:10 +0000 | [diff] [blame] | 903 | |
| 904 | // Otherwise, we have to shift the mantissa bits up to the right location |
Reid Spencer | 1fa111e | 2007-02-27 18:23:40 +0000 | [diff] [blame] | 905 | APInt Tmp(width, mantissa); |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 906 | Tmp = Tmp.shl(exp - 52); |
Zhou Sheng | d93f00c | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 907 | return isNeg ? -Tmp : Tmp; |
| 908 | } |
| 909 | |
Reid Spencer | db3faa6 | 2007-02-13 22:41:58 +0000 | [diff] [blame] | 910 | /// RoundToDouble - This function convert this APInt to a double. |
Zhou Sheng | d93f00c | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 911 | /// The layout for double is as following (IEEE Standard 754): |
| 912 | /// -------------------------------------- |
| 913 | /// | Sign Exponent Fraction Bias | |
| 914 | /// |-------------------------------------- | |
| 915 | /// | 1[63] 11[62-52] 52[51-00] 1023 | |
| 916 | /// -------------------------------------- |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 917 | double APInt::roundToDouble(bool isSigned) const { |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 918 | |
| 919 | // 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] | 920 | if (isSingleWord() || getActiveBits() <= APINT_BITS_PER_WORD) { |
| 921 | if (isSigned) { |
| 922 | int64_t sext = (int64_t(VAL) << (64-BitWidth)) >> (64-BitWidth); |
| 923 | return double(sext); |
| 924 | } else |
| 925 | return double(VAL); |
| 926 | } |
| 927 | |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 928 | // Determine if the value is negative. |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 929 | bool isNeg = isSigned ? (*this)[BitWidth-1] : false; |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 930 | |
| 931 | // Construct the absolute value if we're negative. |
Zhou Sheng | d93f00c | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 932 | APInt Tmp(isNeg ? -(*this) : (*this)); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 933 | |
| 934 | // Figure out how many bits we're using. |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 935 | uint32_t n = Tmp.getActiveBits(); |
Zhou Sheng | d93f00c | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 936 | |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 937 | // The exponent (without bias normalization) is just the number of bits |
| 938 | // we are using. Note that the sign bit is gone since we constructed the |
| 939 | // absolute value. |
| 940 | uint64_t exp = n; |
Zhou Sheng | d93f00c | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 941 | |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 942 | // Return infinity for exponent overflow |
| 943 | if (exp > 1023) { |
| 944 | if (!isSigned || !isNeg) |
Jeff Cohen | 09dfd8e | 2007-03-20 20:42:36 +0000 | [diff] [blame] | 945 | return std::numeric_limits<double>::infinity(); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 946 | else |
Jeff Cohen | 09dfd8e | 2007-03-20 20:42:36 +0000 | [diff] [blame] | 947 | return -std::numeric_limits<double>::infinity(); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 948 | } |
| 949 | exp += 1023; // Increment for 1023 bias |
| 950 | |
| 951 | // Number of bits in mantissa is 52. To obtain the mantissa value, we must |
| 952 | // extract the high 52 bits from the correct words in pVal. |
Zhou Sheng | d93f00c | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 953 | uint64_t mantissa; |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 954 | unsigned hiWord = whichWord(n-1); |
| 955 | if (hiWord == 0) { |
| 956 | mantissa = Tmp.pVal[0]; |
| 957 | if (n > 52) |
| 958 | mantissa >>= n - 52; // shift down, we want the top 52 bits. |
| 959 | } else { |
| 960 | assert(hiWord > 0 && "huh?"); |
| 961 | uint64_t hibits = Tmp.pVal[hiWord] << (52 - n % APINT_BITS_PER_WORD); |
| 962 | uint64_t lobits = Tmp.pVal[hiWord-1] >> (11 + n % APINT_BITS_PER_WORD); |
| 963 | mantissa = hibits | lobits; |
| 964 | } |
| 965 | |
Zhou Sheng | d93f00c | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 966 | // The leading bit of mantissa is implicit, so get rid of it. |
Reid Spencer | 443b570 | 2007-02-18 00:44:22 +0000 | [diff] [blame] | 967 | uint64_t sign = isNeg ? (1ULL << (APINT_BITS_PER_WORD - 1)) : 0; |
Zhou Sheng | d93f00c | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 968 | union { |
| 969 | double D; |
| 970 | uint64_t I; |
| 971 | } T; |
| 972 | T.I = sign | (exp << 52) | mantissa; |
| 973 | return T.D; |
| 974 | } |
| 975 | |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 976 | // Truncate to new width. |
Reid Spencer | 9490077 | 2007-02-28 17:34:32 +0000 | [diff] [blame] | 977 | APInt &APInt::trunc(uint32_t width) { |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 978 | assert(width < BitWidth && "Invalid APInt Truncate request"); |
Reid Spencer | 9af1887 | 2007-12-11 06:53:58 +0000 | [diff] [blame] | 979 | assert(width >= MIN_INT_BITS && "Can't truncate to 0 bits"); |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 980 | uint32_t wordsBefore = getNumWords(); |
| 981 | BitWidth = width; |
| 982 | uint32_t wordsAfter = getNumWords(); |
| 983 | if (wordsBefore != wordsAfter) { |
| 984 | if (wordsAfter == 1) { |
| 985 | uint64_t *tmp = pVal; |
| 986 | VAL = pVal[0]; |
Reid Spencer | 9ac4411 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 987 | delete [] tmp; |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 988 | } else { |
| 989 | uint64_t *newVal = getClearedMemory(wordsAfter); |
| 990 | for (uint32_t i = 0; i < wordsAfter; ++i) |
| 991 | newVal[i] = pVal[i]; |
Reid Spencer | 9ac4411 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 992 | delete [] pVal; |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 993 | pVal = newVal; |
| 994 | } |
| 995 | } |
Reid Spencer | 9490077 | 2007-02-28 17:34:32 +0000 | [diff] [blame] | 996 | return clearUnusedBits(); |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | // Sign extend to a new width. |
Reid Spencer | 9490077 | 2007-02-28 17:34:32 +0000 | [diff] [blame] | 1000 | APInt &APInt::sext(uint32_t width) { |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 1001 | assert(width > BitWidth && "Invalid APInt SignExtend request"); |
Reid Spencer | 9af1887 | 2007-12-11 06:53:58 +0000 | [diff] [blame] | 1002 | assert(width <= MAX_INT_BITS && "Too many bits"); |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 1003 | // If the sign bit isn't set, this is the same as zext. |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1004 | if (!isNegative()) { |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 1005 | zext(width); |
Reid Spencer | 9490077 | 2007-02-28 17:34:32 +0000 | [diff] [blame] | 1006 | return *this; |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | // The sign bit is set. First, get some facts |
| 1010 | uint32_t wordsBefore = getNumWords(); |
| 1011 | uint32_t wordBits = BitWidth % APINT_BITS_PER_WORD; |
| 1012 | BitWidth = width; |
| 1013 | uint32_t wordsAfter = getNumWords(); |
| 1014 | |
| 1015 | // Mask the high order word appropriately |
| 1016 | if (wordsBefore == wordsAfter) { |
| 1017 | uint32_t newWordBits = width % APINT_BITS_PER_WORD; |
| 1018 | // The extension is contained to the wordsBefore-1th word. |
Reid Spencer | 36184ed | 2007-03-02 01:19:42 +0000 | [diff] [blame] | 1019 | uint64_t mask = ~0ULL; |
| 1020 | if (newWordBits) |
| 1021 | mask >>= APINT_BITS_PER_WORD - newWordBits; |
| 1022 | mask <<= wordBits; |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 1023 | if (wordsBefore == 1) |
| 1024 | VAL |= mask; |
| 1025 | else |
| 1026 | pVal[wordsBefore-1] |= mask; |
Reid Spencer | 295e40a | 2007-03-01 23:30:25 +0000 | [diff] [blame] | 1027 | return clearUnusedBits(); |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
Reid Spencer | f30b188 | 2007-02-25 23:54:00 +0000 | [diff] [blame] | 1030 | uint64_t mask = wordBits == 0 ? 0 : ~0ULL << wordBits; |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 1031 | uint64_t *newVal = getMemory(wordsAfter); |
| 1032 | if (wordsBefore == 1) |
| 1033 | newVal[0] = VAL | mask; |
| 1034 | else { |
| 1035 | for (uint32_t i = 0; i < wordsBefore; ++i) |
| 1036 | newVal[i] = pVal[i]; |
| 1037 | newVal[wordsBefore-1] |= mask; |
| 1038 | } |
| 1039 | for (uint32_t i = wordsBefore; i < wordsAfter; i++) |
| 1040 | newVal[i] = -1ULL; |
| 1041 | if (wordsBefore != 1) |
Reid Spencer | 9ac4411 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 1042 | delete [] pVal; |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 1043 | pVal = newVal; |
Reid Spencer | 9490077 | 2007-02-28 17:34:32 +0000 | [diff] [blame] | 1044 | return clearUnusedBits(); |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | // Zero extend to a new width. |
Reid Spencer | 9490077 | 2007-02-28 17:34:32 +0000 | [diff] [blame] | 1048 | APInt &APInt::zext(uint32_t width) { |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 1049 | assert(width > BitWidth && "Invalid APInt ZeroExtend request"); |
Reid Spencer | 9af1887 | 2007-12-11 06:53:58 +0000 | [diff] [blame] | 1050 | assert(width <= MAX_INT_BITS && "Too many bits"); |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 1051 | uint32_t wordsBefore = getNumWords(); |
| 1052 | BitWidth = width; |
| 1053 | uint32_t wordsAfter = getNumWords(); |
| 1054 | if (wordsBefore != wordsAfter) { |
| 1055 | uint64_t *newVal = getClearedMemory(wordsAfter); |
| 1056 | if (wordsBefore == 1) |
| 1057 | newVal[0] = VAL; |
| 1058 | else |
| 1059 | for (uint32_t i = 0; i < wordsBefore; ++i) |
| 1060 | newVal[i] = pVal[i]; |
| 1061 | if (wordsBefore != 1) |
Reid Spencer | 9ac4411 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 1062 | delete [] pVal; |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 1063 | pVal = newVal; |
| 1064 | } |
Reid Spencer | 9490077 | 2007-02-28 17:34:32 +0000 | [diff] [blame] | 1065 | return *this; |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
Reid Spencer | 68e2300 | 2007-03-01 17:15:32 +0000 | [diff] [blame] | 1068 | APInt &APInt::zextOrTrunc(uint32_t width) { |
| 1069 | if (BitWidth < width) |
| 1070 | return zext(width); |
| 1071 | if (BitWidth > width) |
| 1072 | return trunc(width); |
| 1073 | return *this; |
| 1074 | } |
| 1075 | |
| 1076 | APInt &APInt::sextOrTrunc(uint32_t width) { |
| 1077 | if (BitWidth < width) |
| 1078 | return sext(width); |
| 1079 | if (BitWidth > width) |
| 1080 | return trunc(width); |
| 1081 | return *this; |
| 1082 | } |
| 1083 | |
Zhou Sheng | ff4304f | 2007-02-09 07:48:24 +0000 | [diff] [blame] | 1084 | /// Arithmetic right-shift this APInt by shiftAmt. |
Zhou Sheng | 0b706b1 | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1085 | /// @brief Arithmetic right-shift function. |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 1086 | APInt APInt::ashr(uint32_t shiftAmt) const { |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1087 | assert(shiftAmt <= BitWidth && "Invalid shift amount"); |
Reid Spencer | 46f9c94 | 2007-03-02 22:39:11 +0000 | [diff] [blame] | 1088 | // Handle a degenerate case |
| 1089 | if (shiftAmt == 0) |
| 1090 | return *this; |
| 1091 | |
| 1092 | // Handle single word shifts with built-in ashr |
Reid Spencer | 24c4a8f | 2007-02-25 01:56:07 +0000 | [diff] [blame] | 1093 | if (isSingleWord()) { |
| 1094 | if (shiftAmt == BitWidth) |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1095 | return APInt(BitWidth, 0); // undefined |
| 1096 | else { |
| 1097 | uint32_t SignBit = APINT_BITS_PER_WORD - BitWidth; |
Reid Spencer | 24c4a8f | 2007-02-25 01:56:07 +0000 | [diff] [blame] | 1098 | return APInt(BitWidth, |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1099 | (((int64_t(VAL) << SignBit) >> SignBit) >> shiftAmt)); |
| 1100 | } |
Zhou Sheng | 0b706b1 | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1101 | } |
Reid Spencer | 24c4a8f | 2007-02-25 01:56:07 +0000 | [diff] [blame] | 1102 | |
Reid Spencer | 46f9c94 | 2007-03-02 22:39:11 +0000 | [diff] [blame] | 1103 | // If all the bits were shifted out, the result is, technically, undefined. |
| 1104 | // We return -1 if it was negative, 0 otherwise. We check this early to avoid |
| 1105 | // issues in the algorithm below. |
Chris Lattner | a5ae15e | 2007-05-03 18:15:36 +0000 | [diff] [blame] | 1106 | if (shiftAmt == BitWidth) { |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1107 | if (isNegative()) |
| 1108 | return APInt(BitWidth, -1ULL); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 1109 | else |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1110 | return APInt(BitWidth, 0); |
Chris Lattner | a5ae15e | 2007-05-03 18:15:36 +0000 | [diff] [blame] | 1111 | } |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1112 | |
| 1113 | // Create some space for the result. |
| 1114 | uint64_t * val = new uint64_t[getNumWords()]; |
| 1115 | |
Reid Spencer | 46f9c94 | 2007-03-02 22:39:11 +0000 | [diff] [blame] | 1116 | // Compute some values needed by the following shift algorithms |
| 1117 | uint32_t wordShift = shiftAmt % APINT_BITS_PER_WORD; // bits to shift per word |
| 1118 | uint32_t offset = shiftAmt / APINT_BITS_PER_WORD; // word offset for shift |
| 1119 | uint32_t breakWord = getNumWords() - 1 - offset; // last word affected |
| 1120 | uint32_t bitsInWord = whichBit(BitWidth); // how many bits in last word? |
| 1121 | if (bitsInWord == 0) |
| 1122 | bitsInWord = APINT_BITS_PER_WORD; |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1123 | |
| 1124 | // If we are shifting whole words, just move whole words |
| 1125 | if (wordShift == 0) { |
Reid Spencer | 46f9c94 | 2007-03-02 22:39:11 +0000 | [diff] [blame] | 1126 | // Move the words containing significant bits |
| 1127 | for (uint32_t i = 0; i <= breakWord; ++i) |
| 1128 | val[i] = pVal[i+offset]; // move whole word |
| 1129 | |
| 1130 | // Adjust the top significant word for sign bit fill, if negative |
| 1131 | if (isNegative()) |
| 1132 | if (bitsInWord < APINT_BITS_PER_WORD) |
| 1133 | val[breakWord] |= ~0ULL << bitsInWord; // set high bits |
| 1134 | } else { |
| 1135 | // Shift the low order words |
| 1136 | for (uint32_t i = 0; i < breakWord; ++i) { |
| 1137 | // This combines the shifted corresponding word with the low bits from |
| 1138 | // the next word (shifted into this word's high bits). |
| 1139 | val[i] = (pVal[i+offset] >> wordShift) | |
| 1140 | (pVal[i+offset+1] << (APINT_BITS_PER_WORD - wordShift)); |
| 1141 | } |
| 1142 | |
| 1143 | // Shift the break word. In this case there are no bits from the next word |
| 1144 | // to include in this word. |
| 1145 | val[breakWord] = pVal[breakWord+offset] >> wordShift; |
| 1146 | |
| 1147 | // Deal with sign extenstion in the break word, and possibly the word before |
| 1148 | // it. |
Chris Lattner | a5ae15e | 2007-05-03 18:15:36 +0000 | [diff] [blame] | 1149 | if (isNegative()) { |
Reid Spencer | 46f9c94 | 2007-03-02 22:39:11 +0000 | [diff] [blame] | 1150 | if (wordShift > bitsInWord) { |
| 1151 | if (breakWord > 0) |
| 1152 | val[breakWord-1] |= |
| 1153 | ~0ULL << (APINT_BITS_PER_WORD - (wordShift - bitsInWord)); |
| 1154 | val[breakWord] |= ~0ULL; |
| 1155 | } else |
| 1156 | val[breakWord] |= (~0ULL << (bitsInWord - wordShift)); |
Chris Lattner | a5ae15e | 2007-05-03 18:15:36 +0000 | [diff] [blame] | 1157 | } |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
Reid Spencer | 46f9c94 | 2007-03-02 22:39:11 +0000 | [diff] [blame] | 1160 | // Remaining words are 0 or -1, just assign them. |
| 1161 | uint64_t fillValue = (isNegative() ? -1ULL : 0); |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1162 | for (uint32_t i = breakWord+1; i < getNumWords(); ++i) |
Reid Spencer | 46f9c94 | 2007-03-02 22:39:11 +0000 | [diff] [blame] | 1163 | val[i] = fillValue; |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1164 | return APInt(val, BitWidth).clearUnusedBits(); |
Zhou Sheng | 0b706b1 | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Zhou Sheng | ff4304f | 2007-02-09 07:48:24 +0000 | [diff] [blame] | 1167 | /// Logical right-shift this APInt by shiftAmt. |
Zhou Sheng | 0b706b1 | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1168 | /// @brief Logical right-shift function. |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 1169 | APInt APInt::lshr(uint32_t shiftAmt) const { |
Chris Lattner | a5ae15e | 2007-05-03 18:15:36 +0000 | [diff] [blame] | 1170 | if (isSingleWord()) { |
Reid Spencer | 24c4a8f | 2007-02-25 01:56:07 +0000 | [diff] [blame] | 1171 | if (shiftAmt == BitWidth) |
| 1172 | return APInt(BitWidth, 0); |
| 1173 | else |
| 1174 | return APInt(BitWidth, this->VAL >> shiftAmt); |
Chris Lattner | a5ae15e | 2007-05-03 18:15:36 +0000 | [diff] [blame] | 1175 | } |
Reid Spencer | 24c4a8f | 2007-02-25 01:56:07 +0000 | [diff] [blame] | 1176 | |
Reid Spencer | ba81c2b | 2007-02-26 01:19:48 +0000 | [diff] [blame] | 1177 | // If all the bits were shifted out, the result is 0. This avoids issues |
| 1178 | // with shifting by the size of the integer type, which produces undefined |
| 1179 | // results. We define these "undefined results" to always be 0. |
| 1180 | if (shiftAmt == BitWidth) |
| 1181 | return APInt(BitWidth, 0); |
| 1182 | |
Reid Spencer | 02ae8b7 | 2007-05-17 06:26:29 +0000 | [diff] [blame] | 1183 | // If none of the bits are shifted out, the result is *this. This avoids |
| 1184 | // issues with shifting byt he size of the integer type, which produces |
| 1185 | // undefined results in the code below. This is also an optimization. |
| 1186 | if (shiftAmt == 0) |
| 1187 | return *this; |
| 1188 | |
Reid Spencer | ba81c2b | 2007-02-26 01:19:48 +0000 | [diff] [blame] | 1189 | // Create some space for the result. |
| 1190 | uint64_t * val = new uint64_t[getNumWords()]; |
| 1191 | |
| 1192 | // If we are shifting less than a word, compute the shift with a simple carry |
| 1193 | if (shiftAmt < APINT_BITS_PER_WORD) { |
| 1194 | uint64_t carry = 0; |
| 1195 | for (int i = getNumWords()-1; i >= 0; --i) { |
Reid Spencer | af8fb19 | 2007-03-01 05:39:56 +0000 | [diff] [blame] | 1196 | val[i] = (pVal[i] >> shiftAmt) | carry; |
Reid Spencer | ba81c2b | 2007-02-26 01:19:48 +0000 | [diff] [blame] | 1197 | carry = pVal[i] << (APINT_BITS_PER_WORD - shiftAmt); |
| 1198 | } |
| 1199 | return APInt(val, BitWidth).clearUnusedBits(); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
Reid Spencer | ba81c2b | 2007-02-26 01:19:48 +0000 | [diff] [blame] | 1202 | // Compute some values needed by the remaining shift algorithms |
| 1203 | uint32_t wordShift = shiftAmt % APINT_BITS_PER_WORD; |
| 1204 | uint32_t offset = shiftAmt / APINT_BITS_PER_WORD; |
| 1205 | |
| 1206 | // If we are shifting whole words, just move whole words |
| 1207 | if (wordShift == 0) { |
| 1208 | for (uint32_t i = 0; i < getNumWords() - offset; ++i) |
| 1209 | val[i] = pVal[i+offset]; |
| 1210 | for (uint32_t i = getNumWords()-offset; i < getNumWords(); i++) |
| 1211 | val[i] = 0; |
| 1212 | return APInt(val,BitWidth).clearUnusedBits(); |
| 1213 | } |
| 1214 | |
| 1215 | // Shift the low order words |
| 1216 | uint32_t breakWord = getNumWords() - offset -1; |
| 1217 | for (uint32_t i = 0; i < breakWord; ++i) |
Reid Spencer | af8fb19 | 2007-03-01 05:39:56 +0000 | [diff] [blame] | 1218 | val[i] = (pVal[i+offset] >> wordShift) | |
| 1219 | (pVal[i+offset+1] << (APINT_BITS_PER_WORD - wordShift)); |
Reid Spencer | ba81c2b | 2007-02-26 01:19:48 +0000 | [diff] [blame] | 1220 | // Shift the break word. |
| 1221 | val[breakWord] = pVal[breakWord+offset] >> wordShift; |
| 1222 | |
| 1223 | // Remaining words are 0 |
| 1224 | for (uint32_t i = breakWord+1; i < getNumWords(); ++i) |
| 1225 | val[i] = 0; |
| 1226 | return APInt(val, BitWidth).clearUnusedBits(); |
Zhou Sheng | 0b706b1 | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
Zhou Sheng | ff4304f | 2007-02-09 07:48:24 +0000 | [diff] [blame] | 1229 | /// Left-shift this APInt by shiftAmt. |
Zhou Sheng | 0b706b1 | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1230 | /// @brief Left-shift function. |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 1231 | APInt APInt::shl(uint32_t shiftAmt) const { |
Reid Spencer | 5bce854 | 2007-02-24 20:19:37 +0000 | [diff] [blame] | 1232 | assert(shiftAmt <= BitWidth && "Invalid shift amount"); |
Reid Spencer | 8755380 | 2007-02-25 00:56:44 +0000 | [diff] [blame] | 1233 | if (isSingleWord()) { |
Reid Spencer | 5bce854 | 2007-02-24 20:19:37 +0000 | [diff] [blame] | 1234 | if (shiftAmt == BitWidth) |
Reid Spencer | 8755380 | 2007-02-25 00:56:44 +0000 | [diff] [blame] | 1235 | return APInt(BitWidth, 0); // avoid undefined shift results |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1236 | return APInt(BitWidth, VAL << shiftAmt); |
Zhou Sheng | 0b706b1 | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1237 | } |
Reid Spencer | 5bce854 | 2007-02-24 20:19:37 +0000 | [diff] [blame] | 1238 | |
Reid Spencer | 8755380 | 2007-02-25 00:56:44 +0000 | [diff] [blame] | 1239 | // If all the bits were shifted out, the result is 0. This avoids issues |
| 1240 | // with shifting by the size of the integer type, which produces undefined |
| 1241 | // results. We define these "undefined results" to always be 0. |
| 1242 | if (shiftAmt == BitWidth) |
| 1243 | return APInt(BitWidth, 0); |
| 1244 | |
Reid Spencer | 92c7283 | 2007-05-12 18:01:57 +0000 | [diff] [blame] | 1245 | // If none of the bits are shifted out, the result is *this. This avoids a |
| 1246 | // lshr by the words size in the loop below which can produce incorrect |
| 1247 | // results. It also avoids the expensive computation below for a common case. |
| 1248 | if (shiftAmt == 0) |
| 1249 | return *this; |
| 1250 | |
Reid Spencer | 8755380 | 2007-02-25 00:56:44 +0000 | [diff] [blame] | 1251 | // Create some space for the result. |
| 1252 | uint64_t * val = new uint64_t[getNumWords()]; |
| 1253 | |
| 1254 | // If we are shifting less than a word, do it the easy way |
| 1255 | if (shiftAmt < APINT_BITS_PER_WORD) { |
| 1256 | uint64_t carry = 0; |
Reid Spencer | 8755380 | 2007-02-25 00:56:44 +0000 | [diff] [blame] | 1257 | for (uint32_t i = 0; i < getNumWords(); i++) { |
| 1258 | val[i] = pVal[i] << shiftAmt | carry; |
| 1259 | carry = pVal[i] >> (APINT_BITS_PER_WORD - shiftAmt); |
| 1260 | } |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 1261 | return APInt(val, BitWidth).clearUnusedBits(); |
Reid Spencer | 5bce854 | 2007-02-24 20:19:37 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
Reid Spencer | 8755380 | 2007-02-25 00:56:44 +0000 | [diff] [blame] | 1264 | // Compute some values needed by the remaining shift algorithms |
| 1265 | uint32_t wordShift = shiftAmt % APINT_BITS_PER_WORD; |
| 1266 | uint32_t offset = shiftAmt / APINT_BITS_PER_WORD; |
| 1267 | |
| 1268 | // If we are shifting whole words, just move whole words |
| 1269 | if (wordShift == 0) { |
| 1270 | for (uint32_t i = 0; i < offset; i++) |
| 1271 | val[i] = 0; |
| 1272 | for (uint32_t i = offset; i < getNumWords(); i++) |
| 1273 | val[i] = pVal[i-offset]; |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 1274 | return APInt(val,BitWidth).clearUnusedBits(); |
Reid Spencer | 5bce854 | 2007-02-24 20:19:37 +0000 | [diff] [blame] | 1275 | } |
Reid Spencer | 8755380 | 2007-02-25 00:56:44 +0000 | [diff] [blame] | 1276 | |
| 1277 | // Copy whole words from this to Result. |
| 1278 | uint32_t i = getNumWords() - 1; |
| 1279 | for (; i > offset; --i) |
| 1280 | val[i] = pVal[i-offset] << wordShift | |
| 1281 | pVal[i-offset-1] >> (APINT_BITS_PER_WORD - wordShift); |
Reid Spencer | 438d71e | 2007-02-25 01:08:58 +0000 | [diff] [blame] | 1282 | val[offset] = pVal[0] << wordShift; |
Reid Spencer | 8755380 | 2007-02-25 00:56:44 +0000 | [diff] [blame] | 1283 | for (i = 0; i < offset; ++i) |
| 1284 | val[i] = 0; |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 1285 | return APInt(val, BitWidth).clearUnusedBits(); |
Zhou Sheng | 0b706b1 | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
Reid Spencer | 19dc32a | 2007-05-13 23:44:59 +0000 | [diff] [blame] | 1288 | APInt APInt::rotl(uint32_t rotateAmt) const { |
Reid Spencer | 69944e8 | 2007-05-14 00:15:28 +0000 | [diff] [blame] | 1289 | if (rotateAmt == 0) |
| 1290 | return *this; |
Reid Spencer | 19dc32a | 2007-05-13 23:44:59 +0000 | [diff] [blame] | 1291 | // Don't get too fancy, just use existing shift/or facilities |
| 1292 | APInt hi(*this); |
| 1293 | APInt lo(*this); |
| 1294 | hi.shl(rotateAmt); |
| 1295 | lo.lshr(BitWidth - rotateAmt); |
| 1296 | return hi | lo; |
| 1297 | } |
| 1298 | |
| 1299 | APInt APInt::rotr(uint32_t rotateAmt) const { |
Reid Spencer | 69944e8 | 2007-05-14 00:15:28 +0000 | [diff] [blame] | 1300 | if (rotateAmt == 0) |
| 1301 | return *this; |
Reid Spencer | 19dc32a | 2007-05-13 23:44:59 +0000 | [diff] [blame] | 1302 | // Don't get too fancy, just use existing shift/or facilities |
| 1303 | APInt hi(*this); |
| 1304 | APInt lo(*this); |
| 1305 | lo.lshr(rotateAmt); |
| 1306 | hi.shl(BitWidth - rotateAmt); |
| 1307 | return hi | lo; |
| 1308 | } |
Reid Spencer | af8fb19 | 2007-03-01 05:39:56 +0000 | [diff] [blame] | 1309 | |
| 1310 | // Square Root - this method computes and returns the square root of "this". |
| 1311 | // Three mechanisms are used for computation. For small values (<= 5 bits), |
| 1312 | // a table lookup is done. This gets some performance for common cases. For |
| 1313 | // values using less than 52 bits, the value is converted to double and then |
| 1314 | // the libc sqrt function is called. The result is rounded and then converted |
| 1315 | // back to a uint64_t which is then used to construct the result. Finally, |
| 1316 | // the Babylonian method for computing square roots is used. |
| 1317 | APInt APInt::sqrt() const { |
| 1318 | |
| 1319 | // Determine the magnitude of the value. |
| 1320 | uint32_t magnitude = getActiveBits(); |
| 1321 | |
| 1322 | // Use a fast table for some small values. This also gets rid of some |
| 1323 | // rounding errors in libc sqrt for small values. |
| 1324 | if (magnitude <= 5) { |
Reid Spencer | 4e1e87f | 2007-03-01 17:47:31 +0000 | [diff] [blame] | 1325 | static const uint8_t results[32] = { |
Reid Spencer | b5ca2cd | 2007-03-01 06:23:32 +0000 | [diff] [blame] | 1326 | /* 0 */ 0, |
| 1327 | /* 1- 2 */ 1, 1, |
| 1328 | /* 3- 6 */ 2, 2, 2, 2, |
| 1329 | /* 7-12 */ 3, 3, 3, 3, 3, 3, |
| 1330 | /* 13-20 */ 4, 4, 4, 4, 4, 4, 4, 4, |
| 1331 | /* 21-30 */ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
| 1332 | /* 31 */ 6 |
| 1333 | }; |
| 1334 | return APInt(BitWidth, results[ (isSingleWord() ? VAL : pVal[0]) ]); |
Reid Spencer | af8fb19 | 2007-03-01 05:39:56 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | // If the magnitude of the value fits in less than 52 bits (the precision of |
| 1338 | // an IEEE double precision floating point value), then we can use the |
| 1339 | // libc sqrt function which will probably use a hardware sqrt computation. |
| 1340 | // This should be faster than the algorithm below. |
Jeff Cohen | ca5183d | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 1341 | if (magnitude < 52) { |
| 1342 | #ifdef _MSC_VER |
| 1343 | // Amazingly, VC++ doesn't have round(). |
| 1344 | return APInt(BitWidth, |
| 1345 | uint64_t(::sqrt(double(isSingleWord()?VAL:pVal[0]))) + 0.5); |
| 1346 | #else |
Reid Spencer | af8fb19 | 2007-03-01 05:39:56 +0000 | [diff] [blame] | 1347 | return APInt(BitWidth, |
| 1348 | uint64_t(::round(::sqrt(double(isSingleWord()?VAL:pVal[0]))))); |
Jeff Cohen | ca5183d | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 1349 | #endif |
| 1350 | } |
Reid Spencer | af8fb19 | 2007-03-01 05:39:56 +0000 | [diff] [blame] | 1351 | |
| 1352 | // Okay, all the short cuts are exhausted. We must compute it. The following |
| 1353 | // is a classical Babylonian method for computing the square root. This code |
| 1354 | // was adapted to APINt from a wikipedia article on such computations. |
| 1355 | // See http://www.wikipedia.org/ and go to the page named |
| 1356 | // Calculate_an_integer_square_root. |
| 1357 | uint32_t nbits = BitWidth, i = 4; |
| 1358 | APInt testy(BitWidth, 16); |
| 1359 | APInt x_old(BitWidth, 1); |
| 1360 | APInt x_new(BitWidth, 0); |
| 1361 | APInt two(BitWidth, 2); |
| 1362 | |
| 1363 | // Select a good starting value using binary logarithms. |
| 1364 | for (;; i += 2, testy = testy.shl(2)) |
| 1365 | if (i >= nbits || this->ule(testy)) { |
| 1366 | x_old = x_old.shl(i / 2); |
| 1367 | break; |
| 1368 | } |
| 1369 | |
| 1370 | // Use the Babylonian method to arrive at the integer square root: |
| 1371 | for (;;) { |
| 1372 | x_new = (this->udiv(x_old) + x_old).udiv(two); |
| 1373 | if (x_old.ule(x_new)) |
| 1374 | break; |
| 1375 | x_old = x_new; |
| 1376 | } |
| 1377 | |
| 1378 | // Make sure we return the closest approximation |
Reid Spencer | f09aef7 | 2007-03-02 04:21:55 +0000 | [diff] [blame] | 1379 | // NOTE: The rounding calculation below is correct. It will produce an |
| 1380 | // off-by-one discrepancy with results from pari/gp. That discrepancy has been |
| 1381 | // determined to be a rounding issue with pari/gp as it begins to use a |
| 1382 | // floating point representation after 192 bits. There are no discrepancies |
| 1383 | // between this algorithm and pari/gp for bit widths < 192 bits. |
Reid Spencer | af8fb19 | 2007-03-01 05:39:56 +0000 | [diff] [blame] | 1384 | APInt square(x_old * x_old); |
| 1385 | APInt nextSquare((x_old + 1) * (x_old +1)); |
| 1386 | if (this->ult(square)) |
| 1387 | return x_old; |
Reid Spencer | f09aef7 | 2007-03-02 04:21:55 +0000 | [diff] [blame] | 1388 | else if (this->ule(nextSquare)) { |
| 1389 | APInt midpoint((nextSquare - square).udiv(two)); |
| 1390 | APInt offset(*this - square); |
| 1391 | if (offset.ult(midpoint)) |
Reid Spencer | af8fb19 | 2007-03-01 05:39:56 +0000 | [diff] [blame] | 1392 | return x_old; |
Reid Spencer | f09aef7 | 2007-03-02 04:21:55 +0000 | [diff] [blame] | 1393 | else |
| 1394 | return x_old + 1; |
| 1395 | } else |
Reid Spencer | af8fb19 | 2007-03-01 05:39:56 +0000 | [diff] [blame] | 1396 | assert(0 && "Error in APInt::sqrt computation"); |
| 1397 | return x_old + 1; |
| 1398 | } |
| 1399 | |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1400 | /// Implementation of Knuth's Algorithm D (Division of nonnegative integers) |
| 1401 | /// from "Art of Computer Programming, Volume 2", section 4.3.1, p. 272. The |
| 1402 | /// variables here have the same names as in the algorithm. Comments explain |
| 1403 | /// the algorithm and any deviation from it. |
| 1404 | static void KnuthDiv(uint32_t *u, uint32_t *v, uint32_t *q, uint32_t* r, |
| 1405 | uint32_t m, uint32_t n) { |
| 1406 | assert(u && "Must provide dividend"); |
| 1407 | assert(v && "Must provide divisor"); |
| 1408 | assert(q && "Must provide quotient"); |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1409 | assert(u != v && u != q && v != q && "Must us different memory"); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1410 | assert(n>1 && "n must be > 1"); |
| 1411 | |
| 1412 | // Knuth uses the value b as the base of the number system. In our case b |
| 1413 | // is 2^31 so we just set it to -1u. |
| 1414 | uint64_t b = uint64_t(1) << 32; |
| 1415 | |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1416 | DEBUG(cerr << "KnuthDiv: m=" << m << " n=" << n << '\n'); |
| 1417 | DEBUG(cerr << "KnuthDiv: original:"); |
| 1418 | DEBUG(for (int i = m+n; i >=0; i--) cerr << " " << std::setbase(16) << u[i]); |
| 1419 | DEBUG(cerr << " by"); |
| 1420 | DEBUG(for (int i = n; i >0; i--) cerr << " " << std::setbase(16) << v[i-1]); |
| 1421 | DEBUG(cerr << '\n'); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1422 | // D1. [Normalize.] Set d = b / (v[n-1] + 1) and multiply all the digits of |
| 1423 | // u and v by d. Note that we have taken Knuth's advice here to use a power |
| 1424 | // of 2 value for d such that d * v[n-1] >= b/2 (b is the base). A power of |
| 1425 | // 2 allows us to shift instead of multiply and it is easy to determine the |
| 1426 | // shift amount from the leading zeros. We are basically normalizing the u |
| 1427 | // and v so that its high bits are shifted to the top of v's range without |
| 1428 | // overflow. Note that this can require an extra word in u so that u must |
| 1429 | // be of length m+n+1. |
| 1430 | uint32_t shift = CountLeadingZeros_32(v[n-1]); |
| 1431 | uint32_t v_carry = 0; |
| 1432 | uint32_t u_carry = 0; |
| 1433 | if (shift) { |
| 1434 | for (uint32_t i = 0; i < m+n; ++i) { |
| 1435 | uint32_t u_tmp = u[i] >> (32 - shift); |
| 1436 | u[i] = (u[i] << shift) | u_carry; |
| 1437 | u_carry = u_tmp; |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 1438 | } |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1439 | for (uint32_t i = 0; i < n; ++i) { |
| 1440 | uint32_t v_tmp = v[i] >> (32 - shift); |
| 1441 | v[i] = (v[i] << shift) | v_carry; |
| 1442 | v_carry = v_tmp; |
| 1443 | } |
| 1444 | } |
| 1445 | u[m+n] = u_carry; |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1446 | DEBUG(cerr << "KnuthDiv: normal:"); |
| 1447 | DEBUG(for (int i = m+n; i >=0; i--) cerr << " " << std::setbase(16) << u[i]); |
| 1448 | DEBUG(cerr << " by"); |
| 1449 | DEBUG(for (int i = n; i >0; i--) cerr << " " << std::setbase(16) << v[i-1]); |
| 1450 | DEBUG(cerr << '\n'); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1451 | |
| 1452 | // D2. [Initialize j.] Set j to m. This is the loop counter over the places. |
| 1453 | int j = m; |
| 1454 | do { |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1455 | DEBUG(cerr << "KnuthDiv: quotient digit #" << j << '\n'); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1456 | // D3. [Calculate q'.]. |
| 1457 | // Set qp = (u[j+n]*b + u[j+n-1]) / v[n-1]. (qp=qprime=q') |
| 1458 | // Set rp = (u[j+n]*b + u[j+n-1]) % v[n-1]. (rp=rprime=r') |
| 1459 | // Now test if qp == b or qp*v[n-2] > b*rp + u[j+n-2]; if so, decrease |
| 1460 | // qp by 1, inrease rp by v[n-1], and repeat this test if rp < b. The test |
| 1461 | // on v[n-2] determines at high speed most of the cases in which the trial |
| 1462 | // value qp is one too large, and it eliminates all cases where qp is two |
| 1463 | // too large. |
Reid Spencer | 9290463 | 2007-02-23 01:57:13 +0000 | [diff] [blame] | 1464 | 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] | 1465 | DEBUG(cerr << "KnuthDiv: dividend == " << dividend << '\n'); |
Reid Spencer | 9290463 | 2007-02-23 01:57:13 +0000 | [diff] [blame] | 1466 | uint64_t qp = dividend / v[n-1]; |
| 1467 | uint64_t rp = dividend % v[n-1]; |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1468 | if (qp == b || qp*v[n-2] > b*rp + u[j+n-2]) { |
| 1469 | qp--; |
| 1470 | rp += v[n-1]; |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 1471 | 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] | 1472 | qp--; |
Reid Spencer | 9290463 | 2007-02-23 01:57:13 +0000 | [diff] [blame] | 1473 | } |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1474 | DEBUG(cerr << "KnuthDiv: qp == " << qp << ", rp == " << rp << '\n'); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1475 | |
Reid Spencer | 9290463 | 2007-02-23 01:57:13 +0000 | [diff] [blame] | 1476 | // D4. [Multiply and subtract.] Replace (u[j+n]u[j+n-1]...u[j]) with |
| 1477 | // (u[j+n]u[j+n-1]..u[j]) - qp * (v[n-1]...v[1]v[0]). This computation |
| 1478 | // consists of a simple multiplication by a one-place number, combined with |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 1479 | // a subtraction. |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1480 | bool isNeg = false; |
Reid Spencer | 9290463 | 2007-02-23 01:57:13 +0000 | [diff] [blame] | 1481 | for (uint32_t i = 0; i < n; ++i) { |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 1482 | 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] | 1483 | uint64_t subtrahend = uint64_t(qp) * uint64_t(v[i]); |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 1484 | bool borrow = subtrahend > u_tmp; |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1485 | DEBUG(cerr << "KnuthDiv: u_tmp == " << u_tmp |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 1486 | << ", subtrahend == " << subtrahend |
| 1487 | << ", borrow = " << borrow << '\n'); |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1488 | |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 1489 | uint64_t result = u_tmp - subtrahend; |
| 1490 | uint32_t k = j + i; |
| 1491 | u[k++] = result & (b-1); // subtract low word |
| 1492 | u[k++] = result >> 32; // subtract high word |
| 1493 | while (borrow && k <= m+n) { // deal with borrow to the left |
| 1494 | borrow = u[k] == 0; |
| 1495 | u[k]--; |
| 1496 | k++; |
| 1497 | } |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1498 | isNeg |= borrow; |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 1499 | DEBUG(cerr << "KnuthDiv: u[j+i] == " << u[j+i] << ", u[j+i+1] == " << |
| 1500 | u[j+i+1] << '\n'); |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1501 | } |
| 1502 | DEBUG(cerr << "KnuthDiv: after subtraction:"); |
| 1503 | DEBUG(for (int i = m+n; i >=0; i--) cerr << " " << u[i]); |
| 1504 | DEBUG(cerr << '\n'); |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 1505 | // The digits (u[j+n]...u[j]) should be kept positive; if the result of |
| 1506 | // this step is actually negative, (u[j+n]...u[j]) should be left as the |
| 1507 | // 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] | 1508 | // the true value, and a "borrow" to the left should be remembered. |
| 1509 | // |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1510 | if (isNeg) { |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 1511 | bool carry = true; // true because b's complement is "complement + 1" |
| 1512 | for (uint32_t i = 0; i <= m+n; ++i) { |
| 1513 | u[i] = ~u[i] + carry; // b's complement |
| 1514 | carry = carry && u[i] == 0; |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1515 | } |
Reid Spencer | 9290463 | 2007-02-23 01:57:13 +0000 | [diff] [blame] | 1516 | } |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1517 | DEBUG(cerr << "KnuthDiv: after complement:"); |
| 1518 | DEBUG(for (int i = m+n; i >=0; i--) cerr << " " << u[i]); |
| 1519 | DEBUG(cerr << '\n'); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1520 | |
| 1521 | // D5. [Test remainder.] Set q[j] = qp. If the result of step D4 was |
| 1522 | // negative, go to step D6; otherwise go on to step D7. |
| 1523 | q[j] = qp; |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1524 | if (isNeg) { |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1525 | // D6. [Add back]. The probability that this step is necessary is very |
| 1526 | // 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] | 1527 | // this possibility. Decrease q[j] by 1 |
| 1528 | q[j]--; |
| 1529 | // and add (0v[n-1]...v[1]v[0]) to (u[j+n]u[j+n-1]...u[j+1]u[j]). |
| 1530 | // A carry will occur to the left of u[j+n], and it should be ignored |
| 1531 | // since it cancels with the borrow that occurred in D4. |
| 1532 | bool carry = false; |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1533 | for (uint32_t i = 0; i < n; i++) { |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1534 | uint32_t limit = std::min(u[j+i],v[i]); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1535 | u[j+i] += v[i] + carry; |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1536 | carry = u[j+i] < limit || (carry && u[j+i] == limit); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1537 | } |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1538 | u[j+n] += carry; |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1539 | } |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1540 | DEBUG(cerr << "KnuthDiv: after correction:"); |
| 1541 | DEBUG(for (int i = m+n; i >=0; i--) cerr <<" " << u[i]); |
| 1542 | DEBUG(cerr << "\nKnuthDiv: digit result = " << q[j] << '\n'); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1543 | |
Reid Spencer | 9290463 | 2007-02-23 01:57:13 +0000 | [diff] [blame] | 1544 | // D7. [Loop on j.] Decrease j by one. Now if j >= 0, go back to D3. |
| 1545 | } while (--j >= 0); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1546 | |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1547 | DEBUG(cerr << "KnuthDiv: quotient:"); |
| 1548 | DEBUG(for (int i = m; i >=0; i--) cerr <<" " << q[i]); |
| 1549 | DEBUG(cerr << '\n'); |
| 1550 | |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1551 | // D8. [Unnormalize]. Now q[...] is the desired quotient, and the desired |
| 1552 | // remainder may be obtained by dividing u[...] by d. If r is non-null we |
| 1553 | // compute the remainder (urem uses this). |
| 1554 | if (r) { |
| 1555 | // The value d is expressed by the "shift" value above since we avoided |
| 1556 | // multiplication by d by using a shift left. So, all we have to do is |
| 1557 | // shift right here. In order to mak |
Reid Spencer | 1050ec5 | 2007-02-24 20:38:01 +0000 | [diff] [blame] | 1558 | if (shift) { |
| 1559 | uint32_t carry = 0; |
| 1560 | DEBUG(cerr << "KnuthDiv: remainder:"); |
| 1561 | for (int i = n-1; i >= 0; i--) { |
| 1562 | r[i] = (u[i] >> shift) | carry; |
| 1563 | carry = u[i] << (32 - shift); |
| 1564 | DEBUG(cerr << " " << r[i]); |
| 1565 | } |
| 1566 | } else { |
| 1567 | for (int i = n-1; i >= 0; i--) { |
| 1568 | r[i] = u[i]; |
| 1569 | DEBUG(cerr << " " << r[i]); |
| 1570 | } |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1571 | } |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1572 | DEBUG(cerr << '\n'); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1573 | } |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1574 | DEBUG(cerr << std::setbase(10) << '\n'); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1575 | } |
| 1576 | |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1577 | void APInt::divide(const APInt LHS, uint32_t lhsWords, |
| 1578 | const APInt &RHS, uint32_t rhsWords, |
| 1579 | APInt *Quotient, APInt *Remainder) |
| 1580 | { |
| 1581 | assert(lhsWords >= rhsWords && "Fractional result"); |
| 1582 | |
| 1583 | // First, compose the values into an array of 32-bit words instead of |
| 1584 | // 64-bit words. This is a necessity of both the "short division" algorithm |
| 1585 | // and the the Knuth "classical algorithm" which requires there to be native |
| 1586 | // operations for +, -, and * on an m bit value with an m*2 bit result. We |
| 1587 | // can't use 64-bit operands here because we don't have native results of |
| 1588 | // 128-bits. Furthremore, casting the 64-bit values to 32-bit values won't |
| 1589 | // work on large-endian machines. |
| 1590 | uint64_t mask = ~0ull >> (sizeof(uint32_t)*8); |
| 1591 | uint32_t n = rhsWords * 2; |
| 1592 | uint32_t m = (lhsWords * 2) - n; |
Reid Spencer | 24c4a8f | 2007-02-25 01:56:07 +0000 | [diff] [blame] | 1593 | |
| 1594 | // Allocate space for the temporary values we need either on the stack, if |
| 1595 | // it will fit, or on the heap if it won't. |
| 1596 | uint32_t SPACE[128]; |
| 1597 | uint32_t *U = 0; |
| 1598 | uint32_t *V = 0; |
| 1599 | uint32_t *Q = 0; |
| 1600 | uint32_t *R = 0; |
| 1601 | if ((Remainder?4:3)*n+2*m+1 <= 128) { |
| 1602 | U = &SPACE[0]; |
| 1603 | V = &SPACE[m+n+1]; |
| 1604 | Q = &SPACE[(m+n+1) + n]; |
| 1605 | if (Remainder) |
| 1606 | R = &SPACE[(m+n+1) + n + (m+n)]; |
| 1607 | } else { |
| 1608 | U = new uint32_t[m + n + 1]; |
| 1609 | V = new uint32_t[n]; |
| 1610 | Q = new uint32_t[m+n]; |
| 1611 | if (Remainder) |
| 1612 | R = new uint32_t[n]; |
| 1613 | } |
| 1614 | |
| 1615 | // Initialize the dividend |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1616 | memset(U, 0, (m+n+1)*sizeof(uint32_t)); |
| 1617 | for (unsigned i = 0; i < lhsWords; ++i) { |
Reid Spencer | 15aab8a | 2007-02-22 00:58:45 +0000 | [diff] [blame] | 1618 | uint64_t tmp = (LHS.getNumWords() == 1 ? LHS.VAL : LHS.pVal[i]); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1619 | U[i * 2] = tmp & mask; |
| 1620 | U[i * 2 + 1] = tmp >> (sizeof(uint32_t)*8); |
| 1621 | } |
| 1622 | U[m+n] = 0; // this extra word is for "spill" in the Knuth algorithm. |
| 1623 | |
Reid Spencer | 24c4a8f | 2007-02-25 01:56:07 +0000 | [diff] [blame] | 1624 | // Initialize the divisor |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1625 | memset(V, 0, (n)*sizeof(uint32_t)); |
| 1626 | for (unsigned i = 0; i < rhsWords; ++i) { |
Reid Spencer | 15aab8a | 2007-02-22 00:58:45 +0000 | [diff] [blame] | 1627 | uint64_t tmp = (RHS.getNumWords() == 1 ? RHS.VAL : RHS.pVal[i]); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1628 | V[i * 2] = tmp & mask; |
| 1629 | V[i * 2 + 1] = tmp >> (sizeof(uint32_t)*8); |
| 1630 | } |
| 1631 | |
Reid Spencer | 24c4a8f | 2007-02-25 01:56:07 +0000 | [diff] [blame] | 1632 | // initialize the quotient and remainder |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1633 | memset(Q, 0, (m+n) * sizeof(uint32_t)); |
Reid Spencer | 24c4a8f | 2007-02-25 01:56:07 +0000 | [diff] [blame] | 1634 | if (Remainder) |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1635 | memset(R, 0, n * sizeof(uint32_t)); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1636 | |
| 1637 | // Now, adjust m and n for the Knuth division. n is the number of words in |
| 1638 | // the divisor. m is the number of words by which the dividend exceeds the |
| 1639 | // divisor (i.e. m+n is the length of the dividend). These sizes must not |
| 1640 | // contain any zero words or the Knuth algorithm fails. |
| 1641 | for (unsigned i = n; i > 0 && V[i-1] == 0; i--) { |
| 1642 | n--; |
| 1643 | m++; |
| 1644 | } |
| 1645 | for (unsigned i = m+n; i > 0 && U[i-1] == 0; i--) |
| 1646 | m--; |
| 1647 | |
| 1648 | // If we're left with only a single word for the divisor, Knuth doesn't work |
| 1649 | // so we implement the short division algorithm here. This is much simpler |
| 1650 | // and faster because we are certain that we can divide a 64-bit quantity |
| 1651 | // by a 32-bit quantity at hardware speed and short division is simply a |
| 1652 | // series of such operations. This is just like doing short division but we |
| 1653 | // are using base 2^32 instead of base 10. |
| 1654 | assert(n != 0 && "Divide by zero?"); |
| 1655 | if (n == 1) { |
| 1656 | uint32_t divisor = V[0]; |
| 1657 | uint32_t remainder = 0; |
| 1658 | for (int i = m+n-1; i >= 0; i--) { |
| 1659 | uint64_t partial_dividend = uint64_t(remainder) << 32 | U[i]; |
| 1660 | if (partial_dividend == 0) { |
| 1661 | Q[i] = 0; |
| 1662 | remainder = 0; |
| 1663 | } else if (partial_dividend < divisor) { |
| 1664 | Q[i] = 0; |
| 1665 | remainder = partial_dividend; |
| 1666 | } else if (partial_dividend == divisor) { |
| 1667 | Q[i] = 1; |
| 1668 | remainder = 0; |
| 1669 | } else { |
| 1670 | Q[i] = partial_dividend / divisor; |
| 1671 | remainder = partial_dividend - (Q[i] * divisor); |
| 1672 | } |
| 1673 | } |
| 1674 | if (R) |
| 1675 | R[0] = remainder; |
| 1676 | } else { |
| 1677 | // Now we're ready to invoke the Knuth classical divide algorithm. In this |
| 1678 | // case n > 1. |
| 1679 | KnuthDiv(U, V, Q, R, m, n); |
| 1680 | } |
| 1681 | |
| 1682 | // If the caller wants the quotient |
| 1683 | if (Quotient) { |
| 1684 | // Set up the Quotient value's memory. |
| 1685 | if (Quotient->BitWidth != LHS.BitWidth) { |
| 1686 | if (Quotient->isSingleWord()) |
| 1687 | Quotient->VAL = 0; |
| 1688 | else |
Reid Spencer | 9ac4411 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 1689 | delete [] Quotient->pVal; |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1690 | Quotient->BitWidth = LHS.BitWidth; |
| 1691 | if (!Quotient->isSingleWord()) |
Reid Spencer | e0cdd33 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 1692 | Quotient->pVal = getClearedMemory(Quotient->getNumWords()); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1693 | } else |
| 1694 | Quotient->clear(); |
| 1695 | |
| 1696 | // The quotient is in Q. Reconstitute the quotient into Quotient's low |
| 1697 | // order words. |
| 1698 | if (lhsWords == 1) { |
| 1699 | uint64_t tmp = |
| 1700 | uint64_t(Q[0]) | (uint64_t(Q[1]) << (APINT_BITS_PER_WORD / 2)); |
| 1701 | if (Quotient->isSingleWord()) |
| 1702 | Quotient->VAL = tmp; |
| 1703 | else |
| 1704 | Quotient->pVal[0] = tmp; |
| 1705 | } else { |
| 1706 | assert(!Quotient->isSingleWord() && "Quotient APInt not large enough"); |
| 1707 | for (unsigned i = 0; i < lhsWords; ++i) |
| 1708 | Quotient->pVal[i] = |
| 1709 | uint64_t(Q[i*2]) | (uint64_t(Q[i*2+1]) << (APINT_BITS_PER_WORD / 2)); |
| 1710 | } |
| 1711 | } |
| 1712 | |
| 1713 | // If the caller wants the remainder |
| 1714 | if (Remainder) { |
| 1715 | // Set up the Remainder value's memory. |
| 1716 | if (Remainder->BitWidth != RHS.BitWidth) { |
| 1717 | if (Remainder->isSingleWord()) |
| 1718 | Remainder->VAL = 0; |
| 1719 | else |
Reid Spencer | 9ac4411 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 1720 | delete [] Remainder->pVal; |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1721 | Remainder->BitWidth = RHS.BitWidth; |
| 1722 | if (!Remainder->isSingleWord()) |
Reid Spencer | e0cdd33 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 1723 | Remainder->pVal = getClearedMemory(Remainder->getNumWords()); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1724 | } else |
| 1725 | Remainder->clear(); |
| 1726 | |
| 1727 | // The remainder is in R. Reconstitute the remainder into Remainder's low |
| 1728 | // order words. |
| 1729 | if (rhsWords == 1) { |
| 1730 | uint64_t tmp = |
| 1731 | uint64_t(R[0]) | (uint64_t(R[1]) << (APINT_BITS_PER_WORD / 2)); |
| 1732 | if (Remainder->isSingleWord()) |
| 1733 | Remainder->VAL = tmp; |
| 1734 | else |
| 1735 | Remainder->pVal[0] = tmp; |
| 1736 | } else { |
| 1737 | assert(!Remainder->isSingleWord() && "Remainder APInt not large enough"); |
| 1738 | for (unsigned i = 0; i < rhsWords; ++i) |
| 1739 | Remainder->pVal[i] = |
| 1740 | uint64_t(R[i*2]) | (uint64_t(R[i*2+1]) << (APINT_BITS_PER_WORD / 2)); |
| 1741 | } |
| 1742 | } |
| 1743 | |
| 1744 | // Clean up the memory we allocated. |
Reid Spencer | 24c4a8f | 2007-02-25 01:56:07 +0000 | [diff] [blame] | 1745 | if (U != &SPACE[0]) { |
| 1746 | delete [] U; |
| 1747 | delete [] V; |
| 1748 | delete [] Q; |
| 1749 | delete [] R; |
| 1750 | } |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 1753 | APInt APInt::udiv(const APInt& RHS) const { |
Reid Spencer | cd6f2bf | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 1754 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Reid Spencer | 71bd08f | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1755 | |
| 1756 | // First, deal with the easy case |
| 1757 | if (isSingleWord()) { |
| 1758 | assert(RHS.VAL != 0 && "Divide by zero?"); |
| 1759 | return APInt(BitWidth, VAL / RHS.VAL); |
Zhou Sheng | 0b706b1 | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1760 | } |
Reid Spencer | 71bd08f | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1761 | |
Reid Spencer | 71bd08f | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1762 | // 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] | 1763 | uint32_t rhsBits = RHS.getActiveBits(); |
| 1764 | uint32_t rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1); |
Reid Spencer | 71bd08f | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1765 | assert(rhsWords && "Divided by zero???"); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1766 | uint32_t lhsBits = this->getActiveBits(); |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 1767 | uint32_t lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1); |
Reid Spencer | 71bd08f | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1768 | |
| 1769 | // Deal with some degenerate cases |
| 1770 | if (!lhsWords) |
Reid Spencer | e0cdd33 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 1771 | // 0 / X ===> 0 |
| 1772 | return APInt(BitWidth, 0); |
| 1773 | else if (lhsWords < rhsWords || this->ult(RHS)) { |
| 1774 | // X / Y ===> 0, iff X < Y |
| 1775 | return APInt(BitWidth, 0); |
| 1776 | } else if (*this == RHS) { |
| 1777 | // X / X ===> 1 |
| 1778 | return APInt(BitWidth, 1); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1779 | } else if (lhsWords == 1 && rhsWords == 1) { |
Reid Spencer | 71bd08f | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1780 | // All high words are zero, just use native divide |
Reid Spencer | e0cdd33 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 1781 | return APInt(BitWidth, this->pVal[0] / RHS.pVal[0]); |
Reid Spencer | 71bd08f | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1782 | } |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1783 | |
| 1784 | // We have to compute it the hard way. Invoke the Knuth divide algorithm. |
| 1785 | APInt Quotient(1,0); // to hold result. |
| 1786 | divide(*this, lhsWords, RHS, rhsWords, &Quotient, 0); |
| 1787 | return Quotient; |
Zhou Sheng | 0b706b1 | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1788 | } |
| 1789 | |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 1790 | APInt APInt::urem(const APInt& RHS) const { |
Reid Spencer | cd6f2bf | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 1791 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Reid Spencer | 71bd08f | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1792 | if (isSingleWord()) { |
| 1793 | assert(RHS.VAL != 0 && "Remainder by zero?"); |
| 1794 | return APInt(BitWidth, VAL % RHS.VAL); |
Zhou Sheng | 0b706b1 | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1795 | } |
Reid Spencer | 71bd08f | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1796 | |
Reid Spencer | e0cdd33 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 1797 | // Get some facts about the LHS |
| 1798 | uint32_t lhsBits = getActiveBits(); |
| 1799 | uint32_t lhsWords = !lhsBits ? 0 : (whichWord(lhsBits - 1) + 1); |
Reid Spencer | 71bd08f | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1800 | |
| 1801 | // Get some facts about the RHS |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 1802 | uint32_t rhsBits = RHS.getActiveBits(); |
| 1803 | uint32_t rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1); |
Reid Spencer | 71bd08f | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1804 | assert(rhsWords && "Performing remainder operation by zero ???"); |
| 1805 | |
Reid Spencer | 71bd08f | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1806 | // Check the degenerate cases |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1807 | if (lhsWords == 0) { |
Reid Spencer | e0cdd33 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 1808 | // 0 % Y ===> 0 |
| 1809 | return APInt(BitWidth, 0); |
| 1810 | } else if (lhsWords < rhsWords || this->ult(RHS)) { |
| 1811 | // X % Y ===> X, iff X < Y |
| 1812 | return *this; |
| 1813 | } else if (*this == RHS) { |
Reid Spencer | 71bd08f | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1814 | // X % X == 0; |
Reid Spencer | e0cdd33 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 1815 | return APInt(BitWidth, 0); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1816 | } else if (lhsWords == 1) { |
Reid Spencer | 71bd08f | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1817 | // All high words are zero, just use native remainder |
Reid Spencer | e0cdd33 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 1818 | return APInt(BitWidth, pVal[0] % RHS.pVal[0]); |
Reid Spencer | 71bd08f | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1819 | } |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1820 | |
Reid Spencer | 19dc32a | 2007-05-13 23:44:59 +0000 | [diff] [blame] | 1821 | // We have to compute it the hard way. Invoke the Knuth divide algorithm. |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1822 | APInt Remainder(1,0); |
| 1823 | divide(*this, lhsWords, RHS, rhsWords, 0, &Remainder); |
| 1824 | return Remainder; |
Zhou Sheng | 0b706b1 | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1825 | } |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 1826 | |
Reid Spencer | 19dc32a | 2007-05-13 23:44:59 +0000 | [diff] [blame] | 1827 | void APInt::udivrem(const APInt &LHS, const APInt &RHS, |
| 1828 | APInt &Quotient, APInt &Remainder) { |
| 1829 | // Get some size facts about the dividend and divisor |
| 1830 | uint32_t lhsBits = LHS.getActiveBits(); |
| 1831 | uint32_t lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1); |
| 1832 | uint32_t rhsBits = RHS.getActiveBits(); |
| 1833 | uint32_t rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1); |
| 1834 | |
| 1835 | // Check the degenerate cases |
| 1836 | if (lhsWords == 0) { |
| 1837 | Quotient = 0; // 0 / Y ===> 0 |
| 1838 | Remainder = 0; // 0 % Y ===> 0 |
| 1839 | return; |
| 1840 | } |
| 1841 | |
| 1842 | if (lhsWords < rhsWords || LHS.ult(RHS)) { |
| 1843 | Quotient = 0; // X / Y ===> 0, iff X < Y |
| 1844 | Remainder = LHS; // X % Y ===> X, iff X < Y |
| 1845 | return; |
| 1846 | } |
| 1847 | |
| 1848 | if (LHS == RHS) { |
| 1849 | Quotient = 1; // X / X ===> 1 |
| 1850 | Remainder = 0; // X % X ===> 0; |
| 1851 | return; |
| 1852 | } |
| 1853 | |
| 1854 | if (lhsWords == 1 && rhsWords == 1) { |
| 1855 | // There is only one word to consider so use the native versions. |
| 1856 | if (LHS.isSingleWord()) { |
| 1857 | Quotient = APInt(LHS.getBitWidth(), LHS.VAL / RHS.VAL); |
| 1858 | Remainder = APInt(LHS.getBitWidth(), LHS.VAL % RHS.VAL); |
| 1859 | } else { |
| 1860 | Quotient = APInt(LHS.getBitWidth(), LHS.pVal[0] / RHS.pVal[0]); |
| 1861 | Remainder = APInt(LHS.getBitWidth(), LHS.pVal[0] % RHS.pVal[0]); |
| 1862 | } |
| 1863 | return; |
| 1864 | } |
| 1865 | |
| 1866 | // Okay, lets do it the long way |
| 1867 | divide(LHS, lhsWords, RHS, rhsWords, &Quotient, &Remainder); |
| 1868 | } |
| 1869 | |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 1870 | void APInt::fromString(uint32_t numbits, const char *str, uint32_t slen, |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 1871 | uint8_t radix) { |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 1872 | // Check our assumptions here |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 1873 | assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) && |
| 1874 | "Radix should be 2, 8, 10, or 16!"); |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 1875 | assert(str && "String is null?"); |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1876 | bool isNeg = str[0] == '-'; |
| 1877 | if (isNeg) |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 1878 | str++, slen--; |
Chris Lattner | a5ae15e | 2007-05-03 18:15:36 +0000 | [diff] [blame] | 1879 | assert((slen <= numbits || radix != 2) && "Insufficient bit width"); |
| 1880 | assert((slen*3 <= numbits || radix != 8) && "Insufficient bit width"); |
| 1881 | assert((slen*4 <= numbits || radix != 16) && "Insufficient bit width"); |
| 1882 | assert(((slen*64)/22 <= numbits || radix != 10) && "Insufficient bit width"); |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 1883 | |
| 1884 | // Allocate memory |
| 1885 | if (!isSingleWord()) |
| 1886 | pVal = getClearedMemory(getNumWords()); |
| 1887 | |
| 1888 | // Figure out if we can shift instead of multiply |
| 1889 | uint32_t shift = (radix == 16 ? 4 : radix == 8 ? 3 : radix == 2 ? 1 : 0); |
| 1890 | |
| 1891 | // Set up an APInt for the digit to add outside the loop so we don't |
| 1892 | // constantly construct/destruct it. |
| 1893 | APInt apdigit(getBitWidth(), 0); |
| 1894 | APInt apradix(getBitWidth(), radix); |
| 1895 | |
| 1896 | // Enter digit traversal loop |
| 1897 | for (unsigned i = 0; i < slen; i++) { |
| 1898 | // Get a digit |
| 1899 | uint32_t digit = 0; |
| 1900 | char cdigit = str[i]; |
Reid Spencer | 6551dcd | 2007-05-16 19:18:22 +0000 | [diff] [blame] | 1901 | if (radix == 16) { |
| 1902 | if (!isxdigit(cdigit)) |
| 1903 | assert(0 && "Invalid hex digit in string"); |
| 1904 | if (isdigit(cdigit)) |
| 1905 | digit = cdigit - '0'; |
| 1906 | else if (cdigit >= 'a') |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 1907 | digit = cdigit - 'a' + 10; |
| 1908 | else if (cdigit >= 'A') |
| 1909 | digit = cdigit - 'A' + 10; |
| 1910 | else |
Reid Spencer | 6551dcd | 2007-05-16 19:18:22 +0000 | [diff] [blame] | 1911 | assert(0 && "huh? we shouldn't get here"); |
| 1912 | } else if (isdigit(cdigit)) { |
| 1913 | digit = cdigit - '0'; |
| 1914 | } else { |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 1915 | assert(0 && "Invalid character in digit string"); |
Reid Spencer | 6551dcd | 2007-05-16 19:18:22 +0000 | [diff] [blame] | 1916 | } |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 1917 | |
Reid Spencer | 6551dcd | 2007-05-16 19:18:22 +0000 | [diff] [blame] | 1918 | // Shift or multiply the value by the radix |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 1919 | if (shift) |
Reid Spencer | 6551dcd | 2007-05-16 19:18:22 +0000 | [diff] [blame] | 1920 | *this <<= shift; |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 1921 | else |
| 1922 | *this *= apradix; |
| 1923 | |
| 1924 | // Add in the digit we just interpreted |
Reid Spencer | 5bce854 | 2007-02-24 20:19:37 +0000 | [diff] [blame] | 1925 | if (apdigit.isSingleWord()) |
| 1926 | apdigit.VAL = digit; |
| 1927 | else |
| 1928 | apdigit.pVal[0] = digit; |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 1929 | *this += apdigit; |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 1930 | } |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 1931 | // If its negative, put it in two's complement form |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1932 | if (isNeg) { |
| 1933 | (*this)--; |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 1934 | this->flip(); |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 1935 | } |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 1936 | } |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1937 | |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1938 | std::string APInt::toString(uint8_t radix, bool wantSigned) const { |
| 1939 | assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) && |
| 1940 | "Radix should be 2, 8, 10, or 16!"); |
| 1941 | static const char *digits[] = { |
| 1942 | "0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F" |
| 1943 | }; |
| 1944 | std::string result; |
| 1945 | uint32_t bits_used = getActiveBits(); |
| 1946 | if (isSingleWord()) { |
| 1947 | char buf[65]; |
| 1948 | const char *format = (radix == 10 ? (wantSigned ? "%lld" : "%llu") : |
| 1949 | (radix == 16 ? "%llX" : (radix == 8 ? "%llo" : 0))); |
| 1950 | if (format) { |
| 1951 | if (wantSigned) { |
| 1952 | int64_t sextVal = (int64_t(VAL) << (APINT_BITS_PER_WORD-BitWidth)) >> |
| 1953 | (APINT_BITS_PER_WORD-BitWidth); |
| 1954 | sprintf(buf, format, sextVal); |
| 1955 | } else |
| 1956 | sprintf(buf, format, VAL); |
| 1957 | } else { |
| 1958 | memset(buf, 0, 65); |
| 1959 | uint64_t v = VAL; |
| 1960 | while (bits_used) { |
| 1961 | uint32_t bit = v & 1; |
| 1962 | bits_used--; |
| 1963 | buf[bits_used] = digits[bit][0]; |
| 1964 | v >>=1; |
| 1965 | } |
| 1966 | } |
| 1967 | result = buf; |
| 1968 | return result; |
| 1969 | } |
| 1970 | |
| 1971 | if (radix != 10) { |
Reid Spencer | fb0709a | 2007-05-17 19:23:02 +0000 | [diff] [blame] | 1972 | // For the 2, 8 and 16 bit cases, we can just shift instead of divide |
| 1973 | // because the number of bits per digit (1,3 and 4 respectively) divides |
| 1974 | // equaly. We just shift until there value is zero. |
| 1975 | |
| 1976 | // First, check for a zero value and just short circuit the logic below. |
| 1977 | if (*this == 0) |
| 1978 | result = "0"; |
| 1979 | else { |
| 1980 | APInt tmp(*this); |
| 1981 | size_t insert_at = 0; |
| 1982 | if (wantSigned && this->isNegative()) { |
| 1983 | // They want to print the signed version and it is a negative value |
| 1984 | // Flip the bits and add one to turn it into the equivalent positive |
| 1985 | // value and put a '-' in the result. |
| 1986 | tmp.flip(); |
| 1987 | tmp++; |
| 1988 | result = "-"; |
| 1989 | insert_at = 1; |
| 1990 | } |
| 1991 | // Just shift tmp right for each digit width until it becomes zero |
| 1992 | uint32_t shift = (radix == 16 ? 4 : (radix == 8 ? 3 : 1)); |
| 1993 | uint64_t mask = radix - 1; |
| 1994 | APInt zero(tmp.getBitWidth(), 0); |
| 1995 | while (tmp.ne(zero)) { |
Reid Spencer | 20a4c23 | 2007-05-19 00:29:55 +0000 | [diff] [blame] | 1996 | unsigned digit = (tmp.isSingleWord() ? tmp.VAL : tmp.pVal[0]) & mask; |
Reid Spencer | fb0709a | 2007-05-17 19:23:02 +0000 | [diff] [blame] | 1997 | result.insert(insert_at, digits[digit]); |
Reid Spencer | 20a4c23 | 2007-05-19 00:29:55 +0000 | [diff] [blame] | 1998 | tmp = tmp.lshr(shift); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1999 | } |
| 2000 | } |
| 2001 | return result; |
| 2002 | } |
| 2003 | |
| 2004 | APInt tmp(*this); |
| 2005 | APInt divisor(4, radix); |
| 2006 | APInt zero(tmp.getBitWidth(), 0); |
| 2007 | size_t insert_at = 0; |
| 2008 | if (wantSigned && tmp[BitWidth-1]) { |
| 2009 | // They want to print the signed version and it is a negative value |
| 2010 | // Flip the bits and add one to turn it into the equivalent positive |
| 2011 | // value and put a '-' in the result. |
| 2012 | tmp.flip(); |
| 2013 | tmp++; |
| 2014 | result = "-"; |
| 2015 | insert_at = 1; |
| 2016 | } |
Reid Spencer | e549c49 | 2007-02-21 00:29:48 +0000 | [diff] [blame] | 2017 | if (tmp == APInt(tmp.getBitWidth(), 0)) |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 2018 | result = "0"; |
| 2019 | else while (tmp.ne(zero)) { |
| 2020 | APInt APdigit(1,0); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 2021 | APInt tmp2(tmp.getBitWidth(), 0); |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 2022 | divide(tmp, tmp.getNumWords(), divisor, divisor.getNumWords(), &tmp2, |
| 2023 | &APdigit); |
Reid Spencer | 794f472 | 2007-02-26 21:02:27 +0000 | [diff] [blame] | 2024 | uint32_t digit = APdigit.getZExtValue(); |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 2025 | assert(digit < radix && "divide failed"); |
| 2026 | result.insert(insert_at,digits[digit]); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 2027 | tmp = tmp2; |
| 2028 | } |
| 2029 | |
| 2030 | return result; |
| 2031 | } |
| 2032 | |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 2033 | void APInt::dump() const |
| 2034 | { |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 2035 | cerr << "APInt(" << BitWidth << ")=" << std::setbase(16); |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 2036 | if (isSingleWord()) |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 2037 | cerr << VAL; |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 2038 | else for (unsigned i = getNumWords(); i > 0; i--) { |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 2039 | cerr << pVal[i-1] << " "; |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 2040 | } |
Chris Lattner | 9132a2b | 2007-08-23 05:15:32 +0000 | [diff] [blame] | 2041 | cerr << " U(" << this->toStringUnsigned(10) << ") S(" |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 2042 | << this->toStringSigned(10) << ")" << std::setbase(10); |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 2043 | } |
Chris Lattner | fe8e14a | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2044 | |
| 2045 | // This implements a variety of operations on a representation of |
| 2046 | // arbitrary precision, two's-complement, bignum integer values. |
| 2047 | |
| 2048 | /* Assumed by lowHalf, highHalf, partMSB and partLSB. A fairly safe |
| 2049 | and unrestricting assumption. */ |
Chris Lattner | b39cdde | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2050 | COMPILE_TIME_ASSERT(integerPartWidth % 2 == 0); |
Chris Lattner | fe8e14a | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2051 | |
| 2052 | /* Some handy functions local to this file. */ |
| 2053 | namespace { |
| 2054 | |
Chris Lattner | b39cdde | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2055 | /* Returns the integer part with the least significant BITS set. |
| 2056 | BITS cannot be zero. */ |
| 2057 | inline integerPart |
| 2058 | lowBitMask(unsigned int bits) |
| 2059 | { |
| 2060 | assert (bits != 0 && bits <= integerPartWidth); |
| 2061 | |
| 2062 | return ~(integerPart) 0 >> (integerPartWidth - bits); |
| 2063 | } |
| 2064 | |
Neil Booth | 055c0b3 | 2007-10-06 00:43:45 +0000 | [diff] [blame] | 2065 | /* Returns the value of the lower half of PART. */ |
Chris Lattner | b39cdde | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2066 | inline integerPart |
| 2067 | lowHalf(integerPart part) |
| 2068 | { |
| 2069 | return part & lowBitMask(integerPartWidth / 2); |
| 2070 | } |
| 2071 | |
Neil Booth | 055c0b3 | 2007-10-06 00:43:45 +0000 | [diff] [blame] | 2072 | /* Returns the value of the upper half of PART. */ |
Chris Lattner | b39cdde | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2073 | inline integerPart |
| 2074 | highHalf(integerPart part) |
| 2075 | { |
| 2076 | return part >> (integerPartWidth / 2); |
| 2077 | } |
| 2078 | |
Neil Booth | 055c0b3 | 2007-10-06 00:43:45 +0000 | [diff] [blame] | 2079 | /* Returns the bit number of the most significant set bit of a part. |
| 2080 | If the input number has no bits set -1U is returned. */ |
Chris Lattner | fe8e14a | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2081 | unsigned int |
Chris Lattner | b39cdde | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2082 | partMSB(integerPart value) |
Chris Lattner | fe8e14a | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2083 | { |
| 2084 | unsigned int n, msb; |
| 2085 | |
| 2086 | if (value == 0) |
| 2087 | return -1U; |
| 2088 | |
| 2089 | n = integerPartWidth / 2; |
| 2090 | |
| 2091 | msb = 0; |
| 2092 | do { |
| 2093 | if (value >> n) { |
| 2094 | value >>= n; |
| 2095 | msb += n; |
| 2096 | } |
| 2097 | |
| 2098 | n >>= 1; |
| 2099 | } while (n); |
| 2100 | |
| 2101 | return msb; |
| 2102 | } |
| 2103 | |
Neil Booth | 055c0b3 | 2007-10-06 00:43:45 +0000 | [diff] [blame] | 2104 | /* Returns the bit number of the least significant set bit of a |
| 2105 | part. If the input number has no bits set -1U is returned. */ |
Chris Lattner | fe8e14a | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2106 | unsigned int |
| 2107 | partLSB(integerPart value) |
| 2108 | { |
| 2109 | unsigned int n, lsb; |
| 2110 | |
| 2111 | if (value == 0) |
| 2112 | return -1U; |
| 2113 | |
| 2114 | lsb = integerPartWidth - 1; |
| 2115 | n = integerPartWidth / 2; |
| 2116 | |
| 2117 | do { |
| 2118 | if (value << n) { |
| 2119 | value <<= n; |
| 2120 | lsb -= n; |
| 2121 | } |
| 2122 | |
| 2123 | n >>= 1; |
| 2124 | } while (n); |
| 2125 | |
| 2126 | return lsb; |
| 2127 | } |
| 2128 | } |
| 2129 | |
| 2130 | /* Sets the least significant part of a bignum to the input value, and |
| 2131 | zeroes out higher parts. */ |
| 2132 | void |
| 2133 | APInt::tcSet(integerPart *dst, integerPart part, unsigned int parts) |
| 2134 | { |
| 2135 | unsigned int i; |
| 2136 | |
Neil Booth | 68e53ad | 2007-10-08 13:47:12 +0000 | [diff] [blame] | 2137 | assert (parts > 0); |
| 2138 | |
Chris Lattner | fe8e14a | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2139 | dst[0] = part; |
| 2140 | for(i = 1; i < parts; i++) |
| 2141 | dst[i] = 0; |
| 2142 | } |
| 2143 | |
| 2144 | /* Assign one bignum to another. */ |
| 2145 | void |
| 2146 | APInt::tcAssign(integerPart *dst, const integerPart *src, unsigned int parts) |
| 2147 | { |
| 2148 | unsigned int i; |
| 2149 | |
| 2150 | for(i = 0; i < parts; i++) |
| 2151 | dst[i] = src[i]; |
| 2152 | } |
| 2153 | |
| 2154 | /* Returns true if a bignum is zero, false otherwise. */ |
| 2155 | bool |
| 2156 | APInt::tcIsZero(const integerPart *src, unsigned int parts) |
| 2157 | { |
| 2158 | unsigned int i; |
| 2159 | |
| 2160 | for(i = 0; i < parts; i++) |
| 2161 | if (src[i]) |
| 2162 | return false; |
| 2163 | |
| 2164 | return true; |
| 2165 | } |
| 2166 | |
| 2167 | /* Extract the given bit of a bignum; returns 0 or 1. */ |
| 2168 | int |
| 2169 | APInt::tcExtractBit(const integerPart *parts, unsigned int bit) |
| 2170 | { |
| 2171 | return(parts[bit / integerPartWidth] |
| 2172 | & ((integerPart) 1 << bit % integerPartWidth)) != 0; |
| 2173 | } |
| 2174 | |
| 2175 | /* Set the given bit of a bignum. */ |
| 2176 | void |
| 2177 | APInt::tcSetBit(integerPart *parts, unsigned int bit) |
| 2178 | { |
| 2179 | parts[bit / integerPartWidth] |= (integerPart) 1 << (bit % integerPartWidth); |
| 2180 | } |
| 2181 | |
Neil Booth | 055c0b3 | 2007-10-06 00:43:45 +0000 | [diff] [blame] | 2182 | /* Returns the bit number of the least significant set bit of a |
| 2183 | number. If the input number has no bits set -1U is returned. */ |
Chris Lattner | fe8e14a | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2184 | unsigned int |
| 2185 | APInt::tcLSB(const integerPart *parts, unsigned int n) |
| 2186 | { |
| 2187 | unsigned int i, lsb; |
| 2188 | |
| 2189 | for(i = 0; i < n; i++) { |
| 2190 | if (parts[i] != 0) { |
| 2191 | lsb = partLSB(parts[i]); |
| 2192 | |
| 2193 | return lsb + i * integerPartWidth; |
| 2194 | } |
| 2195 | } |
| 2196 | |
| 2197 | return -1U; |
| 2198 | } |
| 2199 | |
Neil Booth | 055c0b3 | 2007-10-06 00:43:45 +0000 | [diff] [blame] | 2200 | /* Returns the bit number of the most significant set bit of a number. |
| 2201 | If the input number has no bits set -1U is returned. */ |
Chris Lattner | fe8e14a | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2202 | unsigned int |
| 2203 | APInt::tcMSB(const integerPart *parts, unsigned int n) |
| 2204 | { |
| 2205 | unsigned int msb; |
| 2206 | |
| 2207 | do { |
| 2208 | --n; |
| 2209 | |
| 2210 | if (parts[n] != 0) { |
Chris Lattner | b39cdde | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2211 | msb = partMSB(parts[n]); |
Chris Lattner | fe8e14a | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2212 | |
| 2213 | return msb + n * integerPartWidth; |
| 2214 | } |
| 2215 | } while (n); |
| 2216 | |
| 2217 | return -1U; |
| 2218 | } |
| 2219 | |
Neil Booth | 68e53ad | 2007-10-08 13:47:12 +0000 | [diff] [blame] | 2220 | /* Copy the bit vector of width srcBITS from SRC, starting at bit |
| 2221 | srcLSB, to DST, of dstCOUNT parts, such that the bit srcLSB becomes |
| 2222 | the least significant bit of DST. All high bits above srcBITS in |
| 2223 | DST are zero-filled. */ |
| 2224 | void |
| 2225 | APInt::tcExtract(integerPart *dst, unsigned int dstCount, const integerPart *src, |
| 2226 | unsigned int srcBits, unsigned int srcLSB) |
| 2227 | { |
| 2228 | unsigned int firstSrcPart, dstParts, shift, n; |
| 2229 | |
| 2230 | dstParts = (srcBits + integerPartWidth - 1) / integerPartWidth; |
| 2231 | assert (dstParts <= dstCount); |
| 2232 | |
| 2233 | firstSrcPart = srcLSB / integerPartWidth; |
| 2234 | tcAssign (dst, src + firstSrcPart, dstParts); |
| 2235 | |
| 2236 | shift = srcLSB % integerPartWidth; |
| 2237 | tcShiftRight (dst, dstParts, shift); |
| 2238 | |
| 2239 | /* We now have (dstParts * integerPartWidth - shift) bits from SRC |
| 2240 | in DST. If this is less that srcBits, append the rest, else |
| 2241 | clear the high bits. */ |
| 2242 | n = dstParts * integerPartWidth - shift; |
| 2243 | if (n < srcBits) { |
| 2244 | integerPart mask = lowBitMask (srcBits - n); |
| 2245 | dst[dstParts - 1] |= ((src[firstSrcPart + dstParts] & mask) |
| 2246 | << n % integerPartWidth); |
| 2247 | } else if (n > srcBits) { |
Neil Booth | 1e8390d | 2007-10-12 15:31:31 +0000 | [diff] [blame] | 2248 | if (srcBits % integerPartWidth) |
| 2249 | dst[dstParts - 1] &= lowBitMask (srcBits % integerPartWidth); |
Neil Booth | 68e53ad | 2007-10-08 13:47:12 +0000 | [diff] [blame] | 2250 | } |
| 2251 | |
| 2252 | /* Clear high parts. */ |
| 2253 | while (dstParts < dstCount) |
| 2254 | dst[dstParts++] = 0; |
| 2255 | } |
| 2256 | |
Chris Lattner | fe8e14a | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2257 | /* DST += RHS + C where C is zero or one. Returns the carry flag. */ |
| 2258 | integerPart |
| 2259 | APInt::tcAdd(integerPart *dst, const integerPart *rhs, |
| 2260 | integerPart c, unsigned int parts) |
| 2261 | { |
| 2262 | unsigned int i; |
| 2263 | |
| 2264 | assert(c <= 1); |
| 2265 | |
| 2266 | for(i = 0; i < parts; i++) { |
| 2267 | integerPart l; |
| 2268 | |
| 2269 | l = dst[i]; |
| 2270 | if (c) { |
| 2271 | dst[i] += rhs[i] + 1; |
| 2272 | c = (dst[i] <= l); |
| 2273 | } else { |
| 2274 | dst[i] += rhs[i]; |
| 2275 | c = (dst[i] < l); |
| 2276 | } |
| 2277 | } |
| 2278 | |
| 2279 | return c; |
| 2280 | } |
| 2281 | |
| 2282 | /* DST -= RHS + C where C is zero or one. Returns the carry flag. */ |
| 2283 | integerPart |
| 2284 | APInt::tcSubtract(integerPart *dst, const integerPart *rhs, |
| 2285 | integerPart c, unsigned int parts) |
| 2286 | { |
| 2287 | unsigned int i; |
| 2288 | |
| 2289 | assert(c <= 1); |
| 2290 | |
| 2291 | for(i = 0; i < parts; i++) { |
| 2292 | integerPart l; |
| 2293 | |
| 2294 | l = dst[i]; |
| 2295 | if (c) { |
| 2296 | dst[i] -= rhs[i] + 1; |
| 2297 | c = (dst[i] >= l); |
| 2298 | } else { |
| 2299 | dst[i] -= rhs[i]; |
| 2300 | c = (dst[i] > l); |
| 2301 | } |
| 2302 | } |
| 2303 | |
| 2304 | return c; |
| 2305 | } |
| 2306 | |
| 2307 | /* Negate a bignum in-place. */ |
| 2308 | void |
| 2309 | APInt::tcNegate(integerPart *dst, unsigned int parts) |
| 2310 | { |
| 2311 | tcComplement(dst, parts); |
| 2312 | tcIncrement(dst, parts); |
| 2313 | } |
| 2314 | |
Neil Booth | 055c0b3 | 2007-10-06 00:43:45 +0000 | [diff] [blame] | 2315 | /* DST += SRC * MULTIPLIER + CARRY if add is true |
| 2316 | DST = SRC * MULTIPLIER + CARRY if add is false |
Chris Lattner | fe8e14a | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2317 | |
| 2318 | Requires 0 <= DSTPARTS <= SRCPARTS + 1. If DST overlaps SRC |
| 2319 | they must start at the same point, i.e. DST == SRC. |
| 2320 | |
| 2321 | If DSTPARTS == SRCPARTS + 1 no overflow occurs and zero is |
| 2322 | returned. Otherwise DST is filled with the least significant |
| 2323 | DSTPARTS parts of the result, and if all of the omitted higher |
| 2324 | parts were zero return zero, otherwise overflow occurred and |
| 2325 | return one. */ |
| 2326 | int |
| 2327 | APInt::tcMultiplyPart(integerPart *dst, const integerPart *src, |
| 2328 | integerPart multiplier, integerPart carry, |
| 2329 | unsigned int srcParts, unsigned int dstParts, |
| 2330 | bool add) |
| 2331 | { |
| 2332 | unsigned int i, n; |
| 2333 | |
| 2334 | /* Otherwise our writes of DST kill our later reads of SRC. */ |
| 2335 | assert(dst <= src || dst >= src + srcParts); |
| 2336 | assert(dstParts <= srcParts + 1); |
| 2337 | |
| 2338 | /* N loops; minimum of dstParts and srcParts. */ |
| 2339 | n = dstParts < srcParts ? dstParts: srcParts; |
| 2340 | |
| 2341 | for(i = 0; i < n; i++) { |
| 2342 | integerPart low, mid, high, srcPart; |
| 2343 | |
| 2344 | /* [ LOW, HIGH ] = MULTIPLIER * SRC[i] + DST[i] + CARRY. |
| 2345 | |
| 2346 | This cannot overflow, because |
| 2347 | |
| 2348 | (n - 1) * (n - 1) + 2 (n - 1) = (n - 1) * (n + 1) |
| 2349 | |
| 2350 | which is less than n^2. */ |
| 2351 | |
| 2352 | srcPart = src[i]; |
| 2353 | |
| 2354 | if (multiplier == 0 || srcPart == 0) { |
| 2355 | low = carry; |
| 2356 | high = 0; |
| 2357 | } else { |
| 2358 | low = lowHalf(srcPart) * lowHalf(multiplier); |
| 2359 | high = highHalf(srcPart) * highHalf(multiplier); |
| 2360 | |
| 2361 | mid = lowHalf(srcPart) * highHalf(multiplier); |
| 2362 | high += highHalf(mid); |
| 2363 | mid <<= integerPartWidth / 2; |
| 2364 | if (low + mid < low) |
| 2365 | high++; |
| 2366 | low += mid; |
| 2367 | |
| 2368 | mid = highHalf(srcPart) * lowHalf(multiplier); |
| 2369 | high += highHalf(mid); |
| 2370 | mid <<= integerPartWidth / 2; |
| 2371 | if (low + mid < low) |
| 2372 | high++; |
| 2373 | low += mid; |
| 2374 | |
| 2375 | /* Now add carry. */ |
| 2376 | if (low + carry < low) |
| 2377 | high++; |
| 2378 | low += carry; |
| 2379 | } |
| 2380 | |
| 2381 | if (add) { |
| 2382 | /* And now DST[i], and store the new low part there. */ |
| 2383 | if (low + dst[i] < low) |
| 2384 | high++; |
| 2385 | dst[i] += low; |
| 2386 | } else |
| 2387 | dst[i] = low; |
| 2388 | |
| 2389 | carry = high; |
| 2390 | } |
| 2391 | |
| 2392 | if (i < dstParts) { |
| 2393 | /* Full multiplication, there is no overflow. */ |
| 2394 | assert(i + 1 == dstParts); |
| 2395 | dst[i] = carry; |
| 2396 | return 0; |
| 2397 | } else { |
| 2398 | /* We overflowed if there is carry. */ |
| 2399 | if (carry) |
| 2400 | return 1; |
| 2401 | |
| 2402 | /* We would overflow if any significant unwritten parts would be |
| 2403 | non-zero. This is true if any remaining src parts are non-zero |
| 2404 | and the multiplier is non-zero. */ |
| 2405 | if (multiplier) |
| 2406 | for(; i < srcParts; i++) |
| 2407 | if (src[i]) |
| 2408 | return 1; |
| 2409 | |
| 2410 | /* We fitted in the narrow destination. */ |
| 2411 | return 0; |
| 2412 | } |
| 2413 | } |
| 2414 | |
| 2415 | /* DST = LHS * RHS, where DST has the same width as the operands and |
| 2416 | is filled with the least significant parts of the result. Returns |
| 2417 | one if overflow occurred, otherwise zero. DST must be disjoint |
| 2418 | from both operands. */ |
| 2419 | int |
| 2420 | APInt::tcMultiply(integerPart *dst, const integerPart *lhs, |
| 2421 | const integerPart *rhs, unsigned int parts) |
| 2422 | { |
| 2423 | unsigned int i; |
| 2424 | int overflow; |
| 2425 | |
| 2426 | assert(dst != lhs && dst != rhs); |
| 2427 | |
| 2428 | overflow = 0; |
| 2429 | tcSet(dst, 0, parts); |
| 2430 | |
| 2431 | for(i = 0; i < parts; i++) |
| 2432 | overflow |= tcMultiplyPart(&dst[i], lhs, rhs[i], 0, parts, |
| 2433 | parts - i, true); |
| 2434 | |
| 2435 | return overflow; |
| 2436 | } |
| 2437 | |
Neil Booth | 978661d | 2007-10-06 00:24:48 +0000 | [diff] [blame] | 2438 | /* DST = LHS * RHS, where DST has width the sum of the widths of the |
| 2439 | operands. No overflow occurs. DST must be disjoint from both |
| 2440 | operands. Returns the number of parts required to hold the |
| 2441 | result. */ |
| 2442 | unsigned int |
Chris Lattner | fe8e14a | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2443 | APInt::tcFullMultiply(integerPart *dst, const integerPart *lhs, |
Neil Booth | 978661d | 2007-10-06 00:24:48 +0000 | [diff] [blame] | 2444 | const integerPart *rhs, unsigned int lhsParts, |
| 2445 | unsigned int rhsParts) |
Chris Lattner | fe8e14a | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2446 | { |
Neil Booth | 978661d | 2007-10-06 00:24:48 +0000 | [diff] [blame] | 2447 | /* Put the narrower number on the LHS for less loops below. */ |
| 2448 | if (lhsParts > rhsParts) { |
| 2449 | return tcFullMultiply (dst, rhs, lhs, rhsParts, lhsParts); |
| 2450 | } else { |
| 2451 | unsigned int n; |
Chris Lattner | fe8e14a | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2452 | |
Neil Booth | 978661d | 2007-10-06 00:24:48 +0000 | [diff] [blame] | 2453 | assert(dst != lhs && dst != rhs); |
Chris Lattner | fe8e14a | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2454 | |
Neil Booth | 978661d | 2007-10-06 00:24:48 +0000 | [diff] [blame] | 2455 | tcSet(dst, 0, rhsParts); |
Chris Lattner | fe8e14a | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2456 | |
Neil Booth | 978661d | 2007-10-06 00:24:48 +0000 | [diff] [blame] | 2457 | for(n = 0; n < lhsParts; n++) |
| 2458 | tcMultiplyPart(&dst[n], rhs, lhs[n], 0, rhsParts, rhsParts + 1, true); |
Chris Lattner | fe8e14a | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2459 | |
Neil Booth | 978661d | 2007-10-06 00:24:48 +0000 | [diff] [blame] | 2460 | n = lhsParts + rhsParts; |
| 2461 | |
| 2462 | return n - (dst[n - 1] == 0); |
| 2463 | } |
Chris Lattner | fe8e14a | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2464 | } |
| 2465 | |
| 2466 | /* If RHS is zero LHS and REMAINDER are left unchanged, return one. |
| 2467 | Otherwise set LHS to LHS / RHS with the fractional part discarded, |
| 2468 | set REMAINDER to the remainder, return zero. i.e. |
| 2469 | |
| 2470 | OLD_LHS = RHS * LHS + REMAINDER |
| 2471 | |
| 2472 | SCRATCH is a bignum of the same size as the operands and result for |
| 2473 | use by the routine; its contents need not be initialized and are |
| 2474 | destroyed. LHS, REMAINDER and SCRATCH must be distinct. |
| 2475 | */ |
| 2476 | int |
| 2477 | APInt::tcDivide(integerPart *lhs, const integerPart *rhs, |
| 2478 | integerPart *remainder, integerPart *srhs, |
| 2479 | unsigned int parts) |
| 2480 | { |
| 2481 | unsigned int n, shiftCount; |
| 2482 | integerPart mask; |
| 2483 | |
| 2484 | assert(lhs != remainder && lhs != srhs && remainder != srhs); |
| 2485 | |
Chris Lattner | b39cdde | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2486 | shiftCount = tcMSB(rhs, parts) + 1; |
| 2487 | if (shiftCount == 0) |
Chris Lattner | fe8e14a | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2488 | return true; |
| 2489 | |
Chris Lattner | b39cdde | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2490 | shiftCount = parts * integerPartWidth - shiftCount; |
Chris Lattner | fe8e14a | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2491 | n = shiftCount / integerPartWidth; |
| 2492 | mask = (integerPart) 1 << (shiftCount % integerPartWidth); |
| 2493 | |
| 2494 | tcAssign(srhs, rhs, parts); |
| 2495 | tcShiftLeft(srhs, parts, shiftCount); |
| 2496 | tcAssign(remainder, lhs, parts); |
| 2497 | tcSet(lhs, 0, parts); |
| 2498 | |
| 2499 | /* Loop, subtracting SRHS if REMAINDER is greater and adding that to |
| 2500 | the total. */ |
| 2501 | for(;;) { |
| 2502 | int compare; |
| 2503 | |
| 2504 | compare = tcCompare(remainder, srhs, parts); |
| 2505 | if (compare >= 0) { |
| 2506 | tcSubtract(remainder, srhs, 0, parts); |
| 2507 | lhs[n] |= mask; |
| 2508 | } |
| 2509 | |
| 2510 | if (shiftCount == 0) |
| 2511 | break; |
| 2512 | shiftCount--; |
| 2513 | tcShiftRight(srhs, parts, 1); |
| 2514 | if ((mask >>= 1) == 0) |
| 2515 | mask = (integerPart) 1 << (integerPartWidth - 1), n--; |
| 2516 | } |
| 2517 | |
| 2518 | return false; |
| 2519 | } |
| 2520 | |
| 2521 | /* Shift a bignum left COUNT bits in-place. Shifted in bits are zero. |
| 2522 | There are no restrictions on COUNT. */ |
| 2523 | void |
| 2524 | APInt::tcShiftLeft(integerPart *dst, unsigned int parts, unsigned int count) |
| 2525 | { |
Neil Booth | 68e53ad | 2007-10-08 13:47:12 +0000 | [diff] [blame] | 2526 | if (count) { |
| 2527 | unsigned int jump, shift; |
Chris Lattner | fe8e14a | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2528 | |
Neil Booth | 68e53ad | 2007-10-08 13:47:12 +0000 | [diff] [blame] | 2529 | /* Jump is the inter-part jump; shift is is intra-part shift. */ |
| 2530 | jump = count / integerPartWidth; |
| 2531 | shift = count % integerPartWidth; |
Chris Lattner | fe8e14a | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2532 | |
Neil Booth | 68e53ad | 2007-10-08 13:47:12 +0000 | [diff] [blame] | 2533 | while (parts > jump) { |
| 2534 | integerPart part; |
Chris Lattner | fe8e14a | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2535 | |
Neil Booth | 68e53ad | 2007-10-08 13:47:12 +0000 | [diff] [blame] | 2536 | parts--; |
Chris Lattner | fe8e14a | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2537 | |
Neil Booth | 68e53ad | 2007-10-08 13:47:12 +0000 | [diff] [blame] | 2538 | /* dst[i] comes from the two parts src[i - jump] and, if we have |
| 2539 | an intra-part shift, src[i - jump - 1]. */ |
| 2540 | part = dst[parts - jump]; |
| 2541 | if (shift) { |
| 2542 | part <<= shift; |
Chris Lattner | fe8e14a | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2543 | if (parts >= jump + 1) |
| 2544 | part |= dst[parts - jump - 1] >> (integerPartWidth - shift); |
| 2545 | } |
| 2546 | |
Neil Booth | 68e53ad | 2007-10-08 13:47:12 +0000 | [diff] [blame] | 2547 | dst[parts] = part; |
| 2548 | } |
Chris Lattner | fe8e14a | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2549 | |
Neil Booth | 68e53ad | 2007-10-08 13:47:12 +0000 | [diff] [blame] | 2550 | while (parts > 0) |
| 2551 | dst[--parts] = 0; |
| 2552 | } |
Chris Lattner | fe8e14a | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2553 | } |
| 2554 | |
| 2555 | /* Shift a bignum right COUNT bits in-place. Shifted in bits are |
| 2556 | zero. There are no restrictions on COUNT. */ |
| 2557 | void |
| 2558 | APInt::tcShiftRight(integerPart *dst, unsigned int parts, unsigned int count) |
| 2559 | { |
Neil Booth | 68e53ad | 2007-10-08 13:47:12 +0000 | [diff] [blame] | 2560 | if (count) { |
| 2561 | unsigned int i, jump, shift; |
Chris Lattner | fe8e14a | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2562 | |
Neil Booth | 68e53ad | 2007-10-08 13:47:12 +0000 | [diff] [blame] | 2563 | /* Jump is the inter-part jump; shift is is intra-part shift. */ |
| 2564 | jump = count / integerPartWidth; |
| 2565 | shift = count % integerPartWidth; |
Chris Lattner | fe8e14a | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2566 | |
Neil Booth | 68e53ad | 2007-10-08 13:47:12 +0000 | [diff] [blame] | 2567 | /* Perform the shift. This leaves the most significant COUNT bits |
| 2568 | of the result at zero. */ |
| 2569 | for(i = 0; i < parts; i++) { |
| 2570 | integerPart part; |
Chris Lattner | fe8e14a | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2571 | |
Neil Booth | 68e53ad | 2007-10-08 13:47:12 +0000 | [diff] [blame] | 2572 | if (i + jump >= parts) { |
| 2573 | part = 0; |
| 2574 | } else { |
| 2575 | part = dst[i + jump]; |
| 2576 | if (shift) { |
| 2577 | part >>= shift; |
| 2578 | if (i + jump + 1 < parts) |
| 2579 | part |= dst[i + jump + 1] << (integerPartWidth - shift); |
| 2580 | } |
Chris Lattner | fe8e14a | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2581 | } |
Chris Lattner | fe8e14a | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2582 | |
Neil Booth | 68e53ad | 2007-10-08 13:47:12 +0000 | [diff] [blame] | 2583 | dst[i] = part; |
| 2584 | } |
Chris Lattner | fe8e14a | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2585 | } |
| 2586 | } |
| 2587 | |
| 2588 | /* Bitwise and of two bignums. */ |
| 2589 | void |
| 2590 | APInt::tcAnd(integerPart *dst, const integerPart *rhs, unsigned int parts) |
| 2591 | { |
| 2592 | unsigned int i; |
| 2593 | |
| 2594 | for(i = 0; i < parts; i++) |
| 2595 | dst[i] &= rhs[i]; |
| 2596 | } |
| 2597 | |
| 2598 | /* Bitwise inclusive or of two bignums. */ |
| 2599 | void |
| 2600 | APInt::tcOr(integerPart *dst, const integerPart *rhs, unsigned int parts) |
| 2601 | { |
| 2602 | unsigned int i; |
| 2603 | |
| 2604 | for(i = 0; i < parts; i++) |
| 2605 | dst[i] |= rhs[i]; |
| 2606 | } |
| 2607 | |
| 2608 | /* Bitwise exclusive or of two bignums. */ |
| 2609 | void |
| 2610 | APInt::tcXor(integerPart *dst, const integerPart *rhs, unsigned int parts) |
| 2611 | { |
| 2612 | unsigned int i; |
| 2613 | |
| 2614 | for(i = 0; i < parts; i++) |
| 2615 | dst[i] ^= rhs[i]; |
| 2616 | } |
| 2617 | |
| 2618 | /* Complement a bignum in-place. */ |
| 2619 | void |
| 2620 | APInt::tcComplement(integerPart *dst, unsigned int parts) |
| 2621 | { |
| 2622 | unsigned int i; |
| 2623 | |
| 2624 | for(i = 0; i < parts; i++) |
| 2625 | dst[i] = ~dst[i]; |
| 2626 | } |
| 2627 | |
| 2628 | /* Comparison (unsigned) of two bignums. */ |
| 2629 | int |
| 2630 | APInt::tcCompare(const integerPart *lhs, const integerPart *rhs, |
| 2631 | unsigned int parts) |
| 2632 | { |
| 2633 | while (parts) { |
| 2634 | parts--; |
| 2635 | if (lhs[parts] == rhs[parts]) |
| 2636 | continue; |
| 2637 | |
| 2638 | if (lhs[parts] > rhs[parts]) |
| 2639 | return 1; |
| 2640 | else |
| 2641 | return -1; |
| 2642 | } |
| 2643 | |
| 2644 | return 0; |
| 2645 | } |
| 2646 | |
| 2647 | /* Increment a bignum in-place, return the carry flag. */ |
| 2648 | integerPart |
| 2649 | APInt::tcIncrement(integerPart *dst, unsigned int parts) |
| 2650 | { |
| 2651 | unsigned int i; |
| 2652 | |
| 2653 | for(i = 0; i < parts; i++) |
| 2654 | if (++dst[i] != 0) |
| 2655 | break; |
| 2656 | |
| 2657 | return i == parts; |
| 2658 | } |
| 2659 | |
| 2660 | /* Set the least significant BITS bits of a bignum, clear the |
| 2661 | rest. */ |
| 2662 | void |
| 2663 | APInt::tcSetLeastSignificantBits(integerPart *dst, unsigned int parts, |
| 2664 | unsigned int bits) |
| 2665 | { |
| 2666 | unsigned int i; |
| 2667 | |
| 2668 | i = 0; |
| 2669 | while (bits > integerPartWidth) { |
| 2670 | dst[i++] = ~(integerPart) 0; |
| 2671 | bits -= integerPartWidth; |
| 2672 | } |
| 2673 | |
| 2674 | if (bits) |
| 2675 | dst[i++] = ~(integerPart) 0 >> (integerPartWidth - bits); |
| 2676 | |
| 2677 | while (i < parts) |
| 2678 | dst[i++] = 0; |
| 2679 | } |