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