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