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