Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 1 | //===-- APInt.cpp - Implement APInt class ---------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Reid Spencer | 96d9137 | 2007-02-27 19:31:09 +0000 | [diff] [blame] | 5 | // This file was developed by Sheng Zhou and is distributed under the |
| 6 | // University of Illinois Open Source 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" |
| 17 | #include "llvm/DerivedTypes.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 | #ifndef NDEBUG |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 25 | #include <iomanip> |
| 26 | #endif |
| 27 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 30 | /// A utility function for allocating memory, checking for allocation failures, |
| 31 | /// and ensuring the contents are zeroed. |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 32 | inline static uint64_t* getClearedMemory(uint32_t numWords) { |
| 33 | uint64_t * result = new uint64_t[numWords]; |
| 34 | assert(result && "APInt memory allocation fails!"); |
| 35 | memset(result, 0, numWords * sizeof(uint64_t)); |
| 36 | return result; |
Zhou Sheng | 353815d | 2007-02-06 06:04:53 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 39 | /// A utility function for allocating memory and checking for allocation |
| 40 | /// failure. The content is not zeroed. |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 41 | inline static uint64_t* getMemory(uint32_t numWords) { |
| 42 | uint64_t * result = new uint64_t[numWords]; |
| 43 | assert(result && "APInt memory allocation fails!"); |
| 44 | return result; |
| 45 | } |
| 46 | |
Reid Spencer | adf2a20 | 2007-03-19 21:19:02 +0000 | [diff] [blame] | 47 | APInt::APInt(uint32_t numBits, uint64_t val, bool isSigned) |
Reid Spencer | 3a34137 | 2007-03-19 20:37:47 +0000 | [diff] [blame] | 48 | : BitWidth(numBits), VAL(0) { |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 49 | assert(BitWidth >= IntegerType::MIN_INT_BITS && "bitwidth too small"); |
| 50 | assert(BitWidth <= IntegerType::MAX_INT_BITS && "bitwidth too large"); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 51 | if (isSingleWord()) |
| 52 | VAL = val; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 53 | else { |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 54 | pVal = getClearedMemory(getNumWords()); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 55 | pVal[0] = val; |
Reid Spencer | 3a34137 | 2007-03-19 20:37:47 +0000 | [diff] [blame] | 56 | if (isSigned && int64_t(val) < 0) |
| 57 | for (unsigned i = 1; i < getNumWords(); ++i) |
| 58 | pVal[i] = -1ULL; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 59 | } |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 60 | clearUnusedBits(); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 63 | APInt::APInt(uint32_t numBits, uint32_t numWords, uint64_t bigVal[]) |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 64 | : BitWidth(numBits), VAL(0) { |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 65 | assert(BitWidth >= IntegerType::MIN_INT_BITS && "bitwidth too small"); |
| 66 | assert(BitWidth <= IntegerType::MAX_INT_BITS && "bitwidth too large"); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 67 | assert(bigVal && "Null pointer detected!"); |
| 68 | if (isSingleWord()) |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 69 | VAL = bigVal[0]; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 70 | else { |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 71 | // Get memory, cleared to 0 |
| 72 | pVal = getClearedMemory(getNumWords()); |
| 73 | // Calculate the number of words to copy |
| 74 | uint32_t words = std::min<uint32_t>(numWords, getNumWords()); |
| 75 | // Copy the words from bigVal to pVal |
| 76 | memcpy(pVal, bigVal, words * APINT_WORD_SIZE); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 77 | } |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 78 | // Make sure unused high bits are cleared |
| 79 | clearUnusedBits(); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 82 | APInt::APInt(uint32_t numbits, const char StrStart[], uint32_t slen, |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 83 | uint8_t radix) |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 84 | : BitWidth(numbits), VAL(0) { |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 85 | fromString(numbits, StrStart, slen, radix); |
Zhou Sheng | a3832fd | 2007-02-07 06:14:53 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 88 | APInt::APInt(uint32_t numbits, const std::string& Val, uint8_t radix) |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 89 | : BitWidth(numbits), VAL(0) { |
Zhou Sheng | a3832fd | 2007-02-07 06:14:53 +0000 | [diff] [blame] | 90 | assert(!Val.empty() && "String empty?"); |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 91 | fromString(numbits, Val.c_str(), Val.size(), radix); |
Zhou Sheng | a3832fd | 2007-02-07 06:14:53 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Reid Spencer | 54362ca | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 94 | APInt::APInt(const APInt& that) |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 95 | : BitWidth(that.BitWidth), VAL(0) { |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 96 | if (isSingleWord()) |
Reid Spencer | 54362ca | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 97 | VAL = that.VAL; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 98 | else { |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 99 | pVal = getMemory(getNumWords()); |
Reid Spencer | 54362ca | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 100 | memcpy(pVal, that.pVal, getNumWords() * APINT_WORD_SIZE); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
| 104 | APInt::~APInt() { |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 105 | if (!isSingleWord() && pVal) |
Reid Spencer | 9ac4411 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 106 | delete [] pVal; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 109 | APInt& APInt::operator=(const APInt& RHS) { |
Reid Spencer | 9ac4411 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 110 | // Don't do anything for X = X |
| 111 | if (this == &RHS) |
| 112 | return *this; |
| 113 | |
| 114 | // If the bitwidths are the same, we can avoid mucking with memory |
| 115 | if (BitWidth == RHS.getBitWidth()) { |
| 116 | if (isSingleWord()) |
| 117 | VAL = RHS.VAL; |
| 118 | else |
| 119 | memcpy(pVal, RHS.pVal, getNumWords() * APINT_WORD_SIZE); |
| 120 | return *this; |
| 121 | } |
| 122 | |
| 123 | if (isSingleWord()) |
| 124 | if (RHS.isSingleWord()) |
| 125 | VAL = RHS.VAL; |
| 126 | else { |
| 127 | VAL = 0; |
| 128 | pVal = getMemory(RHS.getNumWords()); |
| 129 | memcpy(pVal, RHS.pVal, RHS.getNumWords() * APINT_WORD_SIZE); |
| 130 | } |
| 131 | else if (getNumWords() == RHS.getNumWords()) |
| 132 | memcpy(pVal, RHS.pVal, RHS.getNumWords() * APINT_WORD_SIZE); |
| 133 | else if (RHS.isSingleWord()) { |
| 134 | delete [] pVal; |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 135 | VAL = RHS.VAL; |
Reid Spencer | 9ac4411 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 136 | } else { |
| 137 | delete [] pVal; |
| 138 | pVal = getMemory(RHS.getNumWords()); |
| 139 | memcpy(pVal, RHS.pVal, RHS.getNumWords() * APINT_WORD_SIZE); |
| 140 | } |
| 141 | BitWidth = RHS.BitWidth; |
| 142 | return clearUnusedBits(); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 145 | APInt& APInt::operator=(uint64_t RHS) { |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 146 | if (isSingleWord()) |
| 147 | VAL = RHS; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 148 | else { |
| 149 | pVal[0] = RHS; |
Reid Spencer | a58f058 | 2007-02-18 20:09:41 +0000 | [diff] [blame] | 150 | memset(pVal+1, 0, (getNumWords() - 1) * APINT_WORD_SIZE); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 151 | } |
Reid Spencer | 9ac4411 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 152 | return clearUnusedBits(); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 155 | /// add_1 - This function adds a single "digit" integer, y, to the multiple |
| 156 | /// "digit" integer array, x[]. x[] is modified to reflect the addition and |
| 157 | /// 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] | 158 | /// @returns the carry of the addition. |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 159 | 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] | 160 | for (uint32_t i = 0; i < len; ++i) { |
Reid Spencer | f2c521c | 2007-02-18 06:39:42 +0000 | [diff] [blame] | 161 | dest[i] = y + x[i]; |
| 162 | if (dest[i] < y) |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 163 | y = 1; // Carry one to next digit. |
Reid Spencer | f2c521c | 2007-02-18 06:39:42 +0000 | [diff] [blame] | 164 | else { |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 165 | y = 0; // No need to carry so exit early |
Reid Spencer | f2c521c | 2007-02-18 06:39:42 +0000 | [diff] [blame] | 166 | break; |
| 167 | } |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 168 | } |
Reid Spencer | f2c521c | 2007-02-18 06:39:42 +0000 | [diff] [blame] | 169 | return y; |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 172 | /// @brief Prefix increment operator. Increments the APInt by one. |
| 173 | APInt& APInt::operator++() { |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 174 | if (isSingleWord()) |
| 175 | ++VAL; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 176 | else |
Zhou Sheng | a3832fd | 2007-02-07 06:14:53 +0000 | [diff] [blame] | 177 | add_1(pVal, pVal, getNumWords(), 1); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 178 | return clearUnusedBits(); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 181 | /// sub_1 - This function subtracts a single "digit" (64-bit word), y, from |
| 182 | /// the multi-digit integer array, x[], propagating the borrowed 1 value until |
| 183 | /// no further borrowing is neeeded or it runs out of "digits" in x. The result |
| 184 | /// is 1 if "borrowing" exhausted the digits in x, or 0 if x was not exhausted. |
| 185 | /// 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] | 186 | /// @returns the borrow out of the subtraction |
| 187 | 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] | 188 | for (uint32_t i = 0; i < len; ++i) { |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 189 | uint64_t X = x[i]; |
Reid Spencer | f2c521c | 2007-02-18 06:39:42 +0000 | [diff] [blame] | 190 | x[i] -= y; |
| 191 | if (y > X) |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 192 | y = 1; // We have to "borrow 1" from next "digit" |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 193 | else { |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 194 | y = 0; // No need to borrow |
| 195 | break; // Remaining digits are unchanged so exit early |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 196 | } |
| 197 | } |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 198 | return bool(y); |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 201 | /// @brief Prefix decrement operator. Decrements the APInt by one. |
| 202 | APInt& APInt::operator--() { |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 203 | if (isSingleWord()) |
| 204 | --VAL; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 205 | else |
Zhou Sheng | a3832fd | 2007-02-07 06:14:53 +0000 | [diff] [blame] | 206 | sub_1(pVal, getNumWords(), 1); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 207 | return clearUnusedBits(); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 210 | /// add - This function adds the integer array x to the integer array Y and |
| 211 | /// places the result in dest. |
| 212 | /// @returns the carry out from the addition |
| 213 | /// @brief General addition of 64-bit integer arrays |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 214 | static bool add(uint64_t *dest, const uint64_t *x, const uint64_t *y, |
| 215 | uint32_t len) { |
| 216 | bool carry = false; |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 217 | for (uint32_t i = 0; i< len; ++i) { |
Reid Spencer | 9290463 | 2007-02-23 01:57:13 +0000 | [diff] [blame] | 218 | 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] | 219 | dest[i] = x[i] + y[i] + carry; |
Reid Spencer | 60c0a6a | 2007-02-21 05:44:56 +0000 | [diff] [blame] | 220 | carry = dest[i] < limit || (carry && dest[i] == limit); |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 221 | } |
| 222 | return carry; |
| 223 | } |
| 224 | |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 225 | /// Adds the RHS APint to this APInt. |
| 226 | /// @returns this, after addition of RHS. |
| 227 | /// @brief Addition assignment operator. |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 228 | APInt& APInt::operator+=(const APInt& RHS) { |
Reid Spencer | cd6f2bf | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 229 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Reid Spencer | 54362ca | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 230 | if (isSingleWord()) |
| 231 | VAL += RHS.VAL; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 232 | else { |
Reid Spencer | 54362ca | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 233 | add(pVal, pVal, RHS.pVal, getNumWords()); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 234 | } |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 235 | return clearUnusedBits(); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 238 | /// Subtracts the integer array y from the integer array x |
| 239 | /// @returns returns the borrow out. |
| 240 | /// @brief Generalized subtraction of 64-bit integer arrays. |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 241 | static bool sub(uint64_t *dest, const uint64_t *x, const uint64_t *y, |
| 242 | uint32_t len) { |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 243 | bool borrow = false; |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 244 | for (uint32_t i = 0; i < len; ++i) { |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 245 | uint64_t x_tmp = borrow ? x[i] - 1 : x[i]; |
| 246 | borrow = y[i] > x_tmp || (borrow && x[i] == 0); |
| 247 | dest[i] = x_tmp - y[i]; |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 248 | } |
Reid Spencer | 54362ca | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 249 | return borrow; |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 252 | /// Subtracts the RHS APInt from this APInt |
| 253 | /// @returns this, after subtraction |
| 254 | /// @brief Subtraction assignment operator. |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 255 | APInt& APInt::operator-=(const APInt& RHS) { |
Reid Spencer | cd6f2bf | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 256 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 257 | if (isSingleWord()) |
Reid Spencer | 54362ca | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 258 | VAL -= RHS.VAL; |
| 259 | else |
| 260 | sub(pVal, pVal, RHS.pVal, getNumWords()); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 261 | return clearUnusedBits(); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 264 | /// Multiplies an integer array, x by a a uint64_t integer and places the result |
| 265 | /// into dest. |
| 266 | /// @returns the carry out of the multiplication. |
| 267 | /// @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] | 268 | static uint64_t mul_1(uint64_t dest[], uint64_t x[], uint32_t len, uint64_t y) { |
| 269 | // 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] | 270 | uint64_t ly = y & 0xffffffffULL, hy = y >> 32; |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 271 | uint64_t carry = 0; |
| 272 | |
| 273 | // For each digit of x. |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 274 | for (uint32_t i = 0; i < len; ++i) { |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 275 | // Split x into high and low words |
| 276 | uint64_t lx = x[i] & 0xffffffffULL; |
| 277 | uint64_t hx = x[i] >> 32; |
| 278 | // 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] | 279 | // hasCarry == 0, no carry |
| 280 | // hasCarry == 1, has carry |
| 281 | // hasCarry == 2, no carry and the calculation result == 0. |
| 282 | uint8_t hasCarry = 0; |
| 283 | dest[i] = carry + lx * ly; |
| 284 | // Determine if the add above introduces carry. |
| 285 | hasCarry = (dest[i] < carry) ? 1 : 0; |
| 286 | carry = hx * ly + (dest[i] >> 32) + (hasCarry ? (1ULL << 32) : 0); |
| 287 | // The upper limit of carry can be (2^32 - 1)(2^32 - 1) + |
| 288 | // (2^32 - 1) + 2^32 = 2^64. |
| 289 | hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0); |
| 290 | |
| 291 | carry += (lx * hy) & 0xffffffffULL; |
| 292 | dest[i] = (carry << 32) | (dest[i] & 0xffffffffULL); |
| 293 | carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0) + |
| 294 | (carry >> 32) + ((lx * hy) >> 32) + hx * hy; |
| 295 | } |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 296 | return carry; |
| 297 | } |
| 298 | |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 299 | /// Multiplies integer array x by integer array y and stores the result into |
| 300 | /// the integer array dest. Note that dest's size must be >= xlen + ylen. |
| 301 | /// @brief Generalized multiplicate of integer arrays. |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 302 | static void mul(uint64_t dest[], uint64_t x[], uint32_t xlen, uint64_t y[], |
| 303 | uint32_t ylen) { |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 304 | dest[xlen] = mul_1(dest, x, xlen, y[0]); |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 305 | for (uint32_t i = 1; i < ylen; ++i) { |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 306 | uint64_t ly = y[i] & 0xffffffffULL, hy = y[i] >> 32; |
Reid Spencer | e0cdd33 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 307 | uint64_t carry = 0, lx = 0, hx = 0; |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 308 | for (uint32_t j = 0; j < xlen; ++j) { |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 309 | lx = x[j] & 0xffffffffULL; |
| 310 | hx = x[j] >> 32; |
| 311 | // hasCarry - A flag to indicate if has carry. |
| 312 | // hasCarry == 0, no carry |
| 313 | // hasCarry == 1, has carry |
| 314 | // hasCarry == 2, no carry and the calculation result == 0. |
| 315 | uint8_t hasCarry = 0; |
| 316 | uint64_t resul = carry + lx * ly; |
| 317 | hasCarry = (resul < carry) ? 1 : 0; |
| 318 | carry = (hasCarry ? (1ULL << 32) : 0) + hx * ly + (resul >> 32); |
| 319 | hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0); |
| 320 | |
| 321 | carry += (lx * hy) & 0xffffffffULL; |
| 322 | resul = (carry << 32) | (resul & 0xffffffffULL); |
| 323 | dest[i+j] += resul; |
| 324 | carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0)+ |
| 325 | (carry >> 32) + (dest[i+j] < resul ? 1 : 0) + |
| 326 | ((lx * hy) >> 32) + hx * hy; |
| 327 | } |
| 328 | dest[i+xlen] = carry; |
| 329 | } |
| 330 | } |
| 331 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 332 | APInt& APInt::operator*=(const APInt& RHS) { |
Reid Spencer | cd6f2bf | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 333 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Reid Spencer | e0cdd33 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 334 | if (isSingleWord()) { |
Reid Spencer | 61eb180 | 2007-02-20 20:42:10 +0000 | [diff] [blame] | 335 | VAL *= RHS.VAL; |
Reid Spencer | e0cdd33 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 336 | clearUnusedBits(); |
| 337 | return *this; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 338 | } |
Reid Spencer | e0cdd33 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 339 | |
| 340 | // Get some bit facts about LHS and check for zero |
| 341 | uint32_t lhsBits = getActiveBits(); |
| 342 | uint32_t lhsWords = !lhsBits ? 0 : whichWord(lhsBits - 1) + 1; |
| 343 | if (!lhsWords) |
| 344 | // 0 * X ===> 0 |
| 345 | return *this; |
| 346 | |
| 347 | // Get some bit facts about RHS and check for zero |
| 348 | uint32_t rhsBits = RHS.getActiveBits(); |
| 349 | uint32_t rhsWords = !rhsBits ? 0 : whichWord(rhsBits - 1) + 1; |
| 350 | if (!rhsWords) { |
| 351 | // X * 0 ===> 0 |
| 352 | clear(); |
| 353 | return *this; |
| 354 | } |
| 355 | |
| 356 | // Allocate space for the result |
| 357 | uint32_t destWords = rhsWords + lhsWords; |
| 358 | uint64_t *dest = getMemory(destWords); |
| 359 | |
| 360 | // Perform the long multiply |
| 361 | mul(dest, pVal, lhsWords, RHS.pVal, rhsWords); |
| 362 | |
| 363 | // Copy result back into *this |
| 364 | clear(); |
| 365 | uint32_t wordsToCopy = destWords >= getNumWords() ? getNumWords() : destWords; |
| 366 | memcpy(pVal, dest, wordsToCopy * APINT_WORD_SIZE); |
| 367 | |
| 368 | // delete dest array and return |
| 369 | delete[] dest; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 370 | return *this; |
| 371 | } |
| 372 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 373 | APInt& APInt::operator&=(const APInt& RHS) { |
Reid Spencer | cd6f2bf | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 374 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 375 | if (isSingleWord()) { |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 376 | VAL &= RHS.VAL; |
| 377 | return *this; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 378 | } |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 379 | uint32_t numWords = getNumWords(); |
| 380 | for (uint32_t i = 0; i < numWords; ++i) |
| 381 | pVal[i] &= RHS.pVal[i]; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 382 | return *this; |
| 383 | } |
| 384 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 385 | APInt& APInt::operator|=(const APInt& RHS) { |
Reid Spencer | cd6f2bf | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 386 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 387 | if (isSingleWord()) { |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 388 | VAL |= RHS.VAL; |
| 389 | return *this; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 390 | } |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 391 | uint32_t numWords = getNumWords(); |
| 392 | for (uint32_t i = 0; i < numWords; ++i) |
| 393 | pVal[i] |= RHS.pVal[i]; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 394 | return *this; |
| 395 | } |
| 396 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 397 | APInt& APInt::operator^=(const APInt& RHS) { |
Reid Spencer | cd6f2bf | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 398 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 399 | if (isSingleWord()) { |
Reid Spencer | f2c521c | 2007-02-18 06:39:42 +0000 | [diff] [blame] | 400 | VAL ^= RHS.VAL; |
Reid Spencer | 54362ca | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 401 | this->clearUnusedBits(); |
Reid Spencer | f2c521c | 2007-02-18 06:39:42 +0000 | [diff] [blame] | 402 | return *this; |
| 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]; |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 407 | return clearUnusedBits(); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 410 | APInt APInt::operator&(const APInt& RHS) const { |
Reid Spencer | cd6f2bf | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 411 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 412 | if (isSingleWord()) |
| 413 | return APInt(getBitWidth(), VAL & RHS.VAL); |
| 414 | |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 415 | uint32_t numWords = getNumWords(); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 416 | uint64_t* val = getMemory(numWords); |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 417 | for (uint32_t i = 0; i < numWords; ++i) |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 418 | val[i] = pVal[i] & RHS.pVal[i]; |
| 419 | return APInt(val, getBitWidth()); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 422 | APInt APInt::operator|(const APInt& RHS) const { |
Reid Spencer | cd6f2bf | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 423 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 424 | if (isSingleWord()) |
| 425 | return APInt(getBitWidth(), VAL | RHS.VAL); |
Reid Spencer | 54362ca | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 426 | |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 427 | uint32_t numWords = getNumWords(); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 428 | uint64_t *val = getMemory(numWords); |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 429 | for (uint32_t i = 0; i < numWords; ++i) |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 430 | val[i] = pVal[i] | RHS.pVal[i]; |
| 431 | return APInt(val, getBitWidth()); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 434 | APInt APInt::operator^(const APInt& RHS) const { |
Reid Spencer | cd6f2bf | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 435 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 436 | if (isSingleWord()) |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 437 | return APInt(BitWidth, VAL ^ RHS.VAL); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 438 | |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 439 | uint32_t numWords = getNumWords(); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 440 | uint64_t *val = getMemory(numWords); |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 441 | for (uint32_t i = 0; i < numWords; ++i) |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 442 | val[i] = pVal[i] ^ RHS.pVal[i]; |
| 443 | |
| 444 | // 0^0==1 so clear the high bits in case they got set. |
| 445 | return APInt(val, getBitWidth()).clearUnusedBits(); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 448 | bool APInt::operator !() const { |
| 449 | if (isSingleWord()) |
| 450 | return !VAL; |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 451 | |
| 452 | for (uint32_t i = 0; i < getNumWords(); ++i) |
| 453 | if (pVal[i]) |
| 454 | return false; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 455 | return true; |
| 456 | } |
| 457 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 458 | APInt APInt::operator*(const APInt& RHS) const { |
Reid Spencer | cd6f2bf | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 459 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 460 | if (isSingleWord()) |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 461 | return APInt(BitWidth, VAL * RHS.VAL); |
Reid Spencer | 61eb180 | 2007-02-20 20:42:10 +0000 | [diff] [blame] | 462 | APInt Result(*this); |
| 463 | Result *= RHS; |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 464 | return Result.clearUnusedBits(); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 467 | APInt APInt::operator+(const APInt& RHS) const { |
Reid Spencer | cd6f2bf | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 468 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 469 | if (isSingleWord()) |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 470 | return APInt(BitWidth, VAL + RHS.VAL); |
Reid Spencer | 54362ca | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 471 | APInt Result(BitWidth, 0); |
| 472 | add(Result.pVal, this->pVal, RHS.pVal, getNumWords()); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 473 | return Result.clearUnusedBits(); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 476 | APInt APInt::operator-(const APInt& RHS) const { |
Reid Spencer | cd6f2bf | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 477 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 478 | if (isSingleWord()) |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 479 | return APInt(BitWidth, VAL - RHS.VAL); |
Reid Spencer | 54362ca | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 480 | APInt Result(BitWidth, 0); |
| 481 | sub(Result.pVal, this->pVal, RHS.pVal, getNumWords()); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 482 | return Result.clearUnusedBits(); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 485 | bool APInt::operator[](uint32_t bitPosition) const { |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 486 | return (maskBit(bitPosition) & |
| 487 | (isSingleWord() ? VAL : pVal[whichWord(bitPosition)])) != 0; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 490 | bool APInt::operator==(const APInt& RHS) const { |
Reid Spencer | 9ac4411 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 491 | assert(BitWidth == RHS.BitWidth && "Comparison requires equal bit widths"); |
Reid Spencer | 54362ca | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 492 | if (isSingleWord()) |
| 493 | return VAL == RHS.VAL; |
| 494 | |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 495 | // 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] | 496 | uint32_t n1 = getActiveBits(); |
| 497 | uint32_t n2 = RHS.getActiveBits(); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 498 | |
| 499 | // 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] | 500 | if (n1 != n2) |
| 501 | return false; |
| 502 | |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 503 | // 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] | 504 | if (n1 <= APINT_BITS_PER_WORD) |
| 505 | return pVal[0] == RHS.pVal[0]; |
| 506 | |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 507 | // Otherwise, compare everything |
Reid Spencer | 54362ca | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 508 | for (int i = whichWord(n1 - 1); i >= 0; --i) |
| 509 | if (pVal[i] != RHS.pVal[i]) |
| 510 | return false; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 511 | return true; |
| 512 | } |
| 513 | |
Zhou Sheng | a3832fd | 2007-02-07 06:14:53 +0000 | [diff] [blame] | 514 | bool APInt::operator==(uint64_t Val) const { |
| 515 | if (isSingleWord()) |
| 516 | return VAL == Val; |
Reid Spencer | 54362ca | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 517 | |
| 518 | uint32_t n = getActiveBits(); |
| 519 | if (n <= APINT_BITS_PER_WORD) |
| 520 | return pVal[0] == Val; |
| 521 | else |
| 522 | return false; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 525 | bool APInt::ult(const APInt& RHS) const { |
| 526 | assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison"); |
| 527 | if (isSingleWord()) |
| 528 | return VAL < RHS.VAL; |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 529 | |
| 530 | // Get active bit length of both operands |
| 531 | uint32_t n1 = getActiveBits(); |
| 532 | uint32_t n2 = RHS.getActiveBits(); |
| 533 | |
| 534 | // If magnitude of LHS is less than RHS, return true. |
| 535 | if (n1 < n2) |
| 536 | return true; |
| 537 | |
| 538 | // If magnitude of RHS is greather than LHS, return false. |
| 539 | if (n2 < n1) |
| 540 | return false; |
| 541 | |
| 542 | // If they bot fit in a word, just compare the low order word |
| 543 | if (n1 <= APINT_BITS_PER_WORD && n2 <= APINT_BITS_PER_WORD) |
| 544 | return pVal[0] < RHS.pVal[0]; |
| 545 | |
| 546 | // Otherwise, compare all words |
Reid Spencer | 1fa111e | 2007-02-27 18:23:40 +0000 | [diff] [blame] | 547 | uint32_t topWord = whichWord(std::max(n1,n2)-1); |
| 548 | for (int i = topWord; i >= 0; --i) { |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 549 | if (pVal[i] > RHS.pVal[i]) |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 550 | return false; |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 551 | if (pVal[i] < RHS.pVal[i]) |
| 552 | return true; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 553 | } |
| 554 | return false; |
| 555 | } |
| 556 | |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 557 | bool APInt::slt(const APInt& RHS) const { |
| 558 | assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison"); |
Reid Spencer | a58f058 | 2007-02-18 20:09:41 +0000 | [diff] [blame] | 559 | if (isSingleWord()) { |
| 560 | int64_t lhsSext = (int64_t(VAL) << (64-BitWidth)) >> (64-BitWidth); |
| 561 | int64_t rhsSext = (int64_t(RHS.VAL) << (64-BitWidth)) >> (64-BitWidth); |
| 562 | return lhsSext < rhsSext; |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 563 | } |
Reid Spencer | a58f058 | 2007-02-18 20:09:41 +0000 | [diff] [blame] | 564 | |
| 565 | APInt lhs(*this); |
Reid Spencer | 1fa111e | 2007-02-27 18:23:40 +0000 | [diff] [blame] | 566 | APInt rhs(RHS); |
| 567 | bool lhsNeg = isNegative(); |
| 568 | bool rhsNeg = rhs.isNegative(); |
| 569 | if (lhsNeg) { |
| 570 | // 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] | 571 | lhs.flip(); |
| 572 | lhs++; |
| 573 | } |
Reid Spencer | 1fa111e | 2007-02-27 18:23:40 +0000 | [diff] [blame] | 574 | if (rhsNeg) { |
| 575 | // 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] | 576 | rhs.flip(); |
| 577 | rhs++; |
| 578 | } |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 579 | |
| 580 | // Now we have unsigned values to compare so do the comparison if necessary |
| 581 | // based on the negativeness of the values. |
Reid Spencer | 1fa111e | 2007-02-27 18:23:40 +0000 | [diff] [blame] | 582 | if (lhsNeg) |
| 583 | if (rhsNeg) |
| 584 | return lhs.ugt(rhs); |
Reid Spencer | a58f058 | 2007-02-18 20:09:41 +0000 | [diff] [blame] | 585 | else |
| 586 | return true; |
Reid Spencer | 1fa111e | 2007-02-27 18:23:40 +0000 | [diff] [blame] | 587 | else if (rhsNeg) |
Reid Spencer | a58f058 | 2007-02-18 20:09:41 +0000 | [diff] [blame] | 588 | return false; |
| 589 | else |
| 590 | return lhs.ult(rhs); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 593 | APInt& APInt::set(uint32_t bitPosition) { |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 594 | if (isSingleWord()) |
| 595 | VAL |= maskBit(bitPosition); |
| 596 | else |
| 597 | pVal[whichWord(bitPosition)] |= maskBit(bitPosition); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 598 | return *this; |
| 599 | } |
| 600 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 601 | APInt& APInt::set() { |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 602 | if (isSingleWord()) { |
| 603 | VAL = -1ULL; |
| 604 | return clearUnusedBits(); |
Zhou Sheng | b04973e | 2007-02-15 06:36:31 +0000 | [diff] [blame] | 605 | } |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 606 | |
| 607 | // Set all the bits in all the words. |
Zhou Sheng | 6dbe233 | 2007-03-21 04:34:37 +0000 | [diff] [blame] | 608 | for (uint32_t i = 0; i < getNumWords(); ++i) |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 609 | pVal[i] = -1ULL; |
| 610 | // Clear the unused ones |
| 611 | return clearUnusedBits(); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | /// Set the given bit to 0 whose position is given as "bitPosition". |
| 615 | /// @brief Set a given bit to 0. |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 616 | APInt& APInt::clear(uint32_t bitPosition) { |
| 617 | if (isSingleWord()) |
| 618 | VAL &= ~maskBit(bitPosition); |
| 619 | else |
| 620 | pVal[whichWord(bitPosition)] &= ~maskBit(bitPosition); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 621 | return *this; |
| 622 | } |
| 623 | |
| 624 | /// @brief Set every bit to 0. |
| 625 | APInt& APInt::clear() { |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 626 | if (isSingleWord()) |
| 627 | VAL = 0; |
Zhou Sheng | a3832fd | 2007-02-07 06:14:53 +0000 | [diff] [blame] | 628 | else |
Reid Spencer | a58f058 | 2007-02-18 20:09:41 +0000 | [diff] [blame] | 629 | memset(pVal, 0, getNumWords() * APINT_WORD_SIZE); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 630 | return *this; |
| 631 | } |
| 632 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 633 | /// @brief Bitwise NOT operator. Performs a bitwise logical NOT operation on |
| 634 | /// this APInt. |
| 635 | APInt APInt::operator~() const { |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 636 | APInt Result(*this); |
| 637 | Result.flip(); |
| 638 | return Result; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | /// @brief Toggle every bit to its opposite value. |
| 642 | APInt& APInt::flip() { |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 643 | if (isSingleWord()) { |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 644 | VAL ^= -1ULL; |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 645 | return clearUnusedBits(); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 646 | } |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 647 | for (uint32_t i = 0; i < getNumWords(); ++i) |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 648 | pVal[i] ^= -1ULL; |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 649 | return clearUnusedBits(); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | /// Toggle a given bit to its opposite value whose position is given |
| 653 | /// as "bitPosition". |
| 654 | /// @brief Toggles a given bit to its opposite value. |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 655 | APInt& APInt::flip(uint32_t bitPosition) { |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 656 | assert(bitPosition < BitWidth && "Out of the bit-width range!"); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 657 | if ((*this)[bitPosition]) clear(bitPosition); |
| 658 | else set(bitPosition); |
| 659 | return *this; |
| 660 | } |
| 661 | |
Reid Spencer | 794f472 | 2007-02-26 21:02:27 +0000 | [diff] [blame] | 662 | uint64_t APInt::getHashValue() const { |
Reid Spencer | 9ac4411 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 663 | // Put the bit width into the low order bits. |
| 664 | uint64_t hash = BitWidth; |
Reid Spencer | 794f472 | 2007-02-26 21:02:27 +0000 | [diff] [blame] | 665 | |
| 666 | // Add the sum of the words to the hash. |
| 667 | if (isSingleWord()) |
Reid Spencer | 9ac4411 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 668 | hash += VAL << 6; // clear separation of up to 64 bits |
Reid Spencer | 794f472 | 2007-02-26 21:02:27 +0000 | [diff] [blame] | 669 | else |
| 670 | for (uint32_t i = 0; i < getNumWords(); ++i) |
Reid Spencer | 9ac4411 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 671 | hash += pVal[i] << 6; // clear sepration of up to 64 bits |
Reid Spencer | 794f472 | 2007-02-26 21:02:27 +0000 | [diff] [blame] | 672 | return hash; |
| 673 | } |
| 674 | |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 675 | /// HiBits - This function returns the high "numBits" bits of this APInt. |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 676 | APInt APInt::getHiBits(uint32_t numBits) const { |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 677 | return APIntOps::lshr(*this, BitWidth - numBits); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | /// LoBits - This function returns the low "numBits" bits of this APInt. |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 681 | APInt APInt::getLoBits(uint32_t numBits) const { |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 682 | return APIntOps::lshr(APIntOps::shl(*this, BitWidth - numBits), |
| 683 | BitWidth - numBits); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 686 | bool APInt::isPowerOf2() const { |
| 687 | return (!!*this) && !(*this & (*this - APInt(BitWidth,1))); |
| 688 | } |
| 689 | |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 690 | uint32_t APInt::countLeadingZeros() const { |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 691 | uint32_t Count = 0; |
Reid Spencer | e549c49 | 2007-02-21 00:29:48 +0000 | [diff] [blame] | 692 | if (isSingleWord()) |
| 693 | Count = CountLeadingZeros_64(VAL); |
| 694 | else { |
| 695 | for (uint32_t i = getNumWords(); i > 0u; --i) { |
| 696 | if (pVal[i-1] == 0) |
| 697 | Count += APINT_BITS_PER_WORD; |
| 698 | else { |
| 699 | Count += CountLeadingZeros_64(pVal[i-1]); |
| 700 | break; |
| 701 | } |
| 702 | } |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 703 | } |
Reid Spencer | ab2b2c8 | 2007-02-22 00:22:00 +0000 | [diff] [blame] | 704 | uint32_t remainder = BitWidth % APINT_BITS_PER_WORD; |
| 705 | if (remainder) |
| 706 | Count -= APINT_BITS_PER_WORD - remainder; |
| 707 | return Count; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Reid Spencer | 681dcd1 | 2007-02-27 21:59:26 +0000 | [diff] [blame] | 710 | static uint32_t countLeadingOnes_64(uint64_t V, uint32_t skip) { |
| 711 | uint32_t Count = 0; |
| 712 | if (skip) |
| 713 | V <<= skip; |
| 714 | while (V && (V & (1ULL << 63))) { |
| 715 | Count++; |
| 716 | V <<= 1; |
| 717 | } |
| 718 | return Count; |
| 719 | } |
| 720 | |
| 721 | uint32_t APInt::countLeadingOnes() const { |
| 722 | if (isSingleWord()) |
| 723 | return countLeadingOnes_64(VAL, APINT_BITS_PER_WORD - BitWidth); |
| 724 | |
| 725 | uint32_t highWordBits = BitWidth % APINT_BITS_PER_WORD; |
| 726 | uint32_t shift = (highWordBits == 0 ? 0 : APINT_BITS_PER_WORD - highWordBits); |
| 727 | int i = getNumWords() - 1; |
| 728 | uint32_t Count = countLeadingOnes_64(pVal[i], shift); |
| 729 | if (Count == highWordBits) { |
| 730 | for (i--; i >= 0; --i) { |
| 731 | if (pVal[i] == -1ULL) |
| 732 | Count += APINT_BITS_PER_WORD; |
| 733 | else { |
| 734 | Count += countLeadingOnes_64(pVal[i], 0); |
| 735 | break; |
| 736 | } |
| 737 | } |
| 738 | } |
| 739 | return Count; |
| 740 | } |
| 741 | |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 742 | uint32_t APInt::countTrailingZeros() const { |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 743 | if (isSingleWord()) |
Reid Spencer | 443b570 | 2007-02-18 00:44:22 +0000 | [diff] [blame] | 744 | return CountTrailingZeros_64(VAL); |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 745 | uint32_t Count = 0; |
| 746 | uint32_t i = 0; |
| 747 | for (; i < getNumWords() && pVal[i] == 0; ++i) |
| 748 | Count += APINT_BITS_PER_WORD; |
| 749 | if (i < getNumWords()) |
| 750 | Count += CountTrailingZeros_64(pVal[i]); |
| 751 | return Count; |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 754 | uint32_t APInt::countPopulation() const { |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 755 | if (isSingleWord()) |
| 756 | return CountPopulation_64(VAL); |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 757 | uint32_t Count = 0; |
| 758 | for (uint32_t i = 0; i < getNumWords(); ++i) |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 759 | Count += CountPopulation_64(pVal[i]); |
| 760 | return Count; |
| 761 | } |
| 762 | |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 763 | APInt APInt::byteSwap() const { |
| 764 | assert(BitWidth >= 16 && BitWidth % 16 == 0 && "Cannot byteswap!"); |
| 765 | if (BitWidth == 16) |
Jeff Cohen | 09dfd8e | 2007-03-20 20:42:36 +0000 | [diff] [blame] | 766 | return APInt(BitWidth, ByteSwap_16(uint16_t(VAL))); |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 767 | else if (BitWidth == 32) |
Jeff Cohen | 09dfd8e | 2007-03-20 20:42:36 +0000 | [diff] [blame] | 768 | return APInt(BitWidth, ByteSwap_32(uint32_t(VAL))); |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 769 | else if (BitWidth == 48) { |
Jeff Cohen | 09dfd8e | 2007-03-20 20:42:36 +0000 | [diff] [blame] | 770 | uint32_t Tmp1 = uint32_t(VAL >> 16); |
Zhou Sheng | b04973e | 2007-02-15 06:36:31 +0000 | [diff] [blame] | 771 | Tmp1 = ByteSwap_32(Tmp1); |
Jeff Cohen | 09dfd8e | 2007-03-20 20:42:36 +0000 | [diff] [blame] | 772 | uint16_t Tmp2 = uint16_t(VAL); |
Zhou Sheng | b04973e | 2007-02-15 06:36:31 +0000 | [diff] [blame] | 773 | Tmp2 = ByteSwap_16(Tmp2); |
Jeff Cohen | 09dfd8e | 2007-03-20 20:42:36 +0000 | [diff] [blame] | 774 | return APInt(BitWidth, (uint64_t(Tmp2) << 32) | Tmp1); |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 775 | } else if (BitWidth == 64) |
Reid Spencer | cd6f2bf | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 776 | return APInt(BitWidth, ByteSwap_64(VAL)); |
Zhou Sheng | b04973e | 2007-02-15 06:36:31 +0000 | [diff] [blame] | 777 | else { |
Reid Spencer | cd6f2bf | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 778 | APInt Result(BitWidth, 0); |
Zhou Sheng | b04973e | 2007-02-15 06:36:31 +0000 | [diff] [blame] | 779 | char *pByte = (char*)Result.pVal; |
Reid Spencer | a58f058 | 2007-02-18 20:09:41 +0000 | [diff] [blame] | 780 | for (uint32_t i = 0; i < BitWidth / APINT_WORD_SIZE / 2; ++i) { |
Zhou Sheng | b04973e | 2007-02-15 06:36:31 +0000 | [diff] [blame] | 781 | char Tmp = pByte[i]; |
Reid Spencer | a58f058 | 2007-02-18 20:09:41 +0000 | [diff] [blame] | 782 | pByte[i] = pByte[BitWidth / APINT_WORD_SIZE - 1 - i]; |
| 783 | pByte[BitWidth / APINT_WORD_SIZE - i - 1] = Tmp; |
Zhou Sheng | b04973e | 2007-02-15 06:36:31 +0000 | [diff] [blame] | 784 | } |
| 785 | return Result; |
| 786 | } |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Zhou Sheng | 0b706b1 | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 789 | APInt llvm::APIntOps::GreatestCommonDivisor(const APInt& API1, |
| 790 | const APInt& API2) { |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 791 | APInt A = API1, B = API2; |
| 792 | while (!!B) { |
| 793 | APInt T = B; |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 794 | B = APIntOps::urem(A, B); |
Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 795 | A = T; |
| 796 | } |
| 797 | return A; |
| 798 | } |
Chris Lattner | 6ad4c14 | 2007-02-06 05:38:37 +0000 | [diff] [blame] | 799 | |
Reid Spencer | 1fa111e | 2007-02-27 18:23:40 +0000 | [diff] [blame] | 800 | APInt llvm::APIntOps::RoundDoubleToAPInt(double Double, uint32_t width) { |
Zhou Sheng | d93f00c | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 801 | union { |
| 802 | double D; |
| 803 | uint64_t I; |
| 804 | } T; |
| 805 | T.D = Double; |
Reid Spencer | 30f44f3 | 2007-02-27 01:28:10 +0000 | [diff] [blame] | 806 | |
| 807 | // Get the sign bit from the highest order bit |
Zhou Sheng | d93f00c | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 808 | bool isNeg = T.I >> 63; |
Reid Spencer | 30f44f3 | 2007-02-27 01:28:10 +0000 | [diff] [blame] | 809 | |
| 810 | // Get the 11-bit exponent and adjust for the 1023 bit bias |
Zhou Sheng | d93f00c | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 811 | int64_t exp = ((T.I >> 52) & 0x7ff) - 1023; |
Reid Spencer | 30f44f3 | 2007-02-27 01:28:10 +0000 | [diff] [blame] | 812 | |
| 813 | // 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] | 814 | if (exp < 0) |
Reid Spencer | ff60576 | 2007-02-28 01:30:08 +0000 | [diff] [blame] | 815 | return APInt(width, 0u); |
Reid Spencer | 30f44f3 | 2007-02-27 01:28:10 +0000 | [diff] [blame] | 816 | |
| 817 | // Extract the mantissa by clearing the top 12 bits (sign + exponent). |
| 818 | uint64_t mantissa = (T.I & (~0ULL >> 12)) | 1ULL << 52; |
| 819 | |
| 820 | // If the exponent doesn't shift all bits out of the mantissa |
Zhou Sheng | d93f00c | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 821 | if (exp < 52) |
Reid Spencer | 1fa111e | 2007-02-27 18:23:40 +0000 | [diff] [blame] | 822 | return isNeg ? -APInt(width, mantissa >> (52 - exp)) : |
| 823 | APInt(width, mantissa >> (52 - exp)); |
| 824 | |
| 825 | // If the client didn't provide enough bits for us to shift the mantissa into |
| 826 | // then the result is undefined, just return 0 |
| 827 | if (width <= exp - 52) |
| 828 | return APInt(width, 0); |
Reid Spencer | 30f44f3 | 2007-02-27 01:28:10 +0000 | [diff] [blame] | 829 | |
| 830 | // 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] | 831 | APInt Tmp(width, mantissa); |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 832 | Tmp = Tmp.shl(exp - 52); |
Zhou Sheng | d93f00c | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 833 | return isNeg ? -Tmp : Tmp; |
| 834 | } |
| 835 | |
Reid Spencer | db3faa6 | 2007-02-13 22:41:58 +0000 | [diff] [blame] | 836 | /// RoundToDouble - This function convert this APInt to a double. |
Zhou Sheng | d93f00c | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 837 | /// The layout for double is as following (IEEE Standard 754): |
| 838 | /// -------------------------------------- |
| 839 | /// | Sign Exponent Fraction Bias | |
| 840 | /// |-------------------------------------- | |
| 841 | /// | 1[63] 11[62-52] 52[51-00] 1023 | |
| 842 | /// -------------------------------------- |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 843 | double APInt::roundToDouble(bool isSigned) const { |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 844 | |
| 845 | // 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] | 846 | if (isSingleWord() || getActiveBits() <= APINT_BITS_PER_WORD) { |
| 847 | if (isSigned) { |
| 848 | int64_t sext = (int64_t(VAL) << (64-BitWidth)) >> (64-BitWidth); |
| 849 | return double(sext); |
| 850 | } else |
| 851 | return double(VAL); |
| 852 | } |
| 853 | |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 854 | // Determine if the value is negative. |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 855 | bool isNeg = isSigned ? (*this)[BitWidth-1] : false; |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 856 | |
| 857 | // Construct the absolute value if we're negative. |
Zhou Sheng | d93f00c | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 858 | APInt Tmp(isNeg ? -(*this) : (*this)); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 859 | |
| 860 | // Figure out how many bits we're using. |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 861 | uint32_t n = Tmp.getActiveBits(); |
Zhou Sheng | d93f00c | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 862 | |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 863 | // The exponent (without bias normalization) is just the number of bits |
| 864 | // we are using. Note that the sign bit is gone since we constructed the |
| 865 | // absolute value. |
| 866 | uint64_t exp = n; |
Zhou Sheng | d93f00c | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 867 | |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 868 | // Return infinity for exponent overflow |
| 869 | if (exp > 1023) { |
| 870 | if (!isSigned || !isNeg) |
Jeff Cohen | 09dfd8e | 2007-03-20 20:42:36 +0000 | [diff] [blame] | 871 | return std::numeric_limits<double>::infinity(); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 872 | else |
Jeff Cohen | 09dfd8e | 2007-03-20 20:42:36 +0000 | [diff] [blame] | 873 | return -std::numeric_limits<double>::infinity(); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 874 | } |
| 875 | exp += 1023; // Increment for 1023 bias |
| 876 | |
| 877 | // Number of bits in mantissa is 52. To obtain the mantissa value, we must |
| 878 | // extract the high 52 bits from the correct words in pVal. |
Zhou Sheng | d93f00c | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 879 | uint64_t mantissa; |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 880 | unsigned hiWord = whichWord(n-1); |
| 881 | if (hiWord == 0) { |
| 882 | mantissa = Tmp.pVal[0]; |
| 883 | if (n > 52) |
| 884 | mantissa >>= n - 52; // shift down, we want the top 52 bits. |
| 885 | } else { |
| 886 | assert(hiWord > 0 && "huh?"); |
| 887 | uint64_t hibits = Tmp.pVal[hiWord] << (52 - n % APINT_BITS_PER_WORD); |
| 888 | uint64_t lobits = Tmp.pVal[hiWord-1] >> (11 + n % APINT_BITS_PER_WORD); |
| 889 | mantissa = hibits | lobits; |
| 890 | } |
| 891 | |
Zhou Sheng | d93f00c | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 892 | // The leading bit of mantissa is implicit, so get rid of it. |
Reid Spencer | 443b570 | 2007-02-18 00:44:22 +0000 | [diff] [blame] | 893 | uint64_t sign = isNeg ? (1ULL << (APINT_BITS_PER_WORD - 1)) : 0; |
Zhou Sheng | d93f00c | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 894 | union { |
| 895 | double D; |
| 896 | uint64_t I; |
| 897 | } T; |
| 898 | T.I = sign | (exp << 52) | mantissa; |
| 899 | return T.D; |
| 900 | } |
| 901 | |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 902 | // Truncate to new width. |
Reid Spencer | 9490077 | 2007-02-28 17:34:32 +0000 | [diff] [blame] | 903 | APInt &APInt::trunc(uint32_t width) { |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 904 | assert(width < BitWidth && "Invalid APInt Truncate request"); |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 905 | assert(width >= IntegerType::MIN_INT_BITS && "Can't truncate to 0 bits"); |
| 906 | uint32_t wordsBefore = getNumWords(); |
| 907 | BitWidth = width; |
| 908 | uint32_t wordsAfter = getNumWords(); |
| 909 | if (wordsBefore != wordsAfter) { |
| 910 | if (wordsAfter == 1) { |
| 911 | uint64_t *tmp = pVal; |
| 912 | VAL = pVal[0]; |
Reid Spencer | 9ac4411 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 913 | delete [] tmp; |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 914 | } else { |
| 915 | uint64_t *newVal = getClearedMemory(wordsAfter); |
| 916 | for (uint32_t i = 0; i < wordsAfter; ++i) |
| 917 | newVal[i] = pVal[i]; |
Reid Spencer | 9ac4411 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 918 | delete [] pVal; |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 919 | pVal = newVal; |
| 920 | } |
| 921 | } |
Reid Spencer | 9490077 | 2007-02-28 17:34:32 +0000 | [diff] [blame] | 922 | return clearUnusedBits(); |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | // Sign extend to a new width. |
Reid Spencer | 9490077 | 2007-02-28 17:34:32 +0000 | [diff] [blame] | 926 | APInt &APInt::sext(uint32_t width) { |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 927 | assert(width > BitWidth && "Invalid APInt SignExtend request"); |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 928 | assert(width <= IntegerType::MAX_INT_BITS && "Too many bits"); |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 929 | // 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] | 930 | if (!isNegative()) { |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 931 | zext(width); |
Reid Spencer | 9490077 | 2007-02-28 17:34:32 +0000 | [diff] [blame] | 932 | return *this; |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | // The sign bit is set. First, get some facts |
| 936 | uint32_t wordsBefore = getNumWords(); |
| 937 | uint32_t wordBits = BitWidth % APINT_BITS_PER_WORD; |
| 938 | BitWidth = width; |
| 939 | uint32_t wordsAfter = getNumWords(); |
| 940 | |
| 941 | // Mask the high order word appropriately |
| 942 | if (wordsBefore == wordsAfter) { |
| 943 | uint32_t newWordBits = width % APINT_BITS_PER_WORD; |
| 944 | // The extension is contained to the wordsBefore-1th word. |
Reid Spencer | 36184ed | 2007-03-02 01:19:42 +0000 | [diff] [blame] | 945 | uint64_t mask = ~0ULL; |
| 946 | if (newWordBits) |
| 947 | mask >>= APINT_BITS_PER_WORD - newWordBits; |
| 948 | mask <<= wordBits; |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 949 | if (wordsBefore == 1) |
| 950 | VAL |= mask; |
| 951 | else |
| 952 | pVal[wordsBefore-1] |= mask; |
Reid Spencer | 295e40a | 2007-03-01 23:30:25 +0000 | [diff] [blame] | 953 | return clearUnusedBits(); |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 954 | } |
| 955 | |
Reid Spencer | f30b188 | 2007-02-25 23:54:00 +0000 | [diff] [blame] | 956 | uint64_t mask = wordBits == 0 ? 0 : ~0ULL << wordBits; |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 957 | uint64_t *newVal = getMemory(wordsAfter); |
| 958 | if (wordsBefore == 1) |
| 959 | newVal[0] = VAL | mask; |
| 960 | else { |
| 961 | for (uint32_t i = 0; i < wordsBefore; ++i) |
| 962 | newVal[i] = pVal[i]; |
| 963 | newVal[wordsBefore-1] |= mask; |
| 964 | } |
| 965 | for (uint32_t i = wordsBefore; i < wordsAfter; i++) |
| 966 | newVal[i] = -1ULL; |
| 967 | if (wordsBefore != 1) |
Reid Spencer | 9ac4411 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 968 | delete [] pVal; |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 969 | pVal = newVal; |
Reid Spencer | 9490077 | 2007-02-28 17:34:32 +0000 | [diff] [blame] | 970 | return clearUnusedBits(); |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | // Zero extend to a new width. |
Reid Spencer | 9490077 | 2007-02-28 17:34:32 +0000 | [diff] [blame] | 974 | APInt &APInt::zext(uint32_t width) { |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 975 | assert(width > BitWidth && "Invalid APInt ZeroExtend request"); |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 976 | assert(width <= IntegerType::MAX_INT_BITS && "Too many bits"); |
| 977 | uint32_t wordsBefore = getNumWords(); |
| 978 | BitWidth = width; |
| 979 | uint32_t wordsAfter = getNumWords(); |
| 980 | if (wordsBefore != wordsAfter) { |
| 981 | uint64_t *newVal = getClearedMemory(wordsAfter); |
| 982 | if (wordsBefore == 1) |
| 983 | newVal[0] = VAL; |
| 984 | else |
| 985 | for (uint32_t i = 0; i < wordsBefore; ++i) |
| 986 | newVal[i] = pVal[i]; |
| 987 | if (wordsBefore != 1) |
Reid Spencer | 9ac4411 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 988 | delete [] pVal; |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 989 | pVal = newVal; |
| 990 | } |
Reid Spencer | 9490077 | 2007-02-28 17:34:32 +0000 | [diff] [blame] | 991 | return *this; |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 992 | } |
| 993 | |
Reid Spencer | 68e2300 | 2007-03-01 17:15:32 +0000 | [diff] [blame] | 994 | APInt &APInt::zextOrTrunc(uint32_t width) { |
| 995 | if (BitWidth < width) |
| 996 | return zext(width); |
| 997 | if (BitWidth > width) |
| 998 | return trunc(width); |
| 999 | return *this; |
| 1000 | } |
| 1001 | |
| 1002 | APInt &APInt::sextOrTrunc(uint32_t width) { |
| 1003 | if (BitWidth < width) |
| 1004 | return sext(width); |
| 1005 | if (BitWidth > width) |
| 1006 | return trunc(width); |
| 1007 | return *this; |
| 1008 | } |
| 1009 | |
Zhou Sheng | ff4304f | 2007-02-09 07:48:24 +0000 | [diff] [blame] | 1010 | /// Arithmetic right-shift this APInt by shiftAmt. |
Zhou Sheng | 0b706b1 | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1011 | /// @brief Arithmetic right-shift function. |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 1012 | APInt APInt::ashr(uint32_t shiftAmt) const { |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1013 | assert(shiftAmt <= BitWidth && "Invalid shift amount"); |
Reid Spencer | 46f9c94 | 2007-03-02 22:39:11 +0000 | [diff] [blame] | 1014 | // Handle a degenerate case |
| 1015 | if (shiftAmt == 0) |
| 1016 | return *this; |
| 1017 | |
| 1018 | // Handle single word shifts with built-in ashr |
Reid Spencer | 24c4a8f | 2007-02-25 01:56:07 +0000 | [diff] [blame] | 1019 | if (isSingleWord()) { |
| 1020 | if (shiftAmt == BitWidth) |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1021 | return APInt(BitWidth, 0); // undefined |
| 1022 | else { |
| 1023 | uint32_t SignBit = APINT_BITS_PER_WORD - BitWidth; |
Reid Spencer | 24c4a8f | 2007-02-25 01:56:07 +0000 | [diff] [blame] | 1024 | return APInt(BitWidth, |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1025 | (((int64_t(VAL) << SignBit) >> SignBit) >> shiftAmt)); |
| 1026 | } |
Zhou Sheng | 0b706b1 | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1027 | } |
Reid Spencer | 24c4a8f | 2007-02-25 01:56:07 +0000 | [diff] [blame] | 1028 | |
Reid Spencer | 46f9c94 | 2007-03-02 22:39:11 +0000 | [diff] [blame] | 1029 | // If all the bits were shifted out, the result is, technically, undefined. |
| 1030 | // We return -1 if it was negative, 0 otherwise. We check this early to avoid |
| 1031 | // issues in the algorithm below. |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1032 | if (shiftAmt == BitWidth) |
| 1033 | if (isNegative()) |
| 1034 | return APInt(BitWidth, -1ULL); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 1035 | else |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1036 | return APInt(BitWidth, 0); |
| 1037 | |
| 1038 | // Create some space for the result. |
| 1039 | uint64_t * val = new uint64_t[getNumWords()]; |
| 1040 | |
Reid Spencer | 46f9c94 | 2007-03-02 22:39:11 +0000 | [diff] [blame] | 1041 | // Compute some values needed by the following shift algorithms |
| 1042 | uint32_t wordShift = shiftAmt % APINT_BITS_PER_WORD; // bits to shift per word |
| 1043 | uint32_t offset = shiftAmt / APINT_BITS_PER_WORD; // word offset for shift |
| 1044 | uint32_t breakWord = getNumWords() - 1 - offset; // last word affected |
| 1045 | uint32_t bitsInWord = whichBit(BitWidth); // how many bits in last word? |
| 1046 | if (bitsInWord == 0) |
| 1047 | bitsInWord = APINT_BITS_PER_WORD; |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1048 | |
| 1049 | // If we are shifting whole words, just move whole words |
| 1050 | if (wordShift == 0) { |
Reid Spencer | 46f9c94 | 2007-03-02 22:39:11 +0000 | [diff] [blame] | 1051 | // Move the words containing significant bits |
| 1052 | for (uint32_t i = 0; i <= breakWord; ++i) |
| 1053 | val[i] = pVal[i+offset]; // move whole word |
| 1054 | |
| 1055 | // Adjust the top significant word for sign bit fill, if negative |
| 1056 | if (isNegative()) |
| 1057 | if (bitsInWord < APINT_BITS_PER_WORD) |
| 1058 | val[breakWord] |= ~0ULL << bitsInWord; // set high bits |
| 1059 | } else { |
| 1060 | // Shift the low order words |
| 1061 | for (uint32_t i = 0; i < breakWord; ++i) { |
| 1062 | // This combines the shifted corresponding word with the low bits from |
| 1063 | // the next word (shifted into this word's high bits). |
| 1064 | val[i] = (pVal[i+offset] >> wordShift) | |
| 1065 | (pVal[i+offset+1] << (APINT_BITS_PER_WORD - wordShift)); |
| 1066 | } |
| 1067 | |
| 1068 | // Shift the break word. In this case there are no bits from the next word |
| 1069 | // to include in this word. |
| 1070 | val[breakWord] = pVal[breakWord+offset] >> wordShift; |
| 1071 | |
| 1072 | // Deal with sign extenstion in the break word, and possibly the word before |
| 1073 | // it. |
| 1074 | if (isNegative()) |
| 1075 | if (wordShift > bitsInWord) { |
| 1076 | if (breakWord > 0) |
| 1077 | val[breakWord-1] |= |
| 1078 | ~0ULL << (APINT_BITS_PER_WORD - (wordShift - bitsInWord)); |
| 1079 | val[breakWord] |= ~0ULL; |
| 1080 | } else |
| 1081 | val[breakWord] |= (~0ULL << (bitsInWord - wordShift)); |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
Reid Spencer | 46f9c94 | 2007-03-02 22:39:11 +0000 | [diff] [blame] | 1084 | // Remaining words are 0 or -1, just assign them. |
| 1085 | uint64_t fillValue = (isNegative() ? -1ULL : 0); |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1086 | for (uint32_t i = breakWord+1; i < getNumWords(); ++i) |
Reid Spencer | 46f9c94 | 2007-03-02 22:39:11 +0000 | [diff] [blame] | 1087 | val[i] = fillValue; |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1088 | return APInt(val, BitWidth).clearUnusedBits(); |
Zhou Sheng | 0b706b1 | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
Zhou Sheng | ff4304f | 2007-02-09 07:48:24 +0000 | [diff] [blame] | 1091 | /// Logical right-shift this APInt by shiftAmt. |
Zhou Sheng | 0b706b1 | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1092 | /// @brief Logical right-shift function. |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 1093 | APInt APInt::lshr(uint32_t shiftAmt) const { |
Reid Spencer | 24c4a8f | 2007-02-25 01:56:07 +0000 | [diff] [blame] | 1094 | if (isSingleWord()) |
| 1095 | if (shiftAmt == BitWidth) |
| 1096 | return APInt(BitWidth, 0); |
| 1097 | else |
| 1098 | return APInt(BitWidth, this->VAL >> shiftAmt); |
| 1099 | |
Reid Spencer | ba81c2b | 2007-02-26 01:19:48 +0000 | [diff] [blame] | 1100 | // If all the bits were shifted out, the result is 0. This avoids issues |
| 1101 | // with shifting by the size of the integer type, which produces undefined |
| 1102 | // results. We define these "undefined results" to always be 0. |
| 1103 | if (shiftAmt == BitWidth) |
| 1104 | return APInt(BitWidth, 0); |
| 1105 | |
| 1106 | // Create some space for the result. |
| 1107 | uint64_t * val = new uint64_t[getNumWords()]; |
| 1108 | |
| 1109 | // If we are shifting less than a word, compute the shift with a simple carry |
| 1110 | if (shiftAmt < APINT_BITS_PER_WORD) { |
| 1111 | uint64_t carry = 0; |
| 1112 | for (int i = getNumWords()-1; i >= 0; --i) { |
Reid Spencer | af8fb19 | 2007-03-01 05:39:56 +0000 | [diff] [blame] | 1113 | val[i] = (pVal[i] >> shiftAmt) | carry; |
Reid Spencer | ba81c2b | 2007-02-26 01:19:48 +0000 | [diff] [blame] | 1114 | carry = pVal[i] << (APINT_BITS_PER_WORD - shiftAmt); |
| 1115 | } |
| 1116 | return APInt(val, BitWidth).clearUnusedBits(); |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
Reid Spencer | ba81c2b | 2007-02-26 01:19:48 +0000 | [diff] [blame] | 1119 | // Compute some values needed by the remaining shift algorithms |
| 1120 | uint32_t wordShift = shiftAmt % APINT_BITS_PER_WORD; |
| 1121 | uint32_t offset = shiftAmt / APINT_BITS_PER_WORD; |
| 1122 | |
| 1123 | // If we are shifting whole words, just move whole words |
| 1124 | if (wordShift == 0) { |
| 1125 | for (uint32_t i = 0; i < getNumWords() - offset; ++i) |
| 1126 | val[i] = pVal[i+offset]; |
| 1127 | for (uint32_t i = getNumWords()-offset; i < getNumWords(); i++) |
| 1128 | val[i] = 0; |
| 1129 | return APInt(val,BitWidth).clearUnusedBits(); |
| 1130 | } |
| 1131 | |
| 1132 | // Shift the low order words |
| 1133 | uint32_t breakWord = getNumWords() - offset -1; |
| 1134 | for (uint32_t i = 0; i < breakWord; ++i) |
Reid Spencer | af8fb19 | 2007-03-01 05:39:56 +0000 | [diff] [blame] | 1135 | val[i] = (pVal[i+offset] >> wordShift) | |
| 1136 | (pVal[i+offset+1] << (APINT_BITS_PER_WORD - wordShift)); |
Reid Spencer | ba81c2b | 2007-02-26 01:19:48 +0000 | [diff] [blame] | 1137 | // Shift the break word. |
| 1138 | val[breakWord] = pVal[breakWord+offset] >> wordShift; |
| 1139 | |
| 1140 | // Remaining words are 0 |
| 1141 | for (uint32_t i = breakWord+1; i < getNumWords(); ++i) |
| 1142 | val[i] = 0; |
| 1143 | return APInt(val, BitWidth).clearUnusedBits(); |
Zhou Sheng | 0b706b1 | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
Zhou Sheng | ff4304f | 2007-02-09 07:48:24 +0000 | [diff] [blame] | 1146 | /// Left-shift this APInt by shiftAmt. |
Zhou Sheng | 0b706b1 | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1147 | /// @brief Left-shift function. |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 1148 | APInt APInt::shl(uint32_t shiftAmt) const { |
Reid Spencer | 5bce854 | 2007-02-24 20:19:37 +0000 | [diff] [blame] | 1149 | assert(shiftAmt <= BitWidth && "Invalid shift amount"); |
Reid Spencer | 8755380 | 2007-02-25 00:56:44 +0000 | [diff] [blame] | 1150 | if (isSingleWord()) { |
Reid Spencer | 5bce854 | 2007-02-24 20:19:37 +0000 | [diff] [blame] | 1151 | if (shiftAmt == BitWidth) |
Reid Spencer | 8755380 | 2007-02-25 00:56:44 +0000 | [diff] [blame] | 1152 | return APInt(BitWidth, 0); // avoid undefined shift results |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1153 | return APInt(BitWidth, VAL << shiftAmt); |
Zhou Sheng | 0b706b1 | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1154 | } |
Reid Spencer | 5bce854 | 2007-02-24 20:19:37 +0000 | [diff] [blame] | 1155 | |
Reid Spencer | 8755380 | 2007-02-25 00:56:44 +0000 | [diff] [blame] | 1156 | // If all the bits were shifted out, the result is 0. This avoids issues |
| 1157 | // with shifting by the size of the integer type, which produces undefined |
| 1158 | // results. We define these "undefined results" to always be 0. |
| 1159 | if (shiftAmt == BitWidth) |
| 1160 | return APInt(BitWidth, 0); |
| 1161 | |
| 1162 | // Create some space for the result. |
| 1163 | uint64_t * val = new uint64_t[getNumWords()]; |
| 1164 | |
| 1165 | // If we are shifting less than a word, do it the easy way |
| 1166 | if (shiftAmt < APINT_BITS_PER_WORD) { |
| 1167 | uint64_t carry = 0; |
Reid Spencer | 8755380 | 2007-02-25 00:56:44 +0000 | [diff] [blame] | 1168 | for (uint32_t i = 0; i < getNumWords(); i++) { |
| 1169 | val[i] = pVal[i] << shiftAmt | carry; |
| 1170 | carry = pVal[i] >> (APINT_BITS_PER_WORD - shiftAmt); |
| 1171 | } |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 1172 | return APInt(val, BitWidth).clearUnusedBits(); |
Reid Spencer | 5bce854 | 2007-02-24 20:19:37 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
Reid Spencer | 8755380 | 2007-02-25 00:56:44 +0000 | [diff] [blame] | 1175 | // Compute some values needed by the remaining shift algorithms |
| 1176 | uint32_t wordShift = shiftAmt % APINT_BITS_PER_WORD; |
| 1177 | uint32_t offset = shiftAmt / APINT_BITS_PER_WORD; |
| 1178 | |
| 1179 | // If we are shifting whole words, just move whole words |
| 1180 | if (wordShift == 0) { |
| 1181 | for (uint32_t i = 0; i < offset; i++) |
| 1182 | val[i] = 0; |
| 1183 | for (uint32_t i = offset; i < getNumWords(); i++) |
| 1184 | val[i] = pVal[i-offset]; |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 1185 | return APInt(val,BitWidth).clearUnusedBits(); |
Reid Spencer | 5bce854 | 2007-02-24 20:19:37 +0000 | [diff] [blame] | 1186 | } |
Reid Spencer | 8755380 | 2007-02-25 00:56:44 +0000 | [diff] [blame] | 1187 | |
| 1188 | // Copy whole words from this to Result. |
| 1189 | uint32_t i = getNumWords() - 1; |
| 1190 | for (; i > offset; --i) |
| 1191 | val[i] = pVal[i-offset] << wordShift | |
| 1192 | pVal[i-offset-1] >> (APINT_BITS_PER_WORD - wordShift); |
Reid Spencer | 438d71e | 2007-02-25 01:08:58 +0000 | [diff] [blame] | 1193 | val[offset] = pVal[0] << wordShift; |
Reid Spencer | 8755380 | 2007-02-25 00:56:44 +0000 | [diff] [blame] | 1194 | for (i = 0; i < offset; ++i) |
| 1195 | val[i] = 0; |
Reid Spencer | 5d0d05c | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 1196 | return APInt(val, BitWidth).clearUnusedBits(); |
Zhou Sheng | 0b706b1 | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
Reid Spencer | af8fb19 | 2007-03-01 05:39:56 +0000 | [diff] [blame] | 1199 | |
| 1200 | // Square Root - this method computes and returns the square root of "this". |
| 1201 | // Three mechanisms are used for computation. For small values (<= 5 bits), |
| 1202 | // a table lookup is done. This gets some performance for common cases. For |
| 1203 | // values using less than 52 bits, the value is converted to double and then |
| 1204 | // the libc sqrt function is called. The result is rounded and then converted |
| 1205 | // back to a uint64_t which is then used to construct the result. Finally, |
| 1206 | // the Babylonian method for computing square roots is used. |
| 1207 | APInt APInt::sqrt() const { |
| 1208 | |
| 1209 | // Determine the magnitude of the value. |
| 1210 | uint32_t magnitude = getActiveBits(); |
| 1211 | |
| 1212 | // Use a fast table for some small values. This also gets rid of some |
| 1213 | // rounding errors in libc sqrt for small values. |
| 1214 | if (magnitude <= 5) { |
Reid Spencer | 4e1e87f | 2007-03-01 17:47:31 +0000 | [diff] [blame] | 1215 | static const uint8_t results[32] = { |
Reid Spencer | b5ca2cd | 2007-03-01 06:23:32 +0000 | [diff] [blame] | 1216 | /* 0 */ 0, |
| 1217 | /* 1- 2 */ 1, 1, |
| 1218 | /* 3- 6 */ 2, 2, 2, 2, |
| 1219 | /* 7-12 */ 3, 3, 3, 3, 3, 3, |
| 1220 | /* 13-20 */ 4, 4, 4, 4, 4, 4, 4, 4, |
| 1221 | /* 21-30 */ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
| 1222 | /* 31 */ 6 |
| 1223 | }; |
| 1224 | return APInt(BitWidth, results[ (isSingleWord() ? VAL : pVal[0]) ]); |
Reid Spencer | af8fb19 | 2007-03-01 05:39:56 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | // If the magnitude of the value fits in less than 52 bits (the precision of |
| 1228 | // an IEEE double precision floating point value), then we can use the |
| 1229 | // libc sqrt function which will probably use a hardware sqrt computation. |
| 1230 | // This should be faster than the algorithm below. |
Jeff Cohen | ca5183d | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 1231 | if (magnitude < 52) { |
| 1232 | #ifdef _MSC_VER |
| 1233 | // Amazingly, VC++ doesn't have round(). |
| 1234 | return APInt(BitWidth, |
| 1235 | uint64_t(::sqrt(double(isSingleWord()?VAL:pVal[0]))) + 0.5); |
| 1236 | #else |
Reid Spencer | af8fb19 | 2007-03-01 05:39:56 +0000 | [diff] [blame] | 1237 | return APInt(BitWidth, |
| 1238 | uint64_t(::round(::sqrt(double(isSingleWord()?VAL:pVal[0]))))); |
Jeff Cohen | ca5183d | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 1239 | #endif |
| 1240 | } |
Reid Spencer | af8fb19 | 2007-03-01 05:39:56 +0000 | [diff] [blame] | 1241 | |
| 1242 | // Okay, all the short cuts are exhausted. We must compute it. The following |
| 1243 | // is a classical Babylonian method for computing the square root. This code |
| 1244 | // was adapted to APINt from a wikipedia article on such computations. |
| 1245 | // See http://www.wikipedia.org/ and go to the page named |
| 1246 | // Calculate_an_integer_square_root. |
| 1247 | uint32_t nbits = BitWidth, i = 4; |
| 1248 | APInt testy(BitWidth, 16); |
| 1249 | APInt x_old(BitWidth, 1); |
| 1250 | APInt x_new(BitWidth, 0); |
| 1251 | APInt two(BitWidth, 2); |
| 1252 | |
| 1253 | // Select a good starting value using binary logarithms. |
| 1254 | for (;; i += 2, testy = testy.shl(2)) |
| 1255 | if (i >= nbits || this->ule(testy)) { |
| 1256 | x_old = x_old.shl(i / 2); |
| 1257 | break; |
| 1258 | } |
| 1259 | |
| 1260 | // Use the Babylonian method to arrive at the integer square root: |
| 1261 | for (;;) { |
| 1262 | x_new = (this->udiv(x_old) + x_old).udiv(two); |
| 1263 | if (x_old.ule(x_new)) |
| 1264 | break; |
| 1265 | x_old = x_new; |
| 1266 | } |
| 1267 | |
| 1268 | // Make sure we return the closest approximation |
Reid Spencer | f09aef7 | 2007-03-02 04:21:55 +0000 | [diff] [blame] | 1269 | // NOTE: The rounding calculation below is correct. It will produce an |
| 1270 | // off-by-one discrepancy with results from pari/gp. That discrepancy has been |
| 1271 | // determined to be a rounding issue with pari/gp as it begins to use a |
| 1272 | // floating point representation after 192 bits. There are no discrepancies |
| 1273 | // between this algorithm and pari/gp for bit widths < 192 bits. |
Reid Spencer | af8fb19 | 2007-03-01 05:39:56 +0000 | [diff] [blame] | 1274 | APInt square(x_old * x_old); |
| 1275 | APInt nextSquare((x_old + 1) * (x_old +1)); |
| 1276 | if (this->ult(square)) |
| 1277 | return x_old; |
Reid Spencer | f09aef7 | 2007-03-02 04:21:55 +0000 | [diff] [blame] | 1278 | else if (this->ule(nextSquare)) { |
| 1279 | APInt midpoint((nextSquare - square).udiv(two)); |
| 1280 | APInt offset(*this - square); |
| 1281 | if (offset.ult(midpoint)) |
Reid Spencer | af8fb19 | 2007-03-01 05:39:56 +0000 | [diff] [blame] | 1282 | return x_old; |
Reid Spencer | f09aef7 | 2007-03-02 04:21:55 +0000 | [diff] [blame] | 1283 | else |
| 1284 | return x_old + 1; |
| 1285 | } else |
Reid Spencer | af8fb19 | 2007-03-01 05:39:56 +0000 | [diff] [blame] | 1286 | assert(0 && "Error in APInt::sqrt computation"); |
| 1287 | return x_old + 1; |
| 1288 | } |
| 1289 | |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1290 | /// Implementation of Knuth's Algorithm D (Division of nonnegative integers) |
| 1291 | /// from "Art of Computer Programming, Volume 2", section 4.3.1, p. 272. The |
| 1292 | /// variables here have the same names as in the algorithm. Comments explain |
| 1293 | /// the algorithm and any deviation from it. |
| 1294 | static void KnuthDiv(uint32_t *u, uint32_t *v, uint32_t *q, uint32_t* r, |
| 1295 | uint32_t m, uint32_t n) { |
| 1296 | assert(u && "Must provide dividend"); |
| 1297 | assert(v && "Must provide divisor"); |
| 1298 | assert(q && "Must provide quotient"); |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1299 | assert(u != v && u != q && v != q && "Must us different memory"); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1300 | assert(n>1 && "n must be > 1"); |
| 1301 | |
| 1302 | // Knuth uses the value b as the base of the number system. In our case b |
| 1303 | // is 2^31 so we just set it to -1u. |
| 1304 | uint64_t b = uint64_t(1) << 32; |
| 1305 | |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1306 | DEBUG(cerr << "KnuthDiv: m=" << m << " n=" << n << '\n'); |
| 1307 | DEBUG(cerr << "KnuthDiv: original:"); |
| 1308 | DEBUG(for (int i = m+n; i >=0; i--) cerr << " " << std::setbase(16) << u[i]); |
| 1309 | DEBUG(cerr << " by"); |
| 1310 | DEBUG(for (int i = n; i >0; i--) cerr << " " << std::setbase(16) << v[i-1]); |
| 1311 | DEBUG(cerr << '\n'); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1312 | // D1. [Normalize.] Set d = b / (v[n-1] + 1) and multiply all the digits of |
| 1313 | // u and v by d. Note that we have taken Knuth's advice here to use a power |
| 1314 | // of 2 value for d such that d * v[n-1] >= b/2 (b is the base). A power of |
| 1315 | // 2 allows us to shift instead of multiply and it is easy to determine the |
| 1316 | // shift amount from the leading zeros. We are basically normalizing the u |
| 1317 | // and v so that its high bits are shifted to the top of v's range without |
| 1318 | // overflow. Note that this can require an extra word in u so that u must |
| 1319 | // be of length m+n+1. |
| 1320 | uint32_t shift = CountLeadingZeros_32(v[n-1]); |
| 1321 | uint32_t v_carry = 0; |
| 1322 | uint32_t u_carry = 0; |
| 1323 | if (shift) { |
| 1324 | for (uint32_t i = 0; i < m+n; ++i) { |
| 1325 | uint32_t u_tmp = u[i] >> (32 - shift); |
| 1326 | u[i] = (u[i] << shift) | u_carry; |
| 1327 | u_carry = u_tmp; |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 1328 | } |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1329 | for (uint32_t i = 0; i < n; ++i) { |
| 1330 | uint32_t v_tmp = v[i] >> (32 - shift); |
| 1331 | v[i] = (v[i] << shift) | v_carry; |
| 1332 | v_carry = v_tmp; |
| 1333 | } |
| 1334 | } |
| 1335 | u[m+n] = u_carry; |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1336 | DEBUG(cerr << "KnuthDiv: normal:"); |
| 1337 | DEBUG(for (int i = m+n; i >=0; i--) cerr << " " << std::setbase(16) << u[i]); |
| 1338 | DEBUG(cerr << " by"); |
| 1339 | DEBUG(for (int i = n; i >0; i--) cerr << " " << std::setbase(16) << v[i-1]); |
| 1340 | DEBUG(cerr << '\n'); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1341 | |
| 1342 | // D2. [Initialize j.] Set j to m. This is the loop counter over the places. |
| 1343 | int j = m; |
| 1344 | do { |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1345 | DEBUG(cerr << "KnuthDiv: quotient digit #" << j << '\n'); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1346 | // D3. [Calculate q'.]. |
| 1347 | // Set qp = (u[j+n]*b + u[j+n-1]) / v[n-1]. (qp=qprime=q') |
| 1348 | // Set rp = (u[j+n]*b + u[j+n-1]) % v[n-1]. (rp=rprime=r') |
| 1349 | // Now test if qp == b or qp*v[n-2] > b*rp + u[j+n-2]; if so, decrease |
| 1350 | // qp by 1, inrease rp by v[n-1], and repeat this test if rp < b. The test |
| 1351 | // on v[n-2] determines at high speed most of the cases in which the trial |
| 1352 | // value qp is one too large, and it eliminates all cases where qp is two |
| 1353 | // too large. |
Reid Spencer | 9290463 | 2007-02-23 01:57:13 +0000 | [diff] [blame] | 1354 | 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] | 1355 | DEBUG(cerr << "KnuthDiv: dividend == " << dividend << '\n'); |
Reid Spencer | 9290463 | 2007-02-23 01:57:13 +0000 | [diff] [blame] | 1356 | uint64_t qp = dividend / v[n-1]; |
| 1357 | uint64_t rp = dividend % v[n-1]; |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1358 | if (qp == b || qp*v[n-2] > b*rp + u[j+n-2]) { |
| 1359 | qp--; |
| 1360 | rp += v[n-1]; |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 1361 | 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] | 1362 | qp--; |
Reid Spencer | 9290463 | 2007-02-23 01:57:13 +0000 | [diff] [blame] | 1363 | } |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1364 | DEBUG(cerr << "KnuthDiv: qp == " << qp << ", rp == " << rp << '\n'); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1365 | |
Reid Spencer | 9290463 | 2007-02-23 01:57:13 +0000 | [diff] [blame] | 1366 | // D4. [Multiply and subtract.] Replace (u[j+n]u[j+n-1]...u[j]) with |
| 1367 | // (u[j+n]u[j+n-1]..u[j]) - qp * (v[n-1]...v[1]v[0]). This computation |
| 1368 | // consists of a simple multiplication by a one-place number, combined with |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 1369 | // a subtraction. |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1370 | bool isNeg = false; |
Reid Spencer | 9290463 | 2007-02-23 01:57:13 +0000 | [diff] [blame] | 1371 | for (uint32_t i = 0; i < n; ++i) { |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 1372 | 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] | 1373 | uint64_t subtrahend = uint64_t(qp) * uint64_t(v[i]); |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 1374 | bool borrow = subtrahend > u_tmp; |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1375 | DEBUG(cerr << "KnuthDiv: u_tmp == " << u_tmp |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 1376 | << ", subtrahend == " << subtrahend |
| 1377 | << ", borrow = " << borrow << '\n'); |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1378 | |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 1379 | uint64_t result = u_tmp - subtrahend; |
| 1380 | uint32_t k = j + i; |
| 1381 | u[k++] = result & (b-1); // subtract low word |
| 1382 | u[k++] = result >> 32; // subtract high word |
| 1383 | while (borrow && k <= m+n) { // deal with borrow to the left |
| 1384 | borrow = u[k] == 0; |
| 1385 | u[k]--; |
| 1386 | k++; |
| 1387 | } |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1388 | isNeg |= borrow; |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 1389 | DEBUG(cerr << "KnuthDiv: u[j+i] == " << u[j+i] << ", u[j+i+1] == " << |
| 1390 | u[j+i+1] << '\n'); |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1391 | } |
| 1392 | DEBUG(cerr << "KnuthDiv: after subtraction:"); |
| 1393 | DEBUG(for (int i = m+n; i >=0; i--) cerr << " " << u[i]); |
| 1394 | DEBUG(cerr << '\n'); |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 1395 | // The digits (u[j+n]...u[j]) should be kept positive; if the result of |
| 1396 | // this step is actually negative, (u[j+n]...u[j]) should be left as the |
| 1397 | // 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] | 1398 | // the true value, and a "borrow" to the left should be remembered. |
| 1399 | // |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1400 | if (isNeg) { |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 1401 | bool carry = true; // true because b's complement is "complement + 1" |
| 1402 | for (uint32_t i = 0; i <= m+n; ++i) { |
| 1403 | u[i] = ~u[i] + carry; // b's complement |
| 1404 | carry = carry && u[i] == 0; |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1405 | } |
Reid Spencer | 9290463 | 2007-02-23 01:57:13 +0000 | [diff] [blame] | 1406 | } |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1407 | DEBUG(cerr << "KnuthDiv: after complement:"); |
| 1408 | DEBUG(for (int i = m+n; i >=0; i--) cerr << " " << u[i]); |
| 1409 | DEBUG(cerr << '\n'); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1410 | |
| 1411 | // D5. [Test remainder.] Set q[j] = qp. If the result of step D4 was |
| 1412 | // negative, go to step D6; otherwise go on to step D7. |
| 1413 | q[j] = qp; |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1414 | if (isNeg) { |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1415 | // D6. [Add back]. The probability that this step is necessary is very |
| 1416 | // 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] | 1417 | // this possibility. Decrease q[j] by 1 |
| 1418 | q[j]--; |
| 1419 | // and add (0v[n-1]...v[1]v[0]) to (u[j+n]u[j+n-1]...u[j+1]u[j]). |
| 1420 | // A carry will occur to the left of u[j+n], and it should be ignored |
| 1421 | // since it cancels with the borrow that occurred in D4. |
| 1422 | bool carry = false; |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1423 | for (uint32_t i = 0; i < n; i++) { |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1424 | uint32_t limit = std::min(u[j+i],v[i]); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1425 | u[j+i] += v[i] + carry; |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1426 | carry = u[j+i] < limit || (carry && u[j+i] == limit); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1427 | } |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1428 | u[j+n] += carry; |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1429 | } |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1430 | DEBUG(cerr << "KnuthDiv: after correction:"); |
| 1431 | DEBUG(for (int i = m+n; i >=0; i--) cerr <<" " << u[i]); |
| 1432 | DEBUG(cerr << "\nKnuthDiv: digit result = " << q[j] << '\n'); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1433 | |
Reid Spencer | 9290463 | 2007-02-23 01:57:13 +0000 | [diff] [blame] | 1434 | // D7. [Loop on j.] Decrease j by one. Now if j >= 0, go back to D3. |
| 1435 | } while (--j >= 0); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1436 | |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1437 | DEBUG(cerr << "KnuthDiv: quotient:"); |
| 1438 | DEBUG(for (int i = m; i >=0; i--) cerr <<" " << q[i]); |
| 1439 | DEBUG(cerr << '\n'); |
| 1440 | |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1441 | // D8. [Unnormalize]. Now q[...] is the desired quotient, and the desired |
| 1442 | // remainder may be obtained by dividing u[...] by d. If r is non-null we |
| 1443 | // compute the remainder (urem uses this). |
| 1444 | if (r) { |
| 1445 | // The value d is expressed by the "shift" value above since we avoided |
| 1446 | // multiplication by d by using a shift left. So, all we have to do is |
| 1447 | // shift right here. In order to mak |
Reid Spencer | 1050ec5 | 2007-02-24 20:38:01 +0000 | [diff] [blame] | 1448 | if (shift) { |
| 1449 | uint32_t carry = 0; |
| 1450 | DEBUG(cerr << "KnuthDiv: remainder:"); |
| 1451 | for (int i = n-1; i >= 0; i--) { |
| 1452 | r[i] = (u[i] >> shift) | carry; |
| 1453 | carry = u[i] << (32 - shift); |
| 1454 | DEBUG(cerr << " " << r[i]); |
| 1455 | } |
| 1456 | } else { |
| 1457 | for (int i = n-1; i >= 0; i--) { |
| 1458 | r[i] = u[i]; |
| 1459 | DEBUG(cerr << " " << r[i]); |
| 1460 | } |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1461 | } |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1462 | DEBUG(cerr << '\n'); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1463 | } |
Reid Spencer | 9d6c919 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1464 | DEBUG(cerr << std::setbase(10) << '\n'); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1467 | void APInt::divide(const APInt LHS, uint32_t lhsWords, |
| 1468 | const APInt &RHS, uint32_t rhsWords, |
| 1469 | APInt *Quotient, APInt *Remainder) |
| 1470 | { |
| 1471 | assert(lhsWords >= rhsWords && "Fractional result"); |
| 1472 | |
| 1473 | // First, compose the values into an array of 32-bit words instead of |
| 1474 | // 64-bit words. This is a necessity of both the "short division" algorithm |
| 1475 | // and the the Knuth "classical algorithm" which requires there to be native |
| 1476 | // operations for +, -, and * on an m bit value with an m*2 bit result. We |
| 1477 | // can't use 64-bit operands here because we don't have native results of |
| 1478 | // 128-bits. Furthremore, casting the 64-bit values to 32-bit values won't |
| 1479 | // work on large-endian machines. |
| 1480 | uint64_t mask = ~0ull >> (sizeof(uint32_t)*8); |
| 1481 | uint32_t n = rhsWords * 2; |
| 1482 | uint32_t m = (lhsWords * 2) - n; |
Reid Spencer | 24c4a8f | 2007-02-25 01:56:07 +0000 | [diff] [blame] | 1483 | |
| 1484 | // Allocate space for the temporary values we need either on the stack, if |
| 1485 | // it will fit, or on the heap if it won't. |
| 1486 | uint32_t SPACE[128]; |
| 1487 | uint32_t *U = 0; |
| 1488 | uint32_t *V = 0; |
| 1489 | uint32_t *Q = 0; |
| 1490 | uint32_t *R = 0; |
| 1491 | if ((Remainder?4:3)*n+2*m+1 <= 128) { |
| 1492 | U = &SPACE[0]; |
| 1493 | V = &SPACE[m+n+1]; |
| 1494 | Q = &SPACE[(m+n+1) + n]; |
| 1495 | if (Remainder) |
| 1496 | R = &SPACE[(m+n+1) + n + (m+n)]; |
| 1497 | } else { |
| 1498 | U = new uint32_t[m + n + 1]; |
| 1499 | V = new uint32_t[n]; |
| 1500 | Q = new uint32_t[m+n]; |
| 1501 | if (Remainder) |
| 1502 | R = new uint32_t[n]; |
| 1503 | } |
| 1504 | |
| 1505 | // Initialize the dividend |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1506 | memset(U, 0, (m+n+1)*sizeof(uint32_t)); |
| 1507 | for (unsigned i = 0; i < lhsWords; ++i) { |
Reid Spencer | 15aab8a | 2007-02-22 00:58:45 +0000 | [diff] [blame] | 1508 | uint64_t tmp = (LHS.getNumWords() == 1 ? LHS.VAL : LHS.pVal[i]); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1509 | U[i * 2] = tmp & mask; |
| 1510 | U[i * 2 + 1] = tmp >> (sizeof(uint32_t)*8); |
| 1511 | } |
| 1512 | U[m+n] = 0; // this extra word is for "spill" in the Knuth algorithm. |
| 1513 | |
Reid Spencer | 24c4a8f | 2007-02-25 01:56:07 +0000 | [diff] [blame] | 1514 | // Initialize the divisor |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1515 | memset(V, 0, (n)*sizeof(uint32_t)); |
| 1516 | for (unsigned i = 0; i < rhsWords; ++i) { |
Reid Spencer | 15aab8a | 2007-02-22 00:58:45 +0000 | [diff] [blame] | 1517 | uint64_t tmp = (RHS.getNumWords() == 1 ? RHS.VAL : RHS.pVal[i]); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1518 | V[i * 2] = tmp & mask; |
| 1519 | V[i * 2 + 1] = tmp >> (sizeof(uint32_t)*8); |
| 1520 | } |
| 1521 | |
Reid Spencer | 24c4a8f | 2007-02-25 01:56:07 +0000 | [diff] [blame] | 1522 | // initialize the quotient and remainder |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1523 | memset(Q, 0, (m+n) * sizeof(uint32_t)); |
Reid Spencer | 24c4a8f | 2007-02-25 01:56:07 +0000 | [diff] [blame] | 1524 | if (Remainder) |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1525 | memset(R, 0, n * sizeof(uint32_t)); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1526 | |
| 1527 | // Now, adjust m and n for the Knuth division. n is the number of words in |
| 1528 | // the divisor. m is the number of words by which the dividend exceeds the |
| 1529 | // divisor (i.e. m+n is the length of the dividend). These sizes must not |
| 1530 | // contain any zero words or the Knuth algorithm fails. |
| 1531 | for (unsigned i = n; i > 0 && V[i-1] == 0; i--) { |
| 1532 | n--; |
| 1533 | m++; |
| 1534 | } |
| 1535 | for (unsigned i = m+n; i > 0 && U[i-1] == 0; i--) |
| 1536 | m--; |
| 1537 | |
| 1538 | // If we're left with only a single word for the divisor, Knuth doesn't work |
| 1539 | // so we implement the short division algorithm here. This is much simpler |
| 1540 | // and faster because we are certain that we can divide a 64-bit quantity |
| 1541 | // by a 32-bit quantity at hardware speed and short division is simply a |
| 1542 | // series of such operations. This is just like doing short division but we |
| 1543 | // are using base 2^32 instead of base 10. |
| 1544 | assert(n != 0 && "Divide by zero?"); |
| 1545 | if (n == 1) { |
| 1546 | uint32_t divisor = V[0]; |
| 1547 | uint32_t remainder = 0; |
| 1548 | for (int i = m+n-1; i >= 0; i--) { |
| 1549 | uint64_t partial_dividend = uint64_t(remainder) << 32 | U[i]; |
| 1550 | if (partial_dividend == 0) { |
| 1551 | Q[i] = 0; |
| 1552 | remainder = 0; |
| 1553 | } else if (partial_dividend < divisor) { |
| 1554 | Q[i] = 0; |
| 1555 | remainder = partial_dividend; |
| 1556 | } else if (partial_dividend == divisor) { |
| 1557 | Q[i] = 1; |
| 1558 | remainder = 0; |
| 1559 | } else { |
| 1560 | Q[i] = partial_dividend / divisor; |
| 1561 | remainder = partial_dividend - (Q[i] * divisor); |
| 1562 | } |
| 1563 | } |
| 1564 | if (R) |
| 1565 | R[0] = remainder; |
| 1566 | } else { |
| 1567 | // Now we're ready to invoke the Knuth classical divide algorithm. In this |
| 1568 | // case n > 1. |
| 1569 | KnuthDiv(U, V, Q, R, m, n); |
| 1570 | } |
| 1571 | |
| 1572 | // If the caller wants the quotient |
| 1573 | if (Quotient) { |
| 1574 | // Set up the Quotient value's memory. |
| 1575 | if (Quotient->BitWidth != LHS.BitWidth) { |
| 1576 | if (Quotient->isSingleWord()) |
| 1577 | Quotient->VAL = 0; |
| 1578 | else |
Reid Spencer | 9ac4411 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 1579 | delete [] Quotient->pVal; |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1580 | Quotient->BitWidth = LHS.BitWidth; |
| 1581 | if (!Quotient->isSingleWord()) |
Reid Spencer | e0cdd33 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 1582 | Quotient->pVal = getClearedMemory(Quotient->getNumWords()); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1583 | } else |
| 1584 | Quotient->clear(); |
| 1585 | |
| 1586 | // The quotient is in Q. Reconstitute the quotient into Quotient's low |
| 1587 | // order words. |
| 1588 | if (lhsWords == 1) { |
| 1589 | uint64_t tmp = |
| 1590 | uint64_t(Q[0]) | (uint64_t(Q[1]) << (APINT_BITS_PER_WORD / 2)); |
| 1591 | if (Quotient->isSingleWord()) |
| 1592 | Quotient->VAL = tmp; |
| 1593 | else |
| 1594 | Quotient->pVal[0] = tmp; |
| 1595 | } else { |
| 1596 | assert(!Quotient->isSingleWord() && "Quotient APInt not large enough"); |
| 1597 | for (unsigned i = 0; i < lhsWords; ++i) |
| 1598 | Quotient->pVal[i] = |
| 1599 | uint64_t(Q[i*2]) | (uint64_t(Q[i*2+1]) << (APINT_BITS_PER_WORD / 2)); |
| 1600 | } |
| 1601 | } |
| 1602 | |
| 1603 | // If the caller wants the remainder |
| 1604 | if (Remainder) { |
| 1605 | // Set up the Remainder value's memory. |
| 1606 | if (Remainder->BitWidth != RHS.BitWidth) { |
| 1607 | if (Remainder->isSingleWord()) |
| 1608 | Remainder->VAL = 0; |
| 1609 | else |
Reid Spencer | 9ac4411 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 1610 | delete [] Remainder->pVal; |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1611 | Remainder->BitWidth = RHS.BitWidth; |
| 1612 | if (!Remainder->isSingleWord()) |
Reid Spencer | e0cdd33 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 1613 | Remainder->pVal = getClearedMemory(Remainder->getNumWords()); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1614 | } else |
| 1615 | Remainder->clear(); |
| 1616 | |
| 1617 | // The remainder is in R. Reconstitute the remainder into Remainder's low |
| 1618 | // order words. |
| 1619 | if (rhsWords == 1) { |
| 1620 | uint64_t tmp = |
| 1621 | uint64_t(R[0]) | (uint64_t(R[1]) << (APINT_BITS_PER_WORD / 2)); |
| 1622 | if (Remainder->isSingleWord()) |
| 1623 | Remainder->VAL = tmp; |
| 1624 | else |
| 1625 | Remainder->pVal[0] = tmp; |
| 1626 | } else { |
| 1627 | assert(!Remainder->isSingleWord() && "Remainder APInt not large enough"); |
| 1628 | for (unsigned i = 0; i < rhsWords; ++i) |
| 1629 | Remainder->pVal[i] = |
| 1630 | uint64_t(R[i*2]) | (uint64_t(R[i*2+1]) << (APINT_BITS_PER_WORD / 2)); |
| 1631 | } |
| 1632 | } |
| 1633 | |
| 1634 | // Clean up the memory we allocated. |
Reid Spencer | 24c4a8f | 2007-02-25 01:56:07 +0000 | [diff] [blame] | 1635 | if (U != &SPACE[0]) { |
| 1636 | delete [] U; |
| 1637 | delete [] V; |
| 1638 | delete [] Q; |
| 1639 | delete [] R; |
| 1640 | } |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 1643 | APInt APInt::udiv(const APInt& RHS) const { |
Reid Spencer | cd6f2bf | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 1644 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Reid Spencer | 71bd08f | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1645 | |
| 1646 | // First, deal with the easy case |
| 1647 | if (isSingleWord()) { |
| 1648 | assert(RHS.VAL != 0 && "Divide by zero?"); |
| 1649 | return APInt(BitWidth, VAL / RHS.VAL); |
Zhou Sheng | 0b706b1 | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1650 | } |
Reid Spencer | 71bd08f | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1651 | |
Reid Spencer | 71bd08f | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1652 | // 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] | 1653 | uint32_t rhsBits = RHS.getActiveBits(); |
| 1654 | uint32_t rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1); |
Reid Spencer | 71bd08f | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1655 | assert(rhsWords && "Divided by zero???"); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1656 | uint32_t lhsBits = this->getActiveBits(); |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 1657 | uint32_t lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1); |
Reid Spencer | 71bd08f | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1658 | |
| 1659 | // Deal with some degenerate cases |
| 1660 | if (!lhsWords) |
Reid Spencer | e0cdd33 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 1661 | // 0 / X ===> 0 |
| 1662 | return APInt(BitWidth, 0); |
| 1663 | else if (lhsWords < rhsWords || this->ult(RHS)) { |
| 1664 | // X / Y ===> 0, iff X < Y |
| 1665 | return APInt(BitWidth, 0); |
| 1666 | } else if (*this == RHS) { |
| 1667 | // X / X ===> 1 |
| 1668 | return APInt(BitWidth, 1); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1669 | } else if (lhsWords == 1 && rhsWords == 1) { |
Reid Spencer | 71bd08f | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1670 | // All high words are zero, just use native divide |
Reid Spencer | e0cdd33 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 1671 | return APInt(BitWidth, this->pVal[0] / RHS.pVal[0]); |
Reid Spencer | 71bd08f | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1672 | } |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1673 | |
| 1674 | // We have to compute it the hard way. Invoke the Knuth divide algorithm. |
| 1675 | APInt Quotient(1,0); // to hold result. |
| 1676 | divide(*this, lhsWords, RHS, rhsWords, &Quotient, 0); |
| 1677 | return Quotient; |
Zhou Sheng | 0b706b1 | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1678 | } |
| 1679 | |
Reid Spencer | e81d2da | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 1680 | APInt APInt::urem(const APInt& RHS) const { |
Reid Spencer | cd6f2bf | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 1681 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Reid Spencer | 71bd08f | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1682 | if (isSingleWord()) { |
| 1683 | assert(RHS.VAL != 0 && "Remainder by zero?"); |
| 1684 | return APInt(BitWidth, VAL % RHS.VAL); |
Zhou Sheng | 0b706b1 | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1685 | } |
Reid Spencer | 71bd08f | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1686 | |
Reid Spencer | e0cdd33 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 1687 | // Get some facts about the LHS |
| 1688 | uint32_t lhsBits = getActiveBits(); |
| 1689 | uint32_t lhsWords = !lhsBits ? 0 : (whichWord(lhsBits - 1) + 1); |
Reid Spencer | 71bd08f | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1690 | |
| 1691 | // Get some facts about the RHS |
Reid Spencer | af0e956 | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 1692 | uint32_t rhsBits = RHS.getActiveBits(); |
| 1693 | uint32_t rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1); |
Reid Spencer | 71bd08f | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1694 | assert(rhsWords && "Performing remainder operation by zero ???"); |
| 1695 | |
Reid Spencer | 71bd08f | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1696 | // Check the degenerate cases |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1697 | if (lhsWords == 0) { |
Reid Spencer | e0cdd33 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 1698 | // 0 % Y ===> 0 |
| 1699 | return APInt(BitWidth, 0); |
| 1700 | } else if (lhsWords < rhsWords || this->ult(RHS)) { |
| 1701 | // X % Y ===> X, iff X < Y |
| 1702 | return *this; |
| 1703 | } else if (*this == RHS) { |
Reid Spencer | 71bd08f | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1704 | // X % X == 0; |
Reid Spencer | e0cdd33 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 1705 | return APInt(BitWidth, 0); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1706 | } else if (lhsWords == 1) { |
Reid Spencer | 71bd08f | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1707 | // All high words are zero, just use native remainder |
Reid Spencer | e0cdd33 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 1708 | return APInt(BitWidth, pVal[0] % RHS.pVal[0]); |
Reid Spencer | 71bd08f | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1709 | } |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1710 | |
| 1711 | // We have to compute it the hard way. Invoke the Knute divide algorithm. |
| 1712 | APInt Remainder(1,0); |
| 1713 | divide(*this, lhsWords, RHS, rhsWords, 0, &Remainder); |
| 1714 | return Remainder; |
Zhou Sheng | 0b706b1 | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1715 | } |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 1716 | |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 1717 | void APInt::fromString(uint32_t numbits, const char *str, uint32_t slen, |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 1718 | uint8_t radix) { |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 1719 | // Check our assumptions here |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 1720 | assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) && |
| 1721 | "Radix should be 2, 8, 10, or 16!"); |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 1722 | assert(str && "String is null?"); |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1723 | bool isNeg = str[0] == '-'; |
| 1724 | if (isNeg) |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 1725 | str++, slen--; |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 1726 | assert(slen <= numbits || radix != 2 && "Insufficient bit width"); |
| 1727 | assert(slen*3 <= numbits || radix != 8 && "Insufficient bit width"); |
| 1728 | assert(slen*4 <= numbits || radix != 16 && "Insufficient bit width"); |
Reid Spencer | 2cd43e4 | 2007-04-11 13:00:04 +0000 | [diff] [blame] | 1729 | assert((slen*64)/22 <= numbits || radix != 10 && "Insufficient bit width"); |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 1730 | |
| 1731 | // Allocate memory |
| 1732 | if (!isSingleWord()) |
| 1733 | pVal = getClearedMemory(getNumWords()); |
| 1734 | |
| 1735 | // Figure out if we can shift instead of multiply |
| 1736 | uint32_t shift = (radix == 16 ? 4 : radix == 8 ? 3 : radix == 2 ? 1 : 0); |
| 1737 | |
| 1738 | // Set up an APInt for the digit to add outside the loop so we don't |
| 1739 | // constantly construct/destruct it. |
| 1740 | APInt apdigit(getBitWidth(), 0); |
| 1741 | APInt apradix(getBitWidth(), radix); |
| 1742 | |
| 1743 | // Enter digit traversal loop |
| 1744 | for (unsigned i = 0; i < slen; i++) { |
| 1745 | // Get a digit |
| 1746 | uint32_t digit = 0; |
| 1747 | char cdigit = str[i]; |
| 1748 | if (isdigit(cdigit)) |
| 1749 | digit = cdigit - '0'; |
| 1750 | else if (isxdigit(cdigit)) |
| 1751 | if (cdigit >= 'a') |
| 1752 | digit = cdigit - 'a' + 10; |
| 1753 | else if (cdigit >= 'A') |
| 1754 | digit = cdigit - 'A' + 10; |
| 1755 | else |
| 1756 | assert(0 && "huh?"); |
| 1757 | else |
| 1758 | assert(0 && "Invalid character in digit string"); |
| 1759 | |
| 1760 | // Shift or multiple the value by the radix |
| 1761 | if (shift) |
| 1762 | this->shl(shift); |
| 1763 | else |
| 1764 | *this *= apradix; |
| 1765 | |
| 1766 | // Add in the digit we just interpreted |
Reid Spencer | 5bce854 | 2007-02-24 20:19:37 +0000 | [diff] [blame] | 1767 | if (apdigit.isSingleWord()) |
| 1768 | apdigit.VAL = digit; |
| 1769 | else |
| 1770 | apdigit.pVal[0] = digit; |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 1771 | *this += apdigit; |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 1772 | } |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 1773 | // If its negative, put it in two's complement form |
Reid Spencer | 47fbe9e | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1774 | if (isNeg) { |
| 1775 | (*this)--; |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 1776 | this->flip(); |
Reid Spencer | 9eec241 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 1777 | } |
Reid Spencer | 5e0a851 | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 1778 | } |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1779 | |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1780 | std::string APInt::toString(uint8_t radix, bool wantSigned) const { |
| 1781 | assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) && |
| 1782 | "Radix should be 2, 8, 10, or 16!"); |
| 1783 | static const char *digits[] = { |
| 1784 | "0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F" |
| 1785 | }; |
| 1786 | std::string result; |
| 1787 | uint32_t bits_used = getActiveBits(); |
| 1788 | if (isSingleWord()) { |
| 1789 | char buf[65]; |
| 1790 | const char *format = (radix == 10 ? (wantSigned ? "%lld" : "%llu") : |
| 1791 | (radix == 16 ? "%llX" : (radix == 8 ? "%llo" : 0))); |
| 1792 | if (format) { |
| 1793 | if (wantSigned) { |
| 1794 | int64_t sextVal = (int64_t(VAL) << (APINT_BITS_PER_WORD-BitWidth)) >> |
| 1795 | (APINT_BITS_PER_WORD-BitWidth); |
| 1796 | sprintf(buf, format, sextVal); |
| 1797 | } else |
| 1798 | sprintf(buf, format, VAL); |
| 1799 | } else { |
| 1800 | memset(buf, 0, 65); |
| 1801 | uint64_t v = VAL; |
| 1802 | while (bits_used) { |
| 1803 | uint32_t bit = v & 1; |
| 1804 | bits_used--; |
| 1805 | buf[bits_used] = digits[bit][0]; |
| 1806 | v >>=1; |
| 1807 | } |
| 1808 | } |
| 1809 | result = buf; |
| 1810 | return result; |
| 1811 | } |
| 1812 | |
| 1813 | if (radix != 10) { |
| 1814 | uint64_t mask = radix - 1; |
| 1815 | uint32_t shift = (radix == 16 ? 4 : radix == 8 ? 3 : 1); |
| 1816 | uint32_t nibbles = APINT_BITS_PER_WORD / shift; |
| 1817 | for (uint32_t i = 0; i < getNumWords(); ++i) { |
| 1818 | uint64_t value = pVal[i]; |
| 1819 | for (uint32_t j = 0; j < nibbles; ++j) { |
| 1820 | result.insert(0, digits[ value & mask ]); |
| 1821 | value >>= shift; |
| 1822 | } |
| 1823 | } |
| 1824 | return result; |
| 1825 | } |
| 1826 | |
| 1827 | APInt tmp(*this); |
| 1828 | APInt divisor(4, radix); |
| 1829 | APInt zero(tmp.getBitWidth(), 0); |
| 1830 | size_t insert_at = 0; |
| 1831 | if (wantSigned && tmp[BitWidth-1]) { |
| 1832 | // They want to print the signed version and it is a negative value |
| 1833 | // Flip the bits and add one to turn it into the equivalent positive |
| 1834 | // value and put a '-' in the result. |
| 1835 | tmp.flip(); |
| 1836 | tmp++; |
| 1837 | result = "-"; |
| 1838 | insert_at = 1; |
| 1839 | } |
Reid Spencer | e549c49 | 2007-02-21 00:29:48 +0000 | [diff] [blame] | 1840 | if (tmp == APInt(tmp.getBitWidth(), 0)) |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1841 | result = "0"; |
| 1842 | else while (tmp.ne(zero)) { |
| 1843 | APInt APdigit(1,0); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1844 | APInt tmp2(tmp.getBitWidth(), 0); |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 1845 | divide(tmp, tmp.getNumWords(), divisor, divisor.getNumWords(), &tmp2, |
| 1846 | &APdigit); |
Reid Spencer | 794f472 | 2007-02-26 21:02:27 +0000 | [diff] [blame] | 1847 | uint32_t digit = APdigit.getZExtValue(); |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 1848 | assert(digit < radix && "divide failed"); |
| 1849 | result.insert(insert_at,digits[digit]); |
Reid Spencer | 9c0696f | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1850 | tmp = tmp2; |
| 1851 | } |
| 1852 | |
| 1853 | return result; |
| 1854 | } |
| 1855 | |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 1856 | #ifndef NDEBUG |
| 1857 | void APInt::dump() const |
| 1858 | { |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 1859 | cerr << "APInt(" << BitWidth << ")=" << std::setbase(16); |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 1860 | if (isSingleWord()) |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 1861 | cerr << VAL; |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 1862 | else for (unsigned i = getNumWords(); i > 0; i--) { |
Reid Spencer | 610fad8 | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 1863 | cerr << pVal[i-1] << " "; |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 1864 | } |
Reid Spencer | 681dcd1 | 2007-02-27 21:59:26 +0000 | [diff] [blame] | 1865 | cerr << " U(" << this->toString(10) << ") S(" << this->toStringSigned(10) |
| 1866 | << ")\n" << std::setbase(10); |
Reid Spencer | 385f754 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 1867 | } |
| 1868 | #endif |