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