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