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