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