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