Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 1 | //===-- APInt.cpp - Implement APInt class ---------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 10 | // This file implements a class to represent arbitrary precision integer |
| 11 | // constant values and provide a variety of arithmetic operations on them. |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/ADT/APInt.h" |
Mehdi Amini | 47b292d | 2016-04-16 07:51:28 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/ArrayRef.h" |
Ted Kremenek | 5c75d54 | 2008-01-19 04:23:33 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/FoldingSet.h" |
Chandler Carruth | 71bd7d1 | 2012-03-04 12:02:57 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/Hashing.h" |
Chris Lattner | 17f7165 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallString.h" |
Chandler Carruth | 71bd7d1 | 2012-03-04 12:02:57 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringRef.h" |
Reid Spencer | a5e0d20 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Debug.h" |
Torok Edwin | 56d0659 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ErrorHandling.h" |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 0c19df4 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 17f7165 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 25 | #include <cmath> |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 26 | #include <cstdlib> |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 27 | #include <cstring> |
| 28 | #include <limits> |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
Chandler Carruth | 6464826 | 2014-04-22 03:07:47 +0000 | [diff] [blame] | 31 | #define DEBUG_TYPE "apint" |
| 32 | |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 33 | /// A utility function for allocating memory, checking for allocation failures, |
| 34 | /// and ensuring the contents are zeroed. |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 35 | inline static uint64_t* getClearedMemory(unsigned numWords) { |
Reid Spencer | a856b6e | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 36 | uint64_t * result = new uint64_t[numWords]; |
| 37 | assert(result && "APInt memory allocation fails!"); |
| 38 | memset(result, 0, numWords * sizeof(uint64_t)); |
| 39 | return result; |
Zhou Sheng | 94b623a | 2007-02-06 06:04:53 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 42 | /// A utility function for allocating memory and checking for allocation |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 43 | /// failure. The content is not zeroed. |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 44 | inline static uint64_t* getMemory(unsigned numWords) { |
Reid Spencer | a856b6e | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 45 | uint64_t * result = new uint64_t[numWords]; |
| 46 | assert(result && "APInt memory allocation fails!"); |
| 47 | return result; |
| 48 | } |
| 49 | |
Erick Tryzelaar | dadb1571 | 2009-08-21 03:15:28 +0000 | [diff] [blame] | 50 | /// A utility function that converts a character to a digit. |
| 51 | inline static unsigned getDigit(char cdigit, uint8_t radix) { |
Erick Tryzelaar | 6096409 | 2009-08-21 06:48:37 +0000 | [diff] [blame] | 52 | unsigned r; |
| 53 | |
Douglas Gregor | 663c068 | 2011-09-14 15:54:46 +0000 | [diff] [blame] | 54 | if (radix == 16 || radix == 36) { |
Erick Tryzelaar | 6096409 | 2009-08-21 06:48:37 +0000 | [diff] [blame] | 55 | r = cdigit - '0'; |
| 56 | if (r <= 9) |
| 57 | return r; |
| 58 | |
| 59 | r = cdigit - 'A'; |
Douglas Gregor | c98ac85 | 2011-09-20 18:33:29 +0000 | [diff] [blame] | 60 | if (r <= radix - 11U) |
Erick Tryzelaar | 6096409 | 2009-08-21 06:48:37 +0000 | [diff] [blame] | 61 | return r + 10; |
| 62 | |
| 63 | r = cdigit - 'a'; |
Douglas Gregor | c98ac85 | 2011-09-20 18:33:29 +0000 | [diff] [blame] | 64 | if (r <= radix - 11U) |
Erick Tryzelaar | 6096409 | 2009-08-21 06:48:37 +0000 | [diff] [blame] | 65 | return r + 10; |
Douglas Gregor | e4e20f4 | 2011-09-20 18:11:52 +0000 | [diff] [blame] | 66 | |
| 67 | radix = 10; |
Erick Tryzelaar | dadb1571 | 2009-08-21 03:15:28 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Erick Tryzelaar | 6096409 | 2009-08-21 06:48:37 +0000 | [diff] [blame] | 70 | r = cdigit - '0'; |
| 71 | if (r < radix) |
| 72 | return r; |
| 73 | |
| 74 | return -1U; |
Erick Tryzelaar | dadb1571 | 2009-08-21 03:15:28 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 78 | void APInt::initSlowCase(unsigned numBits, uint64_t val, bool isSigned) { |
Chris Lattner | 1ac3e25 | 2008-08-20 17:02:31 +0000 | [diff] [blame] | 79 | pVal = getClearedMemory(getNumWords()); |
| 80 | pVal[0] = val; |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 81 | if (isSigned && int64_t(val) < 0) |
Chris Lattner | 1ac3e25 | 2008-08-20 17:02:31 +0000 | [diff] [blame] | 82 | for (unsigned i = 1; i < getNumWords(); ++i) |
| 83 | pVal[i] = -1ULL; |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Chris Lattner | d57b760 | 2008-10-11 22:07:19 +0000 | [diff] [blame] | 86 | void APInt::initSlowCase(const APInt& that) { |
| 87 | pVal = getMemory(getNumWords()); |
| 88 | memcpy(pVal, that.pVal, getNumWords() * APINT_WORD_SIZE); |
| 89 | } |
| 90 | |
Jeffrey Yasskin | 7a16288 | 2011-07-18 21:45:40 +0000 | [diff] [blame] | 91 | void APInt::initFromArray(ArrayRef<uint64_t> bigVal) { |
Erick Tryzelaar | 1264bcb | 2009-08-21 03:15:14 +0000 | [diff] [blame] | 92 | assert(BitWidth && "Bitwidth too small"); |
Jeffrey Yasskin | 7a16288 | 2011-07-18 21:45:40 +0000 | [diff] [blame] | 93 | assert(bigVal.data() && "Null pointer detected!"); |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 94 | if (isSingleWord()) |
Reid Spencer | df6cf5a | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 95 | VAL = bigVal[0]; |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 96 | else { |
Reid Spencer | df6cf5a | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 97 | // Get memory, cleared to 0 |
| 98 | pVal = getClearedMemory(getNumWords()); |
| 99 | // Calculate the number of words to copy |
Jeffrey Yasskin | 7a16288 | 2011-07-18 21:45:40 +0000 | [diff] [blame] | 100 | unsigned words = std::min<unsigned>(bigVal.size(), getNumWords()); |
Reid Spencer | df6cf5a | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 101 | // Copy the words from bigVal to pVal |
Jeffrey Yasskin | 7a16288 | 2011-07-18 21:45:40 +0000 | [diff] [blame] | 102 | memcpy(pVal, bigVal.data(), words * APINT_WORD_SIZE); |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 103 | } |
Reid Spencer | df6cf5a | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 104 | // Make sure unused high bits are cleared |
| 105 | clearUnusedBits(); |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Jeffrey Yasskin | 7a16288 | 2011-07-18 21:45:40 +0000 | [diff] [blame] | 108 | APInt::APInt(unsigned numBits, ArrayRef<uint64_t> bigVal) |
| 109 | : BitWidth(numBits), VAL(0) { |
| 110 | initFromArray(bigVal); |
| 111 | } |
| 112 | |
| 113 | APInt::APInt(unsigned numBits, unsigned numWords, const uint64_t bigVal[]) |
| 114 | : BitWidth(numBits), VAL(0) { |
| 115 | initFromArray(makeArrayRef(bigVal, numWords)); |
| 116 | } |
| 117 | |
Benjamin Kramer | 92d8998 | 2010-07-14 22:38:02 +0000 | [diff] [blame] | 118 | APInt::APInt(unsigned numbits, StringRef Str, uint8_t radix) |
Reid Spencer | 1ba8335 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 119 | : BitWidth(numbits), VAL(0) { |
Erick Tryzelaar | 1264bcb | 2009-08-21 03:15:14 +0000 | [diff] [blame] | 120 | assert(BitWidth && "Bitwidth too small"); |
Daniel Dunbar | 3a1efd11 | 2009-08-13 02:33:34 +0000 | [diff] [blame] | 121 | fromString(numbits, Str, radix); |
Zhou Sheng | 3e8022d | 2007-02-07 06:14:53 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Chris Lattner | 1ac3e25 | 2008-08-20 17:02:31 +0000 | [diff] [blame] | 124 | APInt& APInt::AssignSlowCase(const APInt& RHS) { |
Reid Spencer | 7c16cd2 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 125 | // Don't do anything for X = X |
| 126 | if (this == &RHS) |
| 127 | return *this; |
| 128 | |
Reid Spencer | 7c16cd2 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 129 | if (BitWidth == RHS.getBitWidth()) { |
Chris Lattner | 1ac3e25 | 2008-08-20 17:02:31 +0000 | [diff] [blame] | 130 | // assume same bit-width single-word case is already handled |
| 131 | assert(!isSingleWord()); |
| 132 | memcpy(pVal, RHS.pVal, getNumWords() * APINT_WORD_SIZE); |
Reid Spencer | 7c16cd2 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 133 | return *this; |
| 134 | } |
| 135 | |
Chris Lattner | 1ac3e25 | 2008-08-20 17:02:31 +0000 | [diff] [blame] | 136 | if (isSingleWord()) { |
| 137 | // assume case where both are single words is already handled |
| 138 | assert(!RHS.isSingleWord()); |
| 139 | VAL = 0; |
| 140 | pVal = getMemory(RHS.getNumWords()); |
| 141 | memcpy(pVal, RHS.pVal, RHS.getNumWords() * APINT_WORD_SIZE); |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 142 | } else if (getNumWords() == RHS.getNumWords()) |
Reid Spencer | 7c16cd2 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 143 | memcpy(pVal, RHS.pVal, RHS.getNumWords() * APINT_WORD_SIZE); |
| 144 | else if (RHS.isSingleWord()) { |
| 145 | delete [] pVal; |
Reid Spencer | a856b6e | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 146 | VAL = RHS.VAL; |
Reid Spencer | 7c16cd2 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 147 | } else { |
| 148 | delete [] pVal; |
| 149 | pVal = getMemory(RHS.getNumWords()); |
| 150 | memcpy(pVal, RHS.pVal, RHS.getNumWords() * APINT_WORD_SIZE); |
| 151 | } |
| 152 | BitWidth = RHS.BitWidth; |
| 153 | return clearUnusedBits(); |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 156 | APInt& APInt::operator=(uint64_t RHS) { |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 157 | if (isSingleWord()) |
Reid Spencer | 1d07212 | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 158 | VAL = RHS; |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 159 | else { |
| 160 | pVal[0] = RHS; |
Reid Spencer | be4ddf6 | 2007-02-18 20:09:41 +0000 | [diff] [blame] | 161 | memset(pVal+1, 0, (getNumWords() - 1) * APINT_WORD_SIZE); |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 162 | } |
Reid Spencer | 7c16cd2 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 163 | return clearUnusedBits(); |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Pawel Bylica | 6eeeac7 | 2015-04-06 13:31:39 +0000 | [diff] [blame] | 166 | /// This method 'profiles' an APInt for use with FoldingSet. |
Ted Kremenek | 5c75d54 | 2008-01-19 04:23:33 +0000 | [diff] [blame] | 167 | void APInt::Profile(FoldingSetNodeID& ID) const { |
Ted Kremenek | 901540f | 2008-02-19 20:50:41 +0000 | [diff] [blame] | 168 | ID.AddInteger(BitWidth); |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 169 | |
Ted Kremenek | 5c75d54 | 2008-01-19 04:23:33 +0000 | [diff] [blame] | 170 | if (isSingleWord()) { |
| 171 | ID.AddInteger(VAL); |
| 172 | return; |
| 173 | } |
| 174 | |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 175 | unsigned NumWords = getNumWords(); |
Ted Kremenek | 5c75d54 | 2008-01-19 04:23:33 +0000 | [diff] [blame] | 176 | for (unsigned i = 0; i < NumWords; ++i) |
| 177 | ID.AddInteger(pVal[i]); |
| 178 | } |
| 179 | |
Pawel Bylica | 6eeeac7 | 2015-04-06 13:31:39 +0000 | [diff] [blame] | 180 | /// This function adds a single "digit" integer, y, to the multiple |
Reid Spencer | a856b6e | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 181 | /// "digit" integer array, x[]. x[] is modified to reflect the addition and |
| 182 | /// 1 is returned if there is a carry out, otherwise 0 is returned. |
Reid Spencer | 100502d | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 183 | /// @returns the carry of the addition. |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 184 | static bool add_1(uint64_t dest[], uint64_t x[], unsigned len, uint64_t y) { |
| 185 | for (unsigned i = 0; i < len; ++i) { |
Reid Spencer | ee0a685 | 2007-02-18 06:39:42 +0000 | [diff] [blame] | 186 | dest[i] = y + x[i]; |
| 187 | if (dest[i] < y) |
Reid Spencer | df6cf5a | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 188 | y = 1; // Carry one to next digit. |
Reid Spencer | ee0a685 | 2007-02-18 06:39:42 +0000 | [diff] [blame] | 189 | else { |
Reid Spencer | df6cf5a | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 190 | y = 0; // No need to carry so exit early |
Reid Spencer | ee0a685 | 2007-02-18 06:39:42 +0000 | [diff] [blame] | 191 | break; |
| 192 | } |
Reid Spencer | 100502d | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 193 | } |
Reid Spencer | ee0a685 | 2007-02-18 06:39:42 +0000 | [diff] [blame] | 194 | return y; |
Reid Spencer | 100502d | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 197 | /// @brief Prefix increment operator. Increments the APInt by one. |
| 198 | APInt& APInt::operator++() { |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 199 | if (isSingleWord()) |
Reid Spencer | 1d07212 | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 200 | ++VAL; |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 201 | else |
Zhou Sheng | 3e8022d | 2007-02-07 06:14:53 +0000 | [diff] [blame] | 202 | add_1(pVal, pVal, getNumWords(), 1); |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 203 | return clearUnusedBits(); |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Pawel Bylica | 6eeeac7 | 2015-04-06 13:31:39 +0000 | [diff] [blame] | 206 | /// This function subtracts a single "digit" (64-bit word), y, from |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 207 | /// the multi-digit integer array, x[], propagating the borrowed 1 value until |
Reid Spencer | a856b6e | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 208 | /// no further borrowing is neeeded or it runs out of "digits" in x. The result |
| 209 | /// is 1 if "borrowing" exhausted the digits in x, or 0 if x was not exhausted. |
| 210 | /// In other words, if y > x then this function returns 1, otherwise 0. |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 211 | /// @returns the borrow out of the subtraction |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 212 | static bool sub_1(uint64_t x[], unsigned len, uint64_t y) { |
| 213 | for (unsigned i = 0; i < len; ++i) { |
Reid Spencer | 100502d | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 214 | uint64_t X = x[i]; |
Reid Spencer | ee0a685 | 2007-02-18 06:39:42 +0000 | [diff] [blame] | 215 | x[i] -= y; |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 216 | if (y > X) |
Reid Spencer | a856b6e | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 217 | y = 1; // We have to "borrow 1" from next "digit" |
Reid Spencer | 100502d | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 218 | else { |
Reid Spencer | a856b6e | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 219 | y = 0; // No need to borrow |
| 220 | break; // Remaining digits are unchanged so exit early |
Reid Spencer | 100502d | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 221 | } |
| 222 | } |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 223 | return bool(y); |
Reid Spencer | 100502d | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 226 | /// @brief Prefix decrement operator. Decrements the APInt by one. |
| 227 | APInt& APInt::operator--() { |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 228 | if (isSingleWord()) |
Reid Spencer | a856b6e | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 229 | --VAL; |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 230 | else |
Zhou Sheng | 3e8022d | 2007-02-07 06:14:53 +0000 | [diff] [blame] | 231 | sub_1(pVal, getNumWords(), 1); |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 232 | return clearUnusedBits(); |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Pawel Bylica | 6eeeac7 | 2015-04-06 13:31:39 +0000 | [diff] [blame] | 235 | /// This function adds the integer array x to the integer array Y and |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 236 | /// places the result in dest. |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 237 | /// @returns the carry out from the addition |
| 238 | /// @brief General addition of 64-bit integer arrays |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 239 | static bool add(uint64_t *dest, const uint64_t *x, const uint64_t *y, |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 240 | unsigned len) { |
Reid Spencer | a5e0d20 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 241 | bool carry = false; |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 242 | for (unsigned i = 0; i< len; ++i) { |
Reid Spencer | cb292e4 | 2007-02-23 01:57:13 +0000 | [diff] [blame] | 243 | uint64_t limit = std::min(x[i],y[i]); // must come first in case dest == x |
Reid Spencer | 7a6a8d5 | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 244 | dest[i] = x[i] + y[i] + carry; |
Reid Spencer | db2abec | 2007-02-21 05:44:56 +0000 | [diff] [blame] | 245 | carry = dest[i] < limit || (carry && dest[i] == limit); |
Reid Spencer | 100502d | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 246 | } |
| 247 | return carry; |
| 248 | } |
| 249 | |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 250 | /// Adds the RHS APint to this APInt. |
| 251 | /// @returns this, after addition of RHS. |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 252 | /// @brief Addition assignment operator. |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 253 | APInt& APInt::operator+=(const APInt& RHS) { |
Reid Spencer | a32372d1 | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 254 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 255 | if (isSingleWord()) |
Reid Spencer | 7a6a8d5 | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 256 | VAL += RHS.VAL; |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 257 | else { |
Reid Spencer | 7a6a8d5 | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 258 | add(pVal, pVal, RHS.pVal, getNumWords()); |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 259 | } |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 260 | return clearUnusedBits(); |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 263 | /// Subtracts the integer array y from the integer array x |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 264 | /// @returns returns the borrow out. |
| 265 | /// @brief Generalized subtraction of 64-bit integer arrays. |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 266 | static bool sub(uint64_t *dest, const uint64_t *x, const uint64_t *y, |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 267 | unsigned len) { |
Reid Spencer | 1ba8335 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 268 | bool borrow = false; |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 269 | for (unsigned i = 0; i < len; ++i) { |
Reid Spencer | 1ba8335 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 270 | uint64_t x_tmp = borrow ? x[i] - 1 : x[i]; |
| 271 | borrow = y[i] > x_tmp || (borrow && x[i] == 0); |
| 272 | dest[i] = x_tmp - y[i]; |
Reid Spencer | 100502d | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 273 | } |
Reid Spencer | 7a6a8d5 | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 274 | return borrow; |
Reid Spencer | 100502d | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 277 | /// Subtracts the RHS APInt from this APInt |
| 278 | /// @returns this, after subtraction |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 279 | /// @brief Subtraction assignment operator. |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 280 | APInt& APInt::operator-=(const APInt& RHS) { |
Reid Spencer | a32372d1 | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 281 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 282 | if (isSingleWord()) |
Reid Spencer | 7a6a8d5 | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 283 | VAL -= RHS.VAL; |
| 284 | else |
| 285 | sub(pVal, pVal, RHS.pVal, getNumWords()); |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 286 | return clearUnusedBits(); |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Dan Gohman | 4a61882 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 289 | /// Multiplies an integer array, x, by a uint64_t integer and places the result |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 290 | /// into dest. |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 291 | /// @returns the carry out of the multiplication. |
| 292 | /// @brief Multiply a multi-digit APInt by a single digit (64-bit) integer. |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 293 | static uint64_t mul_1(uint64_t dest[], uint64_t x[], unsigned len, uint64_t y) { |
Reid Spencer | df6cf5a | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 294 | // Split y into high 32-bit part (hy) and low 32-bit part (ly) |
Reid Spencer | 100502d | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 295 | uint64_t ly = y & 0xffffffffULL, hy = y >> 32; |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 296 | uint64_t carry = 0; |
| 297 | |
| 298 | // For each digit of x. |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 299 | for (unsigned i = 0; i < len; ++i) { |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 300 | // Split x into high and low words |
| 301 | uint64_t lx = x[i] & 0xffffffffULL; |
| 302 | uint64_t hx = x[i] >> 32; |
| 303 | // hasCarry - A flag to indicate if there is a carry to the next digit. |
Reid Spencer | 100502d | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 304 | // hasCarry == 0, no carry |
| 305 | // hasCarry == 1, has carry |
| 306 | // hasCarry == 2, no carry and the calculation result == 0. |
| 307 | uint8_t hasCarry = 0; |
| 308 | dest[i] = carry + lx * ly; |
| 309 | // Determine if the add above introduces carry. |
| 310 | hasCarry = (dest[i] < carry) ? 1 : 0; |
| 311 | carry = hx * ly + (dest[i] >> 32) + (hasCarry ? (1ULL << 32) : 0); |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 312 | // The upper limit of carry can be (2^32 - 1)(2^32 - 1) + |
Reid Spencer | 100502d | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 313 | // (2^32 - 1) + 2^32 = 2^64. |
| 314 | hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0); |
| 315 | |
| 316 | carry += (lx * hy) & 0xffffffffULL; |
| 317 | dest[i] = (carry << 32) | (dest[i] & 0xffffffffULL); |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 318 | carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0) + |
Reid Spencer | 100502d | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 319 | (carry >> 32) + ((lx * hy) >> 32) + hx * hy; |
| 320 | } |
Reid Spencer | 100502d | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 321 | return carry; |
| 322 | } |
| 323 | |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 324 | /// Multiplies integer array x by integer array y and stores the result into |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 325 | /// the integer array dest. Note that dest's size must be >= xlen + ylen. |
| 326 | /// @brief Generalized multiplicate of integer arrays. |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 327 | static void mul(uint64_t dest[], uint64_t x[], unsigned xlen, uint64_t y[], |
| 328 | unsigned ylen) { |
Reid Spencer | 100502d | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 329 | dest[xlen] = mul_1(dest, x, xlen, y[0]); |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 330 | for (unsigned i = 1; i < ylen; ++i) { |
Reid Spencer | 100502d | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 331 | uint64_t ly = y[i] & 0xffffffffULL, hy = y[i] >> 32; |
Reid Spencer | 58a6a43 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 332 | uint64_t carry = 0, lx = 0, hx = 0; |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 333 | for (unsigned j = 0; j < xlen; ++j) { |
Reid Spencer | 100502d | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 334 | lx = x[j] & 0xffffffffULL; |
| 335 | hx = x[j] >> 32; |
| 336 | // hasCarry - A flag to indicate if has carry. |
| 337 | // hasCarry == 0, no carry |
| 338 | // hasCarry == 1, has carry |
| 339 | // hasCarry == 2, no carry and the calculation result == 0. |
| 340 | uint8_t hasCarry = 0; |
| 341 | uint64_t resul = carry + lx * ly; |
| 342 | hasCarry = (resul < carry) ? 1 : 0; |
| 343 | carry = (hasCarry ? (1ULL << 32) : 0) + hx * ly + (resul >> 32); |
| 344 | hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0); |
| 345 | |
| 346 | carry += (lx * hy) & 0xffffffffULL; |
| 347 | resul = (carry << 32) | (resul & 0xffffffffULL); |
| 348 | dest[i+j] += resul; |
| 349 | carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0)+ |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 350 | (carry >> 32) + (dest[i+j] < resul ? 1 : 0) + |
Reid Spencer | 100502d | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 351 | ((lx * hy) >> 32) + hx * hy; |
| 352 | } |
| 353 | dest[i+xlen] = carry; |
| 354 | } |
| 355 | } |
| 356 | |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 357 | APInt& APInt::operator*=(const APInt& RHS) { |
Reid Spencer | a32372d1 | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 358 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Reid Spencer | 58a6a43 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 359 | if (isSingleWord()) { |
Reid Spencer | 4bb430c | 2007-02-20 20:42:10 +0000 | [diff] [blame] | 360 | VAL *= RHS.VAL; |
Reid Spencer | 58a6a43 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 361 | clearUnusedBits(); |
| 362 | return *this; |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 363 | } |
Reid Spencer | 58a6a43 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 364 | |
| 365 | // Get some bit facts about LHS and check for zero |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 366 | unsigned lhsBits = getActiveBits(); |
| 367 | unsigned lhsWords = !lhsBits ? 0 : whichWord(lhsBits - 1) + 1; |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 368 | if (!lhsWords) |
Reid Spencer | 58a6a43 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 369 | // 0 * X ===> 0 |
| 370 | return *this; |
| 371 | |
| 372 | // Get some bit facts about RHS and check for zero |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 373 | unsigned rhsBits = RHS.getActiveBits(); |
| 374 | unsigned rhsWords = !rhsBits ? 0 : whichWord(rhsBits - 1) + 1; |
Reid Spencer | 58a6a43 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 375 | if (!rhsWords) { |
| 376 | // X * 0 ===> 0 |
Jay Foad | 25a5e4c | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 377 | clearAllBits(); |
Reid Spencer | 58a6a43 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 378 | return *this; |
| 379 | } |
| 380 | |
| 381 | // Allocate space for the result |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 382 | unsigned destWords = rhsWords + lhsWords; |
Reid Spencer | 58a6a43 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 383 | uint64_t *dest = getMemory(destWords); |
| 384 | |
| 385 | // Perform the long multiply |
| 386 | mul(dest, pVal, lhsWords, RHS.pVal, rhsWords); |
| 387 | |
| 388 | // Copy result back into *this |
Jay Foad | 25a5e4c | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 389 | clearAllBits(); |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 390 | unsigned wordsToCopy = destWords >= getNumWords() ? getNumWords() : destWords; |
Reid Spencer | 58a6a43 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 391 | memcpy(pVal, dest, wordsToCopy * APINT_WORD_SIZE); |
Eli Friedman | 1954641 | 2011-10-07 23:40:49 +0000 | [diff] [blame] | 392 | clearUnusedBits(); |
Reid Spencer | 58a6a43 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 393 | |
| 394 | // delete dest array and return |
| 395 | delete[] dest; |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 396 | return *this; |
| 397 | } |
| 398 | |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 399 | APInt& APInt::operator&=(const APInt& RHS) { |
Reid Spencer | a32372d1 | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 400 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 401 | if (isSingleWord()) { |
Reid Spencer | a856b6e | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 402 | VAL &= RHS.VAL; |
| 403 | return *this; |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 404 | } |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 405 | unsigned numWords = getNumWords(); |
| 406 | for (unsigned i = 0; i < numWords; ++i) |
Reid Spencer | a856b6e | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 407 | pVal[i] &= RHS.pVal[i]; |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 408 | return *this; |
| 409 | } |
| 410 | |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 411 | APInt& APInt::operator|=(const APInt& RHS) { |
Reid Spencer | a32372d1 | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 412 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 413 | if (isSingleWord()) { |
Reid Spencer | a856b6e | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 414 | VAL |= RHS.VAL; |
| 415 | return *this; |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 416 | } |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 417 | unsigned numWords = getNumWords(); |
| 418 | for (unsigned i = 0; i < numWords; ++i) |
Reid Spencer | a856b6e | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 419 | pVal[i] |= RHS.pVal[i]; |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 420 | return *this; |
| 421 | } |
| 422 | |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 423 | APInt& APInt::operator^=(const APInt& RHS) { |
Reid Spencer | a32372d1 | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 424 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 425 | if (isSingleWord()) { |
Reid Spencer | ee0a685 | 2007-02-18 06:39:42 +0000 | [diff] [blame] | 426 | VAL ^= RHS.VAL; |
Reid Spencer | 7a6a8d5 | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 427 | this->clearUnusedBits(); |
Reid Spencer | ee0a685 | 2007-02-18 06:39:42 +0000 | [diff] [blame] | 428 | return *this; |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 429 | } |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 430 | unsigned numWords = getNumWords(); |
| 431 | for (unsigned i = 0; i < numWords; ++i) |
Reid Spencer | a856b6e | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 432 | pVal[i] ^= RHS.pVal[i]; |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 433 | return clearUnusedBits(); |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Chris Lattner | 1ac3e25 | 2008-08-20 17:02:31 +0000 | [diff] [blame] | 436 | APInt APInt::AndSlowCase(const APInt& RHS) const { |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 437 | unsigned numWords = getNumWords(); |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 438 | uint64_t* val = getMemory(numWords); |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 439 | for (unsigned i = 0; i < numWords; ++i) |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 440 | val[i] = pVal[i] & RHS.pVal[i]; |
| 441 | return APInt(val, getBitWidth()); |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Chris Lattner | 1ac3e25 | 2008-08-20 17:02:31 +0000 | [diff] [blame] | 444 | APInt APInt::OrSlowCase(const APInt& RHS) const { |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 445 | unsigned numWords = getNumWords(); |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 446 | uint64_t *val = getMemory(numWords); |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 447 | for (unsigned i = 0; i < numWords; ++i) |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 448 | val[i] = pVal[i] | RHS.pVal[i]; |
| 449 | return APInt(val, getBitWidth()); |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Chris Lattner | 1ac3e25 | 2008-08-20 17:02:31 +0000 | [diff] [blame] | 452 | APInt APInt::XorSlowCase(const APInt& RHS) const { |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 453 | unsigned numWords = getNumWords(); |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 454 | uint64_t *val = getMemory(numWords); |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 455 | for (unsigned i = 0; i < numWords; ++i) |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 456 | val[i] = pVal[i] ^ RHS.pVal[i]; |
| 457 | |
Benjamin Kramer | f9a2975 | 2014-10-10 10:18:12 +0000 | [diff] [blame] | 458 | APInt Result(val, getBitWidth()); |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 459 | // 0^0==1 so clear the high bits in case they got set. |
Benjamin Kramer | f9a2975 | 2014-10-10 10:18:12 +0000 | [diff] [blame] | 460 | Result.clearUnusedBits(); |
| 461 | return Result; |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 464 | APInt APInt::operator*(const APInt& RHS) const { |
Reid Spencer | a32372d1 | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 465 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 466 | if (isSingleWord()) |
Reid Spencer | aa8dcfe | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 467 | return APInt(BitWidth, VAL * RHS.VAL); |
Reid Spencer | 4bb430c | 2007-02-20 20:42:10 +0000 | [diff] [blame] | 468 | APInt Result(*this); |
| 469 | Result *= RHS; |
Eli Friedman | 1954641 | 2011-10-07 23:40:49 +0000 | [diff] [blame] | 470 | return Result; |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 473 | APInt APInt::operator+(const APInt& RHS) const { |
Reid Spencer | a32372d1 | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 474 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 475 | if (isSingleWord()) |
Reid Spencer | aa8dcfe | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 476 | return APInt(BitWidth, VAL + RHS.VAL); |
Reid Spencer | 7a6a8d5 | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 477 | APInt Result(BitWidth, 0); |
| 478 | add(Result.pVal, this->pVal, RHS.pVal, getNumWords()); |
Benjamin Kramer | f9a2975 | 2014-10-10 10:18:12 +0000 | [diff] [blame] | 479 | Result.clearUnusedBits(); |
| 480 | return Result; |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Pete Cooper | 18e9102 | 2016-05-27 03:42:17 +0000 | [diff] [blame] | 483 | APInt APInt::operator+(uint64_t RHS) const { |
| 484 | if (isSingleWord()) |
| 485 | return APInt(BitWidth, VAL + RHS); |
| 486 | APInt Result(*this); |
| 487 | add_1(Result.pVal, Result.pVal, getNumWords(), RHS); |
| 488 | Result.clearUnusedBits(); |
| 489 | return Result; |
| 490 | } |
| 491 | |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 492 | APInt APInt::operator-(const APInt& RHS) const { |
Reid Spencer | a32372d1 | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 493 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 494 | if (isSingleWord()) |
Reid Spencer | aa8dcfe | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 495 | return APInt(BitWidth, VAL - RHS.VAL); |
Reid Spencer | 7a6a8d5 | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 496 | APInt Result(BitWidth, 0); |
| 497 | sub(Result.pVal, this->pVal, RHS.pVal, getNumWords()); |
Benjamin Kramer | f9a2975 | 2014-10-10 10:18:12 +0000 | [diff] [blame] | 498 | Result.clearUnusedBits(); |
| 499 | return Result; |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Pete Cooper | 18e9102 | 2016-05-27 03:42:17 +0000 | [diff] [blame] | 502 | APInt APInt::operator-(uint64_t RHS) const { |
| 503 | if (isSingleWord()) |
| 504 | return APInt(BitWidth, VAL - RHS); |
| 505 | APInt Result(*this); |
| 506 | sub_1(Result.pVal, getNumWords(), RHS); |
| 507 | Result.clearUnusedBits(); |
| 508 | return Result; |
| 509 | } |
| 510 | |
Chris Lattner | 1ac3e25 | 2008-08-20 17:02:31 +0000 | [diff] [blame] | 511 | bool APInt::EqualSlowCase(const APInt& RHS) const { |
Matthias Braun | 5117fcd | 2016-02-15 20:06:19 +0000 | [diff] [blame] | 512 | return std::equal(pVal, pVal + getNumWords(), RHS.pVal); |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Chris Lattner | 1ac3e25 | 2008-08-20 17:02:31 +0000 | [diff] [blame] | 515 | bool APInt::EqualSlowCase(uint64_t Val) const { |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 516 | unsigned n = getActiveBits(); |
Reid Spencer | 7a6a8d5 | 2007-02-20 23:40:25 +0000 | [diff] [blame] | 517 | if (n <= APINT_BITS_PER_WORD) |
| 518 | return pVal[0] == Val; |
| 519 | else |
| 520 | return false; |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Reid Spencer | 1d07212 | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 523 | bool APInt::ult(const APInt& RHS) const { |
| 524 | assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison"); |
| 525 | if (isSingleWord()) |
| 526 | return VAL < RHS.VAL; |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 527 | |
| 528 | // Get active bit length of both operands |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 529 | unsigned n1 = getActiveBits(); |
| 530 | unsigned n2 = RHS.getActiveBits(); |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 531 | |
| 532 | // If magnitude of LHS is less than RHS, return true. |
| 533 | if (n1 < n2) |
| 534 | return true; |
| 535 | |
| 536 | // If magnitude of RHS is greather than LHS, return false. |
| 537 | if (n2 < n1) |
| 538 | return false; |
| 539 | |
| 540 | // If they bot fit in a word, just compare the low order word |
| 541 | if (n1 <= APINT_BITS_PER_WORD && n2 <= APINT_BITS_PER_WORD) |
| 542 | return pVal[0] < RHS.pVal[0]; |
| 543 | |
| 544 | // Otherwise, compare all words |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 545 | unsigned topWord = whichWord(std::max(n1,n2)-1); |
Reid Spencer | 54abdcf | 2007-02-27 18:23:40 +0000 | [diff] [blame] | 546 | for (int i = topWord; i >= 0; --i) { |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 547 | if (pVal[i] > RHS.pVal[i]) |
Reid Spencer | 1d07212 | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 548 | return false; |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 549 | if (pVal[i] < RHS.pVal[i]) |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 550 | return true; |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 551 | } |
| 552 | return false; |
| 553 | } |
| 554 | |
Reid Spencer | 1d07212 | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 555 | bool APInt::slt(const APInt& RHS) const { |
| 556 | assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison"); |
Reid Spencer | be4ddf6 | 2007-02-18 20:09:41 +0000 | [diff] [blame] | 557 | if (isSingleWord()) { |
| 558 | int64_t lhsSext = (int64_t(VAL) << (64-BitWidth)) >> (64-BitWidth); |
| 559 | int64_t rhsSext = (int64_t(RHS.VAL) << (64-BitWidth)) >> (64-BitWidth); |
| 560 | return lhsSext < rhsSext; |
Reid Spencer | 1d07212 | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 561 | } |
Reid Spencer | be4ddf6 | 2007-02-18 20:09:41 +0000 | [diff] [blame] | 562 | |
Reid Spencer | 54abdcf | 2007-02-27 18:23:40 +0000 | [diff] [blame] | 563 | bool lhsNeg = isNegative(); |
Pete Cooper | d6e6bf1 | 2016-05-26 17:40:07 +0000 | [diff] [blame] | 564 | bool rhsNeg = RHS.isNegative(); |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 565 | |
Pete Cooper | d6e6bf1 | 2016-05-26 17:40:07 +0000 | [diff] [blame] | 566 | // If the sign bits don't match, then (LHS < RHS) if LHS is negative |
| 567 | if (lhsNeg != rhsNeg) |
| 568 | return lhsNeg; |
| 569 | |
| 570 | // Otherwise we can just use an unsigned comparision, because even negative |
| 571 | // numbers compare correctly this way if both have the same signed-ness. |
| 572 | return ult(RHS); |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Jay Foad | 25a5e4c | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 575 | void APInt::setBit(unsigned bitPosition) { |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 576 | if (isSingleWord()) |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 577 | VAL |= maskBit(bitPosition); |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 578 | else |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 579 | pVal[whichWord(bitPosition)] |= maskBit(bitPosition); |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 582 | /// Set the given bit to 0 whose position is given as "bitPosition". |
| 583 | /// @brief Set a given bit to 0. |
Jay Foad | 25a5e4c | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 584 | void APInt::clearBit(unsigned bitPosition) { |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 585 | if (isSingleWord()) |
Reid Spencer | a856b6e | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 586 | VAL &= ~maskBit(bitPosition); |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 587 | else |
Reid Spencer | a856b6e | 2007-02-18 18:38:44 +0000 | [diff] [blame] | 588 | pVal[whichWord(bitPosition)] &= ~maskBit(bitPosition); |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 591 | /// @brief Toggle every bit to its opposite value. |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 592 | |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 593 | /// Toggle a given bit to its opposite value whose position is given |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 594 | /// as "bitPosition". |
| 595 | /// @brief Toggles a given bit to its opposite value. |
Jay Foad | 25a5e4c | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 596 | void APInt::flipBit(unsigned bitPosition) { |
Reid Spencer | 1d07212 | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 597 | assert(bitPosition < BitWidth && "Out of the bit-width range!"); |
Jay Foad | 25a5e4c | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 598 | if ((*this)[bitPosition]) clearBit(bitPosition); |
| 599 | else setBit(bitPosition); |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Benjamin Kramer | 92d8998 | 2010-07-14 22:38:02 +0000 | [diff] [blame] | 602 | unsigned APInt::getBitsNeeded(StringRef str, uint8_t radix) { |
Daniel Dunbar | 3a1efd11 | 2009-08-13 02:33:34 +0000 | [diff] [blame] | 603 | assert(!str.empty() && "Invalid string length"); |
Douglas Gregor | 663c068 | 2011-09-14 15:54:46 +0000 | [diff] [blame] | 604 | assert((radix == 10 || radix == 8 || radix == 16 || radix == 2 || |
| 605 | radix == 36) && |
| 606 | "Radix should be 2, 8, 10, 16, or 36!"); |
Daniel Dunbar | 3a1efd11 | 2009-08-13 02:33:34 +0000 | [diff] [blame] | 607 | |
| 608 | size_t slen = str.size(); |
Reid Spencer | 9329e7b | 2007-04-13 19:19:07 +0000 | [diff] [blame] | 609 | |
Eric Christopher | 43a1dec | 2009-08-21 04:10:31 +0000 | [diff] [blame] | 610 | // Each computation below needs to know if it's negative. |
Erick Tryzelaar | 1264bcb | 2009-08-21 03:15:14 +0000 | [diff] [blame] | 611 | StringRef::iterator p = str.begin(); |
Eric Christopher | 43a1dec | 2009-08-21 04:10:31 +0000 | [diff] [blame] | 612 | unsigned isNegative = *p == '-'; |
Erick Tryzelaar | 1264bcb | 2009-08-21 03:15:14 +0000 | [diff] [blame] | 613 | if (*p == '-' || *p == '+') { |
| 614 | p++; |
Reid Spencer | 9329e7b | 2007-04-13 19:19:07 +0000 | [diff] [blame] | 615 | slen--; |
Eric Christopher | 43a1dec | 2009-08-21 04:10:31 +0000 | [diff] [blame] | 616 | assert(slen && "String is only a sign, needs a value."); |
Reid Spencer | 9329e7b | 2007-04-13 19:19:07 +0000 | [diff] [blame] | 617 | } |
Eric Christopher | 43a1dec | 2009-08-21 04:10:31 +0000 | [diff] [blame] | 618 | |
Reid Spencer | 9329e7b | 2007-04-13 19:19:07 +0000 | [diff] [blame] | 619 | // For radixes of power-of-two values, the bits required is accurately and |
| 620 | // easily computed |
| 621 | if (radix == 2) |
| 622 | return slen + isNegative; |
| 623 | if (radix == 8) |
| 624 | return slen * 3 + isNegative; |
| 625 | if (radix == 16) |
| 626 | return slen * 4 + isNegative; |
| 627 | |
Douglas Gregor | 663c068 | 2011-09-14 15:54:46 +0000 | [diff] [blame] | 628 | // FIXME: base 36 |
| 629 | |
Reid Spencer | 9329e7b | 2007-04-13 19:19:07 +0000 | [diff] [blame] | 630 | // This is grossly inefficient but accurate. We could probably do something |
| 631 | // with a computation of roughly slen*64/20 and then adjust by the value of |
| 632 | // the first few digits. But, I'm not sure how accurate that could be. |
| 633 | |
| 634 | // Compute a sufficient number of bits that is always large enough but might |
Erick Tryzelaar | dadb1571 | 2009-08-21 03:15:28 +0000 | [diff] [blame] | 635 | // be too large. This avoids the assertion in the constructor. This |
| 636 | // calculation doesn't work appropriately for the numbers 0-9, so just use 4 |
| 637 | // bits in that case. |
Douglas Gregor | 663c068 | 2011-09-14 15:54:46 +0000 | [diff] [blame] | 638 | unsigned sufficient |
| 639 | = radix == 10? (slen == 1 ? 4 : slen * 64/18) |
| 640 | : (slen == 1 ? 7 : slen * 16/3); |
Reid Spencer | 9329e7b | 2007-04-13 19:19:07 +0000 | [diff] [blame] | 641 | |
| 642 | // Convert to the actual binary value. |
Erick Tryzelaar | 1264bcb | 2009-08-21 03:15:14 +0000 | [diff] [blame] | 643 | APInt tmp(sufficient, StringRef(p, slen), radix); |
Reid Spencer | 9329e7b | 2007-04-13 19:19:07 +0000 | [diff] [blame] | 644 | |
Erick Tryzelaar | dadb1571 | 2009-08-21 03:15:28 +0000 | [diff] [blame] | 645 | // Compute how many bits are required. If the log is infinite, assume we need |
| 646 | // just bit. |
| 647 | unsigned log = tmp.logBase2(); |
| 648 | if (log == (unsigned)-1) { |
| 649 | return isNegative + 1; |
| 650 | } else { |
| 651 | return isNegative + log + 1; |
| 652 | } |
Reid Spencer | 9329e7b | 2007-04-13 19:19:07 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Chandler Carruth | 71bd7d1 | 2012-03-04 12:02:57 +0000 | [diff] [blame] | 655 | hash_code llvm::hash_value(const APInt &Arg) { |
| 656 | if (Arg.isSingleWord()) |
| 657 | return hash_combine(Arg.VAL); |
Reid Spencer | b2bc985 | 2007-02-26 21:02:27 +0000 | [diff] [blame] | 658 | |
Chandler Carruth | 71bd7d1 | 2012-03-04 12:02:57 +0000 | [diff] [blame] | 659 | return hash_combine_range(Arg.pVal, Arg.pVal + Arg.getNumWords()); |
Reid Spencer | b2bc985 | 2007-02-26 21:02:27 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Benjamin Kramer | b4b5150 | 2015-03-25 16:49:59 +0000 | [diff] [blame] | 662 | bool APInt::isSplat(unsigned SplatSizeInBits) const { |
| 663 | assert(getBitWidth() % SplatSizeInBits == 0 && |
| 664 | "SplatSizeInBits must divide width!"); |
| 665 | // We can check that all parts of an integer are equal by making use of a |
| 666 | // little trick: rotate and check if it's still the same value. |
| 667 | return *this == rotl(SplatSizeInBits); |
| 668 | } |
| 669 | |
Pawel Bylica | 6eeeac7 | 2015-04-06 13:31:39 +0000 | [diff] [blame] | 670 | /// This function returns the high "numBits" bits of this APInt. |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 671 | APInt APInt::getHiBits(unsigned numBits) const { |
Reid Spencer | 1d07212 | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 672 | return APIntOps::lshr(*this, BitWidth - numBits); |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Pawel Bylica | 6eeeac7 | 2015-04-06 13:31:39 +0000 | [diff] [blame] | 675 | /// This function returns the low "numBits" bits of this APInt. |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 676 | APInt APInt::getLoBits(unsigned numBits) const { |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 677 | return APIntOps::lshr(APIntOps::shl(*this, BitWidth - numBits), |
Reid Spencer | 1d07212 | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 678 | BitWidth - numBits); |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 679 | } |
| 680 | |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 681 | unsigned APInt::countLeadingZerosSlowCase() const { |
Matthias Braun | a6be4e8 | 2016-02-15 20:06:22 +0000 | [diff] [blame] | 682 | unsigned Count = 0; |
| 683 | for (int i = getNumWords()-1; i >= 0; --i) { |
| 684 | integerPart V = pVal[i]; |
| 685 | if (V == 0) |
Chris Lattner | 1ac3e25 | 2008-08-20 17:02:31 +0000 | [diff] [blame] | 686 | Count += APINT_BITS_PER_WORD; |
| 687 | else { |
Matthias Braun | a6be4e8 | 2016-02-15 20:06:22 +0000 | [diff] [blame] | 688 | Count += llvm::countLeadingZeros(V); |
Chris Lattner | 1ac3e25 | 2008-08-20 17:02:31 +0000 | [diff] [blame] | 689 | break; |
Reid Spencer | 74cf82e | 2007-02-21 00:29:48 +0000 | [diff] [blame] | 690 | } |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 691 | } |
Matthias Braun | a6be4e8 | 2016-02-15 20:06:22 +0000 | [diff] [blame] | 692 | // Adjust for unused bits in the most significant word (they are zero). |
| 693 | unsigned Mod = BitWidth % APINT_BITS_PER_WORD; |
| 694 | Count -= Mod > 0 ? APINT_BITS_PER_WORD - Mod : 0; |
John McCall | df951bd | 2010-02-03 03:42:44 +0000 | [diff] [blame] | 695 | return Count; |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 698 | unsigned APInt::countLeadingOnes() const { |
Reid Spencer | 31acef5 | 2007-02-27 21:59:26 +0000 | [diff] [blame] | 699 | if (isSingleWord()) |
Benjamin Kramer | 5f6a907 | 2015-02-12 15:35:40 +0000 | [diff] [blame] | 700 | return llvm::countLeadingOnes(VAL << (APINT_BITS_PER_WORD - BitWidth)); |
Reid Spencer | 31acef5 | 2007-02-27 21:59:26 +0000 | [diff] [blame] | 701 | |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 702 | unsigned highWordBits = BitWidth % APINT_BITS_PER_WORD; |
Torok Edwin | ec39eb8 | 2009-01-27 18:06:03 +0000 | [diff] [blame] | 703 | unsigned shift; |
| 704 | if (!highWordBits) { |
| 705 | highWordBits = APINT_BITS_PER_WORD; |
| 706 | shift = 0; |
| 707 | } else { |
| 708 | shift = APINT_BITS_PER_WORD - highWordBits; |
| 709 | } |
Reid Spencer | 31acef5 | 2007-02-27 21:59:26 +0000 | [diff] [blame] | 710 | int i = getNumWords() - 1; |
Benjamin Kramer | 5f6a907 | 2015-02-12 15:35:40 +0000 | [diff] [blame] | 711 | unsigned Count = llvm::countLeadingOnes(pVal[i] << shift); |
Reid Spencer | 31acef5 | 2007-02-27 21:59:26 +0000 | [diff] [blame] | 712 | if (Count == highWordBits) { |
| 713 | for (i--; i >= 0; --i) { |
| 714 | if (pVal[i] == -1ULL) |
| 715 | Count += APINT_BITS_PER_WORD; |
| 716 | else { |
Benjamin Kramer | 5f6a907 | 2015-02-12 15:35:40 +0000 | [diff] [blame] | 717 | Count += llvm::countLeadingOnes(pVal[i]); |
Reid Spencer | 31acef5 | 2007-02-27 21:59:26 +0000 | [diff] [blame] | 718 | break; |
| 719 | } |
| 720 | } |
| 721 | } |
| 722 | return Count; |
| 723 | } |
| 724 | |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 725 | unsigned APInt::countTrailingZeros() const { |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 726 | if (isSingleWord()) |
Michael J. Spencer | df1ecbd7 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 727 | return std::min(unsigned(llvm::countTrailingZeros(VAL)), BitWidth); |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 728 | unsigned Count = 0; |
| 729 | unsigned i = 0; |
Reid Spencer | aa8dcfe | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 730 | for (; i < getNumWords() && pVal[i] == 0; ++i) |
| 731 | Count += APINT_BITS_PER_WORD; |
| 732 | if (i < getNumWords()) |
Michael J. Spencer | df1ecbd7 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 733 | Count += llvm::countTrailingZeros(pVal[i]); |
Chris Lattner | c2c4c74 | 2007-11-23 22:36:25 +0000 | [diff] [blame] | 734 | return std::min(Count, BitWidth); |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 737 | unsigned APInt::countTrailingOnesSlowCase() const { |
| 738 | unsigned Count = 0; |
| 739 | unsigned i = 0; |
Dan Gohman | c354ebd | 2008-02-14 22:38:45 +0000 | [diff] [blame] | 740 | for (; i < getNumWords() && pVal[i] == -1ULL; ++i) |
Dan Gohman | 8b4fa9d | 2008-02-13 21:11:05 +0000 | [diff] [blame] | 741 | Count += APINT_BITS_PER_WORD; |
| 742 | if (i < getNumWords()) |
Benjamin Kramer | 5f6a907 | 2015-02-12 15:35:40 +0000 | [diff] [blame] | 743 | Count += llvm::countTrailingOnes(pVal[i]); |
Dan Gohman | 8b4fa9d | 2008-02-13 21:11:05 +0000 | [diff] [blame] | 744 | return std::min(Count, BitWidth); |
| 745 | } |
| 746 | |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 747 | unsigned APInt::countPopulationSlowCase() const { |
| 748 | unsigned Count = 0; |
| 749 | for (unsigned i = 0; i < getNumWords(); ++i) |
Benjamin Kramer | 5f6a907 | 2015-02-12 15:35:40 +0000 | [diff] [blame] | 750 | Count += llvm::countPopulation(pVal[i]); |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 751 | return Count; |
| 752 | } |
| 753 | |
Richard Smith | 4f9a808 | 2011-11-23 21:33:37 +0000 | [diff] [blame] | 754 | /// Perform a logical right-shift from Src to Dst, which must be equal or |
| 755 | /// non-overlapping, of Words words, by Shift, which must be less than 64. |
| 756 | static void lshrNear(uint64_t *Dst, uint64_t *Src, unsigned Words, |
| 757 | unsigned Shift) { |
| 758 | uint64_t Carry = 0; |
| 759 | for (int I = Words - 1; I >= 0; --I) { |
| 760 | uint64_t Tmp = Src[I]; |
| 761 | Dst[I] = (Tmp >> Shift) | Carry; |
| 762 | Carry = Tmp << (64 - Shift); |
| 763 | } |
| 764 | } |
| 765 | |
Reid Spencer | 1d07212 | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 766 | APInt APInt::byteSwap() const { |
| 767 | assert(BitWidth >= 16 && BitWidth % 16 == 0 && "Cannot byteswap!"); |
| 768 | if (BitWidth == 16) |
Jeff Cohen | e06855e | 2007-03-20 20:42:36 +0000 | [diff] [blame] | 769 | return APInt(BitWidth, ByteSwap_16(uint16_t(VAL))); |
Richard Smith | 4f9a808 | 2011-11-23 21:33:37 +0000 | [diff] [blame] | 770 | if (BitWidth == 32) |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 771 | return APInt(BitWidth, ByteSwap_32(unsigned(VAL))); |
Richard Smith | 4f9a808 | 2011-11-23 21:33:37 +0000 | [diff] [blame] | 772 | if (BitWidth == 48) { |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 773 | unsigned Tmp1 = unsigned(VAL >> 16); |
Zhou Sheng | cfa2ac0 | 2007-02-15 06:36:31 +0000 | [diff] [blame] | 774 | Tmp1 = ByteSwap_32(Tmp1); |
Jeff Cohen | e06855e | 2007-03-20 20:42:36 +0000 | [diff] [blame] | 775 | uint16_t Tmp2 = uint16_t(VAL); |
Zhou Sheng | cfa2ac0 | 2007-02-15 06:36:31 +0000 | [diff] [blame] | 776 | Tmp2 = ByteSwap_16(Tmp2); |
Jeff Cohen | e06855e | 2007-03-20 20:42:36 +0000 | [diff] [blame] | 777 | return APInt(BitWidth, (uint64_t(Tmp2) << 32) | Tmp1); |
Zhou Sheng | cfa2ac0 | 2007-02-15 06:36:31 +0000 | [diff] [blame] | 778 | } |
Richard Smith | 4f9a808 | 2011-11-23 21:33:37 +0000 | [diff] [blame] | 779 | if (BitWidth == 64) |
| 780 | return APInt(BitWidth, ByteSwap_64(VAL)); |
| 781 | |
| 782 | APInt Result(getNumWords() * APINT_BITS_PER_WORD, 0); |
| 783 | for (unsigned I = 0, N = getNumWords(); I != N; ++I) |
| 784 | Result.pVal[I] = ByteSwap_64(pVal[N - I - 1]); |
| 785 | if (Result.BitWidth != BitWidth) { |
| 786 | lshrNear(Result.pVal, Result.pVal, getNumWords(), |
| 787 | Result.BitWidth - BitWidth); |
| 788 | Result.BitWidth = BitWidth; |
| 789 | } |
| 790 | return Result; |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Matt Arsenault | 155dda9 | 2016-03-21 15:00:35 +0000 | [diff] [blame] | 793 | APInt APInt::reverseBits() const { |
| 794 | switch (BitWidth) { |
| 795 | case 64: |
| 796 | return APInt(BitWidth, llvm::reverseBits<uint64_t>(VAL)); |
| 797 | case 32: |
| 798 | return APInt(BitWidth, llvm::reverseBits<uint32_t>(VAL)); |
| 799 | case 16: |
| 800 | return APInt(BitWidth, llvm::reverseBits<uint16_t>(VAL)); |
| 801 | case 8: |
| 802 | return APInt(BitWidth, llvm::reverseBits<uint8_t>(VAL)); |
| 803 | default: |
| 804 | break; |
| 805 | } |
| 806 | |
| 807 | APInt Val(*this); |
| 808 | APInt Reversed(*this); |
| 809 | int S = BitWidth - 1; |
| 810 | |
| 811 | const APInt One(BitWidth, 1); |
| 812 | |
| 813 | for ((Val = Val.lshr(1)); Val != 0; (Val = Val.lshr(1))) { |
| 814 | Reversed <<= 1; |
| 815 | Reversed |= (Val & One); |
| 816 | --S; |
| 817 | } |
| 818 | |
| 819 | Reversed <<= S; |
| 820 | return Reversed; |
| 821 | } |
| 822 | |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 823 | APInt llvm::APIntOps::GreatestCommonDivisor(const APInt& API1, |
Zhou Sheng | fbf61ea | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 824 | const APInt& API2) { |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 825 | APInt A = API1, B = API2; |
| 826 | while (!!B) { |
| 827 | APInt T = B; |
Reid Spencer | 1d07212 | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 828 | B = APIntOps::urem(A, B); |
Zhou Sheng | dac6378 | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 829 | A = T; |
| 830 | } |
| 831 | return A; |
| 832 | } |
Chris Lattner | 28cbd1d | 2007-02-06 05:38:37 +0000 | [diff] [blame] | 833 | |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 834 | APInt llvm::APIntOps::RoundDoubleToAPInt(double Double, unsigned width) { |
Zhou Sheng | d707d63 | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 835 | union { |
| 836 | double D; |
| 837 | uint64_t I; |
| 838 | } T; |
| 839 | T.D = Double; |
Reid Spencer | 974551a | 2007-02-27 01:28:10 +0000 | [diff] [blame] | 840 | |
| 841 | // Get the sign bit from the highest order bit |
Zhou Sheng | d707d63 | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 842 | bool isNeg = T.I >> 63; |
Reid Spencer | 974551a | 2007-02-27 01:28:10 +0000 | [diff] [blame] | 843 | |
| 844 | // Get the 11-bit exponent and adjust for the 1023 bit bias |
Zhou Sheng | d707d63 | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 845 | int64_t exp = ((T.I >> 52) & 0x7ff) - 1023; |
Reid Spencer | 974551a | 2007-02-27 01:28:10 +0000 | [diff] [blame] | 846 | |
| 847 | // If the exponent is negative, the value is < 0 so just return 0. |
Zhou Sheng | d707d63 | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 848 | if (exp < 0) |
Reid Spencer | 66d0d57 | 2007-02-28 01:30:08 +0000 | [diff] [blame] | 849 | return APInt(width, 0u); |
Reid Spencer | 974551a | 2007-02-27 01:28:10 +0000 | [diff] [blame] | 850 | |
| 851 | // Extract the mantissa by clearing the top 12 bits (sign + exponent). |
| 852 | uint64_t mantissa = (T.I & (~0ULL >> 12)) | 1ULL << 52; |
| 853 | |
| 854 | // If the exponent doesn't shift all bits out of the mantissa |
Zhou Sheng | d707d63 | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 855 | if (exp < 52) |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 856 | return isNeg ? -APInt(width, mantissa >> (52 - exp)) : |
Reid Spencer | 54abdcf | 2007-02-27 18:23:40 +0000 | [diff] [blame] | 857 | APInt(width, mantissa >> (52 - exp)); |
| 858 | |
| 859 | // If the client didn't provide enough bits for us to shift the mantissa into |
| 860 | // then the result is undefined, just return 0 |
| 861 | if (width <= exp - 52) |
| 862 | return APInt(width, 0); |
Reid Spencer | 974551a | 2007-02-27 01:28:10 +0000 | [diff] [blame] | 863 | |
| 864 | // Otherwise, we have to shift the mantissa bits up to the right location |
Reid Spencer | 54abdcf | 2007-02-27 18:23:40 +0000 | [diff] [blame] | 865 | APInt Tmp(width, mantissa); |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 866 | Tmp = Tmp.shl((unsigned)exp - 52); |
Zhou Sheng | d707d63 | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 867 | return isNeg ? -Tmp : Tmp; |
| 868 | } |
| 869 | |
Pawel Bylica | 6eeeac7 | 2015-04-06 13:31:39 +0000 | [diff] [blame] | 870 | /// This function converts this APInt to a double. |
Zhou Sheng | d707d63 | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 871 | /// The layout for double is as following (IEEE Standard 754): |
| 872 | /// -------------------------------------- |
| 873 | /// | Sign Exponent Fraction Bias | |
| 874 | /// |-------------------------------------- | |
| 875 | /// | 1[63] 11[62-52] 52[51-00] 1023 | |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 876 | /// -------------------------------------- |
Reid Spencer | 1d07212 | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 877 | double APInt::roundToDouble(bool isSigned) const { |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 878 | |
| 879 | // Handle the simple case where the value is contained in one uint64_t. |
Dale Johannesen | 54be785 | 2009-08-12 18:04:11 +0000 | [diff] [blame] | 880 | // It is wrong to optimize getWord(0) to VAL; there might be more than one word. |
Reid Spencer | be4ddf6 | 2007-02-18 20:09:41 +0000 | [diff] [blame] | 881 | if (isSingleWord() || getActiveBits() <= APINT_BITS_PER_WORD) { |
| 882 | if (isSigned) { |
Dale Johannesen | 34c08bb | 2009-08-12 17:42:34 +0000 | [diff] [blame] | 883 | int64_t sext = (int64_t(getWord(0)) << (64-BitWidth)) >> (64-BitWidth); |
Reid Spencer | be4ddf6 | 2007-02-18 20:09:41 +0000 | [diff] [blame] | 884 | return double(sext); |
| 885 | } else |
Dale Johannesen | 34c08bb | 2009-08-12 17:42:34 +0000 | [diff] [blame] | 886 | return double(getWord(0)); |
Reid Spencer | be4ddf6 | 2007-02-18 20:09:41 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 889 | // Determine if the value is negative. |
Reid Spencer | 1d07212 | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 890 | bool isNeg = isSigned ? (*this)[BitWidth-1] : false; |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 891 | |
| 892 | // Construct the absolute value if we're negative. |
Zhou Sheng | d707d63 | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 893 | APInt Tmp(isNeg ? -(*this) : (*this)); |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 894 | |
| 895 | // Figure out how many bits we're using. |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 896 | unsigned n = Tmp.getActiveBits(); |
Zhou Sheng | d707d63 | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 897 | |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 898 | // The exponent (without bias normalization) is just the number of bits |
| 899 | // we are using. Note that the sign bit is gone since we constructed the |
| 900 | // absolute value. |
| 901 | uint64_t exp = n; |
Zhou Sheng | d707d63 | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 902 | |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 903 | // Return infinity for exponent overflow |
| 904 | if (exp > 1023) { |
| 905 | if (!isSigned || !isNeg) |
Jeff Cohen | e06855e | 2007-03-20 20:42:36 +0000 | [diff] [blame] | 906 | return std::numeric_limits<double>::infinity(); |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 907 | else |
Jeff Cohen | e06855e | 2007-03-20 20:42:36 +0000 | [diff] [blame] | 908 | return -std::numeric_limits<double>::infinity(); |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 909 | } |
| 910 | exp += 1023; // Increment for 1023 bias |
| 911 | |
| 912 | // Number of bits in mantissa is 52. To obtain the mantissa value, we must |
| 913 | // extract the high 52 bits from the correct words in pVal. |
Zhou Sheng | d707d63 | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 914 | uint64_t mantissa; |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 915 | unsigned hiWord = whichWord(n-1); |
| 916 | if (hiWord == 0) { |
| 917 | mantissa = Tmp.pVal[0]; |
| 918 | if (n > 52) |
| 919 | mantissa >>= n - 52; // shift down, we want the top 52 bits. |
| 920 | } else { |
| 921 | assert(hiWord > 0 && "huh?"); |
| 922 | uint64_t hibits = Tmp.pVal[hiWord] << (52 - n % APINT_BITS_PER_WORD); |
| 923 | uint64_t lobits = Tmp.pVal[hiWord-1] >> (11 + n % APINT_BITS_PER_WORD); |
| 924 | mantissa = hibits | lobits; |
| 925 | } |
| 926 | |
Zhou Sheng | d707d63 | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 927 | // The leading bit of mantissa is implicit, so get rid of it. |
Reid Spencer | fbd48a5 | 2007-02-18 00:44:22 +0000 | [diff] [blame] | 928 | uint64_t sign = isNeg ? (1ULL << (APINT_BITS_PER_WORD - 1)) : 0; |
Zhou Sheng | d707d63 | 2007-02-12 20:02:55 +0000 | [diff] [blame] | 929 | union { |
| 930 | double D; |
| 931 | uint64_t I; |
| 932 | } T; |
| 933 | T.I = sign | (exp << 52) | mantissa; |
| 934 | return T.D; |
| 935 | } |
| 936 | |
Reid Spencer | 1d07212 | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 937 | // Truncate to new width. |
Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 938 | APInt APInt::trunc(unsigned width) const { |
Reid Spencer | 1d07212 | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 939 | assert(width < BitWidth && "Invalid APInt Truncate request"); |
Chris Lattner | 1ac3e25 | 2008-08-20 17:02:31 +0000 | [diff] [blame] | 940 | assert(width && "Can't truncate to 0 bits"); |
Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 941 | |
| 942 | if (width <= APINT_BITS_PER_WORD) |
| 943 | return APInt(width, getRawData()[0]); |
| 944 | |
| 945 | APInt Result(getMemory(getNumWords(width)), width); |
| 946 | |
| 947 | // Copy full words. |
| 948 | unsigned i; |
| 949 | for (i = 0; i != width / APINT_BITS_PER_WORD; i++) |
| 950 | Result.pVal[i] = pVal[i]; |
| 951 | |
| 952 | // Truncate and copy any partial word. |
| 953 | unsigned bits = (0 - width) % APINT_BITS_PER_WORD; |
| 954 | if (bits != 0) |
| 955 | Result.pVal[i] = pVal[i] << bits >> bits; |
| 956 | |
| 957 | return Result; |
Reid Spencer | 1d07212 | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | // Sign extend to a new width. |
Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 961 | APInt APInt::sext(unsigned width) const { |
Reid Spencer | 1d07212 | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 962 | assert(width > BitWidth && "Invalid APInt SignExtend request"); |
Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 963 | |
| 964 | if (width <= APINT_BITS_PER_WORD) { |
| 965 | uint64_t val = VAL << (APINT_BITS_PER_WORD - BitWidth); |
| 966 | val = (int64_t)val >> (width - BitWidth); |
| 967 | return APInt(width, val >> (APINT_BITS_PER_WORD - width)); |
Reid Spencer | b6b5cc3 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 970 | APInt Result(getMemory(getNumWords(width)), width); |
Reid Spencer | b6b5cc3 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 971 | |
Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 972 | // Copy full words. |
| 973 | unsigned i; |
| 974 | uint64_t word = 0; |
| 975 | for (i = 0; i != BitWidth / APINT_BITS_PER_WORD; i++) { |
| 976 | word = getRawData()[i]; |
| 977 | Result.pVal[i] = word; |
Reid Spencer | b6b5cc3 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 978 | } |
| 979 | |
Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 980 | // Read and sign-extend any partial word. |
| 981 | unsigned bits = (0 - BitWidth) % APINT_BITS_PER_WORD; |
| 982 | if (bits != 0) |
| 983 | word = (int64_t)getRawData()[i] << bits >> bits; |
| 984 | else |
| 985 | word = (int64_t)word >> (APINT_BITS_PER_WORD - 1); |
| 986 | |
| 987 | // Write remaining full words. |
| 988 | for (; i != width / APINT_BITS_PER_WORD; i++) { |
| 989 | Result.pVal[i] = word; |
| 990 | word = (int64_t)word >> (APINT_BITS_PER_WORD - 1); |
Reid Spencer | b6b5cc3 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 991 | } |
Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 992 | |
| 993 | // Write any partial word. |
| 994 | bits = (0 - width) % APINT_BITS_PER_WORD; |
| 995 | if (bits != 0) |
| 996 | Result.pVal[i] = word << bits >> bits; |
| 997 | |
| 998 | return Result; |
Reid Spencer | 1d07212 | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | // Zero extend to a new width. |
Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 1002 | APInt APInt::zext(unsigned width) const { |
Reid Spencer | 1d07212 | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 1003 | assert(width > BitWidth && "Invalid APInt ZeroExtend request"); |
Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 1004 | |
| 1005 | if (width <= APINT_BITS_PER_WORD) |
| 1006 | return APInt(width, VAL); |
| 1007 | |
| 1008 | APInt Result(getMemory(getNumWords(width)), width); |
| 1009 | |
| 1010 | // Copy words. |
| 1011 | unsigned i; |
| 1012 | for (i = 0; i != getNumWords(); i++) |
| 1013 | Result.pVal[i] = getRawData()[i]; |
| 1014 | |
| 1015 | // Zero remaining words. |
| 1016 | memset(&Result.pVal[i], 0, (Result.getNumWords() - i) * APINT_WORD_SIZE); |
| 1017 | |
| 1018 | return Result; |
Reid Spencer | 1d07212 | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 1021 | APInt APInt::zextOrTrunc(unsigned width) const { |
Reid Spencer | 742d170 | 2007-03-01 17:15:32 +0000 | [diff] [blame] | 1022 | if (BitWidth < width) |
| 1023 | return zext(width); |
| 1024 | if (BitWidth > width) |
| 1025 | return trunc(width); |
| 1026 | return *this; |
| 1027 | } |
| 1028 | |
Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 1029 | APInt APInt::sextOrTrunc(unsigned width) const { |
Reid Spencer | 742d170 | 2007-03-01 17:15:32 +0000 | [diff] [blame] | 1030 | if (BitWidth < width) |
| 1031 | return sext(width); |
| 1032 | if (BitWidth > width) |
| 1033 | return trunc(width); |
| 1034 | return *this; |
| 1035 | } |
| 1036 | |
Rafael Espindola | bb893fe | 2012-01-27 23:33:07 +0000 | [diff] [blame] | 1037 | APInt APInt::zextOrSelf(unsigned width) const { |
| 1038 | if (BitWidth < width) |
| 1039 | return zext(width); |
| 1040 | return *this; |
| 1041 | } |
| 1042 | |
| 1043 | APInt APInt::sextOrSelf(unsigned width) const { |
| 1044 | if (BitWidth < width) |
| 1045 | return sext(width); |
| 1046 | return *this; |
| 1047 | } |
| 1048 | |
Zhou Sheng | e93db8f | 2007-02-09 07:48:24 +0000 | [diff] [blame] | 1049 | /// Arithmetic right-shift this APInt by shiftAmt. |
Zhou Sheng | fbf61ea | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1050 | /// @brief Arithmetic right-shift function. |
Dan Gohman | 105c1d4 | 2008-02-29 01:40:47 +0000 | [diff] [blame] | 1051 | APInt APInt::ashr(const APInt &shiftAmt) const { |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1052 | return ashr((unsigned)shiftAmt.getLimitedValue(BitWidth)); |
Dan Gohman | 105c1d4 | 2008-02-29 01:40:47 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | /// Arithmetic right-shift this APInt by shiftAmt. |
| 1056 | /// @brief Arithmetic right-shift function. |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1057 | APInt APInt::ashr(unsigned shiftAmt) const { |
Reid Spencer | aa8dcfe | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1058 | assert(shiftAmt <= BitWidth && "Invalid shift amount"); |
Reid Spencer | 1825dd0 | 2007-03-02 22:39:11 +0000 | [diff] [blame] | 1059 | // Handle a degenerate case |
| 1060 | if (shiftAmt == 0) |
| 1061 | return *this; |
| 1062 | |
| 1063 | // Handle single word shifts with built-in ashr |
Reid Spencer | 522ca7c | 2007-02-25 01:56:07 +0000 | [diff] [blame] | 1064 | if (isSingleWord()) { |
| 1065 | if (shiftAmt == BitWidth) |
Reid Spencer | aa8dcfe | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1066 | return APInt(BitWidth, 0); // undefined |
| 1067 | else { |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1068 | unsigned SignBit = APINT_BITS_PER_WORD - BitWidth; |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1069 | return APInt(BitWidth, |
Reid Spencer | aa8dcfe | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1070 | (((int64_t(VAL) << SignBit) >> SignBit) >> shiftAmt)); |
| 1071 | } |
Zhou Sheng | fbf61ea | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1072 | } |
Reid Spencer | 522ca7c | 2007-02-25 01:56:07 +0000 | [diff] [blame] | 1073 | |
Reid Spencer | 1825dd0 | 2007-03-02 22:39:11 +0000 | [diff] [blame] | 1074 | // If all the bits were shifted out, the result is, technically, undefined. |
| 1075 | // We return -1 if it was negative, 0 otherwise. We check this early to avoid |
| 1076 | // issues in the algorithm below. |
Chris Lattner | dad2d09 | 2007-05-03 18:15:36 +0000 | [diff] [blame] | 1077 | if (shiftAmt == BitWidth) { |
Reid Spencer | aa8dcfe | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1078 | if (isNegative()) |
Zhou Sheng | 1247c07 | 2008-06-05 13:27:38 +0000 | [diff] [blame] | 1079 | return APInt(BitWidth, -1ULL, true); |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 1080 | else |
Reid Spencer | aa8dcfe | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1081 | return APInt(BitWidth, 0); |
Chris Lattner | dad2d09 | 2007-05-03 18:15:36 +0000 | [diff] [blame] | 1082 | } |
Reid Spencer | aa8dcfe | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1083 | |
| 1084 | // Create some space for the result. |
| 1085 | uint64_t * val = new uint64_t[getNumWords()]; |
| 1086 | |
Reid Spencer | 1825dd0 | 2007-03-02 22:39:11 +0000 | [diff] [blame] | 1087 | // Compute some values needed by the following shift algorithms |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1088 | unsigned wordShift = shiftAmt % APINT_BITS_PER_WORD; // bits to shift per word |
| 1089 | unsigned offset = shiftAmt / APINT_BITS_PER_WORD; // word offset for shift |
| 1090 | unsigned breakWord = getNumWords() - 1 - offset; // last word affected |
| 1091 | unsigned bitsInWord = whichBit(BitWidth); // how many bits in last word? |
Reid Spencer | 1825dd0 | 2007-03-02 22:39:11 +0000 | [diff] [blame] | 1092 | if (bitsInWord == 0) |
| 1093 | bitsInWord = APINT_BITS_PER_WORD; |
Reid Spencer | aa8dcfe | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1094 | |
| 1095 | // If we are shifting whole words, just move whole words |
| 1096 | if (wordShift == 0) { |
Reid Spencer | 1825dd0 | 2007-03-02 22:39:11 +0000 | [diff] [blame] | 1097 | // Move the words containing significant bits |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1098 | for (unsigned i = 0; i <= breakWord; ++i) |
Reid Spencer | 1825dd0 | 2007-03-02 22:39:11 +0000 | [diff] [blame] | 1099 | val[i] = pVal[i+offset]; // move whole word |
| 1100 | |
| 1101 | // Adjust the top significant word for sign bit fill, if negative |
| 1102 | if (isNegative()) |
| 1103 | if (bitsInWord < APINT_BITS_PER_WORD) |
| 1104 | val[breakWord] |= ~0ULL << bitsInWord; // set high bits |
| 1105 | } else { |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1106 | // Shift the low order words |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1107 | for (unsigned i = 0; i < breakWord; ++i) { |
Reid Spencer | 1825dd0 | 2007-03-02 22:39:11 +0000 | [diff] [blame] | 1108 | // This combines the shifted corresponding word with the low bits from |
| 1109 | // the next word (shifted into this word's high bits). |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1110 | val[i] = (pVal[i+offset] >> wordShift) | |
Reid Spencer | 1825dd0 | 2007-03-02 22:39:11 +0000 | [diff] [blame] | 1111 | (pVal[i+offset+1] << (APINT_BITS_PER_WORD - wordShift)); |
| 1112 | } |
| 1113 | |
| 1114 | // Shift the break word. In this case there are no bits from the next word |
| 1115 | // to include in this word. |
| 1116 | val[breakWord] = pVal[breakWord+offset] >> wordShift; |
| 1117 | |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 1118 | // Deal with sign extension in the break word, and possibly the word before |
Reid Spencer | 1825dd0 | 2007-03-02 22:39:11 +0000 | [diff] [blame] | 1119 | // it. |
Chris Lattner | dad2d09 | 2007-05-03 18:15:36 +0000 | [diff] [blame] | 1120 | if (isNegative()) { |
Reid Spencer | 1825dd0 | 2007-03-02 22:39:11 +0000 | [diff] [blame] | 1121 | if (wordShift > bitsInWord) { |
| 1122 | if (breakWord > 0) |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1123 | val[breakWord-1] |= |
Reid Spencer | 1825dd0 | 2007-03-02 22:39:11 +0000 | [diff] [blame] | 1124 | ~0ULL << (APINT_BITS_PER_WORD - (wordShift - bitsInWord)); |
| 1125 | val[breakWord] |= ~0ULL; |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1126 | } else |
Reid Spencer | 1825dd0 | 2007-03-02 22:39:11 +0000 | [diff] [blame] | 1127 | val[breakWord] |= (~0ULL << (bitsInWord - wordShift)); |
Chris Lattner | dad2d09 | 2007-05-03 18:15:36 +0000 | [diff] [blame] | 1128 | } |
Reid Spencer | aa8dcfe | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
Reid Spencer | 1825dd0 | 2007-03-02 22:39:11 +0000 | [diff] [blame] | 1131 | // Remaining words are 0 or -1, just assign them. |
| 1132 | uint64_t fillValue = (isNegative() ? -1ULL : 0); |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1133 | for (unsigned i = breakWord+1; i < getNumWords(); ++i) |
Reid Spencer | 1825dd0 | 2007-03-02 22:39:11 +0000 | [diff] [blame] | 1134 | val[i] = fillValue; |
Benjamin Kramer | f9a2975 | 2014-10-10 10:18:12 +0000 | [diff] [blame] | 1135 | APInt Result(val, BitWidth); |
| 1136 | Result.clearUnusedBits(); |
| 1137 | return Result; |
Zhou Sheng | fbf61ea | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
Zhou Sheng | e93db8f | 2007-02-09 07:48:24 +0000 | [diff] [blame] | 1140 | /// Logical right-shift this APInt by shiftAmt. |
Zhou Sheng | fbf61ea | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1141 | /// @brief Logical right-shift function. |
Dan Gohman | 105c1d4 | 2008-02-29 01:40:47 +0000 | [diff] [blame] | 1142 | APInt APInt::lshr(const APInt &shiftAmt) const { |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1143 | return lshr((unsigned)shiftAmt.getLimitedValue(BitWidth)); |
Dan Gohman | 105c1d4 | 2008-02-29 01:40:47 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | /// Logical right-shift this APInt by shiftAmt. |
| 1147 | /// @brief Logical right-shift function. |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1148 | APInt APInt::lshr(unsigned shiftAmt) const { |
Chris Lattner | dad2d09 | 2007-05-03 18:15:36 +0000 | [diff] [blame] | 1149 | if (isSingleWord()) { |
Ahmed Charles | 0dca5d8 | 2012-02-24 19:06:15 +0000 | [diff] [blame] | 1150 | if (shiftAmt >= BitWidth) |
Reid Spencer | 522ca7c | 2007-02-25 01:56:07 +0000 | [diff] [blame] | 1151 | return APInt(BitWidth, 0); |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1152 | else |
Reid Spencer | 522ca7c | 2007-02-25 01:56:07 +0000 | [diff] [blame] | 1153 | return APInt(BitWidth, this->VAL >> shiftAmt); |
Chris Lattner | dad2d09 | 2007-05-03 18:15:36 +0000 | [diff] [blame] | 1154 | } |
Reid Spencer | 522ca7c | 2007-02-25 01:56:07 +0000 | [diff] [blame] | 1155 | |
Reid Spencer | 44eef16 | 2007-02-26 01:19:48 +0000 | [diff] [blame] | 1156 | // If all the bits were shifted out, the result is 0. This avoids issues |
| 1157 | // with shifting by the size of the integer type, which produces undefined |
| 1158 | // results. We define these "undefined results" to always be 0. |
Chad Rosier | 3d464d8 | 2012-06-08 18:04:52 +0000 | [diff] [blame] | 1159 | if (shiftAmt >= BitWidth) |
Reid Spencer | 44eef16 | 2007-02-26 01:19:48 +0000 | [diff] [blame] | 1160 | return APInt(BitWidth, 0); |
| 1161 | |
Reid Spencer | fffdf10 | 2007-05-17 06:26:29 +0000 | [diff] [blame] | 1162 | // If none of the bits are shifted out, the result is *this. This avoids |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1163 | // issues with shifting by the size of the integer type, which produces |
Reid Spencer | fffdf10 | 2007-05-17 06:26:29 +0000 | [diff] [blame] | 1164 | // undefined results in the code below. This is also an optimization. |
| 1165 | if (shiftAmt == 0) |
| 1166 | return *this; |
| 1167 | |
Reid Spencer | 44eef16 | 2007-02-26 01:19:48 +0000 | [diff] [blame] | 1168 | // Create some space for the result. |
| 1169 | uint64_t * val = new uint64_t[getNumWords()]; |
| 1170 | |
| 1171 | // If we are shifting less than a word, compute the shift with a simple carry |
| 1172 | if (shiftAmt < APINT_BITS_PER_WORD) { |
Richard Smith | 4f9a808 | 2011-11-23 21:33:37 +0000 | [diff] [blame] | 1173 | lshrNear(val, pVal, getNumWords(), shiftAmt); |
Benjamin Kramer | f9a2975 | 2014-10-10 10:18:12 +0000 | [diff] [blame] | 1174 | APInt Result(val, BitWidth); |
| 1175 | Result.clearUnusedBits(); |
| 1176 | return Result; |
Reid Spencer | a41e93b | 2007-02-25 19:32:03 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
Reid Spencer | 44eef16 | 2007-02-26 01:19:48 +0000 | [diff] [blame] | 1179 | // Compute some values needed by the remaining shift algorithms |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1180 | unsigned wordShift = shiftAmt % APINT_BITS_PER_WORD; |
| 1181 | unsigned offset = shiftAmt / APINT_BITS_PER_WORD; |
Reid Spencer | 44eef16 | 2007-02-26 01:19:48 +0000 | [diff] [blame] | 1182 | |
| 1183 | // If we are shifting whole words, just move whole words |
| 1184 | if (wordShift == 0) { |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1185 | for (unsigned i = 0; i < getNumWords() - offset; ++i) |
Reid Spencer | 44eef16 | 2007-02-26 01:19:48 +0000 | [diff] [blame] | 1186 | val[i] = pVal[i+offset]; |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1187 | for (unsigned i = getNumWords()-offset; i < getNumWords(); i++) |
Reid Spencer | 44eef16 | 2007-02-26 01:19:48 +0000 | [diff] [blame] | 1188 | val[i] = 0; |
Benjamin Kramer | f9a2975 | 2014-10-10 10:18:12 +0000 | [diff] [blame] | 1189 | APInt Result(val, BitWidth); |
| 1190 | Result.clearUnusedBits(); |
| 1191 | return Result; |
Reid Spencer | 44eef16 | 2007-02-26 01:19:48 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1194 | // Shift the low order words |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1195 | unsigned breakWord = getNumWords() - offset -1; |
| 1196 | for (unsigned i = 0; i < breakWord; ++i) |
Reid Spencer | d99feaf | 2007-03-01 05:39:56 +0000 | [diff] [blame] | 1197 | val[i] = (pVal[i+offset] >> wordShift) | |
| 1198 | (pVal[i+offset+1] << (APINT_BITS_PER_WORD - wordShift)); |
Reid Spencer | 44eef16 | 2007-02-26 01:19:48 +0000 | [diff] [blame] | 1199 | // Shift the break word. |
| 1200 | val[breakWord] = pVal[breakWord+offset] >> wordShift; |
| 1201 | |
| 1202 | // Remaining words are 0 |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1203 | for (unsigned i = breakWord+1; i < getNumWords(); ++i) |
Reid Spencer | 44eef16 | 2007-02-26 01:19:48 +0000 | [diff] [blame] | 1204 | val[i] = 0; |
Benjamin Kramer | f9a2975 | 2014-10-10 10:18:12 +0000 | [diff] [blame] | 1205 | APInt Result(val, BitWidth); |
| 1206 | Result.clearUnusedBits(); |
| 1207 | return Result; |
Zhou Sheng | fbf61ea | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
Zhou Sheng | e93db8f | 2007-02-09 07:48:24 +0000 | [diff] [blame] | 1210 | /// Left-shift this APInt by shiftAmt. |
Zhou Sheng | fbf61ea | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1211 | /// @brief Left-shift function. |
Dan Gohman | 105c1d4 | 2008-02-29 01:40:47 +0000 | [diff] [blame] | 1212 | APInt APInt::shl(const APInt &shiftAmt) const { |
Nick Lewycky | 030c450 | 2009-01-19 17:42:33 +0000 | [diff] [blame] | 1213 | // It's undefined behavior in C to shift by BitWidth or greater. |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1214 | return shl((unsigned)shiftAmt.getLimitedValue(BitWidth)); |
Dan Gohman | 105c1d4 | 2008-02-29 01:40:47 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1217 | APInt APInt::shlSlowCase(unsigned shiftAmt) const { |
Reid Spencer | a5c84d9 | 2007-02-25 00:56:44 +0000 | [diff] [blame] | 1218 | // If all the bits were shifted out, the result is 0. This avoids issues |
| 1219 | // with shifting by the size of the integer type, which produces undefined |
| 1220 | // results. We define these "undefined results" to always be 0. |
| 1221 | if (shiftAmt == BitWidth) |
| 1222 | return APInt(BitWidth, 0); |
| 1223 | |
Reid Spencer | 81ee020 | 2007-05-12 18:01:57 +0000 | [diff] [blame] | 1224 | // If none of the bits are shifted out, the result is *this. This avoids a |
| 1225 | // lshr by the words size in the loop below which can produce incorrect |
| 1226 | // results. It also avoids the expensive computation below for a common case. |
| 1227 | if (shiftAmt == 0) |
| 1228 | return *this; |
| 1229 | |
Reid Spencer | a5c84d9 | 2007-02-25 00:56:44 +0000 | [diff] [blame] | 1230 | // Create some space for the result. |
| 1231 | uint64_t * val = new uint64_t[getNumWords()]; |
| 1232 | |
| 1233 | // If we are shifting less than a word, do it the easy way |
| 1234 | if (shiftAmt < APINT_BITS_PER_WORD) { |
| 1235 | uint64_t carry = 0; |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1236 | for (unsigned i = 0; i < getNumWords(); i++) { |
Reid Spencer | a5c84d9 | 2007-02-25 00:56:44 +0000 | [diff] [blame] | 1237 | val[i] = pVal[i] << shiftAmt | carry; |
| 1238 | carry = pVal[i] >> (APINT_BITS_PER_WORD - shiftAmt); |
| 1239 | } |
Benjamin Kramer | f9a2975 | 2014-10-10 10:18:12 +0000 | [diff] [blame] | 1240 | APInt Result(val, BitWidth); |
| 1241 | Result.clearUnusedBits(); |
| 1242 | return Result; |
Reid Spencer | 632ebdf | 2007-02-24 20:19:37 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Reid Spencer | a5c84d9 | 2007-02-25 00:56:44 +0000 | [diff] [blame] | 1245 | // Compute some values needed by the remaining shift algorithms |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1246 | unsigned wordShift = shiftAmt % APINT_BITS_PER_WORD; |
| 1247 | unsigned offset = shiftAmt / APINT_BITS_PER_WORD; |
Reid Spencer | a5c84d9 | 2007-02-25 00:56:44 +0000 | [diff] [blame] | 1248 | |
| 1249 | // If we are shifting whole words, just move whole words |
| 1250 | if (wordShift == 0) { |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1251 | for (unsigned i = 0; i < offset; i++) |
Reid Spencer | a5c84d9 | 2007-02-25 00:56:44 +0000 | [diff] [blame] | 1252 | val[i] = 0; |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1253 | for (unsigned i = offset; i < getNumWords(); i++) |
Reid Spencer | a5c84d9 | 2007-02-25 00:56:44 +0000 | [diff] [blame] | 1254 | val[i] = pVal[i-offset]; |
Benjamin Kramer | f9a2975 | 2014-10-10 10:18:12 +0000 | [diff] [blame] | 1255 | APInt Result(val, BitWidth); |
| 1256 | Result.clearUnusedBits(); |
| 1257 | return Result; |
Reid Spencer | 632ebdf | 2007-02-24 20:19:37 +0000 | [diff] [blame] | 1258 | } |
Reid Spencer | a5c84d9 | 2007-02-25 00:56:44 +0000 | [diff] [blame] | 1259 | |
| 1260 | // Copy whole words from this to Result. |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1261 | unsigned i = getNumWords() - 1; |
Reid Spencer | a5c84d9 | 2007-02-25 00:56:44 +0000 | [diff] [blame] | 1262 | for (; i > offset; --i) |
| 1263 | val[i] = pVal[i-offset] << wordShift | |
| 1264 | pVal[i-offset-1] >> (APINT_BITS_PER_WORD - wordShift); |
Reid Spencer | ab0e08a | 2007-02-25 01:08:58 +0000 | [diff] [blame] | 1265 | val[offset] = pVal[0] << wordShift; |
Reid Spencer | a5c84d9 | 2007-02-25 00:56:44 +0000 | [diff] [blame] | 1266 | for (i = 0; i < offset; ++i) |
| 1267 | val[i] = 0; |
Benjamin Kramer | f9a2975 | 2014-10-10 10:18:12 +0000 | [diff] [blame] | 1268 | APInt Result(val, BitWidth); |
| 1269 | Result.clearUnusedBits(); |
| 1270 | return Result; |
Zhou Sheng | fbf61ea | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
Dan Gohman | 105c1d4 | 2008-02-29 01:40:47 +0000 | [diff] [blame] | 1273 | APInt APInt::rotl(const APInt &rotateAmt) const { |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1274 | return rotl((unsigned)rotateAmt.getLimitedValue(BitWidth)); |
Dan Gohman | 105c1d4 | 2008-02-29 01:40:47 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1277 | APInt APInt::rotl(unsigned rotateAmt) const { |
Eli Friedman | 2aae94f | 2011-12-22 03:15:35 +0000 | [diff] [blame] | 1278 | rotateAmt %= BitWidth; |
Reid Spencer | 98ed7db | 2007-05-14 00:15:28 +0000 | [diff] [blame] | 1279 | if (rotateAmt == 0) |
| 1280 | return *this; |
Eli Friedman | 2aae94f | 2011-12-22 03:15:35 +0000 | [diff] [blame] | 1281 | return shl(rotateAmt) | lshr(BitWidth - rotateAmt); |
Reid Spencer | 4c50b52 | 2007-05-13 23:44:59 +0000 | [diff] [blame] | 1282 | } |
| 1283 | |
Dan Gohman | 105c1d4 | 2008-02-29 01:40:47 +0000 | [diff] [blame] | 1284 | APInt APInt::rotr(const APInt &rotateAmt) const { |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1285 | return rotr((unsigned)rotateAmt.getLimitedValue(BitWidth)); |
Dan Gohman | 105c1d4 | 2008-02-29 01:40:47 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1288 | APInt APInt::rotr(unsigned rotateAmt) const { |
Eli Friedman | 2aae94f | 2011-12-22 03:15:35 +0000 | [diff] [blame] | 1289 | rotateAmt %= BitWidth; |
Reid Spencer | 98ed7db | 2007-05-14 00:15:28 +0000 | [diff] [blame] | 1290 | if (rotateAmt == 0) |
| 1291 | return *this; |
Eli Friedman | 2aae94f | 2011-12-22 03:15:35 +0000 | [diff] [blame] | 1292 | return lshr(rotateAmt) | shl(BitWidth - rotateAmt); |
Reid Spencer | 4c50b52 | 2007-05-13 23:44:59 +0000 | [diff] [blame] | 1293 | } |
Reid Spencer | d99feaf | 2007-03-01 05:39:56 +0000 | [diff] [blame] | 1294 | |
| 1295 | // Square Root - this method computes and returns the square root of "this". |
| 1296 | // Three mechanisms are used for computation. For small values (<= 5 bits), |
| 1297 | // a table lookup is done. This gets some performance for common cases. For |
| 1298 | // values using less than 52 bits, the value is converted to double and then |
| 1299 | // the libc sqrt function is called. The result is rounded and then converted |
| 1300 | // back to a uint64_t which is then used to construct the result. Finally, |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1301 | // the Babylonian method for computing square roots is used. |
Reid Spencer | d99feaf | 2007-03-01 05:39:56 +0000 | [diff] [blame] | 1302 | APInt APInt::sqrt() const { |
| 1303 | |
| 1304 | // Determine the magnitude of the value. |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1305 | unsigned magnitude = getActiveBits(); |
Reid Spencer | d99feaf | 2007-03-01 05:39:56 +0000 | [diff] [blame] | 1306 | |
| 1307 | // Use a fast table for some small values. This also gets rid of some |
| 1308 | // rounding errors in libc sqrt for small values. |
| 1309 | if (magnitude <= 5) { |
Reid Spencer | 2f6ad4d | 2007-03-01 17:47:31 +0000 | [diff] [blame] | 1310 | static const uint8_t results[32] = { |
Reid Spencer | c8841d2 | 2007-03-01 06:23:32 +0000 | [diff] [blame] | 1311 | /* 0 */ 0, |
| 1312 | /* 1- 2 */ 1, 1, |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1313 | /* 3- 6 */ 2, 2, 2, 2, |
Reid Spencer | c8841d2 | 2007-03-01 06:23:32 +0000 | [diff] [blame] | 1314 | /* 7-12 */ 3, 3, 3, 3, 3, 3, |
| 1315 | /* 13-20 */ 4, 4, 4, 4, 4, 4, 4, 4, |
| 1316 | /* 21-30 */ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
| 1317 | /* 31 */ 6 |
| 1318 | }; |
| 1319 | return APInt(BitWidth, results[ (isSingleWord() ? VAL : pVal[0]) ]); |
Reid Spencer | d99feaf | 2007-03-01 05:39:56 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | // If the magnitude of the value fits in less than 52 bits (the precision of |
| 1323 | // an IEEE double precision floating point value), then we can use the |
| 1324 | // libc sqrt function which will probably use a hardware sqrt computation. |
| 1325 | // This should be faster than the algorithm below. |
Jeff Cohen | b622c11 | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 1326 | if (magnitude < 52) { |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1327 | return APInt(BitWidth, |
Reid Spencer | d99feaf | 2007-03-01 05:39:56 +0000 | [diff] [blame] | 1328 | uint64_t(::round(::sqrt(double(isSingleWord()?VAL:pVal[0]))))); |
Jeff Cohen | b622c11 | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 1329 | } |
Reid Spencer | d99feaf | 2007-03-01 05:39:56 +0000 | [diff] [blame] | 1330 | |
| 1331 | // Okay, all the short cuts are exhausted. We must compute it. The following |
| 1332 | // is a classical Babylonian method for computing the square root. This code |
Sanjay Patel | 4cb54e0 | 2014-09-11 15:41:01 +0000 | [diff] [blame] | 1333 | // was adapted to APInt from a wikipedia article on such computations. |
Reid Spencer | d99feaf | 2007-03-01 05:39:56 +0000 | [diff] [blame] | 1334 | // See http://www.wikipedia.org/ and go to the page named |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1335 | // Calculate_an_integer_square_root. |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1336 | unsigned nbits = BitWidth, i = 4; |
Reid Spencer | d99feaf | 2007-03-01 05:39:56 +0000 | [diff] [blame] | 1337 | APInt testy(BitWidth, 16); |
| 1338 | APInt x_old(BitWidth, 1); |
| 1339 | APInt x_new(BitWidth, 0); |
| 1340 | APInt two(BitWidth, 2); |
| 1341 | |
| 1342 | // Select a good starting value using binary logarithms. |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1343 | for (;; i += 2, testy = testy.shl(2)) |
Reid Spencer | d99feaf | 2007-03-01 05:39:56 +0000 | [diff] [blame] | 1344 | if (i >= nbits || this->ule(testy)) { |
| 1345 | x_old = x_old.shl(i / 2); |
| 1346 | break; |
| 1347 | } |
| 1348 | |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1349 | // Use the Babylonian method to arrive at the integer square root: |
Reid Spencer | d99feaf | 2007-03-01 05:39:56 +0000 | [diff] [blame] | 1350 | for (;;) { |
| 1351 | x_new = (this->udiv(x_old) + x_old).udiv(two); |
| 1352 | if (x_old.ule(x_new)) |
| 1353 | break; |
| 1354 | x_old = x_new; |
| 1355 | } |
| 1356 | |
| 1357 | // Make sure we return the closest approximation |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1358 | // NOTE: The rounding calculation below is correct. It will produce an |
Reid Spencer | cf81756 | 2007-03-02 04:21:55 +0000 | [diff] [blame] | 1359 | // off-by-one discrepancy with results from pari/gp. That discrepancy has been |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1360 | // determined to be a rounding issue with pari/gp as it begins to use a |
Reid Spencer | cf81756 | 2007-03-02 04:21:55 +0000 | [diff] [blame] | 1361 | // floating point representation after 192 bits. There are no discrepancies |
| 1362 | // between this algorithm and pari/gp for bit widths < 192 bits. |
Reid Spencer | d99feaf | 2007-03-01 05:39:56 +0000 | [diff] [blame] | 1363 | APInt square(x_old * x_old); |
| 1364 | APInt nextSquare((x_old + 1) * (x_old +1)); |
| 1365 | if (this->ult(square)) |
| 1366 | return x_old; |
David Blaikie | 54c9462 | 2011-12-01 20:58:30 +0000 | [diff] [blame] | 1367 | assert(this->ule(nextSquare) && "Error in APInt::sqrt computation"); |
| 1368 | APInt midpoint((nextSquare - square).udiv(two)); |
| 1369 | APInt offset(*this - square); |
| 1370 | if (offset.ult(midpoint)) |
| 1371 | return x_old; |
Reid Spencer | d99feaf | 2007-03-01 05:39:56 +0000 | [diff] [blame] | 1372 | return x_old + 1; |
| 1373 | } |
| 1374 | |
Wojciech Matyjewicz | 41b744d | 2008-06-23 19:39:50 +0000 | [diff] [blame] | 1375 | /// Computes the multiplicative inverse of this APInt for a given modulo. The |
| 1376 | /// iterative extended Euclidean algorithm is used to solve for this value, |
| 1377 | /// however we simplify it to speed up calculating only the inverse, and take |
| 1378 | /// advantage of div+rem calculations. We also use some tricks to avoid copying |
| 1379 | /// (potentially large) APInts around. |
| 1380 | APInt APInt::multiplicativeInverse(const APInt& modulo) const { |
| 1381 | assert(ult(modulo) && "This APInt must be smaller than the modulo"); |
| 1382 | |
| 1383 | // Using the properties listed at the following web page (accessed 06/21/08): |
| 1384 | // http://www.numbertheory.org/php/euclid.html |
| 1385 | // (especially the properties numbered 3, 4 and 9) it can be proved that |
| 1386 | // BitWidth bits suffice for all the computations in the algorithm implemented |
| 1387 | // below. More precisely, this number of bits suffice if the multiplicative |
| 1388 | // inverse exists, but may not suffice for the general extended Euclidean |
| 1389 | // algorithm. |
| 1390 | |
| 1391 | APInt r[2] = { modulo, *this }; |
| 1392 | APInt t[2] = { APInt(BitWidth, 0), APInt(BitWidth, 1) }; |
| 1393 | APInt q(BitWidth, 0); |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1394 | |
Wojciech Matyjewicz | 41b744d | 2008-06-23 19:39:50 +0000 | [diff] [blame] | 1395 | unsigned i; |
| 1396 | for (i = 0; r[i^1] != 0; i ^= 1) { |
| 1397 | // An overview of the math without the confusing bit-flipping: |
| 1398 | // q = r[i-2] / r[i-1] |
| 1399 | // r[i] = r[i-2] % r[i-1] |
| 1400 | // t[i] = t[i-2] - t[i-1] * q |
| 1401 | udivrem(r[i], r[i^1], q, r[i]); |
| 1402 | t[i] -= t[i^1] * q; |
| 1403 | } |
| 1404 | |
| 1405 | // If this APInt and the modulo are not coprime, there is no multiplicative |
| 1406 | // inverse, so return 0. We check this by looking at the next-to-last |
| 1407 | // remainder, which is the gcd(*this,modulo) as calculated by the Euclidean |
| 1408 | // algorithm. |
| 1409 | if (r[i] != 1) |
| 1410 | return APInt(BitWidth, 0); |
| 1411 | |
| 1412 | // The next-to-last t is the multiplicative inverse. However, we are |
| 1413 | // interested in a positive inverse. Calcuate a positive one from a negative |
| 1414 | // one if necessary. A simple addition of the modulo suffices because |
Wojciech Matyjewicz | f0d21cd | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 1415 | // abs(t[i]) is known to be less than *this/2 (see the link above). |
Wojciech Matyjewicz | 41b744d | 2008-06-23 19:39:50 +0000 | [diff] [blame] | 1416 | return t[i].isNegative() ? t[i] + modulo : t[i]; |
| 1417 | } |
| 1418 | |
Jay Foad | fe0c648 | 2009-04-30 10:15:35 +0000 | [diff] [blame] | 1419 | /// Calculate the magic numbers required to implement a signed integer division |
| 1420 | /// by a constant as a sequence of multiplies, adds and shifts. Requires that |
| 1421 | /// the divisor not be 0, 1, or -1. Taken from "Hacker's Delight", Henry S. |
| 1422 | /// Warren, Jr., chapter 10. |
| 1423 | APInt::ms APInt::magic() const { |
| 1424 | const APInt& d = *this; |
| 1425 | unsigned p; |
| 1426 | APInt ad, anc, delta, q1, r1, q2, r2, t; |
Jay Foad | fe0c648 | 2009-04-30 10:15:35 +0000 | [diff] [blame] | 1427 | APInt signedMin = APInt::getSignedMinValue(d.getBitWidth()); |
Jay Foad | fe0c648 | 2009-04-30 10:15:35 +0000 | [diff] [blame] | 1428 | struct ms mag; |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1429 | |
Jay Foad | fe0c648 | 2009-04-30 10:15:35 +0000 | [diff] [blame] | 1430 | ad = d.abs(); |
| 1431 | t = signedMin + (d.lshr(d.getBitWidth() - 1)); |
| 1432 | anc = t - 1 - t.urem(ad); // absolute value of nc |
| 1433 | p = d.getBitWidth() - 1; // initialize p |
| 1434 | q1 = signedMin.udiv(anc); // initialize q1 = 2p/abs(nc) |
| 1435 | r1 = signedMin - q1*anc; // initialize r1 = rem(2p,abs(nc)) |
| 1436 | q2 = signedMin.udiv(ad); // initialize q2 = 2p/abs(d) |
| 1437 | r2 = signedMin - q2*ad; // initialize r2 = rem(2p,abs(d)) |
| 1438 | do { |
| 1439 | p = p + 1; |
| 1440 | q1 = q1<<1; // update q1 = 2p/abs(nc) |
| 1441 | r1 = r1<<1; // update r1 = rem(2p/abs(nc)) |
| 1442 | if (r1.uge(anc)) { // must be unsigned comparison |
| 1443 | q1 = q1 + 1; |
| 1444 | r1 = r1 - anc; |
| 1445 | } |
| 1446 | q2 = q2<<1; // update q2 = 2p/abs(d) |
| 1447 | r2 = r2<<1; // update r2 = rem(2p/abs(d)) |
| 1448 | if (r2.uge(ad)) { // must be unsigned comparison |
| 1449 | q2 = q2 + 1; |
| 1450 | r2 = r2 - ad; |
| 1451 | } |
| 1452 | delta = ad - r2; |
Cameron Zwarich | 8731d0c | 2011-02-21 00:22:02 +0000 | [diff] [blame] | 1453 | } while (q1.ult(delta) || (q1 == delta && r1 == 0)); |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1454 | |
Jay Foad | fe0c648 | 2009-04-30 10:15:35 +0000 | [diff] [blame] | 1455 | mag.m = q2 + 1; |
| 1456 | if (d.isNegative()) mag.m = -mag.m; // resulting magic number |
| 1457 | mag.s = p - d.getBitWidth(); // resulting shift |
| 1458 | return mag; |
| 1459 | } |
| 1460 | |
| 1461 | /// Calculate the magic numbers required to implement an unsigned integer |
| 1462 | /// division by a constant as a sequence of multiplies, adds and shifts. |
| 1463 | /// Requires that the divisor not be 0. Taken from "Hacker's Delight", Henry |
| 1464 | /// S. Warren, Jr., chapter 10. |
Benjamin Kramer | 09a51ba | 2011-03-17 20:39:06 +0000 | [diff] [blame] | 1465 | /// LeadingZeros can be used to simplify the calculation if the upper bits |
Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 1466 | /// of the divided value are known zero. |
Benjamin Kramer | 09a51ba | 2011-03-17 20:39:06 +0000 | [diff] [blame] | 1467 | APInt::mu APInt::magicu(unsigned LeadingZeros) const { |
Jay Foad | fe0c648 | 2009-04-30 10:15:35 +0000 | [diff] [blame] | 1468 | const APInt& d = *this; |
| 1469 | unsigned p; |
| 1470 | APInt nc, delta, q1, r1, q2, r2; |
| 1471 | struct mu magu; |
| 1472 | magu.a = 0; // initialize "add" indicator |
Benjamin Kramer | 09a51ba | 2011-03-17 20:39:06 +0000 | [diff] [blame] | 1473 | APInt allOnes = APInt::getAllOnesValue(d.getBitWidth()).lshr(LeadingZeros); |
Jay Foad | fe0c648 | 2009-04-30 10:15:35 +0000 | [diff] [blame] | 1474 | APInt signedMin = APInt::getSignedMinValue(d.getBitWidth()); |
| 1475 | APInt signedMax = APInt::getSignedMaxValue(d.getBitWidth()); |
| 1476 | |
Benjamin Kramer | 3aab6a8 | 2012-07-11 18:31:59 +0000 | [diff] [blame] | 1477 | nc = allOnes - (allOnes - d).urem(d); |
Jay Foad | fe0c648 | 2009-04-30 10:15:35 +0000 | [diff] [blame] | 1478 | p = d.getBitWidth() - 1; // initialize p |
| 1479 | q1 = signedMin.udiv(nc); // initialize q1 = 2p/nc |
| 1480 | r1 = signedMin - q1*nc; // initialize r1 = rem(2p,nc) |
| 1481 | q2 = signedMax.udiv(d); // initialize q2 = (2p-1)/d |
| 1482 | r2 = signedMax - q2*d; // initialize r2 = rem((2p-1),d) |
| 1483 | do { |
| 1484 | p = p + 1; |
| 1485 | if (r1.uge(nc - r1)) { |
| 1486 | q1 = q1 + q1 + 1; // update q1 |
| 1487 | r1 = r1 + r1 - nc; // update r1 |
| 1488 | } |
| 1489 | else { |
| 1490 | q1 = q1+q1; // update q1 |
| 1491 | r1 = r1+r1; // update r1 |
| 1492 | } |
| 1493 | if ((r2 + 1).uge(d - r2)) { |
| 1494 | if (q2.uge(signedMax)) magu.a = 1; |
| 1495 | q2 = q2+q2 + 1; // update q2 |
| 1496 | r2 = r2+r2 + 1 - d; // update r2 |
| 1497 | } |
| 1498 | else { |
| 1499 | if (q2.uge(signedMin)) magu.a = 1; |
| 1500 | q2 = q2+q2; // update q2 |
| 1501 | r2 = r2+r2 + 1; // update r2 |
| 1502 | } |
| 1503 | delta = d - 1 - r2; |
| 1504 | } while (p < d.getBitWidth()*2 && |
| 1505 | (q1.ult(delta) || (q1 == delta && r1 == 0))); |
| 1506 | magu.m = q2 + 1; // resulting magic number |
| 1507 | magu.s = p - d.getBitWidth(); // resulting shift |
| 1508 | return magu; |
| 1509 | } |
| 1510 | |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1511 | /// Implementation of Knuth's Algorithm D (Division of nonnegative integers) |
| 1512 | /// from "Art of Computer Programming, Volume 2", section 4.3.1, p. 272. The |
| 1513 | /// variables here have the same names as in the algorithm. Comments explain |
| 1514 | /// the algorithm and any deviation from it. |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1515 | static void KnuthDiv(unsigned *u, unsigned *v, unsigned *q, unsigned* r, |
| 1516 | unsigned m, unsigned n) { |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1517 | assert(u && "Must provide dividend"); |
| 1518 | assert(v && "Must provide divisor"); |
| 1519 | assert(q && "Must provide quotient"); |
Yaron Keren | 39fc5a6 | 2015-03-26 19:45:19 +0000 | [diff] [blame] | 1520 | assert(u != v && u != q && v != q && "Must use different memory"); |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1521 | assert(n>1 && "n must be > 1"); |
| 1522 | |
Yaron Keren | 39fc5a6 | 2015-03-26 19:45:19 +0000 | [diff] [blame] | 1523 | // b denotes the base of the number system. In our case b is 2^32. |
| 1524 | LLVM_CONSTEXPR uint64_t b = uint64_t(1) << 32; |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1525 | |
David Greene | f32fcb4 | 2010-01-05 01:28:52 +0000 | [diff] [blame] | 1526 | DEBUG(dbgs() << "KnuthDiv: m=" << m << " n=" << n << '\n'); |
| 1527 | DEBUG(dbgs() << "KnuthDiv: original:"); |
| 1528 | DEBUG(for (int i = m+n; i >=0; i--) dbgs() << " " << u[i]); |
| 1529 | DEBUG(dbgs() << " by"); |
| 1530 | DEBUG(for (int i = n; i >0; i--) dbgs() << " " << v[i-1]); |
| 1531 | DEBUG(dbgs() << '\n'); |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1532 | // D1. [Normalize.] Set d = b / (v[n-1] + 1) and multiply all the digits of |
| 1533 | // u and v by d. Note that we have taken Knuth's advice here to use a power |
| 1534 | // of 2 value for d such that d * v[n-1] >= b/2 (b is the base). A power of |
| 1535 | // 2 allows us to shift instead of multiply and it is easy to determine the |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1536 | // shift amount from the leading zeros. We are basically normalizing the u |
| 1537 | // and v so that its high bits are shifted to the top of v's range without |
| 1538 | // overflow. Note that this can require an extra word in u so that u must |
| 1539 | // be of length m+n+1. |
Michael J. Spencer | df1ecbd7 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 1540 | unsigned shift = countLeadingZeros(v[n-1]); |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1541 | unsigned v_carry = 0; |
| 1542 | unsigned u_carry = 0; |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1543 | if (shift) { |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1544 | for (unsigned i = 0; i < m+n; ++i) { |
| 1545 | unsigned u_tmp = u[i] >> (32 - shift); |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1546 | u[i] = (u[i] << shift) | u_carry; |
| 1547 | u_carry = u_tmp; |
Reid Spencer | 100502d | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 1548 | } |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1549 | for (unsigned i = 0; i < n; ++i) { |
| 1550 | unsigned v_tmp = v[i] >> (32 - shift); |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1551 | v[i] = (v[i] << shift) | v_carry; |
| 1552 | v_carry = v_tmp; |
| 1553 | } |
| 1554 | } |
| 1555 | u[m+n] = u_carry; |
Yaron Keren | 39fc5a6 | 2015-03-26 19:45:19 +0000 | [diff] [blame] | 1556 | |
David Greene | f32fcb4 | 2010-01-05 01:28:52 +0000 | [diff] [blame] | 1557 | DEBUG(dbgs() << "KnuthDiv: normal:"); |
| 1558 | DEBUG(for (int i = m+n; i >=0; i--) dbgs() << " " << u[i]); |
| 1559 | DEBUG(dbgs() << " by"); |
| 1560 | DEBUG(for (int i = n; i >0; i--) dbgs() << " " << v[i-1]); |
| 1561 | DEBUG(dbgs() << '\n'); |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1562 | |
| 1563 | // D2. [Initialize j.] Set j to m. This is the loop counter over the places. |
| 1564 | int j = m; |
| 1565 | do { |
David Greene | f32fcb4 | 2010-01-05 01:28:52 +0000 | [diff] [blame] | 1566 | DEBUG(dbgs() << "KnuthDiv: quotient digit #" << j << '\n'); |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1567 | // D3. [Calculate q'.]. |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1568 | // Set qp = (u[j+n]*b + u[j+n-1]) / v[n-1]. (qp=qprime=q') |
| 1569 | // Set rp = (u[j+n]*b + u[j+n-1]) % v[n-1]. (rp=rprime=r') |
| 1570 | // Now test if qp == b or qp*v[n-2] > b*rp + u[j+n-2]; if so, decrease |
| 1571 | // qp by 1, inrease rp by v[n-1], and repeat this test if rp < b. The test |
| 1572 | // on v[n-2] determines at high speed most of the cases in which the trial |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1573 | // value qp is one too large, and it eliminates all cases where qp is two |
| 1574 | // too large. |
Reid Spencer | cb292e4 | 2007-02-23 01:57:13 +0000 | [diff] [blame] | 1575 | uint64_t dividend = ((uint64_t(u[j+n]) << 32) + u[j+n-1]); |
David Greene | f32fcb4 | 2010-01-05 01:28:52 +0000 | [diff] [blame] | 1576 | DEBUG(dbgs() << "KnuthDiv: dividend == " << dividend << '\n'); |
Reid Spencer | cb292e4 | 2007-02-23 01:57:13 +0000 | [diff] [blame] | 1577 | uint64_t qp = dividend / v[n-1]; |
| 1578 | uint64_t rp = dividend % v[n-1]; |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1579 | if (qp == b || qp*v[n-2] > b*rp + u[j+n-2]) { |
| 1580 | qp--; |
| 1581 | rp += v[n-1]; |
Reid Spencer | df6cf5a | 2007-02-24 10:01:42 +0000 | [diff] [blame] | 1582 | if (rp < b && (qp == b || qp*v[n-2] > b*rp + u[j+n-2])) |
Reid Spencer | a5e0d20 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1583 | qp--; |
Reid Spencer | cb292e4 | 2007-02-23 01:57:13 +0000 | [diff] [blame] | 1584 | } |
David Greene | f32fcb4 | 2010-01-05 01:28:52 +0000 | [diff] [blame] | 1585 | DEBUG(dbgs() << "KnuthDiv: qp == " << qp << ", rp == " << rp << '\n'); |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1586 | |
Reid Spencer | cb292e4 | 2007-02-23 01:57:13 +0000 | [diff] [blame] | 1587 | // D4. [Multiply and subtract.] Replace (u[j+n]u[j+n-1]...u[j]) with |
| 1588 | // (u[j+n]u[j+n-1]..u[j]) - qp * (v[n-1]...v[1]v[0]). This computation |
| 1589 | // consists of a simple multiplication by a one-place number, combined with |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1590 | // a subtraction. |
Yaron Keren | 39fc5a6 | 2015-03-26 19:45:19 +0000 | [diff] [blame] | 1591 | // The digits (u[j+n]...u[j]) should be kept positive; if the result of |
| 1592 | // this step is actually negative, (u[j+n]...u[j]) should be left as the |
| 1593 | // true value plus b**(n+1), namely as the b's complement of |
| 1594 | // the true value, and a "borrow" to the left should be remembered. |
Pawel Bylica | 86ac447 | 2015-04-24 07:38:39 +0000 | [diff] [blame] | 1595 | int64_t borrow = 0; |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1596 | for (unsigned i = 0; i < n; ++i) { |
Pawel Bylica | 86ac447 | 2015-04-24 07:38:39 +0000 | [diff] [blame] | 1597 | uint64_t p = uint64_t(qp) * uint64_t(v[i]); |
| 1598 | int64_t subres = int64_t(u[j+i]) - borrow - (unsigned)p; |
| 1599 | u[j+i] = (unsigned)subres; |
| 1600 | borrow = (p >> 32) - (subres >> 32); |
| 1601 | DEBUG(dbgs() << "KnuthDiv: u[j+i] = " << u[j+i] |
Daniel Dunbar | 763ace9 | 2009-07-13 05:27:30 +0000 | [diff] [blame] | 1602 | << ", borrow = " << borrow << '\n'); |
Reid Spencer | a5e0d20 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1603 | } |
Pawel Bylica | 86ac447 | 2015-04-24 07:38:39 +0000 | [diff] [blame] | 1604 | bool isNeg = u[j+n] < borrow; |
| 1605 | u[j+n] -= (unsigned)borrow; |
| 1606 | |
David Greene | f32fcb4 | 2010-01-05 01:28:52 +0000 | [diff] [blame] | 1607 | DEBUG(dbgs() << "KnuthDiv: after subtraction:"); |
| 1608 | DEBUG(for (int i = m+n; i >=0; i--) dbgs() << " " << u[i]); |
| 1609 | DEBUG(dbgs() << '\n'); |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1610 | |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1611 | // D5. [Test remainder.] Set q[j] = qp. If the result of step D4 was |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1612 | // negative, go to step D6; otherwise go on to step D7. |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1613 | q[j] = (unsigned)qp; |
Reid Spencer | aa8dcfe | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 1614 | if (isNeg) { |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1615 | // D6. [Add back]. The probability that this step is necessary is very |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1616 | // small, on the order of only 2/b. Make sure that test data accounts for |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1617 | // this possibility. Decrease q[j] by 1 |
Reid Spencer | cb292e4 | 2007-02-23 01:57:13 +0000 | [diff] [blame] | 1618 | q[j]--; |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1619 | // and add (0v[n-1]...v[1]v[0]) to (u[j+n]u[j+n-1]...u[j+1]u[j]). |
| 1620 | // A carry will occur to the left of u[j+n], and it should be ignored |
Reid Spencer | cb292e4 | 2007-02-23 01:57:13 +0000 | [diff] [blame] | 1621 | // since it cancels with the borrow that occurred in D4. |
| 1622 | bool carry = false; |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1623 | for (unsigned i = 0; i < n; i++) { |
| 1624 | unsigned limit = std::min(u[j+i],v[i]); |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1625 | u[j+i] += v[i] + carry; |
Reid Spencer | a5e0d20 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1626 | carry = u[j+i] < limit || (carry && u[j+i] == limit); |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1627 | } |
Reid Spencer | a5e0d20 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1628 | u[j+n] += carry; |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1629 | } |
David Greene | f32fcb4 | 2010-01-05 01:28:52 +0000 | [diff] [blame] | 1630 | DEBUG(dbgs() << "KnuthDiv: after correction:"); |
Yaron Keren | 39fc5a6 | 2015-03-26 19:45:19 +0000 | [diff] [blame] | 1631 | DEBUG(for (int i = m+n; i >=0; i--) dbgs() << " " << u[i]); |
David Greene | f32fcb4 | 2010-01-05 01:28:52 +0000 | [diff] [blame] | 1632 | DEBUG(dbgs() << "\nKnuthDiv: digit result = " << q[j] << '\n'); |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1633 | |
Reid Spencer | cb292e4 | 2007-02-23 01:57:13 +0000 | [diff] [blame] | 1634 | // D7. [Loop on j.] Decrease j by one. Now if j >= 0, go back to D3. |
| 1635 | } while (--j >= 0); |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1636 | |
David Greene | f32fcb4 | 2010-01-05 01:28:52 +0000 | [diff] [blame] | 1637 | DEBUG(dbgs() << "KnuthDiv: quotient:"); |
| 1638 | DEBUG(for (int i = m; i >=0; i--) dbgs() <<" " << q[i]); |
| 1639 | DEBUG(dbgs() << '\n'); |
Reid Spencer | a5e0d20 | 2007-02-24 03:58:46 +0000 | [diff] [blame] | 1640 | |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1641 | // D8. [Unnormalize]. Now q[...] is the desired quotient, and the desired |
| 1642 | // remainder may be obtained by dividing u[...] by d. If r is non-null we |
| 1643 | // compute the remainder (urem uses this). |
| 1644 | if (r) { |
| 1645 | // The value d is expressed by the "shift" value above since we avoided |
| 1646 | // multiplication by d by using a shift left. So, all we have to do is |
| 1647 | // shift right here. In order to mak |
Reid Spencer | 468ad911 | 2007-02-24 20:38:01 +0000 | [diff] [blame] | 1648 | if (shift) { |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1649 | unsigned carry = 0; |
David Greene | f32fcb4 | 2010-01-05 01:28:52 +0000 | [diff] [blame] | 1650 | DEBUG(dbgs() << "KnuthDiv: remainder:"); |
Reid Spencer | 468ad911 | 2007-02-24 20:38:01 +0000 | [diff] [blame] | 1651 | for (int i = n-1; i >= 0; i--) { |
| 1652 | r[i] = (u[i] >> shift) | carry; |
| 1653 | carry = u[i] << (32 - shift); |
David Greene | f32fcb4 | 2010-01-05 01:28:52 +0000 | [diff] [blame] | 1654 | DEBUG(dbgs() << " " << r[i]); |
Reid Spencer | 468ad911 | 2007-02-24 20:38:01 +0000 | [diff] [blame] | 1655 | } |
| 1656 | } else { |
| 1657 | for (int i = n-1; i >= 0; i--) { |
| 1658 | r[i] = u[i]; |
David Greene | f32fcb4 | 2010-01-05 01:28:52 +0000 | [diff] [blame] | 1659 | DEBUG(dbgs() << " " << r[i]); |
Reid Spencer | 468ad911 | 2007-02-24 20:38:01 +0000 | [diff] [blame] | 1660 | } |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1661 | } |
David Greene | f32fcb4 | 2010-01-05 01:28:52 +0000 | [diff] [blame] | 1662 | DEBUG(dbgs() << '\n'); |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1663 | } |
David Greene | f32fcb4 | 2010-01-05 01:28:52 +0000 | [diff] [blame] | 1664 | DEBUG(dbgs() << '\n'); |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1665 | } |
| 1666 | |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1667 | void APInt::divide(const APInt LHS, unsigned lhsWords, |
| 1668 | const APInt &RHS, unsigned rhsWords, |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1669 | APInt *Quotient, APInt *Remainder) |
| 1670 | { |
| 1671 | assert(lhsWords >= rhsWords && "Fractional result"); |
| 1672 | |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1673 | // First, compose the values into an array of 32-bit words instead of |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1674 | // 64-bit words. This is a necessity of both the "short division" algorithm |
Dan Gohman | 4a61882 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 1675 | // and the Knuth "classical algorithm" which requires there to be native |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1676 | // operations for +, -, and * on an m bit value with an m*2 bit result. We |
| 1677 | // can't use 64-bit operands here because we don't have native results of |
| 1678 | // 128-bits. Furthermore, casting the 64-bit values to 32-bit values won't |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1679 | // work on large-endian machines. |
Dan Gohman | cff6953 | 2009-04-01 18:45:54 +0000 | [diff] [blame] | 1680 | uint64_t mask = ~0ull >> (sizeof(unsigned)*CHAR_BIT); |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1681 | unsigned n = rhsWords * 2; |
| 1682 | unsigned m = (lhsWords * 2) - n; |
Reid Spencer | 522ca7c | 2007-02-25 01:56:07 +0000 | [diff] [blame] | 1683 | |
| 1684 | // Allocate space for the temporary values we need either on the stack, if |
| 1685 | // it will fit, or on the heap if it won't. |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1686 | unsigned SPACE[128]; |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 1687 | unsigned *U = nullptr; |
| 1688 | unsigned *V = nullptr; |
| 1689 | unsigned *Q = nullptr; |
| 1690 | unsigned *R = nullptr; |
Reid Spencer | 522ca7c | 2007-02-25 01:56:07 +0000 | [diff] [blame] | 1691 | if ((Remainder?4:3)*n+2*m+1 <= 128) { |
| 1692 | U = &SPACE[0]; |
| 1693 | V = &SPACE[m+n+1]; |
| 1694 | Q = &SPACE[(m+n+1) + n]; |
| 1695 | if (Remainder) |
| 1696 | R = &SPACE[(m+n+1) + n + (m+n)]; |
| 1697 | } else { |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1698 | U = new unsigned[m + n + 1]; |
| 1699 | V = new unsigned[n]; |
| 1700 | Q = new unsigned[m+n]; |
Reid Spencer | 522ca7c | 2007-02-25 01:56:07 +0000 | [diff] [blame] | 1701 | if (Remainder) |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1702 | R = new unsigned[n]; |
Reid Spencer | 522ca7c | 2007-02-25 01:56:07 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | // Initialize the dividend |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1706 | memset(U, 0, (m+n+1)*sizeof(unsigned)); |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1707 | for (unsigned i = 0; i < lhsWords; ++i) { |
Reid Spencer | 867b406 | 2007-02-22 00:58:45 +0000 | [diff] [blame] | 1708 | uint64_t tmp = (LHS.getNumWords() == 1 ? LHS.VAL : LHS.pVal[i]); |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1709 | U[i * 2] = (unsigned)(tmp & mask); |
Dan Gohman | cff6953 | 2009-04-01 18:45:54 +0000 | [diff] [blame] | 1710 | U[i * 2 + 1] = (unsigned)(tmp >> (sizeof(unsigned)*CHAR_BIT)); |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1711 | } |
| 1712 | U[m+n] = 0; // this extra word is for "spill" in the Knuth algorithm. |
| 1713 | |
Reid Spencer | 522ca7c | 2007-02-25 01:56:07 +0000 | [diff] [blame] | 1714 | // Initialize the divisor |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1715 | memset(V, 0, (n)*sizeof(unsigned)); |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1716 | for (unsigned i = 0; i < rhsWords; ++i) { |
Reid Spencer | 867b406 | 2007-02-22 00:58:45 +0000 | [diff] [blame] | 1717 | uint64_t tmp = (RHS.getNumWords() == 1 ? RHS.VAL : RHS.pVal[i]); |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1718 | V[i * 2] = (unsigned)(tmp & mask); |
Dan Gohman | cff6953 | 2009-04-01 18:45:54 +0000 | [diff] [blame] | 1719 | V[i * 2 + 1] = (unsigned)(tmp >> (sizeof(unsigned)*CHAR_BIT)); |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
Reid Spencer | 522ca7c | 2007-02-25 01:56:07 +0000 | [diff] [blame] | 1722 | // initialize the quotient and remainder |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1723 | memset(Q, 0, (m+n) * sizeof(unsigned)); |
Reid Spencer | 522ca7c | 2007-02-25 01:56:07 +0000 | [diff] [blame] | 1724 | if (Remainder) |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1725 | memset(R, 0, n * sizeof(unsigned)); |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1726 | |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1727 | // Now, adjust m and n for the Knuth division. n is the number of words in |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1728 | // the divisor. m is the number of words by which the dividend exceeds the |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1729 | // divisor (i.e. m+n is the length of the dividend). These sizes must not |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1730 | // contain any zero words or the Knuth algorithm fails. |
| 1731 | for (unsigned i = n; i > 0 && V[i-1] == 0; i--) { |
| 1732 | n--; |
| 1733 | m++; |
| 1734 | } |
| 1735 | for (unsigned i = m+n; i > 0 && U[i-1] == 0; i--) |
| 1736 | m--; |
| 1737 | |
| 1738 | // If we're left with only a single word for the divisor, Knuth doesn't work |
| 1739 | // so we implement the short division algorithm here. This is much simpler |
| 1740 | // and faster because we are certain that we can divide a 64-bit quantity |
| 1741 | // by a 32-bit quantity at hardware speed and short division is simply a |
| 1742 | // series of such operations. This is just like doing short division but we |
| 1743 | // are using base 2^32 instead of base 10. |
| 1744 | assert(n != 0 && "Divide by zero?"); |
| 1745 | if (n == 1) { |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1746 | unsigned divisor = V[0]; |
| 1747 | unsigned remainder = 0; |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1748 | for (int i = m+n-1; i >= 0; i--) { |
| 1749 | uint64_t partial_dividend = uint64_t(remainder) << 32 | U[i]; |
| 1750 | if (partial_dividend == 0) { |
| 1751 | Q[i] = 0; |
| 1752 | remainder = 0; |
| 1753 | } else if (partial_dividend < divisor) { |
| 1754 | Q[i] = 0; |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1755 | remainder = (unsigned)partial_dividend; |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1756 | } else if (partial_dividend == divisor) { |
| 1757 | Q[i] = 1; |
| 1758 | remainder = 0; |
| 1759 | } else { |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1760 | Q[i] = (unsigned)(partial_dividend / divisor); |
| 1761 | remainder = (unsigned)(partial_dividend - (Q[i] * divisor)); |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1762 | } |
| 1763 | } |
| 1764 | if (R) |
| 1765 | R[0] = remainder; |
| 1766 | } else { |
| 1767 | // Now we're ready to invoke the Knuth classical divide algorithm. In this |
| 1768 | // case n > 1. |
| 1769 | KnuthDiv(U, V, Q, R, m, n); |
| 1770 | } |
| 1771 | |
| 1772 | // If the caller wants the quotient |
| 1773 | if (Quotient) { |
| 1774 | // Set up the Quotient value's memory. |
| 1775 | if (Quotient->BitWidth != LHS.BitWidth) { |
| 1776 | if (Quotient->isSingleWord()) |
| 1777 | Quotient->VAL = 0; |
| 1778 | else |
Reid Spencer | 7c16cd2 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 1779 | delete [] Quotient->pVal; |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1780 | Quotient->BitWidth = LHS.BitWidth; |
| 1781 | if (!Quotient->isSingleWord()) |
Reid Spencer | 58a6a43 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 1782 | Quotient->pVal = getClearedMemory(Quotient->getNumWords()); |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1783 | } else |
Jay Foad | 25a5e4c | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 1784 | Quotient->clearAllBits(); |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1785 | |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1786 | // The quotient is in Q. Reconstitute the quotient into Quotient's low |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1787 | // order words. |
Yaron Keren | 39fc5a6 | 2015-03-26 19:45:19 +0000 | [diff] [blame] | 1788 | // This case is currently dead as all users of divide() handle trivial cases |
| 1789 | // earlier. |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1790 | if (lhsWords == 1) { |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1791 | uint64_t tmp = |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1792 | uint64_t(Q[0]) | (uint64_t(Q[1]) << (APINT_BITS_PER_WORD / 2)); |
| 1793 | if (Quotient->isSingleWord()) |
| 1794 | Quotient->VAL = tmp; |
| 1795 | else |
| 1796 | Quotient->pVal[0] = tmp; |
| 1797 | } else { |
| 1798 | assert(!Quotient->isSingleWord() && "Quotient APInt not large enough"); |
| 1799 | for (unsigned i = 0; i < lhsWords; ++i) |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1800 | Quotient->pVal[i] = |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1801 | uint64_t(Q[i*2]) | (uint64_t(Q[i*2+1]) << (APINT_BITS_PER_WORD / 2)); |
| 1802 | } |
| 1803 | } |
| 1804 | |
| 1805 | // If the caller wants the remainder |
| 1806 | if (Remainder) { |
| 1807 | // Set up the Remainder value's memory. |
| 1808 | if (Remainder->BitWidth != RHS.BitWidth) { |
| 1809 | if (Remainder->isSingleWord()) |
| 1810 | Remainder->VAL = 0; |
| 1811 | else |
Reid Spencer | 7c16cd2 | 2007-02-26 23:38:21 +0000 | [diff] [blame] | 1812 | delete [] Remainder->pVal; |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1813 | Remainder->BitWidth = RHS.BitWidth; |
| 1814 | if (!Remainder->isSingleWord()) |
Reid Spencer | 58a6a43 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 1815 | Remainder->pVal = getClearedMemory(Remainder->getNumWords()); |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1816 | } else |
Jay Foad | 25a5e4c | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 1817 | Remainder->clearAllBits(); |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1818 | |
| 1819 | // The remainder is in R. Reconstitute the remainder into Remainder's low |
| 1820 | // order words. |
| 1821 | if (rhsWords == 1) { |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1822 | uint64_t tmp = |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1823 | uint64_t(R[0]) | (uint64_t(R[1]) << (APINT_BITS_PER_WORD / 2)); |
| 1824 | if (Remainder->isSingleWord()) |
| 1825 | Remainder->VAL = tmp; |
| 1826 | else |
| 1827 | Remainder->pVal[0] = tmp; |
| 1828 | } else { |
| 1829 | assert(!Remainder->isSingleWord() && "Remainder APInt not large enough"); |
| 1830 | for (unsigned i = 0; i < rhsWords; ++i) |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1831 | Remainder->pVal[i] = |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1832 | uint64_t(R[i*2]) | (uint64_t(R[i*2+1]) << (APINT_BITS_PER_WORD / 2)); |
| 1833 | } |
| 1834 | } |
| 1835 | |
| 1836 | // Clean up the memory we allocated. |
Reid Spencer | 522ca7c | 2007-02-25 01:56:07 +0000 | [diff] [blame] | 1837 | if (U != &SPACE[0]) { |
| 1838 | delete [] U; |
| 1839 | delete [] V; |
| 1840 | delete [] Q; |
| 1841 | delete [] R; |
| 1842 | } |
Reid Spencer | 100502d | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 1843 | } |
| 1844 | |
Reid Spencer | 1d07212 | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 1845 | APInt APInt::udiv(const APInt& RHS) const { |
Reid Spencer | a32372d1 | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 1846 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Reid Spencer | 3986776 | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1847 | |
| 1848 | // First, deal with the easy case |
| 1849 | if (isSingleWord()) { |
| 1850 | assert(RHS.VAL != 0 && "Divide by zero?"); |
| 1851 | return APInt(BitWidth, VAL / RHS.VAL); |
Zhou Sheng | fbf61ea | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1852 | } |
Reid Spencer | 3986776 | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1853 | |
Reid Spencer | 3986776 | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1854 | // Get some facts about the LHS and RHS number of bits and words |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1855 | unsigned rhsBits = RHS.getActiveBits(); |
| 1856 | unsigned rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1); |
Reid Spencer | 3986776 | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1857 | assert(rhsWords && "Divided by zero???"); |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1858 | unsigned lhsBits = this->getActiveBits(); |
| 1859 | unsigned lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1); |
Reid Spencer | 3986776 | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1860 | |
| 1861 | // Deal with some degenerate cases |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1862 | if (!lhsWords) |
Reid Spencer | 58a6a43 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 1863 | // 0 / X ===> 0 |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1864 | return APInt(BitWidth, 0); |
Reid Spencer | 58a6a43 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 1865 | else if (lhsWords < rhsWords || this->ult(RHS)) { |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1866 | // X / Y ===> 0, iff X < Y |
Reid Spencer | 58a6a43 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 1867 | return APInt(BitWidth, 0); |
| 1868 | } else if (*this == RHS) { |
| 1869 | // X / X ===> 1 |
| 1870 | return APInt(BitWidth, 1); |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1871 | } else if (lhsWords == 1 && rhsWords == 1) { |
Reid Spencer | 3986776 | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1872 | // All high words are zero, just use native divide |
Reid Spencer | 58a6a43 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 1873 | return APInt(BitWidth, this->pVal[0] / RHS.pVal[0]); |
Reid Spencer | 3986776 | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1874 | } |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1875 | |
| 1876 | // We have to compute it the hard way. Invoke the Knuth divide algorithm. |
| 1877 | APInt Quotient(1,0); // to hold result. |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 1878 | divide(*this, lhsWords, RHS, rhsWords, &Quotient, nullptr); |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1879 | return Quotient; |
Zhou Sheng | fbf61ea | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1880 | } |
| 1881 | |
Jakub Staszak | 6605c60 | 2013-02-20 00:17:42 +0000 | [diff] [blame] | 1882 | APInt APInt::sdiv(const APInt &RHS) const { |
| 1883 | if (isNegative()) { |
| 1884 | if (RHS.isNegative()) |
| 1885 | return (-(*this)).udiv(-RHS); |
| 1886 | return -((-(*this)).udiv(RHS)); |
| 1887 | } |
| 1888 | if (RHS.isNegative()) |
| 1889 | return -(this->udiv(-RHS)); |
| 1890 | return this->udiv(RHS); |
| 1891 | } |
| 1892 | |
Reid Spencer | 1d07212 | 2007-02-16 22:36:51 +0000 | [diff] [blame] | 1893 | APInt APInt::urem(const APInt& RHS) const { |
Reid Spencer | a32372d1 | 2007-02-17 00:18:01 +0000 | [diff] [blame] | 1894 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
Reid Spencer | 3986776 | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1895 | if (isSingleWord()) { |
| 1896 | assert(RHS.VAL != 0 && "Remainder by zero?"); |
| 1897 | return APInt(BitWidth, VAL % RHS.VAL); |
Zhou Sheng | fbf61ea | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1898 | } |
Reid Spencer | 3986776 | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1899 | |
Reid Spencer | 58a6a43 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 1900 | // Get some facts about the LHS |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1901 | unsigned lhsBits = getActiveBits(); |
| 1902 | unsigned lhsWords = !lhsBits ? 0 : (whichWord(lhsBits - 1) + 1); |
Reid Spencer | 3986776 | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1903 | |
| 1904 | // Get some facts about the RHS |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1905 | unsigned rhsBits = RHS.getActiveBits(); |
| 1906 | unsigned rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1); |
Reid Spencer | 3986776 | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1907 | assert(rhsWords && "Performing remainder operation by zero ???"); |
| 1908 | |
Reid Spencer | 3986776 | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1909 | // Check the degenerate cases |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1910 | if (lhsWords == 0) { |
Reid Spencer | 58a6a43 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 1911 | // 0 % Y ===> 0 |
| 1912 | return APInt(BitWidth, 0); |
| 1913 | } else if (lhsWords < rhsWords || this->ult(RHS)) { |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1914 | // X % Y ===> X, iff X < Y |
Reid Spencer | 58a6a43 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 1915 | return *this; |
| 1916 | } else if (*this == RHS) { |
Reid Spencer | 3986776 | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1917 | // X % X == 0; |
Reid Spencer | 58a6a43 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 1918 | return APInt(BitWidth, 0); |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1919 | } else if (lhsWords == 1) { |
Reid Spencer | 3986776 | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1920 | // All high words are zero, just use native remainder |
Reid Spencer | 58a6a43 | 2007-02-21 08:21:52 +0000 | [diff] [blame] | 1921 | return APInt(BitWidth, pVal[0] % RHS.pVal[0]); |
Reid Spencer | 3986776 | 2007-02-17 02:07:07 +0000 | [diff] [blame] | 1922 | } |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1923 | |
Reid Spencer | 4c50b52 | 2007-05-13 23:44:59 +0000 | [diff] [blame] | 1924 | // We have to compute it the hard way. Invoke the Knuth divide algorithm. |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1925 | APInt Remainder(1,0); |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 1926 | divide(*this, lhsWords, RHS, rhsWords, nullptr, &Remainder); |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 1927 | return Remainder; |
Zhou Sheng | fbf61ea | 2007-02-08 14:35:19 +0000 | [diff] [blame] | 1928 | } |
Reid Spencer | 100502d | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 1929 | |
Jakub Staszak | 6605c60 | 2013-02-20 00:17:42 +0000 | [diff] [blame] | 1930 | APInt APInt::srem(const APInt &RHS) const { |
| 1931 | if (isNegative()) { |
| 1932 | if (RHS.isNegative()) |
| 1933 | return -((-(*this)).urem(-RHS)); |
| 1934 | return -((-(*this)).urem(RHS)); |
| 1935 | } |
| 1936 | if (RHS.isNegative()) |
| 1937 | return this->urem(-RHS); |
| 1938 | return this->urem(RHS); |
| 1939 | } |
| 1940 | |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1941 | void APInt::udivrem(const APInt &LHS, const APInt &RHS, |
Reid Spencer | 4c50b52 | 2007-05-13 23:44:59 +0000 | [diff] [blame] | 1942 | APInt &Quotient, APInt &Remainder) { |
David Majnemer | 7f03920 | 2014-12-14 09:41:56 +0000 | [diff] [blame] | 1943 | assert(LHS.BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
| 1944 | |
| 1945 | // First, deal with the easy case |
| 1946 | if (LHS.isSingleWord()) { |
| 1947 | assert(RHS.VAL != 0 && "Divide by zero?"); |
| 1948 | uint64_t QuotVal = LHS.VAL / RHS.VAL; |
| 1949 | uint64_t RemVal = LHS.VAL % RHS.VAL; |
| 1950 | Quotient = APInt(LHS.BitWidth, QuotVal); |
| 1951 | Remainder = APInt(LHS.BitWidth, RemVal); |
| 1952 | return; |
| 1953 | } |
| 1954 | |
Reid Spencer | 4c50b52 | 2007-05-13 23:44:59 +0000 | [diff] [blame] | 1955 | // Get some size facts about the dividend and divisor |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1956 | unsigned lhsBits = LHS.getActiveBits(); |
| 1957 | unsigned lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1); |
| 1958 | unsigned rhsBits = RHS.getActiveBits(); |
| 1959 | unsigned rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1); |
Reid Spencer | 4c50b52 | 2007-05-13 23:44:59 +0000 | [diff] [blame] | 1960 | |
| 1961 | // Check the degenerate cases |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1962 | if (lhsWords == 0) { |
Reid Spencer | 4c50b52 | 2007-05-13 23:44:59 +0000 | [diff] [blame] | 1963 | Quotient = 0; // 0 / Y ===> 0 |
| 1964 | Remainder = 0; // 0 % Y ===> 0 |
| 1965 | return; |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1966 | } |
| 1967 | |
| 1968 | if (lhsWords < rhsWords || LHS.ult(RHS)) { |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1969 | Remainder = LHS; // X % Y ===> X, iff X < Y |
| 1970 | Quotient = 0; // X / Y ===> 0, iff X < Y |
Reid Spencer | 4c50b52 | 2007-05-13 23:44:59 +0000 | [diff] [blame] | 1971 | return; |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1972 | } |
| 1973 | |
Reid Spencer | 4c50b52 | 2007-05-13 23:44:59 +0000 | [diff] [blame] | 1974 | if (LHS == RHS) { |
| 1975 | Quotient = 1; // X / X ===> 1 |
| 1976 | Remainder = 0; // X % X ===> 0; |
| 1977 | return; |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 1978 | } |
| 1979 | |
Reid Spencer | 4c50b52 | 2007-05-13 23:44:59 +0000 | [diff] [blame] | 1980 | if (lhsWords == 1 && rhsWords == 1) { |
| 1981 | // There is only one word to consider so use the native versions. |
Wojciech Matyjewicz | 41b744d | 2008-06-23 19:39:50 +0000 | [diff] [blame] | 1982 | uint64_t lhsValue = LHS.isSingleWord() ? LHS.VAL : LHS.pVal[0]; |
| 1983 | uint64_t rhsValue = RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0]; |
| 1984 | Quotient = APInt(LHS.getBitWidth(), lhsValue / rhsValue); |
| 1985 | Remainder = APInt(LHS.getBitWidth(), lhsValue % rhsValue); |
Reid Spencer | 4c50b52 | 2007-05-13 23:44:59 +0000 | [diff] [blame] | 1986 | return; |
| 1987 | } |
| 1988 | |
| 1989 | // Okay, lets do it the long way |
| 1990 | divide(LHS, lhsWords, RHS, rhsWords, &Quotient, &Remainder); |
| 1991 | } |
| 1992 | |
Jakub Staszak | 6605c60 | 2013-02-20 00:17:42 +0000 | [diff] [blame] | 1993 | void APInt::sdivrem(const APInt &LHS, const APInt &RHS, |
| 1994 | APInt &Quotient, APInt &Remainder) { |
| 1995 | if (LHS.isNegative()) { |
| 1996 | if (RHS.isNegative()) |
| 1997 | APInt::udivrem(-LHS, -RHS, Quotient, Remainder); |
| 1998 | else { |
| 1999 | APInt::udivrem(-LHS, RHS, Quotient, Remainder); |
| 2000 | Quotient = -Quotient; |
| 2001 | } |
| 2002 | Remainder = -Remainder; |
| 2003 | } else if (RHS.isNegative()) { |
| 2004 | APInt::udivrem(LHS, -RHS, Quotient, Remainder); |
| 2005 | Quotient = -Quotient; |
| 2006 | } else { |
| 2007 | APInt::udivrem(LHS, RHS, Quotient, Remainder); |
| 2008 | } |
| 2009 | } |
| 2010 | |
Chris Lattner | 2c819b0 | 2010-10-13 23:54:10 +0000 | [diff] [blame] | 2011 | APInt APInt::sadd_ov(const APInt &RHS, bool &Overflow) const { |
Chris Lattner | 79bdd88 | 2010-10-13 23:46:33 +0000 | [diff] [blame] | 2012 | APInt Res = *this+RHS; |
| 2013 | Overflow = isNonNegative() == RHS.isNonNegative() && |
| 2014 | Res.isNonNegative() != isNonNegative(); |
| 2015 | return Res; |
| 2016 | } |
| 2017 | |
Chris Lattner | 698661c | 2010-10-14 00:05:07 +0000 | [diff] [blame] | 2018 | APInt APInt::uadd_ov(const APInt &RHS, bool &Overflow) const { |
| 2019 | APInt Res = *this+RHS; |
| 2020 | Overflow = Res.ult(RHS); |
| 2021 | return Res; |
| 2022 | } |
| 2023 | |
Chris Lattner | 2c819b0 | 2010-10-13 23:54:10 +0000 | [diff] [blame] | 2024 | APInt APInt::ssub_ov(const APInt &RHS, bool &Overflow) const { |
Chris Lattner | 79bdd88 | 2010-10-13 23:46:33 +0000 | [diff] [blame] | 2025 | APInt Res = *this - RHS; |
| 2026 | Overflow = isNonNegative() != RHS.isNonNegative() && |
| 2027 | Res.isNonNegative() != isNonNegative(); |
| 2028 | return Res; |
| 2029 | } |
| 2030 | |
Chris Lattner | 698661c | 2010-10-14 00:05:07 +0000 | [diff] [blame] | 2031 | APInt APInt::usub_ov(const APInt &RHS, bool &Overflow) const { |
Chris Lattner | b9681ad | 2010-10-14 00:30:00 +0000 | [diff] [blame] | 2032 | APInt Res = *this-RHS; |
| 2033 | Overflow = Res.ugt(*this); |
Chris Lattner | 698661c | 2010-10-14 00:05:07 +0000 | [diff] [blame] | 2034 | return Res; |
| 2035 | } |
| 2036 | |
Chris Lattner | 2c819b0 | 2010-10-13 23:54:10 +0000 | [diff] [blame] | 2037 | APInt APInt::sdiv_ov(const APInt &RHS, bool &Overflow) const { |
Chris Lattner | 79bdd88 | 2010-10-13 23:46:33 +0000 | [diff] [blame] | 2038 | // MININT/-1 --> overflow. |
| 2039 | Overflow = isMinSignedValue() && RHS.isAllOnesValue(); |
| 2040 | return sdiv(RHS); |
| 2041 | } |
| 2042 | |
Chris Lattner | 2c819b0 | 2010-10-13 23:54:10 +0000 | [diff] [blame] | 2043 | APInt APInt::smul_ov(const APInt &RHS, bool &Overflow) const { |
Chris Lattner | 79bdd88 | 2010-10-13 23:46:33 +0000 | [diff] [blame] | 2044 | APInt Res = *this * RHS; |
| 2045 | |
| 2046 | if (*this != 0 && RHS != 0) |
| 2047 | Overflow = Res.sdiv(RHS) != *this || Res.sdiv(*this) != RHS; |
| 2048 | else |
| 2049 | Overflow = false; |
| 2050 | return Res; |
| 2051 | } |
| 2052 | |
Frits van Bommel | 0bb2ad2 | 2011-03-27 14:26:13 +0000 | [diff] [blame] | 2053 | APInt APInt::umul_ov(const APInt &RHS, bool &Overflow) const { |
| 2054 | APInt Res = *this * RHS; |
| 2055 | |
| 2056 | if (*this != 0 && RHS != 0) |
| 2057 | Overflow = Res.udiv(RHS) != *this || Res.udiv(*this) != RHS; |
| 2058 | else |
| 2059 | Overflow = false; |
| 2060 | return Res; |
| 2061 | } |
| 2062 | |
David Majnemer | a252138 | 2014-10-13 21:48:30 +0000 | [diff] [blame] | 2063 | APInt APInt::sshl_ov(const APInt &ShAmt, bool &Overflow) const { |
| 2064 | Overflow = ShAmt.uge(getBitWidth()); |
Chris Lattner | 79bdd88 | 2010-10-13 23:46:33 +0000 | [diff] [blame] | 2065 | if (Overflow) |
David Majnemer | a252138 | 2014-10-13 21:48:30 +0000 | [diff] [blame] | 2066 | return APInt(BitWidth, 0); |
Chris Lattner | 79bdd88 | 2010-10-13 23:46:33 +0000 | [diff] [blame] | 2067 | |
| 2068 | if (isNonNegative()) // Don't allow sign change. |
David Majnemer | a252138 | 2014-10-13 21:48:30 +0000 | [diff] [blame] | 2069 | Overflow = ShAmt.uge(countLeadingZeros()); |
Chris Lattner | 79bdd88 | 2010-10-13 23:46:33 +0000 | [diff] [blame] | 2070 | else |
David Majnemer | a252138 | 2014-10-13 21:48:30 +0000 | [diff] [blame] | 2071 | Overflow = ShAmt.uge(countLeadingOnes()); |
Chris Lattner | 79bdd88 | 2010-10-13 23:46:33 +0000 | [diff] [blame] | 2072 | |
| 2073 | return *this << ShAmt; |
| 2074 | } |
| 2075 | |
David Majnemer | a252138 | 2014-10-13 21:48:30 +0000 | [diff] [blame] | 2076 | APInt APInt::ushl_ov(const APInt &ShAmt, bool &Overflow) const { |
| 2077 | Overflow = ShAmt.uge(getBitWidth()); |
| 2078 | if (Overflow) |
| 2079 | return APInt(BitWidth, 0); |
| 2080 | |
| 2081 | Overflow = ShAmt.ugt(countLeadingZeros()); |
| 2082 | |
| 2083 | return *this << ShAmt; |
| 2084 | } |
| 2085 | |
Chris Lattner | 79bdd88 | 2010-10-13 23:46:33 +0000 | [diff] [blame] | 2086 | |
| 2087 | |
| 2088 | |
Benjamin Kramer | 92d8998 | 2010-07-14 22:38:02 +0000 | [diff] [blame] | 2089 | void APInt::fromString(unsigned numbits, StringRef str, uint8_t radix) { |
Reid Spencer | 1ba8335 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 2090 | // Check our assumptions here |
Erick Tryzelaar | 1264bcb | 2009-08-21 03:15:14 +0000 | [diff] [blame] | 2091 | assert(!str.empty() && "Invalid string length"); |
Douglas Gregor | 663c068 | 2011-09-14 15:54:46 +0000 | [diff] [blame] | 2092 | assert((radix == 10 || radix == 8 || radix == 16 || radix == 2 || |
| 2093 | radix == 36) && |
| 2094 | "Radix should be 2, 8, 10, 16, or 36!"); |
Erick Tryzelaar | 1264bcb | 2009-08-21 03:15:14 +0000 | [diff] [blame] | 2095 | |
Daniel Dunbar | 3a1efd11 | 2009-08-13 02:33:34 +0000 | [diff] [blame] | 2096 | StringRef::iterator p = str.begin(); |
| 2097 | size_t slen = str.size(); |
| 2098 | bool isNeg = *p == '-'; |
Erick Tryzelaar | 1264bcb | 2009-08-21 03:15:14 +0000 | [diff] [blame] | 2099 | if (*p == '-' || *p == '+') { |
Daniel Dunbar | 3a1efd11 | 2009-08-13 02:33:34 +0000 | [diff] [blame] | 2100 | p++; |
| 2101 | slen--; |
Eric Christopher | 43a1dec | 2009-08-21 04:10:31 +0000 | [diff] [blame] | 2102 | assert(slen && "String is only a sign, needs a value."); |
Daniel Dunbar | 3a1efd11 | 2009-08-13 02:33:34 +0000 | [diff] [blame] | 2103 | } |
Chris Lattner | dad2d09 | 2007-05-03 18:15:36 +0000 | [diff] [blame] | 2104 | assert((slen <= numbits || radix != 2) && "Insufficient bit width"); |
Chris Lattner | b869a0a | 2009-04-25 18:34:04 +0000 | [diff] [blame] | 2105 | assert(((slen-1)*3 <= numbits || radix != 8) && "Insufficient bit width"); |
| 2106 | assert(((slen-1)*4 <= numbits || radix != 16) && "Insufficient bit width"); |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2107 | assert((((slen-1)*64)/22 <= numbits || radix != 10) && |
| 2108 | "Insufficient bit width"); |
Reid Spencer | 1ba8335 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 2109 | |
| 2110 | // Allocate memory |
| 2111 | if (!isSingleWord()) |
| 2112 | pVal = getClearedMemory(getNumWords()); |
| 2113 | |
| 2114 | // Figure out if we can shift instead of multiply |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 2115 | unsigned shift = (radix == 16 ? 4 : radix == 8 ? 3 : radix == 2 ? 1 : 0); |
Reid Spencer | 1ba8335 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 2116 | |
| 2117 | // Set up an APInt for the digit to add outside the loop so we don't |
| 2118 | // constantly construct/destruct it. |
| 2119 | APInt apdigit(getBitWidth(), 0); |
| 2120 | APInt apradix(getBitWidth(), radix); |
| 2121 | |
| 2122 | // Enter digit traversal loop |
Daniel Dunbar | 3a1efd11 | 2009-08-13 02:33:34 +0000 | [diff] [blame] | 2123 | for (StringRef::iterator e = str.end(); p != e; ++p) { |
Erick Tryzelaar | dadb1571 | 2009-08-21 03:15:28 +0000 | [diff] [blame] | 2124 | unsigned digit = getDigit(*p, radix); |
Erick Tryzelaar | 6096409 | 2009-08-21 06:48:37 +0000 | [diff] [blame] | 2125 | assert(digit < radix && "Invalid character in digit string"); |
Reid Spencer | 1ba8335 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 2126 | |
Reid Spencer | a93c981 | 2007-05-16 19:18:22 +0000 | [diff] [blame] | 2127 | // Shift or multiply the value by the radix |
Chris Lattner | b869a0a | 2009-04-25 18:34:04 +0000 | [diff] [blame] | 2128 | if (slen > 1) { |
| 2129 | if (shift) |
| 2130 | *this <<= shift; |
| 2131 | else |
| 2132 | *this *= apradix; |
| 2133 | } |
Reid Spencer | 1ba8335 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 2134 | |
| 2135 | // Add in the digit we just interpreted |
Reid Spencer | 632ebdf | 2007-02-24 20:19:37 +0000 | [diff] [blame] | 2136 | if (apdigit.isSingleWord()) |
| 2137 | apdigit.VAL = digit; |
| 2138 | else |
| 2139 | apdigit.pVal[0] = digit; |
Reid Spencer | 1ba8335 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 2140 | *this += apdigit; |
Reid Spencer | 100502d | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 2141 | } |
Reid Spencer | b6b5cc3 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 2142 | // If its negative, put it in two's complement form |
Reid Spencer | aa8dcfe | 2007-02-26 07:44:38 +0000 | [diff] [blame] | 2143 | if (isNeg) { |
Jakub Staszak | 773be0c | 2013-03-20 23:56:19 +0000 | [diff] [blame] | 2144 | --(*this); |
Jay Foad | 25a5e4c | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 2145 | this->flipAllBits(); |
Reid Spencer | b6b5cc3 | 2007-02-25 23:44:53 +0000 | [diff] [blame] | 2146 | } |
Reid Spencer | 100502d | 2007-02-17 03:16:00 +0000 | [diff] [blame] | 2147 | } |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 2148 | |
Chris Lattner | 17f7165 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 2149 | void APInt::toString(SmallVectorImpl<char> &Str, unsigned Radix, |
Ted Kremenek | b05f02e | 2011-06-15 00:51:55 +0000 | [diff] [blame] | 2150 | bool Signed, bool formatAsCLiteral) const { |
Douglas Gregor | 663c068 | 2011-09-14 15:54:46 +0000 | [diff] [blame] | 2151 | assert((Radix == 10 || Radix == 8 || Radix == 16 || Radix == 2 || |
| 2152 | Radix == 36) && |
Dylan Noblesmith | 1c419ff | 2011-12-16 20:36:31 +0000 | [diff] [blame] | 2153 | "Radix should be 2, 8, 10, 16, or 36!"); |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 2154 | |
Ted Kremenek | b05f02e | 2011-06-15 00:51:55 +0000 | [diff] [blame] | 2155 | const char *Prefix = ""; |
| 2156 | if (formatAsCLiteral) { |
| 2157 | switch (Radix) { |
| 2158 | case 2: |
| 2159 | // Binary literals are a non-standard extension added in gcc 4.3: |
| 2160 | // http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Binary-constants.html |
| 2161 | Prefix = "0b"; |
| 2162 | break; |
| 2163 | case 8: |
| 2164 | Prefix = "0"; |
| 2165 | break; |
Dylan Noblesmith | 1c419ff | 2011-12-16 20:36:31 +0000 | [diff] [blame] | 2166 | case 10: |
| 2167 | break; // No prefix |
Ted Kremenek | b05f02e | 2011-06-15 00:51:55 +0000 | [diff] [blame] | 2168 | case 16: |
| 2169 | Prefix = "0x"; |
| 2170 | break; |
Dylan Noblesmith | 1c419ff | 2011-12-16 20:36:31 +0000 | [diff] [blame] | 2171 | default: |
| 2172 | llvm_unreachable("Invalid radix!"); |
Ted Kremenek | b05f02e | 2011-06-15 00:51:55 +0000 | [diff] [blame] | 2173 | } |
| 2174 | } |
| 2175 | |
Chris Lattner | 17f7165 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 2176 | // First, check for a zero value and just short circuit the logic below. |
| 2177 | if (*this == 0) { |
Ted Kremenek | b05f02e | 2011-06-15 00:51:55 +0000 | [diff] [blame] | 2178 | while (*Prefix) { |
| 2179 | Str.push_back(*Prefix); |
| 2180 | ++Prefix; |
| 2181 | }; |
Chris Lattner | 17f7165 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 2182 | Str.push_back('0'); |
| 2183 | return; |
| 2184 | } |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 2185 | |
Douglas Gregor | 663c068 | 2011-09-14 15:54:46 +0000 | [diff] [blame] | 2186 | static const char Digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 2187 | |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 2188 | if (isSingleWord()) { |
Chris Lattner | 17f7165 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 2189 | char Buffer[65]; |
| 2190 | char *BufPtr = Buffer+65; |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 2191 | |
Chris Lattner | 17f7165 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 2192 | uint64_t N; |
Chris Lattner | b91c903 | 2010-08-18 00:33:47 +0000 | [diff] [blame] | 2193 | if (!Signed) { |
Chris Lattner | 17f7165 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 2194 | N = getZExtValue(); |
Chris Lattner | b91c903 | 2010-08-18 00:33:47 +0000 | [diff] [blame] | 2195 | } else { |
| 2196 | int64_t I = getSExtValue(); |
| 2197 | if (I >= 0) { |
| 2198 | N = I; |
| 2199 | } else { |
| 2200 | Str.push_back('-'); |
| 2201 | N = -(uint64_t)I; |
| 2202 | } |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 2203 | } |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 2204 | |
Ted Kremenek | b05f02e | 2011-06-15 00:51:55 +0000 | [diff] [blame] | 2205 | while (*Prefix) { |
| 2206 | Str.push_back(*Prefix); |
| 2207 | ++Prefix; |
| 2208 | }; |
| 2209 | |
Chris Lattner | 17f7165 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 2210 | while (N) { |
| 2211 | *--BufPtr = Digits[N % Radix]; |
| 2212 | N /= Radix; |
| 2213 | } |
| 2214 | Str.append(BufPtr, Buffer+65); |
| 2215 | return; |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 2216 | } |
| 2217 | |
Chris Lattner | 17f7165 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 2218 | APInt Tmp(*this); |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 2219 | |
Chris Lattner | 17f7165 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 2220 | if (Signed && isNegative()) { |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 2221 | // They want to print the signed version and it is a negative value |
| 2222 | // Flip the bits and add one to turn it into the equivalent positive |
| 2223 | // value and put a '-' in the result. |
Jay Foad | 25a5e4c | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 2224 | Tmp.flipAllBits(); |
Jakub Staszak | 773be0c | 2013-03-20 23:56:19 +0000 | [diff] [blame] | 2225 | ++Tmp; |
Chris Lattner | 17f7165 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 2226 | Str.push_back('-'); |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 2227 | } |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 2228 | |
Ted Kremenek | b05f02e | 2011-06-15 00:51:55 +0000 | [diff] [blame] | 2229 | while (*Prefix) { |
| 2230 | Str.push_back(*Prefix); |
| 2231 | ++Prefix; |
| 2232 | }; |
| 2233 | |
Chris Lattner | 17f7165 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 2234 | // We insert the digits backward, then reverse them to get the right order. |
| 2235 | unsigned StartDig = Str.size(); |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 2236 | |
| 2237 | // For the 2, 8 and 16 bit cases, we can just shift instead of divide |
| 2238 | // because the number of bits per digit (1, 3 and 4 respectively) divides |
Chris Lattner | 17f7165 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 2239 | // equaly. We just shift until the value is zero. |
Douglas Gregor | 663c068 | 2011-09-14 15:54:46 +0000 | [diff] [blame] | 2240 | if (Radix == 2 || Radix == 8 || Radix == 16) { |
Chris Lattner | 17f7165 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 2241 | // Just shift tmp right for each digit width until it becomes zero |
| 2242 | unsigned ShiftAmt = (Radix == 16 ? 4 : (Radix == 8 ? 3 : 1)); |
| 2243 | unsigned MaskAmt = Radix - 1; |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 2244 | |
Chris Lattner | 17f7165 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 2245 | while (Tmp != 0) { |
| 2246 | unsigned Digit = unsigned(Tmp.getRawData()[0]) & MaskAmt; |
| 2247 | Str.push_back(Digits[Digit]); |
| 2248 | Tmp = Tmp.lshr(ShiftAmt); |
| 2249 | } |
| 2250 | } else { |
Douglas Gregor | 663c068 | 2011-09-14 15:54:46 +0000 | [diff] [blame] | 2251 | APInt divisor(Radix == 10? 4 : 8, Radix); |
Chris Lattner | 17f7165 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 2252 | while (Tmp != 0) { |
| 2253 | APInt APdigit(1, 0); |
| 2254 | APInt tmp2(Tmp.getBitWidth(), 0); |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 2255 | divide(Tmp, Tmp.getNumWords(), divisor, divisor.getNumWords(), &tmp2, |
Chris Lattner | 17f7165 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 2256 | &APdigit); |
Chris Lattner | 77527f5 | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 2257 | unsigned Digit = (unsigned)APdigit.getZExtValue(); |
Chris Lattner | 17f7165 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 2258 | assert(Digit < Radix && "divide failed"); |
| 2259 | Str.push_back(Digits[Digit]); |
| 2260 | Tmp = tmp2; |
| 2261 | } |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 2262 | } |
Eric Christopher | 820256b | 2009-08-21 04:06:45 +0000 | [diff] [blame] | 2263 | |
Chris Lattner | 17f7165 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 2264 | // Reverse the digits before returning. |
| 2265 | std::reverse(Str.begin()+StartDig, Str.end()); |
Reid Spencer | fb77b2b | 2007-02-20 08:51:03 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
Pawel Bylica | 6eeeac7 | 2015-04-06 13:31:39 +0000 | [diff] [blame] | 2268 | /// Returns the APInt as a std::string. Note that this is an inefficient method. |
| 2269 | /// It is better to pass in a SmallVector/SmallString to the methods above. |
Chris Lattner | 17f7165 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 2270 | std::string APInt::toString(unsigned Radix = 10, bool Signed = true) const { |
| 2271 | SmallString<40> S; |
Ted Kremenek | b05f02e | 2011-06-15 00:51:55 +0000 | [diff] [blame] | 2272 | toString(S, Radix, Signed, /* formatAsCLiteral = */false); |
Daniel Dunbar | 8b0b115 | 2009-08-19 20:07:03 +0000 | [diff] [blame] | 2273 | return S.str(); |
Reid Spencer | 1ba8335 | 2007-02-21 03:55:44 +0000 | [diff] [blame] | 2274 | } |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2275 | |
Chris Lattner | 17f7165 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 2276 | |
Yaron Keren | eb2a254 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 2277 | LLVM_DUMP_METHOD void APInt::dump() const { |
Chris Lattner | 17f7165 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 2278 | SmallString<40> S, U; |
| 2279 | this->toStringUnsigned(U); |
| 2280 | this->toStringSigned(S); |
David Greene | f32fcb4 | 2010-01-05 01:28:52 +0000 | [diff] [blame] | 2281 | dbgs() << "APInt(" << BitWidth << "b, " |
Yaron Keren | 09fb7c6 | 2015-03-10 07:33:23 +0000 | [diff] [blame] | 2282 | << U << "u " << S << "s)"; |
Chris Lattner | 17f7165 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 2283 | } |
| 2284 | |
Chris Lattner | 0c19df4 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 2285 | void APInt::print(raw_ostream &OS, bool isSigned) const { |
Chris Lattner | 17f7165 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 2286 | SmallString<40> S; |
Ted Kremenek | b05f02e | 2011-06-15 00:51:55 +0000 | [diff] [blame] | 2287 | this->toString(S, 10, isSigned, /* formatAsCLiteral = */false); |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 2288 | OS << S; |
Chris Lattner | 17f7165 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 2289 | } |
| 2290 | |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2291 | // This implements a variety of operations on a representation of |
| 2292 | // arbitrary precision, two's-complement, bignum integer values. |
| 2293 | |
Chris Lattner | 96cffa6 | 2009-08-23 23:11:28 +0000 | [diff] [blame] | 2294 | // Assumed by lowHalf, highHalf, partMSB and partLSB. A fairly safe |
| 2295 | // and unrestricting assumption. |
Benjamin Kramer | 7000ca3 | 2014-10-12 17:56:40 +0000 | [diff] [blame] | 2296 | static_assert(integerPartWidth % 2 == 0, "Part width must be divisible by 2!"); |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2297 | |
| 2298 | /* Some handy functions local to this file. */ |
| 2299 | namespace { |
| 2300 | |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2301 | /* Returns the integer part with the least significant BITS set. |
| 2302 | BITS cannot be zero. */ |
Dan Gohman | f4bc782 | 2008-04-10 21:11:47 +0000 | [diff] [blame] | 2303 | static inline integerPart |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2304 | lowBitMask(unsigned int bits) |
| 2305 | { |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2306 | assert(bits != 0 && bits <= integerPartWidth); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2307 | |
| 2308 | return ~(integerPart) 0 >> (integerPartWidth - bits); |
| 2309 | } |
| 2310 | |
Neil Booth | c8b650a | 2007-10-06 00:43:45 +0000 | [diff] [blame] | 2311 | /* Returns the value of the lower half of PART. */ |
Dan Gohman | f4bc782 | 2008-04-10 21:11:47 +0000 | [diff] [blame] | 2312 | static inline integerPart |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2313 | lowHalf(integerPart part) |
| 2314 | { |
| 2315 | return part & lowBitMask(integerPartWidth / 2); |
| 2316 | } |
| 2317 | |
Neil Booth | c8b650a | 2007-10-06 00:43:45 +0000 | [diff] [blame] | 2318 | /* Returns the value of the upper half of PART. */ |
Dan Gohman | f4bc782 | 2008-04-10 21:11:47 +0000 | [diff] [blame] | 2319 | static inline integerPart |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2320 | highHalf(integerPart part) |
| 2321 | { |
| 2322 | return part >> (integerPartWidth / 2); |
| 2323 | } |
| 2324 | |
Neil Booth | c8b650a | 2007-10-06 00:43:45 +0000 | [diff] [blame] | 2325 | /* Returns the bit number of the most significant set bit of a part. |
| 2326 | If the input number has no bits set -1U is returned. */ |
Dan Gohman | f4bc782 | 2008-04-10 21:11:47 +0000 | [diff] [blame] | 2327 | static unsigned int |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2328 | partMSB(integerPart value) |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2329 | { |
Benjamin Kramer | b565f89 | 2013-06-01 11:26:39 +0000 | [diff] [blame] | 2330 | return findLastSet(value, ZB_Max); |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2331 | } |
| 2332 | |
Neil Booth | c8b650a | 2007-10-06 00:43:45 +0000 | [diff] [blame] | 2333 | /* Returns the bit number of the least significant set bit of a |
| 2334 | part. If the input number has no bits set -1U is returned. */ |
Dan Gohman | f4bc782 | 2008-04-10 21:11:47 +0000 | [diff] [blame] | 2335 | static unsigned int |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2336 | partLSB(integerPart value) |
| 2337 | { |
Benjamin Kramer | b565f89 | 2013-06-01 11:26:39 +0000 | [diff] [blame] | 2338 | return findFirstSet(value, ZB_Max); |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2339 | } |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 2340 | } |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2341 | |
| 2342 | /* Sets the least significant part of a bignum to the input value, and |
| 2343 | zeroes out higher parts. */ |
| 2344 | void |
| 2345 | APInt::tcSet(integerPart *dst, integerPart part, unsigned int parts) |
| 2346 | { |
| 2347 | unsigned int i; |
| 2348 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2349 | assert(parts > 0); |
Neil Booth | b618216 | 2007-10-08 13:47:12 +0000 | [diff] [blame] | 2350 | |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2351 | dst[0] = part; |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2352 | for (i = 1; i < parts; i++) |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2353 | dst[i] = 0; |
| 2354 | } |
| 2355 | |
| 2356 | /* Assign one bignum to another. */ |
| 2357 | void |
| 2358 | APInt::tcAssign(integerPart *dst, const integerPart *src, unsigned int parts) |
| 2359 | { |
| 2360 | unsigned int i; |
| 2361 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2362 | for (i = 0; i < parts; i++) |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2363 | dst[i] = src[i]; |
| 2364 | } |
| 2365 | |
| 2366 | /* Returns true if a bignum is zero, false otherwise. */ |
| 2367 | bool |
| 2368 | APInt::tcIsZero(const integerPart *src, unsigned int parts) |
| 2369 | { |
| 2370 | unsigned int i; |
| 2371 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2372 | for (i = 0; i < parts; i++) |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2373 | if (src[i]) |
| 2374 | return false; |
| 2375 | |
| 2376 | return true; |
| 2377 | } |
| 2378 | |
| 2379 | /* Extract the given bit of a bignum; returns 0 or 1. */ |
| 2380 | int |
| 2381 | APInt::tcExtractBit(const integerPart *parts, unsigned int bit) |
| 2382 | { |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2383 | return (parts[bit / integerPartWidth] & |
| 2384 | ((integerPart) 1 << bit % integerPartWidth)) != 0; |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2385 | } |
| 2386 | |
John McCall | dcb9a7a | 2010-02-28 02:51:25 +0000 | [diff] [blame] | 2387 | /* Set the given bit of a bignum. */ |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2388 | void |
| 2389 | APInt::tcSetBit(integerPart *parts, unsigned int bit) |
| 2390 | { |
| 2391 | parts[bit / integerPartWidth] |= (integerPart) 1 << (bit % integerPartWidth); |
| 2392 | } |
| 2393 | |
John McCall | dcb9a7a | 2010-02-28 02:51:25 +0000 | [diff] [blame] | 2394 | /* Clears the given bit of a bignum. */ |
| 2395 | void |
| 2396 | APInt::tcClearBit(integerPart *parts, unsigned int bit) |
| 2397 | { |
| 2398 | parts[bit / integerPartWidth] &= |
| 2399 | ~((integerPart) 1 << (bit % integerPartWidth)); |
| 2400 | } |
| 2401 | |
Neil Booth | c8b650a | 2007-10-06 00:43:45 +0000 | [diff] [blame] | 2402 | /* Returns the bit number of the least significant set bit of a |
| 2403 | number. If the input number has no bits set -1U is returned. */ |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2404 | unsigned int |
| 2405 | APInt::tcLSB(const integerPart *parts, unsigned int n) |
| 2406 | { |
| 2407 | unsigned int i, lsb; |
| 2408 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2409 | for (i = 0; i < n; i++) { |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2410 | if (parts[i] != 0) { |
| 2411 | lsb = partLSB(parts[i]); |
| 2412 | |
| 2413 | return lsb + i * integerPartWidth; |
| 2414 | } |
| 2415 | } |
| 2416 | |
| 2417 | return -1U; |
| 2418 | } |
| 2419 | |
Neil Booth | c8b650a | 2007-10-06 00:43:45 +0000 | [diff] [blame] | 2420 | /* Returns the bit number of the most significant set bit of a number. |
| 2421 | If the input number has no bits set -1U is returned. */ |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2422 | unsigned int |
| 2423 | APInt::tcMSB(const integerPart *parts, unsigned int n) |
| 2424 | { |
| 2425 | unsigned int msb; |
| 2426 | |
| 2427 | do { |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2428 | --n; |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2429 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2430 | if (parts[n] != 0) { |
| 2431 | msb = partMSB(parts[n]); |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2432 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2433 | return msb + n * integerPartWidth; |
| 2434 | } |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2435 | } while (n); |
| 2436 | |
| 2437 | return -1U; |
| 2438 | } |
| 2439 | |
Neil Booth | b618216 | 2007-10-08 13:47:12 +0000 | [diff] [blame] | 2440 | /* Copy the bit vector of width srcBITS from SRC, starting at bit |
| 2441 | srcLSB, to DST, of dstCOUNT parts, such that the bit srcLSB becomes |
| 2442 | the least significant bit of DST. All high bits above srcBITS in |
| 2443 | DST are zero-filled. */ |
| 2444 | void |
Evan Cheng | db338f3 | 2009-05-21 23:47:47 +0000 | [diff] [blame] | 2445 | APInt::tcExtract(integerPart *dst, unsigned int dstCount,const integerPart *src, |
Neil Booth | b618216 | 2007-10-08 13:47:12 +0000 | [diff] [blame] | 2446 | unsigned int srcBits, unsigned int srcLSB) |
| 2447 | { |
| 2448 | unsigned int firstSrcPart, dstParts, shift, n; |
| 2449 | |
| 2450 | dstParts = (srcBits + integerPartWidth - 1) / integerPartWidth; |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2451 | assert(dstParts <= dstCount); |
Neil Booth | b618216 | 2007-10-08 13:47:12 +0000 | [diff] [blame] | 2452 | |
| 2453 | firstSrcPart = srcLSB / integerPartWidth; |
| 2454 | tcAssign (dst, src + firstSrcPart, dstParts); |
| 2455 | |
| 2456 | shift = srcLSB % integerPartWidth; |
| 2457 | tcShiftRight (dst, dstParts, shift); |
| 2458 | |
| 2459 | /* We now have (dstParts * integerPartWidth - shift) bits from SRC |
| 2460 | in DST. If this is less that srcBits, append the rest, else |
| 2461 | clear the high bits. */ |
| 2462 | n = dstParts * integerPartWidth - shift; |
| 2463 | if (n < srcBits) { |
| 2464 | integerPart mask = lowBitMask (srcBits - n); |
| 2465 | dst[dstParts - 1] |= ((src[firstSrcPart + dstParts] & mask) |
| 2466 | << n % integerPartWidth); |
| 2467 | } else if (n > srcBits) { |
Neil Booth | 7e74b17 | 2007-10-12 15:31:31 +0000 | [diff] [blame] | 2468 | if (srcBits % integerPartWidth) |
| 2469 | dst[dstParts - 1] &= lowBitMask (srcBits % integerPartWidth); |
Neil Booth | b618216 | 2007-10-08 13:47:12 +0000 | [diff] [blame] | 2470 | } |
| 2471 | |
| 2472 | /* Clear high parts. */ |
| 2473 | while (dstParts < dstCount) |
| 2474 | dst[dstParts++] = 0; |
| 2475 | } |
| 2476 | |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2477 | /* DST += RHS + C where C is zero or one. Returns the carry flag. */ |
| 2478 | integerPart |
| 2479 | APInt::tcAdd(integerPart *dst, const integerPart *rhs, |
| 2480 | integerPart c, unsigned int parts) |
| 2481 | { |
| 2482 | unsigned int i; |
| 2483 | |
| 2484 | assert(c <= 1); |
| 2485 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2486 | for (i = 0; i < parts; i++) { |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2487 | integerPart l; |
| 2488 | |
| 2489 | l = dst[i]; |
| 2490 | if (c) { |
| 2491 | dst[i] += rhs[i] + 1; |
| 2492 | c = (dst[i] <= l); |
| 2493 | } else { |
| 2494 | dst[i] += rhs[i]; |
| 2495 | c = (dst[i] < l); |
| 2496 | } |
| 2497 | } |
| 2498 | |
| 2499 | return c; |
| 2500 | } |
| 2501 | |
| 2502 | /* DST -= RHS + C where C is zero or one. Returns the carry flag. */ |
| 2503 | integerPart |
| 2504 | APInt::tcSubtract(integerPart *dst, const integerPart *rhs, |
| 2505 | integerPart c, unsigned int parts) |
| 2506 | { |
| 2507 | unsigned int i; |
| 2508 | |
| 2509 | assert(c <= 1); |
| 2510 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2511 | for (i = 0; i < parts; i++) { |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2512 | integerPart l; |
| 2513 | |
| 2514 | l = dst[i]; |
| 2515 | if (c) { |
| 2516 | dst[i] -= rhs[i] + 1; |
| 2517 | c = (dst[i] >= l); |
| 2518 | } else { |
| 2519 | dst[i] -= rhs[i]; |
| 2520 | c = (dst[i] > l); |
| 2521 | } |
| 2522 | } |
| 2523 | |
| 2524 | return c; |
| 2525 | } |
| 2526 | |
| 2527 | /* Negate a bignum in-place. */ |
| 2528 | void |
| 2529 | APInt::tcNegate(integerPart *dst, unsigned int parts) |
| 2530 | { |
| 2531 | tcComplement(dst, parts); |
| 2532 | tcIncrement(dst, parts); |
| 2533 | } |
| 2534 | |
Neil Booth | c8b650a | 2007-10-06 00:43:45 +0000 | [diff] [blame] | 2535 | /* DST += SRC * MULTIPLIER + CARRY if add is true |
| 2536 | DST = SRC * MULTIPLIER + CARRY if add is false |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2537 | |
| 2538 | Requires 0 <= DSTPARTS <= SRCPARTS + 1. If DST overlaps SRC |
| 2539 | they must start at the same point, i.e. DST == SRC. |
| 2540 | |
| 2541 | If DSTPARTS == SRCPARTS + 1 no overflow occurs and zero is |
| 2542 | returned. Otherwise DST is filled with the least significant |
| 2543 | DSTPARTS parts of the result, and if all of the omitted higher |
| 2544 | parts were zero return zero, otherwise overflow occurred and |
| 2545 | return one. */ |
| 2546 | int |
| 2547 | APInt::tcMultiplyPart(integerPart *dst, const integerPart *src, |
| 2548 | integerPart multiplier, integerPart carry, |
| 2549 | unsigned int srcParts, unsigned int dstParts, |
| 2550 | bool add) |
| 2551 | { |
| 2552 | unsigned int i, n; |
| 2553 | |
| 2554 | /* Otherwise our writes of DST kill our later reads of SRC. */ |
| 2555 | assert(dst <= src || dst >= src + srcParts); |
| 2556 | assert(dstParts <= srcParts + 1); |
| 2557 | |
| 2558 | /* N loops; minimum of dstParts and srcParts. */ |
| 2559 | n = dstParts < srcParts ? dstParts: srcParts; |
| 2560 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2561 | for (i = 0; i < n; i++) { |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2562 | integerPart low, mid, high, srcPart; |
| 2563 | |
| 2564 | /* [ LOW, HIGH ] = MULTIPLIER * SRC[i] + DST[i] + CARRY. |
| 2565 | |
| 2566 | This cannot overflow, because |
| 2567 | |
| 2568 | (n - 1) * (n - 1) + 2 (n - 1) = (n - 1) * (n + 1) |
| 2569 | |
| 2570 | which is less than n^2. */ |
| 2571 | |
| 2572 | srcPart = src[i]; |
| 2573 | |
| 2574 | if (multiplier == 0 || srcPart == 0) { |
| 2575 | low = carry; |
| 2576 | high = 0; |
| 2577 | } else { |
| 2578 | low = lowHalf(srcPart) * lowHalf(multiplier); |
| 2579 | high = highHalf(srcPart) * highHalf(multiplier); |
| 2580 | |
| 2581 | mid = lowHalf(srcPart) * highHalf(multiplier); |
| 2582 | high += highHalf(mid); |
| 2583 | mid <<= integerPartWidth / 2; |
| 2584 | if (low + mid < low) |
| 2585 | high++; |
| 2586 | low += mid; |
| 2587 | |
| 2588 | mid = highHalf(srcPart) * lowHalf(multiplier); |
| 2589 | high += highHalf(mid); |
| 2590 | mid <<= integerPartWidth / 2; |
| 2591 | if (low + mid < low) |
| 2592 | high++; |
| 2593 | low += mid; |
| 2594 | |
| 2595 | /* Now add carry. */ |
| 2596 | if (low + carry < low) |
| 2597 | high++; |
| 2598 | low += carry; |
| 2599 | } |
| 2600 | |
| 2601 | if (add) { |
| 2602 | /* And now DST[i], and store the new low part there. */ |
| 2603 | if (low + dst[i] < low) |
| 2604 | high++; |
| 2605 | dst[i] += low; |
| 2606 | } else |
| 2607 | dst[i] = low; |
| 2608 | |
| 2609 | carry = high; |
| 2610 | } |
| 2611 | |
| 2612 | if (i < dstParts) { |
| 2613 | /* Full multiplication, there is no overflow. */ |
| 2614 | assert(i + 1 == dstParts); |
| 2615 | dst[i] = carry; |
| 2616 | return 0; |
| 2617 | } else { |
| 2618 | /* We overflowed if there is carry. */ |
| 2619 | if (carry) |
| 2620 | return 1; |
| 2621 | |
| 2622 | /* We would overflow if any significant unwritten parts would be |
| 2623 | non-zero. This is true if any remaining src parts are non-zero |
| 2624 | and the multiplier is non-zero. */ |
| 2625 | if (multiplier) |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2626 | for (; i < srcParts; i++) |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2627 | if (src[i]) |
| 2628 | return 1; |
| 2629 | |
| 2630 | /* We fitted in the narrow destination. */ |
| 2631 | return 0; |
| 2632 | } |
| 2633 | } |
| 2634 | |
| 2635 | /* DST = LHS * RHS, where DST has the same width as the operands and |
| 2636 | is filled with the least significant parts of the result. Returns |
| 2637 | one if overflow occurred, otherwise zero. DST must be disjoint |
| 2638 | from both operands. */ |
| 2639 | int |
| 2640 | APInt::tcMultiply(integerPart *dst, const integerPart *lhs, |
| 2641 | const integerPart *rhs, unsigned int parts) |
| 2642 | { |
| 2643 | unsigned int i; |
| 2644 | int overflow; |
| 2645 | |
| 2646 | assert(dst != lhs && dst != rhs); |
| 2647 | |
| 2648 | overflow = 0; |
| 2649 | tcSet(dst, 0, parts); |
| 2650 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2651 | for (i = 0; i < parts; i++) |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2652 | overflow |= tcMultiplyPart(&dst[i], lhs, rhs[i], 0, parts, |
| 2653 | parts - i, true); |
| 2654 | |
| 2655 | return overflow; |
| 2656 | } |
| 2657 | |
Neil Booth | 0ea72a9 | 2007-10-06 00:24:48 +0000 | [diff] [blame] | 2658 | /* DST = LHS * RHS, where DST has width the sum of the widths of the |
| 2659 | operands. No overflow occurs. DST must be disjoint from both |
| 2660 | operands. Returns the number of parts required to hold the |
| 2661 | result. */ |
| 2662 | unsigned int |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2663 | APInt::tcFullMultiply(integerPart *dst, const integerPart *lhs, |
Neil Booth | 0ea72a9 | 2007-10-06 00:24:48 +0000 | [diff] [blame] | 2664 | const integerPart *rhs, unsigned int lhsParts, |
| 2665 | unsigned int rhsParts) |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2666 | { |
Neil Booth | 0ea72a9 | 2007-10-06 00:24:48 +0000 | [diff] [blame] | 2667 | /* Put the narrower number on the LHS for less loops below. */ |
| 2668 | if (lhsParts > rhsParts) { |
| 2669 | return tcFullMultiply (dst, rhs, lhs, rhsParts, lhsParts); |
| 2670 | } else { |
| 2671 | unsigned int n; |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2672 | |
Neil Booth | 0ea72a9 | 2007-10-06 00:24:48 +0000 | [diff] [blame] | 2673 | assert(dst != lhs && dst != rhs); |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2674 | |
Neil Booth | 0ea72a9 | 2007-10-06 00:24:48 +0000 | [diff] [blame] | 2675 | tcSet(dst, 0, rhsParts); |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2676 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2677 | for (n = 0; n < lhsParts; n++) |
Neil Booth | 0ea72a9 | 2007-10-06 00:24:48 +0000 | [diff] [blame] | 2678 | tcMultiplyPart(&dst[n], rhs, lhs[n], 0, rhsParts, rhsParts + 1, true); |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2679 | |
Neil Booth | 0ea72a9 | 2007-10-06 00:24:48 +0000 | [diff] [blame] | 2680 | n = lhsParts + rhsParts; |
| 2681 | |
| 2682 | return n - (dst[n - 1] == 0); |
| 2683 | } |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2684 | } |
| 2685 | |
| 2686 | /* If RHS is zero LHS and REMAINDER are left unchanged, return one. |
| 2687 | Otherwise set LHS to LHS / RHS with the fractional part discarded, |
| 2688 | set REMAINDER to the remainder, return zero. i.e. |
| 2689 | |
| 2690 | OLD_LHS = RHS * LHS + REMAINDER |
| 2691 | |
| 2692 | SCRATCH is a bignum of the same size as the operands and result for |
| 2693 | use by the routine; its contents need not be initialized and are |
| 2694 | destroyed. LHS, REMAINDER and SCRATCH must be distinct. |
| 2695 | */ |
| 2696 | int |
| 2697 | APInt::tcDivide(integerPart *lhs, const integerPart *rhs, |
| 2698 | integerPart *remainder, integerPart *srhs, |
| 2699 | unsigned int parts) |
| 2700 | { |
| 2701 | unsigned int n, shiftCount; |
| 2702 | integerPart mask; |
| 2703 | |
| 2704 | assert(lhs != remainder && lhs != srhs && remainder != srhs); |
| 2705 | |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2706 | shiftCount = tcMSB(rhs, parts) + 1; |
| 2707 | if (shiftCount == 0) |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2708 | return true; |
| 2709 | |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2710 | shiftCount = parts * integerPartWidth - shiftCount; |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2711 | n = shiftCount / integerPartWidth; |
| 2712 | mask = (integerPart) 1 << (shiftCount % integerPartWidth); |
| 2713 | |
| 2714 | tcAssign(srhs, rhs, parts); |
| 2715 | tcShiftLeft(srhs, parts, shiftCount); |
| 2716 | tcAssign(remainder, lhs, parts); |
| 2717 | tcSet(lhs, 0, parts); |
| 2718 | |
| 2719 | /* Loop, subtracting SRHS if REMAINDER is greater and adding that to |
| 2720 | the total. */ |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2721 | for (;;) { |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2722 | int compare; |
| 2723 | |
| 2724 | compare = tcCompare(remainder, srhs, parts); |
| 2725 | if (compare >= 0) { |
| 2726 | tcSubtract(remainder, srhs, 0, parts); |
| 2727 | lhs[n] |= mask; |
| 2728 | } |
| 2729 | |
| 2730 | if (shiftCount == 0) |
| 2731 | break; |
| 2732 | shiftCount--; |
| 2733 | tcShiftRight(srhs, parts, 1); |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 2734 | if ((mask >>= 1) == 0) { |
| 2735 | mask = (integerPart) 1 << (integerPartWidth - 1); |
| 2736 | n--; |
| 2737 | } |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2738 | } |
| 2739 | |
| 2740 | return false; |
| 2741 | } |
| 2742 | |
| 2743 | /* Shift a bignum left COUNT bits in-place. Shifted in bits are zero. |
| 2744 | There are no restrictions on COUNT. */ |
| 2745 | void |
| 2746 | APInt::tcShiftLeft(integerPart *dst, unsigned int parts, unsigned int count) |
| 2747 | { |
Neil Booth | b618216 | 2007-10-08 13:47:12 +0000 | [diff] [blame] | 2748 | if (count) { |
| 2749 | unsigned int jump, shift; |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2750 | |
Neil Booth | b618216 | 2007-10-08 13:47:12 +0000 | [diff] [blame] | 2751 | /* Jump is the inter-part jump; shift is is intra-part shift. */ |
| 2752 | jump = count / integerPartWidth; |
| 2753 | shift = count % integerPartWidth; |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2754 | |
Neil Booth | b618216 | 2007-10-08 13:47:12 +0000 | [diff] [blame] | 2755 | while (parts > jump) { |
| 2756 | integerPart part; |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2757 | |
Neil Booth | b618216 | 2007-10-08 13:47:12 +0000 | [diff] [blame] | 2758 | parts--; |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2759 | |
Neil Booth | b618216 | 2007-10-08 13:47:12 +0000 | [diff] [blame] | 2760 | /* dst[i] comes from the two parts src[i - jump] and, if we have |
| 2761 | an intra-part shift, src[i - jump - 1]. */ |
| 2762 | part = dst[parts - jump]; |
| 2763 | if (shift) { |
| 2764 | part <<= shift; |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2765 | if (parts >= jump + 1) |
| 2766 | part |= dst[parts - jump - 1] >> (integerPartWidth - shift); |
| 2767 | } |
| 2768 | |
Neil Booth | b618216 | 2007-10-08 13:47:12 +0000 | [diff] [blame] | 2769 | dst[parts] = part; |
| 2770 | } |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2771 | |
Neil Booth | b618216 | 2007-10-08 13:47:12 +0000 | [diff] [blame] | 2772 | while (parts > 0) |
| 2773 | dst[--parts] = 0; |
| 2774 | } |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2775 | } |
| 2776 | |
| 2777 | /* Shift a bignum right COUNT bits in-place. Shifted in bits are |
| 2778 | zero. There are no restrictions on COUNT. */ |
| 2779 | void |
| 2780 | APInt::tcShiftRight(integerPart *dst, unsigned int parts, unsigned int count) |
| 2781 | { |
Neil Booth | b618216 | 2007-10-08 13:47:12 +0000 | [diff] [blame] | 2782 | if (count) { |
| 2783 | unsigned int i, jump, shift; |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2784 | |
Neil Booth | b618216 | 2007-10-08 13:47:12 +0000 | [diff] [blame] | 2785 | /* Jump is the inter-part jump; shift is is intra-part shift. */ |
| 2786 | jump = count / integerPartWidth; |
| 2787 | shift = count % integerPartWidth; |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2788 | |
Neil Booth | b618216 | 2007-10-08 13:47:12 +0000 | [diff] [blame] | 2789 | /* Perform the shift. This leaves the most significant COUNT bits |
| 2790 | of the result at zero. */ |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2791 | for (i = 0; i < parts; i++) { |
Neil Booth | b618216 | 2007-10-08 13:47:12 +0000 | [diff] [blame] | 2792 | integerPart part; |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2793 | |
Neil Booth | b618216 | 2007-10-08 13:47:12 +0000 | [diff] [blame] | 2794 | if (i + jump >= parts) { |
| 2795 | part = 0; |
| 2796 | } else { |
| 2797 | part = dst[i + jump]; |
| 2798 | if (shift) { |
| 2799 | part >>= shift; |
| 2800 | if (i + jump + 1 < parts) |
| 2801 | part |= dst[i + jump + 1] << (integerPartWidth - shift); |
| 2802 | } |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2803 | } |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2804 | |
Neil Booth | b618216 | 2007-10-08 13:47:12 +0000 | [diff] [blame] | 2805 | dst[i] = part; |
| 2806 | } |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2807 | } |
| 2808 | } |
| 2809 | |
| 2810 | /* Bitwise and of two bignums. */ |
| 2811 | void |
| 2812 | APInt::tcAnd(integerPart *dst, const integerPart *rhs, unsigned int parts) |
| 2813 | { |
| 2814 | unsigned int i; |
| 2815 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2816 | for (i = 0; i < parts; i++) |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2817 | dst[i] &= rhs[i]; |
| 2818 | } |
| 2819 | |
| 2820 | /* Bitwise inclusive or of two bignums. */ |
| 2821 | void |
| 2822 | APInt::tcOr(integerPart *dst, const integerPart *rhs, unsigned int parts) |
| 2823 | { |
| 2824 | unsigned int i; |
| 2825 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2826 | for (i = 0; i < parts; i++) |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2827 | dst[i] |= rhs[i]; |
| 2828 | } |
| 2829 | |
| 2830 | /* Bitwise exclusive or of two bignums. */ |
| 2831 | void |
| 2832 | APInt::tcXor(integerPart *dst, const integerPart *rhs, unsigned int parts) |
| 2833 | { |
| 2834 | unsigned int i; |
| 2835 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2836 | for (i = 0; i < parts; i++) |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2837 | dst[i] ^= rhs[i]; |
| 2838 | } |
| 2839 | |
| 2840 | /* Complement a bignum in-place. */ |
| 2841 | void |
| 2842 | APInt::tcComplement(integerPart *dst, unsigned int parts) |
| 2843 | { |
| 2844 | unsigned int i; |
| 2845 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2846 | for (i = 0; i < parts; i++) |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2847 | dst[i] = ~dst[i]; |
| 2848 | } |
| 2849 | |
| 2850 | /* Comparison (unsigned) of two bignums. */ |
| 2851 | int |
| 2852 | APInt::tcCompare(const integerPart *lhs, const integerPart *rhs, |
| 2853 | unsigned int parts) |
| 2854 | { |
| 2855 | while (parts) { |
| 2856 | parts--; |
| 2857 | if (lhs[parts] == rhs[parts]) |
| 2858 | continue; |
| 2859 | |
| 2860 | if (lhs[parts] > rhs[parts]) |
| 2861 | return 1; |
| 2862 | else |
| 2863 | return -1; |
| 2864 | } |
| 2865 | |
| 2866 | return 0; |
| 2867 | } |
| 2868 | |
| 2869 | /* Increment a bignum in-place, return the carry flag. */ |
| 2870 | integerPart |
| 2871 | APInt::tcIncrement(integerPart *dst, unsigned int parts) |
| 2872 | { |
| 2873 | unsigned int i; |
| 2874 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2875 | for (i = 0; i < parts; i++) |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2876 | if (++dst[i] != 0) |
| 2877 | break; |
| 2878 | |
| 2879 | return i == parts; |
| 2880 | } |
| 2881 | |
Michael Gottesman | 9d406f4 | 2013-05-28 19:50:20 +0000 | [diff] [blame] | 2882 | /* Decrement a bignum in-place, return the borrow flag. */ |
| 2883 | integerPart |
| 2884 | APInt::tcDecrement(integerPart *dst, unsigned int parts) { |
| 2885 | for (unsigned int i = 0; i < parts; i++) { |
| 2886 | // If the current word is non-zero, then the decrement has no effect on the |
| 2887 | // higher-order words of the integer and no borrow can occur. Exit early. |
| 2888 | if (dst[i]--) |
| 2889 | return 0; |
| 2890 | } |
| 2891 | // If every word was zero, then there is a borrow. |
| 2892 | return 1; |
| 2893 | } |
| 2894 | |
| 2895 | |
Chris Lattner | 6b69568 | 2007-08-16 15:56:55 +0000 | [diff] [blame] | 2896 | /* Set the least significant BITS bits of a bignum, clear the |
| 2897 | rest. */ |
| 2898 | void |
| 2899 | APInt::tcSetLeastSignificantBits(integerPart *dst, unsigned int parts, |
| 2900 | unsigned int bits) |
| 2901 | { |
| 2902 | unsigned int i; |
| 2903 | |
| 2904 | i = 0; |
| 2905 | while (bits > integerPartWidth) { |
| 2906 | dst[i++] = ~(integerPart) 0; |
| 2907 | bits -= integerPartWidth; |
| 2908 | } |
| 2909 | |
| 2910 | if (bits) |
| 2911 | dst[i++] = ~(integerPart) 0 >> (integerPartWidth - bits); |
| 2912 | |
| 2913 | while (i < parts) |
| 2914 | dst[i++] = 0; |
| 2915 | } |