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