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