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