~ubuntu-branches/ubuntu/trusty/llvm-toolchain-snapshot/trusty-201310232150

« back to all changes in this revision

Viewing changes to include/llvm/ADT/APInt.h

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-27 15:01:57 UTC
  • mfrom: (0.10.1) (0.9.1) (0.8.1) (0.7.1) (0.6.1) (0.5.2)
  • Revision ID: package-import@ubuntu.com-20130527150157-tdkrsjpuvht7v0qx
Tags: 1:3.4~svn182733-1~exp1
* New snapshot release (3.4 release)
* Add a symlink of libLLVM-3.4.so.1 to usr/lib/llvm-3.4/lib/libLLVM-3.4.so
    to fix make the llvm-config-3.4 --libdir work (Closes: #708677)
  * Various packages rename to allow co installations:
    * libclang1 => libclang1-3.4
    * libclang1-dbg => libclang1-3.4-dbg
    * libclang-dev => libclang-3.4-dev
    * libclang-common-dev => libclang-common-3.4-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
// License. See LICENSE.TXT for details.
7
7
//
8
8
//===----------------------------------------------------------------------===//
9
 
//
10
 
// This file implements a class to represent arbitrary precision integral
11
 
// constant values and operations on them.
12
 
//
 
9
///
 
10
/// \file
 
11
/// \brief This file implements a class to represent arbitrary precision
 
12
/// integral constant values and operations on them.
 
13
///
13
14
//===----------------------------------------------------------------------===//
14
15
 
15
16
#ifndef LLVM_ADT_APINT_H
24
25
#include <string>
25
26
 
26
27
namespace llvm {
27
 
  class Deserializer;
28
 
  class FoldingSetNodeID;
29
 
  class Serializer;
30
 
  class StringRef;
31
 
  class hash_code;
32
 
  class raw_ostream;
33
 
 
34
 
  template<typename T>
35
 
  class SmallVectorImpl;
36
 
 
37
 
  // An unsigned host type used as a single part of a multi-part
38
 
  // bignum.
39
 
  typedef uint64_t integerPart;
40
 
 
41
 
  const unsigned int host_char_bit = 8;
42
 
  const unsigned int integerPartWidth = host_char_bit *
43
 
    static_cast<unsigned int>(sizeof(integerPart));
 
28
class Deserializer;
 
29
class FoldingSetNodeID;
 
30
class Serializer;
 
31
class StringRef;
 
32
class hash_code;
 
33
class raw_ostream;
 
34
 
 
35
template <typename T> class SmallVectorImpl;
 
36
 
 
37
// An unsigned host type used as a single part of a multi-part
 
38
// bignum.
 
39
typedef uint64_t integerPart;
 
40
 
 
41
const unsigned int host_char_bit = 8;
 
42
const unsigned int integerPartWidth =
 
43
    host_char_bit * static_cast<unsigned int>(sizeof(integerPart));
44
44
 
45
45
//===----------------------------------------------------------------------===//
46
46
//                              APInt Class
47
47
//===----------------------------------------------------------------------===//
48
48
 
49
 
/// APInt - This class represents arbitrary precision constant integral values.
50
 
/// It is a functional replacement for common case unsigned integer type like
 
49
/// \brief Class for arbitrary precision integers.
 
50
///
 
51
/// APInt is a functional replacement for common case unsigned integer type like
51
52
/// "unsigned", "unsigned long" or "uint64_t", but also allows non-byte-width
52
53
/// integer sizes and large integer value types such as 3-bits, 15-bits, or more
53
54
/// than 64-bits of precision. APInt provides a variety of arithmetic operators
71
72
///   * In general, the class tries to follow the style of computation that LLVM
72
73
///     uses in its IR. This simplifies its use for LLVM.
73
74
///
74
 
/// @brief Class for arbitrary precision integers.
75
75
class APInt {
76
 
  unsigned BitWidth;      ///< The number of bits in this APInt.
 
76
  unsigned BitWidth; ///< The number of bits in this APInt.
77
77
 
78
78
  /// This union is used to store the integer value. When the
79
79
  /// integer bit-width <= 64, it uses VAL, otherwise it uses pVal.
80
80
  union {
81
 
    uint64_t VAL;    ///< Used to store the <= 64 bits integer value.
82
 
    uint64_t *pVal;  ///< Used to store the >64 bits integer value.
 
81
    uint64_t VAL;   ///< Used to store the <= 64 bits integer value.
 
82
    uint64_t *pVal; ///< Used to store the >64 bits integer value.
83
83
  };
84
84
 
85
85
  /// This enum is used to hold the constants we needed for APInt.
86
86
  enum {
87
87
    /// Bits in a word
88
 
    APINT_BITS_PER_WORD = static_cast<unsigned int>(sizeof(uint64_t)) *
89
 
                          CHAR_BIT,
 
88
    APINT_BITS_PER_WORD =
 
89
        static_cast<unsigned int>(sizeof(uint64_t)) * CHAR_BIT,
90
90
    /// Byte size of a word
91
91
    APINT_WORD_SIZE = static_cast<unsigned int>(sizeof(uint64_t))
92
92
  };
93
93
 
 
94
  /// \brief Fast internal constructor
 
95
  ///
94
96
  /// This constructor is used only internally for speed of construction of
95
97
  /// temporaries. It is unsafe for general use so it is not public.
96
 
  /// @brief Fast internal constructor
97
 
  APInt(uint64_t* val, unsigned bits) : BitWidth(bits), pVal(val) { }
98
 
 
99
 
  /// @returns true if the number of bits <= 64, false otherwise.
100
 
  /// @brief Determine if this APInt just has one word to store value.
101
 
  bool isSingleWord() const {
102
 
    return BitWidth <= APINT_BITS_PER_WORD;
103
 
  }
104
 
 
105
 
  /// @returns the word position for the specified bit position.
106
 
  /// @brief Determine which word a bit is in.
 
98
  APInt(uint64_t *val, unsigned bits) : BitWidth(bits), pVal(val) {}
 
99
 
 
100
  /// \brief Determine if this APInt just has one word to store value.
 
101
  ///
 
102
  /// \returns true if the number of bits <= 64, false otherwise.
 
103
  bool isSingleWord() const { return BitWidth <= APINT_BITS_PER_WORD; }
 
104
 
 
105
  /// \brief Determine which word a bit is in.
 
106
  ///
 
107
  /// \returns the word position for the specified bit position.
107
108
  static unsigned whichWord(unsigned bitPosition) {
108
109
    return bitPosition / APINT_BITS_PER_WORD;
109
110
  }
110
111
 
111
 
  /// @returns the bit position in a word for the specified bit position
 
112
  /// \brief Determine which bit in a word a bit is in.
 
113
  ///
 
114
  /// \returns the bit position in a word for the specified bit position
112
115
  /// in the APInt.
113
 
  /// @brief Determine which bit in a word a bit is in.
114
116
  static unsigned whichBit(unsigned bitPosition) {
115
117
    return bitPosition % APINT_BITS_PER_WORD;
116
118
  }
117
119
 
 
120
  /// \brief Get a single bit mask.
 
121
  ///
 
122
  /// \returns a uint64_t with only bit at "whichBit(bitPosition)" set
118
123
  /// This method generates and returns a uint64_t (word) mask for a single
119
124
  /// bit at a specific bit position. This is used to mask the bit in the
120
125
  /// corresponding word.
121
 
  /// @returns a uint64_t with only bit at "whichBit(bitPosition)" set
122
 
  /// @brief Get a single bit mask.
123
126
  static uint64_t maskBit(unsigned bitPosition) {
124
127
    return 1ULL << whichBit(bitPosition);
125
128
  }
126
129
 
 
130
  /// \brief Clear unused high order bits
 
131
  ///
127
132
  /// This method is used internally to clear the to "N" bits in the high order
128
133
  /// word that are not used by the APInt. This is needed after the most
129
134
  /// significant word is assigned a value to ensure that those bits are
130
135
  /// zero'd out.
131
 
  /// @brief Clear unused high order bits
132
 
  APInt& clearUnusedBits() {
 
136
  APInt &clearUnusedBits() {
133
137
    // Compute how many bits are used in the final word
134
138
    unsigned wordBits = BitWidth % APINT_BITS_PER_WORD;
135
139
    if (wordBits == 0)
147
151
    return *this;
148
152
  }
149
153
 
150
 
  /// @returns the corresponding word for the specified bit position.
151
 
  /// @brief Get the word corresponding to a bit position
 
154
  /// \brief Get the word corresponding to a bit position
 
155
  /// \returns the corresponding word for the specified bit position.
152
156
  uint64_t getWord(unsigned bitPosition) const {
153
157
    return isSingleWord() ? VAL : pVal[whichWord(bitPosition)];
154
158
  }
155
159
 
 
160
  /// \brief Convert a char array into an APInt
 
161
  ///
 
162
  /// \param radix 2, 8, 10, 16, or 36
156
163
  /// Converts a string into a number.  The string must be non-empty
157
164
  /// and well-formed as a number of the given base. The bit-width
158
165
  /// must be sufficient to hold the result.
162
169
  /// StringRef::getAsInteger is superficially similar but (1) does
163
170
  /// not assume that the string is well-formed and (2) grows the
164
171
  /// result to hold the input.
165
 
  ///
166
 
  /// @param radix 2, 8, 10, 16, or 36
167
 
  /// @brief Convert a char array into an APInt
168
172
  void fromString(unsigned numBits, StringRef str, uint8_t radix);
169
173
 
 
174
  /// \brief An internal division function for dividing APInts.
 
175
  ///
170
176
  /// This is used by the toString method to divide by the radix. It simply
171
177
  /// provides a more convenient form of divide for internal use since KnuthDiv
172
178
  /// has specific constraints on its inputs. If those constraints are not met
173
179
  /// then it provides a simpler form of divide.
174
 
  /// @brief An internal division function for dividing APInts.
175
 
  static void divide(const APInt LHS, unsigned lhsWords,
176
 
                     const APInt &RHS, unsigned rhsWords,
177
 
                     APInt *Quotient, APInt *Remainder);
 
180
  static void divide(const APInt LHS, unsigned lhsWords, const APInt &RHS,
 
181
                     unsigned rhsWords, APInt *Quotient, APInt *Remainder);
178
182
 
179
183
  /// out-of-line slow case for inline constructor
180
184
  void initSlowCase(unsigned numBits, uint64_t val, bool isSigned);
183
187
  void initFromArray(ArrayRef<uint64_t> array);
184
188
 
185
189
  /// out-of-line slow case for inline copy constructor
186
 
  void initSlowCase(const APInt& that);
 
190
  void initSlowCase(const APInt &that);
187
191
 
188
192
  /// out-of-line slow case for shl
189
193
  APInt shlSlowCase(unsigned shiftAmt) const;
190
194
 
191
195
  /// out-of-line slow case for operator&
192
 
  APInt AndSlowCase(const APInt& RHS) const;
 
196
  APInt AndSlowCase(const APInt &RHS) const;
193
197
 
194
198
  /// out-of-line slow case for operator|
195
 
  APInt OrSlowCase(const APInt& RHS) const;
 
199
  APInt OrSlowCase(const APInt &RHS) const;
196
200
 
197
201
  /// out-of-line slow case for operator^
198
 
  APInt XorSlowCase(const APInt& RHS) const;
 
202
  APInt XorSlowCase(const APInt &RHS) const;
199
203
 
200
204
  /// out-of-line slow case for operator=
201
 
  APInt& AssignSlowCase(const APInt& RHS);
 
205
  APInt &AssignSlowCase(const APInt &RHS);
202
206
 
203
207
  /// out-of-line slow case for operator==
204
 
  bool EqualSlowCase(const APInt& RHS) const;
 
208
  bool EqualSlowCase(const APInt &RHS) const;
205
209
 
206
210
  /// out-of-line slow case for operator==
207
211
  bool EqualSlowCase(uint64_t Val) const;
216
220
  unsigned countPopulationSlowCase() const;
217
221
 
218
222
public:
219
 
  /// @name Constructors
 
223
  /// \name Constructors
220
224
  /// @{
 
225
 
 
226
  /// \brief Create a new APInt of numBits width, initialized as val.
 
227
  ///
221
228
  /// If isSigned is true then val is treated as if it were a signed value
222
229
  /// (i.e. as an int64_t) and the appropriate sign extension to the bit width
223
230
  /// will be done. Otherwise, no sign extension occurs (high order bits beyond
224
231
  /// the range of val are zero filled).
225
 
  /// @param numBits the bit width of the constructed APInt
226
 
  /// @param val the initial value of the APInt
227
 
  /// @param isSigned how to treat signedness of val
228
 
  /// @brief Create a new APInt of numBits width, initialized as val.
 
232
  ///
 
233
  /// \param numBits the bit width of the constructed APInt
 
234
  /// \param val the initial value of the APInt
 
235
  /// \param isSigned how to treat signedness of val
229
236
  APInt(unsigned numBits, uint64_t val, bool isSigned = false)
230
 
    : BitWidth(numBits), VAL(0) {
 
237
      : BitWidth(numBits), VAL(0) {
231
238
    assert(BitWidth && "bitwidth too small");
232
239
    if (isSingleWord())
233
240
      VAL = val;
236
243
    clearUnusedBits();
237
244
  }
238
245
 
 
246
  /// \brief Construct an APInt of numBits width, initialized as bigVal[].
 
247
  ///
239
248
  /// Note that bigVal.size() can be smaller or larger than the corresponding
240
249
  /// bit width but any extraneous bits will be dropped.
241
 
  /// @param numBits the bit width of the constructed APInt
242
 
  /// @param bigVal a sequence of words to form the initial value of the APInt
243
 
  /// @brief Construct an APInt of numBits width, initialized as bigVal[].
 
250
  ///
 
251
  /// \param numBits the bit width of the constructed APInt
 
252
  /// \param bigVal a sequence of words to form the initial value of the APInt
244
253
  APInt(unsigned numBits, ArrayRef<uint64_t> bigVal);
 
254
 
245
255
  /// Equivalent to APInt(numBits, ArrayRef<uint64_t>(bigVal, numWords)), but
246
256
  /// deprecated because this constructor is prone to ambiguity with the
247
257
  /// APInt(unsigned, uint64_t, bool) constructor.
251
261
  /// constructor.
252
262
  APInt(unsigned numBits, unsigned numWords, const uint64_t bigVal[]);
253
263
 
 
264
  /// \brief Construct an APInt from a string representation.
 
265
  ///
254
266
  /// This constructor interprets the string \p str in the given radix. The
255
267
  /// interpretation stops when the first character that is not suitable for the
256
268
  /// radix is encountered, or the end of the string. Acceptable radix values
257
 
  /// are 2, 8, 10, 16, and 36. It is an error for the value implied by the 
 
269
  /// are 2, 8, 10, 16, and 36. It is an error for the value implied by the
258
270
  /// string to require more bits than numBits.
259
271
  ///
260
 
  /// @param numBits the bit width of the constructed APInt
261
 
  /// @param str the string to be interpreted
262
 
  /// @param radix the radix to use for the conversion 
263
 
  /// @brief Construct an APInt from a string representation.
 
272
  /// \param numBits the bit width of the constructed APInt
 
273
  /// \param str the string to be interpreted
 
274
  /// \param radix the radix to use for the conversion
264
275
  APInt(unsigned numBits, StringRef str, uint8_t radix);
265
276
 
266
277
  /// Simply makes *this a copy of that.
267
278
  /// @brief Copy Constructor.
268
 
  APInt(const APInt& that)
269
 
    : BitWidth(that.BitWidth), VAL(0) {
 
279
  APInt(const APInt &that) : BitWidth(that.BitWidth), VAL(0) {
270
280
    assert(BitWidth && "bitwidth too small");
271
281
    if (isSingleWord())
272
282
      VAL = that.VAL;
275
285
  }
276
286
 
277
287
#if LLVM_HAS_RVALUE_REFERENCES
278
 
  /// @brief Move Constructor.
279
 
  APInt(APInt&& that) : BitWidth(that.BitWidth), VAL(that.VAL) {
 
288
  /// \brief Move Constructor.
 
289
  APInt(APInt &&that) : BitWidth(that.BitWidth), VAL(that.VAL) {
280
290
    that.BitWidth = 0;
281
291
  }
282
292
#endif
283
293
 
284
 
  /// @brief Destructor.
 
294
  /// \brief Destructor.
285
295
  ~APInt() {
286
296
    if (!isSingleWord())
287
 
      delete [] pVal;
 
297
      delete[] pVal;
288
298
  }
289
299
 
290
 
  /// Default constructor that creates an uninitialized APInt.  This is useful
291
 
  ///  for object deserialization (pair this with the static method Read).
 
300
  /// \brief Default constructor that creates an uninitialized APInt.
 
301
  ///
 
302
  /// This is useful for object deserialization (pair this with the static
 
303
  ///  method Read).
292
304
  explicit APInt() : BitWidth(1) {}
293
305
 
294
 
  /// Profile - Used to insert APInt objects, or objects that contain APInt
295
 
  ///  objects, into FoldingSets.
296
 
  void Profile(FoldingSetNodeID& id) const;
 
306
  /// Used to insert APInt objects, or objects that contain APInt objects, into
 
307
  ///  FoldingSets.
 
308
  void Profile(FoldingSetNodeID &id) const;
297
309
 
298
310
  /// @}
299
 
  /// @name Value Tests
 
311
  /// \name Value Tests
300
312
  /// @{
 
313
 
 
314
  /// \brief Determine sign of this APInt.
 
315
  ///
301
316
  /// This tests the high bit of this APInt to determine if it is set.
302
 
  /// @returns true if this APInt is negative, false otherwise
303
 
  /// @brief Determine sign of this APInt.
304
 
  bool isNegative() const {
305
 
    return (*this)[BitWidth - 1];
306
 
  }
 
317
  ///
 
318
  /// \returns true if this APInt is negative, false otherwise
 
319
  bool isNegative() const { return (*this)[BitWidth - 1]; }
307
320
 
 
321
  /// \brief Determine if this APInt Value is non-negative (>= 0)
 
322
  ///
308
323
  /// This tests the high bit of the APInt to determine if it is unset.
309
 
  /// @brief Determine if this APInt Value is non-negative (>= 0)
310
 
  bool isNonNegative() const {
311
 
    return !isNegative();
312
 
  }
 
324
  bool isNonNegative() const { return !isNegative(); }
313
325
 
 
326
  /// \brief Determine if this APInt Value is positive.
 
327
  ///
314
328
  /// This tests if the value of this APInt is positive (> 0). Note
315
329
  /// that 0 is not a positive value.
316
 
  /// @returns true if this APInt is positive.
317
 
  /// @brief Determine if this APInt Value is positive.
318
 
  bool isStrictlyPositive() const {
319
 
    return isNonNegative() && !!*this;
320
 
  }
 
330
  ///
 
331
  /// \returns true if this APInt is positive.
 
332
  bool isStrictlyPositive() const { return isNonNegative() && !!*this; }
321
333
 
 
334
  /// \brief Determine if all bits are set
 
335
  ///
322
336
  /// This checks to see if the value has all bits of the APInt are set or not.
323
 
  /// @brief Determine if all bits are set
324
 
  bool isAllOnesValue() const {
325
 
    return countPopulation() == BitWidth;
326
 
  }
 
337
  bool isAllOnesValue() const { return countPopulation() == BitWidth; }
327
338
 
 
339
  /// \brief Determine if this is the largest unsigned value.
 
340
  ///
328
341
  /// This checks to see if the value of this APInt is the maximum unsigned
329
342
  /// value for the APInt's bit width.
330
 
  /// @brief Determine if this is the largest unsigned value.
331
 
  bool isMaxValue() const {
332
 
    return countPopulation() == BitWidth;
333
 
  }
 
343
  bool isMaxValue() const { return countPopulation() == BitWidth; }
334
344
 
 
345
  /// \brief Determine if this is the largest signed value.
 
346
  ///
335
347
  /// This checks to see if the value of this APInt is the maximum signed
336
348
  /// value for the APInt's bit width.
337
 
  /// @brief Determine if this is the largest signed value.
338
349
  bool isMaxSignedValue() const {
339
 
    return BitWidth == 1 ? VAL == 0 :
340
 
                          !isNegative() && countPopulation() == BitWidth - 1;
 
350
    return BitWidth == 1 ? VAL == 0
 
351
                         : !isNegative() && countPopulation() == BitWidth - 1;
341
352
  }
342
353
 
 
354
  /// \brief Determine if this is the smallest unsigned value.
 
355
  ///
343
356
  /// This checks to see if the value of this APInt is the minimum unsigned
344
357
  /// value for the APInt's bit width.
345
 
  /// @brief Determine if this is the smallest unsigned value.
346
 
  bool isMinValue() const {
347
 
    return !*this;
348
 
  }
 
358
  bool isMinValue() const { return !*this; }
349
359
 
 
360
  /// \brief Determine if this is the smallest signed value.
 
361
  ///
350
362
  /// This checks to see if the value of this APInt is the minimum signed
351
363
  /// value for the APInt's bit width.
352
 
  /// @brief Determine if this is the smallest signed value.
353
364
  bool isMinSignedValue() const {
354
365
    return BitWidth == 1 ? VAL == 1 : isNegative() && isPowerOf2();
355
366
  }
356
367
 
357
 
  /// @brief Check if this APInt has an N-bits unsigned integer value.
 
368
  /// \brief Check if this APInt has an N-bits unsigned integer value.
358
369
  bool isIntN(unsigned N) const {
359
370
    assert(N && "N == 0 ???");
360
371
    return getActiveBits() <= N;
361
372
  }
362
373
 
363
 
  /// @brief Check if this APInt has an N-bits signed integer value.
 
374
  /// \brief Check if this APInt has an N-bits signed integer value.
364
375
  bool isSignedIntN(unsigned N) const {
365
376
    assert(N && "N == 0 ???");
366
377
    return getMinSignedBits() <= N;
367
378
  }
368
379
 
369
 
  /// @returns true if the argument APInt value is a power of two > 0.
 
380
  /// \brief Check if this APInt's value is a power of two greater than zero.
 
381
  ///
 
382
  /// \returns true if the argument APInt value is a power of two > 0.
370
383
  bool isPowerOf2() const {
371
384
    if (isSingleWord())
372
385
      return isPowerOf2_64(VAL);
373
386
    return countPopulationSlowCase() == 1;
374
387
  }
375
388
 
376
 
  /// isSignBit - Return true if this is the value returned by getSignBit.
 
389
  /// \brief Check if the APInt's value is returned by getSignBit.
 
390
  ///
 
391
  /// \returns true if this is the value returned by getSignBit.
377
392
  bool isSignBit() const { return isMinSignedValue(); }
378
393
 
 
394
  /// \brief Convert APInt to a boolean value.
 
395
  ///
379
396
  /// This converts the APInt to a boolean value as a test against zero.
380
 
  /// @brief Boolean conversion function.
381
 
  bool getBoolValue() const {
382
 
    return !!*this;
383
 
  }
 
397
  bool getBoolValue() const { return !!*this; }
384
398
 
385
 
  /// getLimitedValue - If this value is smaller than the specified limit,
386
 
  /// return it, otherwise return the limit value.  This causes the value
387
 
  /// to saturate to the limit.
 
399
  /// If this value is smaller than the specified limit, return it, otherwise
 
400
  /// return the limit value.  This causes the value to saturate to the limit.
388
401
  uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
389
 
    return (getActiveBits() > 64 || getZExtValue() > Limit) ?
390
 
      Limit :  getZExtValue();
 
402
    return (getActiveBits() > 64 || getZExtValue() > Limit) ? Limit
 
403
                                                            : getZExtValue();
391
404
  }
392
405
 
393
406
  /// @}
394
 
  /// @name Value Generators
 
407
  /// \name Value Generators
395
408
  /// @{
396
 
  /// @brief Gets maximum unsigned value of APInt for specific bit width.
 
409
 
 
410
  /// \brief Gets maximum unsigned value of APInt for specific bit width.
397
411
  static APInt getMaxValue(unsigned numBits) {
398
412
    return getAllOnesValue(numBits);
399
413
  }
400
414
 
401
 
  /// @brief Gets maximum signed value of APInt for a specific bit width.
 
415
  /// \brief Gets maximum signed value of APInt for a specific bit width.
402
416
  static APInt getSignedMaxValue(unsigned numBits) {
403
417
    APInt API = getAllOnesValue(numBits);
404
418
    API.clearBit(numBits - 1);
405
419
    return API;
406
420
  }
407
421
 
408
 
  /// @brief Gets minimum unsigned value of APInt for a specific bit width.
409
 
  static APInt getMinValue(unsigned numBits) {
410
 
    return APInt(numBits, 0);
411
 
  }
 
422
  /// \brief Gets minimum unsigned value of APInt for a specific bit width.
 
423
  static APInt getMinValue(unsigned numBits) { return APInt(numBits, 0); }
412
424
 
413
 
  /// @brief Gets minimum signed value of APInt for a specific bit width.
 
425
  /// \brief Gets minimum signed value of APInt for a specific bit width.
414
426
  static APInt getSignedMinValue(unsigned numBits) {
415
427
    APInt API(numBits, 0);
416
428
    API.setBit(numBits - 1);
417
429
    return API;
418
430
  }
419
431
 
420
 
  /// getSignBit - This is just a wrapper function of getSignedMinValue(), and
421
 
  /// it helps code readability when we want to get a SignBit.
422
 
  /// @brief Get the SignBit for a specific bit width.
 
432
  /// \brief Get the SignBit for a specific bit width.
 
433
  ///
 
434
  /// This is just a wrapper function of getSignedMinValue(), and it helps code
 
435
  /// readability when we want to get a SignBit.
423
436
  static APInt getSignBit(unsigned BitWidth) {
424
437
    return getSignedMinValue(BitWidth);
425
438
  }
426
439
 
427
 
  /// @returns the all-ones value for an APInt of the specified bit-width.
428
 
  /// @brief Get the all-ones value.
 
440
  /// \brief Get the all-ones value.
 
441
  ///
 
442
  /// \returns the all-ones value for an APInt of the specified bit-width.
429
443
  static APInt getAllOnesValue(unsigned numBits) {
430
444
    return APInt(numBits, UINT64_MAX, true);
431
445
  }
432
446
 
433
 
  /// @returns the '0' value for an APInt of the specified bit-width.
434
 
  /// @brief Get the '0' value.
435
 
  static APInt getNullValue(unsigned numBits) {
436
 
    return APInt(numBits, 0);
437
 
  }
 
447
  /// \brief Get the '0' value.
 
448
  ///
 
449
  /// \returns the '0' value for an APInt of the specified bit-width.
 
450
  static APInt getNullValue(unsigned numBits) { return APInt(numBits, 0); }
438
451
 
 
452
  /// \brief Compute an APInt containing numBits highbits from this APInt.
 
453
  ///
439
454
  /// Get an APInt with the same BitWidth as this APInt, just zero mask
440
455
  /// the low bits and right shift to the least significant bit.
441
 
  /// @returns the high "numBits" bits of this APInt.
 
456
  ///
 
457
  /// \returns the high "numBits" bits of this APInt.
442
458
  APInt getHiBits(unsigned numBits) const;
443
459
 
 
460
  /// \brief Compute an APInt containing numBits lowbits from this APInt.
 
461
  ///
444
462
  /// Get an APInt with the same BitWidth as this APInt, just zero mask
445
463
  /// the high bits.
446
 
  /// @returns the low "numBits" bits of this APInt.
 
464
  ///
 
465
  /// \returns the low "numBits" bits of this APInt.
447
466
  APInt getLoBits(unsigned numBits) const;
448
467
 
449
 
  /// getOneBitSet - Return an APInt with exactly one bit set in the result.
 
468
  /// \brief Return an APInt with exactly one bit set in the result.
450
469
  static APInt getOneBitSet(unsigned numBits, unsigned BitNo) {
451
470
    APInt Res(numBits, 0);
452
471
    Res.setBit(BitNo);
453
472
    return Res;
454
473
  }
455
 
  
 
474
 
 
475
  /// \brief Get a value with a block of bits set.
 
476
  ///
456
477
  /// Constructs an APInt value that has a contiguous range of bits set. The
457
478
  /// bits from loBit (inclusive) to hiBit (exclusive) will be set. All other
458
479
  /// bits will be zero. For example, with parameters(32, 0, 16) you would get
459
480
  /// 0x0000FFFF. If hiBit is less than loBit then the set bits "wrap". For
460
481
  /// example, with parameters (32, 28, 4), you would get 0xF000000F.
461
 
  /// @param numBits the intended bit width of the result
462
 
  /// @param loBit the index of the lowest bit set.
463
 
  /// @param hiBit the index of the highest bit set.
464
 
  /// @returns An APInt value with the requested bits set.
465
 
  /// @brief Get a value with a block of bits set.
 
482
  ///
 
483
  /// \param numBits the intended bit width of the result
 
484
  /// \param loBit the index of the lowest bit set.
 
485
  /// \param hiBit the index of the highest bit set.
 
486
  ///
 
487
  /// \returns An APInt value with the requested bits set.
466
488
  static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit) {
467
489
    assert(hiBit <= numBits && "hiBit out of range");
468
490
    assert(loBit < numBits && "loBit out of range");
469
491
    if (hiBit < loBit)
470
492
      return getLowBitsSet(numBits, hiBit) |
471
 
             getHighBitsSet(numBits, numBits-loBit);
472
 
    return getLowBitsSet(numBits, hiBit-loBit).shl(loBit);
 
493
             getHighBitsSet(numBits, numBits - loBit);
 
494
    return getLowBitsSet(numBits, hiBit - loBit).shl(loBit);
473
495
  }
474
496
 
 
497
  /// \brief Get a value with high bits set
 
498
  ///
475
499
  /// Constructs an APInt value that has the top hiBitsSet bits set.
476
 
  /// @param numBits the bitwidth of the result
477
 
  /// @param hiBitsSet the number of high-order bits set in the result.
478
 
  /// @brief Get a value with high bits set
 
500
  ///
 
501
  /// \param numBits the bitwidth of the result
 
502
  /// \param hiBitsSet the number of high-order bits set in the result.
479
503
  static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet) {
480
504
    assert(hiBitsSet <= numBits && "Too many bits to set!");
481
505
    // Handle a degenerate case, to avoid shifting by word size
488
512
    return getAllOnesValue(numBits).shl(shiftAmt);
489
513
  }
490
514
 
 
515
  /// \brief Get a value with low bits set
 
516
  ///
491
517
  /// Constructs an APInt value that has the bottom loBitsSet bits set.
492
 
  /// @param numBits the bitwidth of the result
493
 
  /// @param loBitsSet the number of low-order bits set in the result.
494
 
  /// @brief Get a value with low bits set
 
518
  ///
 
519
  /// \param numBits the bitwidth of the result
 
520
  /// \param loBitsSet the number of low-order bits set in the result.
495
521
  static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet) {
496
522
    assert(loBitsSet <= numBits && "Too many bits to set!");
497
523
    // Handle a degenerate case, to avoid shifting by word size
527
553
 
528
554
    return I1.zext(I2.getBitWidth()) == I2;
529
555
  }
530
 
  
 
556
 
531
557
  /// \brief Overload to compute a hash_code for an APInt value.
532
558
  friend hash_code hash_value(const APInt &Arg);
533
559
 
534
560
  /// This function returns a pointer to the internal storage of the APInt.
535
561
  /// This is useful for writing out the APInt in binary form without any
536
562
  /// conversions.
537
 
  const uint64_t* getRawData() const {
 
563
  const uint64_t *getRawData() const {
538
564
    if (isSingleWord())
539
565
      return &VAL;
540
566
    return &pVal[0];
541
567
  }
542
568
 
543
569
  /// @}
544
 
  /// @name Unary Operators
 
570
  /// \name Unary Operators
545
571
  /// @{
546
 
  /// @returns a new APInt value representing *this incremented by one
547
 
  /// @brief Postfix increment operator.
 
572
 
 
573
  /// \brief Postfix increment operator.
 
574
  ///
 
575
  /// \returns a new APInt value representing *this incremented by one
548
576
  const APInt operator++(int) {
549
577
    APInt API(*this);
550
578
    ++(*this);
551
579
    return API;
552
580
  }
553
581
 
554
 
  /// @returns *this incremented by one
555
 
  /// @brief Prefix increment operator.
556
 
  APInt& operator++();
 
582
  /// \brief Prefix increment operator.
 
583
  ///
 
584
  /// \returns *this incremented by one
 
585
  APInt &operator++();
557
586
 
558
 
  /// @returns a new APInt representing *this decremented by one.
559
 
  /// @brief Postfix decrement operator.
 
587
  /// \brief Postfix decrement operator.
 
588
  ///
 
589
  /// \returns a new APInt representing *this decremented by one.
560
590
  const APInt operator--(int) {
561
591
    APInt API(*this);
562
592
    --(*this);
563
593
    return API;
564
594
  }
565
595
 
566
 
  /// @returns *this decremented by one.
567
 
  /// @brief Prefix decrement operator.
568
 
  APInt& operator--();
 
596
  /// \brief Prefix decrement operator.
 
597
  ///
 
598
  /// \returns *this decremented by one.
 
599
  APInt &operator--();
569
600
 
 
601
  /// \brief Unary bitwise complement operator.
 
602
  ///
570
603
  /// Performs a bitwise complement operation on this APInt.
571
 
  /// @returns an APInt that is the bitwise complement of *this
572
 
  /// @brief Unary bitwise complement operator.
 
604
  ///
 
605
  /// \returns an APInt that is the bitwise complement of *this
573
606
  APInt operator~() const {
574
607
    APInt Result(*this);
575
608
    Result.flipAllBits();
576
609
    return Result;
577
610
  }
578
611
 
 
612
  /// \brief Unary negation operator
 
613
  ///
579
614
  /// Negates *this using two's complement logic.
580
 
  /// @returns An APInt value representing the negation of *this.
581
 
  /// @brief Unary negation operator
582
 
  APInt operator-() const {
583
 
    return APInt(BitWidth, 0) - (*this);
584
 
  }
 
615
  ///
 
616
  /// \returns An APInt value representing the negation of *this.
 
617
  APInt operator-() const { return APInt(BitWidth, 0) - (*this); }
585
618
 
 
619
  /// \brief Logical negation operator.
 
620
  ///
586
621
  /// Performs logical negation operation on this APInt.
587
 
  /// @returns true if *this is zero, false otherwise.
588
 
  /// @brief Logical negation operator.
 
622
  ///
 
623
  /// \returns true if *this is zero, false otherwise.
589
624
  bool operator!() const {
590
625
    if (isSingleWord())
591
626
      return !VAL;
597
632
  }
598
633
 
599
634
  /// @}
600
 
  /// @name Assignment Operators
 
635
  /// \name Assignment Operators
601
636
  /// @{
602
 
  /// @returns *this after assignment of RHS.
603
 
  /// @brief Copy assignment operator.
604
 
  APInt& operator=(const APInt& RHS) {
 
637
 
 
638
  /// \brief Copy assignment operator.
 
639
  ///
 
640
  /// \returns *this after assignment of RHS.
 
641
  APInt &operator=(const APInt &RHS) {
605
642
    // If the bitwidths are the same, we can avoid mucking with memory
606
643
    if (isSingleWord() && RHS.isSingleWord()) {
607
644
      VAL = RHS.VAL;
614
651
 
615
652
#if LLVM_HAS_RVALUE_REFERENCES
616
653
  /// @brief Move assignment operator.
617
 
  APInt& operator=(APInt&& that) {
 
654
  APInt &operator=(APInt &&that) {
618
655
    if (!isSingleWord())
619
 
      delete [] pVal;
 
656
      delete[] pVal;
620
657
 
621
658
    BitWidth = that.BitWidth;
622
659
    VAL = that.VAL;
627
664
  }
628
665
#endif
629
666
 
 
667
  /// \brief Assignment operator.
 
668
  ///
630
669
  /// The RHS value is assigned to *this. If the significant bits in RHS exceed
631
670
  /// the bit width, the excess bits are truncated. If the bit width is larger
632
671
  /// than 64, the value is zero filled in the unspecified high order bits.
633
 
  /// @returns *this after assignment of RHS value.
634
 
  /// @brief Assignment operator.
635
 
  APInt& operator=(uint64_t RHS);
 
672
  ///
 
673
  /// \returns *this after assignment of RHS value.
 
674
  APInt &operator=(uint64_t RHS);
636
675
 
 
676
  /// \brief Bitwise AND assignment operator.
 
677
  ///
637
678
  /// Performs a bitwise AND operation on this APInt and RHS. The result is
638
679
  /// assigned to *this.
639
 
  /// @returns *this after ANDing with RHS.
640
 
  /// @brief Bitwise AND assignment operator.
641
 
  APInt& operator&=(const APInt& RHS);
 
680
  ///
 
681
  /// \returns *this after ANDing with RHS.
 
682
  APInt &operator&=(const APInt &RHS);
642
683
 
 
684
  /// \brief Bitwise OR assignment operator.
 
685
  ///
643
686
  /// Performs a bitwise OR operation on this APInt and RHS. The result is
644
687
  /// assigned *this;
645
 
  /// @returns *this after ORing with RHS.
646
 
  /// @brief Bitwise OR assignment operator.
647
 
  APInt& operator|=(const APInt& RHS);
 
688
  ///
 
689
  /// \returns *this after ORing with RHS.
 
690
  APInt &operator|=(const APInt &RHS);
648
691
 
 
692
  /// \brief Bitwise OR assignment operator.
 
693
  ///
649
694
  /// Performs a bitwise OR operation on this APInt and RHS. RHS is
650
695
  /// logically zero-extended or truncated to match the bit-width of
651
696
  /// the LHS.
652
 
  /// 
653
 
  /// @brief Bitwise OR assignment operator.
654
 
  APInt& operator|=(uint64_t RHS) {
 
697
  APInt &operator|=(uint64_t RHS) {
655
698
    if (isSingleWord()) {
656
699
      VAL |= RHS;
657
700
      clearUnusedBits();
661
704
    return *this;
662
705
  }
663
706
 
 
707
  /// \brief Bitwise XOR assignment operator.
 
708
  ///
664
709
  /// Performs a bitwise XOR operation on this APInt and RHS. The result is
665
710
  /// assigned to *this.
666
 
  /// @returns *this after XORing with RHS.
667
 
  /// @brief Bitwise XOR assignment operator.
668
 
  APInt& operator^=(const APInt& RHS);
 
711
  ///
 
712
  /// \returns *this after XORing with RHS.
 
713
  APInt &operator^=(const APInt &RHS);
669
714
 
 
715
  /// \brief Multiplication assignment operator.
 
716
  ///
670
717
  /// Multiplies this APInt by RHS and assigns the result to *this.
671
 
  /// @returns *this
672
 
  /// @brief Multiplication assignment operator.
673
 
  APInt& operator*=(const APInt& RHS);
 
718
  ///
 
719
  /// \returns *this
 
720
  APInt &operator*=(const APInt &RHS);
674
721
 
 
722
  /// \brief Addition assignment operator.
 
723
  ///
675
724
  /// Adds RHS to *this and assigns the result to *this.
676
 
  /// @returns *this
677
 
  /// @brief Addition assignment operator.
678
 
  APInt& operator+=(const APInt& RHS);
 
725
  ///
 
726
  /// \returns *this
 
727
  APInt &operator+=(const APInt &RHS);
679
728
 
 
729
  /// \brief Subtraction assignment operator.
 
730
  ///
680
731
  /// Subtracts RHS from *this and assigns the result to *this.
681
 
  /// @returns *this
682
 
  /// @brief Subtraction assignment operator.
683
 
  APInt& operator-=(const APInt& RHS);
 
732
  ///
 
733
  /// \returns *this
 
734
  APInt &operator-=(const APInt &RHS);
684
735
 
 
736
  /// \brief Left-shift assignment function.
 
737
  ///
685
738
  /// Shifts *this left by shiftAmt and assigns the result to *this.
686
 
  /// @returns *this after shifting left by shiftAmt
687
 
  /// @brief Left-shift assignment function.
688
 
  APInt& operator<<=(unsigned shiftAmt) {
 
739
  ///
 
740
  /// \returns *this after shifting left by shiftAmt
 
741
  APInt &operator<<=(unsigned shiftAmt) {
689
742
    *this = shl(shiftAmt);
690
743
    return *this;
691
744
  }
692
745
 
693
746
  /// @}
694
 
  /// @name Binary Operators
 
747
  /// \name Binary Operators
695
748
  /// @{
 
749
 
 
750
  /// \brief Bitwise AND operator.
 
751
  ///
696
752
  /// Performs a bitwise AND operation on *this and RHS.
697
 
  /// @returns An APInt value representing the bitwise AND of *this and RHS.
698
 
  /// @brief Bitwise AND operator.
699
 
  APInt operator&(const APInt& RHS) const {
 
753
  ///
 
754
  /// \returns An APInt value representing the bitwise AND of *this and RHS.
 
755
  APInt operator&(const APInt &RHS) const {
700
756
    assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
701
757
    if (isSingleWord())
702
758
      return APInt(getBitWidth(), VAL & RHS.VAL);
703
759
    return AndSlowCase(RHS);
704
760
  }
705
 
  APInt And(const APInt& RHS) const {
706
 
    return this->operator&(RHS);
707
 
  }
 
761
  APInt And(const APInt &RHS) const { return this->operator&(RHS); }
708
762
 
 
763
  /// \brief Bitwise OR operator.
 
764
  ///
709
765
  /// Performs a bitwise OR operation on *this and RHS.
710
 
  /// @returns An APInt value representing the bitwise OR of *this and RHS.
711
 
  /// @brief Bitwise OR operator.
712
 
  APInt operator|(const APInt& RHS) const {
 
766
  ///
 
767
  /// \returns An APInt value representing the bitwise OR of *this and RHS.
 
768
  APInt operator|(const APInt &RHS) const {
713
769
    assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
714
770
    if (isSingleWord())
715
771
      return APInt(getBitWidth(), VAL | RHS.VAL);
716
772
    return OrSlowCase(RHS);
717
773
  }
718
 
  APInt Or(const APInt& RHS) const {
719
 
    return this->operator|(RHS);
720
 
  }
721
 
 
 
774
 
 
775
  /// \brief Bitwise OR function.
 
776
  ///
 
777
  /// Performs a bitwise or on *this and RHS. This is implemented bny simply
 
778
  /// calling operator|.
 
779
  ///
 
780
  /// \returns An APInt value representing the bitwise OR of *this and RHS.
 
781
  APInt Or(const APInt &RHS) const { return this->operator|(RHS); }
 
782
 
 
783
  /// \brief Bitwise XOR operator.
 
784
  ///
722
785
  /// Performs a bitwise XOR operation on *this and RHS.
723
 
  /// @returns An APInt value representing the bitwise XOR of *this and RHS.
724
 
  /// @brief Bitwise XOR operator.
725
 
  APInt operator^(const APInt& RHS) const {
 
786
  ///
 
787
  /// \returns An APInt value representing the bitwise XOR of *this and RHS.
 
788
  APInt operator^(const APInt &RHS) const {
726
789
    assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
727
790
    if (isSingleWord())
728
791
      return APInt(BitWidth, VAL ^ RHS.VAL);
729
792
    return XorSlowCase(RHS);
730
793
  }
731
 
  APInt Xor(const APInt& RHS) const {
732
 
    return this->operator^(RHS);
733
 
  }
734
 
 
 
794
 
 
795
  /// \brief Bitwise XOR function.
 
796
  ///
 
797
  /// Performs a bitwise XOR operation on *this and RHS. This is implemented
 
798
  /// through the usage of operator^.
 
799
  ///
 
800
  /// \returns An APInt value representing the bitwise XOR of *this and RHS.
 
801
  APInt Xor(const APInt &RHS) const { return this->operator^(RHS); }
 
802
 
 
803
  /// \brief Multiplication operator.
 
804
  ///
735
805
  /// Multiplies this APInt by RHS and returns the result.
736
 
  /// @brief Multiplication operator.
737
 
  APInt operator*(const APInt& RHS) const;
 
806
  APInt operator*(const APInt &RHS) const;
738
807
 
 
808
  /// \brief Addition operator.
 
809
  ///
739
810
  /// Adds RHS to this APInt and returns the result.
740
 
  /// @brief Addition operator.
741
 
  APInt operator+(const APInt& RHS) const;
742
 
  APInt operator+(uint64_t RHS) const {
743
 
    return (*this) + APInt(BitWidth, RHS);
744
 
  }
 
811
  APInt operator+(const APInt &RHS) const;
 
812
  APInt operator+(uint64_t RHS) const { return (*this) + APInt(BitWidth, RHS); }
745
813
 
 
814
  /// \brief Subtraction operator.
 
815
  ///
746
816
  /// Subtracts RHS from this APInt and returns the result.
747
 
  /// @brief Subtraction operator.
748
 
  APInt operator-(const APInt& RHS) const;
749
 
  APInt operator-(uint64_t RHS) const {
750
 
    return (*this) - APInt(BitWidth, RHS);
751
 
  }
752
 
 
753
 
  APInt operator<<(unsigned Bits) const {
754
 
    return shl(Bits);
755
 
  }
756
 
 
757
 
  APInt operator<<(const APInt &Bits) const {
758
 
    return shl(Bits);
759
 
  }
760
 
 
 
817
  APInt operator-(const APInt &RHS) const;
 
818
  APInt operator-(uint64_t RHS) const { return (*this) - APInt(BitWidth, RHS); }
 
819
 
 
820
  /// \brief Left logical shift operator.
 
821
  ///
 
822
  /// Shifts this APInt left by \p Bits and returns the result.
 
823
  APInt operator<<(unsigned Bits) const { return shl(Bits); }
 
824
 
 
825
  /// \brief Left logical shift operator.
 
826
  ///
 
827
  /// Shifts this APInt left by \p Bits and returns the result.
 
828
  APInt operator<<(const APInt &Bits) const { return shl(Bits); }
 
829
 
 
830
  /// \brief Arithmetic right-shift function.
 
831
  ///
761
832
  /// Arithmetic right-shift this APInt by shiftAmt.
762
 
  /// @brief Arithmetic right-shift function.
763
833
  APInt ashr(unsigned shiftAmt) const;
764
834
 
 
835
  /// \brief Logical right-shift function.
 
836
  ///
765
837
  /// Logical right-shift this APInt by shiftAmt.
766
 
  /// @brief Logical right-shift function.
767
838
  APInt lshr(unsigned shiftAmt) const;
768
839
 
 
840
  /// \brief Left-shift function.
 
841
  ///
769
842
  /// Left-shift this APInt by shiftAmt.
770
 
  /// @brief Left-shift function.
771
843
  APInt shl(unsigned shiftAmt) const {
772
844
    assert(shiftAmt <= BitWidth && "Invalid shift amount");
773
845
    if (isSingleWord()) {
778
850
    return shlSlowCase(shiftAmt);
779
851
  }
780
852
 
781
 
  /// @brief Rotate left by rotateAmt.
 
853
  /// \brief Rotate left by rotateAmt.
782
854
  APInt rotl(unsigned rotateAmt) const;
783
855
 
784
 
  /// @brief Rotate right by rotateAmt.
 
856
  /// \brief Rotate right by rotateAmt.
785
857
  APInt rotr(unsigned rotateAmt) const;
786
858
 
 
859
  /// \brief Arithmetic right-shift function.
 
860
  ///
787
861
  /// Arithmetic right-shift this APInt by shiftAmt.
788
 
  /// @brief Arithmetic right-shift function.
789
862
  APInt ashr(const APInt &shiftAmt) const;
790
863
 
 
864
  /// \brief Logical right-shift function.
 
865
  ///
791
866
  /// Logical right-shift this APInt by shiftAmt.
792
 
  /// @brief Logical right-shift function.
793
867
  APInt lshr(const APInt &shiftAmt) const;
794
868
 
 
869
  /// \brief Left-shift function.
 
870
  ///
795
871
  /// Left-shift this APInt by shiftAmt.
796
 
  /// @brief Left-shift function.
797
872
  APInt shl(const APInt &shiftAmt) const;
798
873
 
799
 
  /// @brief Rotate left by rotateAmt.
 
874
  /// \brief Rotate left by rotateAmt.
800
875
  APInt rotl(const APInt &rotateAmt) const;
801
876
 
802
 
  /// @brief Rotate right by rotateAmt.
 
877
  /// \brief Rotate right by rotateAmt.
803
878
  APInt rotr(const APInt &rotateAmt) const;
804
879
 
 
880
  /// \brief Unsigned division operation.
 
881
  ///
805
882
  /// Perform an unsigned divide operation on this APInt by RHS. Both this and
806
883
  /// RHS are treated as unsigned quantities for purposes of this division.
807
 
  /// @returns a new APInt value containing the division result
808
 
  /// @brief Unsigned division operation.
 
884
  ///
 
885
  /// \returns a new APInt value containing the division result
809
886
  APInt udiv(const APInt &RHS) const;
810
887
 
 
888
  /// \brief Signed division function for APInt.
 
889
  ///
811
890
  /// Signed divide this APInt by APInt RHS.
812
 
  /// @brief Signed division function for APInt.
813
891
  APInt sdiv(const APInt &RHS) const;
814
892
 
 
893
  /// \brief Unsigned remainder operation.
 
894
  ///
815
895
  /// Perform an unsigned remainder operation on this APInt with RHS being the
816
896
  /// divisor. Both this and RHS are treated as unsigned quantities for purposes
817
 
  /// of this operation. Note that this is a true remainder operation and not
818
 
  /// a modulo operation because the sign follows the sign of the dividend
819
 
  /// which is *this.
820
 
  /// @returns a new APInt value containing the remainder result
821
 
  /// @brief Unsigned remainder operation.
 
897
  /// of this operation. Note that this is a true remainder operation and not a
 
898
  /// modulo operation because the sign follows the sign of the dividend which
 
899
  /// is *this.
 
900
  ///
 
901
  /// \returns a new APInt value containing the remainder result
822
902
  APInt urem(const APInt &RHS) const;
823
903
 
 
904
  /// \brief Function for signed remainder operation.
 
905
  ///
824
906
  /// Signed remainder operation on APInt.
825
 
  /// @brief Function for signed remainder operation.
826
907
  APInt srem(const APInt &RHS) const;
827
908
 
 
909
  /// \brief Dual division/remainder interface.
 
910
  ///
828
911
  /// Sometimes it is convenient to divide two APInt values and obtain both the
829
912
  /// quotient and remainder. This function does both operations in the same
830
913
  /// computation making it a little more efficient. The pair of input arguments
831
914
  /// may overlap with the pair of output arguments. It is safe to call
832
915
  /// udivrem(X, Y, X, Y), for example.
833
 
  /// @brief Dual division/remainder interface.
834
 
  static void udivrem(const APInt &LHS, const APInt &RHS,
835
 
                      APInt &Quotient, APInt &Remainder);
836
 
 
837
 
  static void sdivrem(const APInt &LHS, const APInt &RHS,
838
 
                      APInt &Quotient, APInt &Remainder);
839
 
 
 
916
  static void udivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient,
 
917
                      APInt &Remainder);
 
918
 
 
919
  static void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient,
 
920
                      APInt &Remainder);
840
921
 
841
922
  // Operations that return overflow indicators.
842
923
  APInt sadd_ov(const APInt &RHS, bool &Overflow) const;
848
929
  APInt umul_ov(const APInt &RHS, bool &Overflow) const;
849
930
  APInt sshl_ov(unsigned Amt, bool &Overflow) const;
850
931
 
851
 
  /// @returns the bit value at bitPosition
852
 
  /// @brief Array-indexing support.
 
932
  /// \brief Array-indexing support.
 
933
  ///
 
934
  /// \returns the bit value at bitPosition
853
935
  bool operator[](unsigned bitPosition) const {
854
936
    assert(bitPosition < getBitWidth() && "Bit position out of bounds!");
855
937
    return (maskBit(bitPosition) &
856
 
            (isSingleWord() ? VAL : pVal[whichWord(bitPosition)])) != 0;
 
938
            (isSingleWord() ? VAL : pVal[whichWord(bitPosition)])) !=
 
939
           0;
857
940
  }
858
941
 
859
942
  /// @}
860
 
  /// @name Comparison Operators
 
943
  /// \name Comparison Operators
861
944
  /// @{
 
945
 
 
946
  /// \brief Equality operator.
 
947
  ///
862
948
  /// Compares this APInt with RHS for the validity of the equality
863
949
  /// relationship.
864
 
  /// @brief Equality operator.
865
 
  bool operator==(const APInt& RHS) const {
 
950
  bool operator==(const APInt &RHS) const {
866
951
    assert(BitWidth == RHS.BitWidth && "Comparison requires equal bit widths");
867
952
    if (isSingleWord())
868
953
      return VAL == RHS.VAL;
869
954
    return EqualSlowCase(RHS);
870
955
  }
871
956
 
 
957
  /// \brief Equality operator.
 
958
  ///
872
959
  /// Compares this APInt with a uint64_t for the validity of the equality
873
960
  /// relationship.
874
 
  /// @returns true if *this == Val
875
 
  /// @brief Equality operator.
 
961
  ///
 
962
  /// \returns true if *this == Val
876
963
  bool operator==(uint64_t Val) const {
877
964
    if (isSingleWord())
878
965
      return VAL == Val;
879
966
    return EqualSlowCase(Val);
880
967
  }
881
968
 
 
969
  /// \brief Equality comparison.
 
970
  ///
882
971
  /// Compares this APInt with RHS for the validity of the equality
883
972
  /// relationship.
884
 
  /// @returns true if *this == Val
885
 
  /// @brief Equality comparison.
886
 
  bool eq(const APInt &RHS) const {
887
 
    return (*this) == RHS;
888
 
  }
 
973
  ///
 
974
  /// \returns true if *this == Val
 
975
  bool eq(const APInt &RHS) const { return (*this) == RHS; }
889
976
 
 
977
  /// \brief Inequality operator.
 
978
  ///
890
979
  /// Compares this APInt with RHS for the validity of the inequality
891
980
  /// relationship.
892
 
  /// @returns true if *this != Val
893
 
  /// @brief Inequality operator.
894
 
  bool operator!=(const APInt& RHS) const {
895
 
    return !((*this) == RHS);
896
 
  }
 
981
  ///
 
982
  /// \returns true if *this != Val
 
983
  bool operator!=(const APInt &RHS) const { return !((*this) == RHS); }
897
984
 
 
985
  /// \brief Inequality operator.
 
986
  ///
898
987
  /// Compares this APInt with a uint64_t for the validity of the inequality
899
988
  /// relationship.
900
 
  /// @returns true if *this != Val
901
 
  /// @brief Inequality operator.
902
 
  bool operator!=(uint64_t Val) const {
903
 
    return !((*this) == Val);
904
 
  }
 
989
  ///
 
990
  /// \returns true if *this != Val
 
991
  bool operator!=(uint64_t Val) const { return !((*this) == Val); }
905
992
 
 
993
  /// \brief Inequality comparison
 
994
  ///
906
995
  /// Compares this APInt with RHS for the validity of the inequality
907
996
  /// relationship.
908
 
  /// @returns true if *this != Val
909
 
  /// @brief Inequality comparison
910
 
  bool ne(const APInt &RHS) const {
911
 
    return !((*this) == RHS);
912
 
  }
 
997
  ///
 
998
  /// \returns true if *this != Val
 
999
  bool ne(const APInt &RHS) const { return !((*this) == RHS); }
913
1000
 
 
1001
  /// \brief Unsigned less than comparison
 
1002
  ///
914
1003
  /// Regards both *this and RHS as unsigned quantities and compares them for
915
1004
  /// the validity of the less-than relationship.
916
 
  /// @returns true if *this < RHS when both are considered unsigned.
917
 
  /// @brief Unsigned less than comparison
 
1005
  ///
 
1006
  /// \returns true if *this < RHS when both are considered unsigned.
918
1007
  bool ult(const APInt &RHS) const;
919
1008
 
 
1009
  /// \brief Unsigned less than comparison
 
1010
  ///
920
1011
  /// Regards both *this as an unsigned quantity and compares it with RHS for
921
1012
  /// the validity of the less-than relationship.
922
 
  /// @returns true if *this < RHS when considered unsigned.
923
 
  /// @brief Unsigned less than comparison
924
 
  bool ult(uint64_t RHS) const {
925
 
    return ult(APInt(getBitWidth(), RHS));
926
 
  }
 
1013
  ///
 
1014
  /// \returns true if *this < RHS when considered unsigned.
 
1015
  bool ult(uint64_t RHS) const { return ult(APInt(getBitWidth(), RHS)); }
927
1016
 
 
1017
  /// \brief Signed less than comparison
 
1018
  ///
928
1019
  /// Regards both *this and RHS as signed quantities and compares them for
929
1020
  /// validity of the less-than relationship.
930
 
  /// @returns true if *this < RHS when both are considered signed.
931
 
  /// @brief Signed less than comparison
932
 
  bool slt(const APInt& RHS) const;
 
1021
  ///
 
1022
  /// \returns true if *this < RHS when both are considered signed.
 
1023
  bool slt(const APInt &RHS) const;
933
1024
 
 
1025
  /// \brief Signed less than comparison
 
1026
  ///
934
1027
  /// Regards both *this as a signed quantity and compares it with RHS for
935
1028
  /// the validity of the less-than relationship.
936
 
  /// @returns true if *this < RHS when considered signed.
937
 
  /// @brief Signed less than comparison
938
 
  bool slt(uint64_t RHS) const {
939
 
    return slt(APInt(getBitWidth(), RHS));
940
 
  }
941
 
 
942
 
  /// Regards both *this and RHS as unsigned quantities and compares them for
943
 
  /// validity of the less-or-equal relationship.
944
 
  /// @returns true if *this <= RHS when both are considered unsigned.
945
 
  /// @brief Unsigned less or equal comparison
946
 
  bool ule(const APInt& RHS) const {
947
 
    return ult(RHS) || eq(RHS);
948
 
  }
949
 
 
950
 
  /// Regards both *this as an unsigned quantity and compares it with RHS for
951
 
  /// the validity of the less-or-equal relationship.
952
 
  /// @returns true if *this <= RHS when considered unsigned.
953
 
  /// @brief Unsigned less or equal comparison
954
 
  bool ule(uint64_t RHS) const {
955
 
    return ule(APInt(getBitWidth(), RHS));
956
 
  }
957
 
 
958
 
  /// Regards both *this and RHS as signed quantities and compares them for
959
 
  /// validity of the less-or-equal relationship.
960
 
  /// @returns true if *this <= RHS when both are considered signed.
961
 
  /// @brief Signed less or equal comparison
962
 
  bool sle(const APInt& RHS) const {
963
 
    return slt(RHS) || eq(RHS);
964
 
  }
965
 
 
966
 
  /// Regards both *this as a signed quantity and compares it with RHS for
967
 
  /// the validity of the less-or-equal relationship.
968
 
  /// @returns true if *this <= RHS when considered signed.
969
 
  /// @brief Signed less or equal comparison
970
 
  bool sle(uint64_t RHS) const {
971
 
    return sle(APInt(getBitWidth(), RHS));
972
 
  }
973
 
 
974
 
  /// Regards both *this and RHS as unsigned quantities and compares them for
975
 
  /// the validity of the greater-than relationship.
976
 
  /// @returns true if *this > RHS when both are considered unsigned.
977
 
  /// @brief Unsigned greather than comparison
978
 
  bool ugt(const APInt& RHS) const {
979
 
    return !ult(RHS) && !eq(RHS);
980
 
  }
981
 
 
982
 
  /// Regards both *this as an unsigned quantity and compares it with RHS for
983
 
  /// the validity of the greater-than relationship.
984
 
  /// @returns true if *this > RHS when considered unsigned.
985
 
  /// @brief Unsigned greater than comparison
986
 
  bool ugt(uint64_t RHS) const {
987
 
    return ugt(APInt(getBitWidth(), RHS));
988
 
  }
989
 
 
990
 
  /// Regards both *this and RHS as signed quantities and compares them for
991
 
  /// the validity of the greater-than relationship.
992
 
  /// @returns true if *this > RHS when both are considered signed.
993
 
  /// @brief Signed greather than comparison
994
 
  bool sgt(const APInt& RHS) const {
995
 
    return !slt(RHS) && !eq(RHS);
996
 
  }
997
 
 
998
 
  /// Regards both *this as a signed quantity and compares it with RHS for
999
 
  /// the validity of the greater-than relationship.
1000
 
  /// @returns true if *this > RHS when considered signed.
1001
 
  /// @brief Signed greater than comparison
1002
 
  bool sgt(uint64_t RHS) const {
1003
 
    return sgt(APInt(getBitWidth(), RHS));
1004
 
  }
1005
 
 
1006
 
  /// Regards both *this and RHS as unsigned quantities and compares them for
1007
 
  /// validity of the greater-or-equal relationship.
1008
 
  /// @returns true if *this >= RHS when both are considered unsigned.
1009
 
  /// @brief Unsigned greater or equal comparison
1010
 
  bool uge(const APInt& RHS) const {
1011
 
    return !ult(RHS);
1012
 
  }
1013
 
 
1014
 
  /// Regards both *this as an unsigned quantity and compares it with RHS for
1015
 
  /// the validity of the greater-or-equal relationship.
1016
 
  /// @returns true if *this >= RHS when considered unsigned.
1017
 
  /// @brief Unsigned greater or equal comparison
1018
 
  bool uge(uint64_t RHS) const {
1019
 
    return uge(APInt(getBitWidth(), RHS));
1020
 
  }
1021
 
 
1022
 
  /// Regards both *this and RHS as signed quantities and compares them for
1023
 
  /// validity of the greater-or-equal relationship.
1024
 
  /// @returns true if *this >= RHS when both are considered signed.
1025
 
  /// @brief Signed greather or equal comparison
1026
 
  bool sge(const APInt& RHS) const {
1027
 
    return !slt(RHS);
1028
 
  }
1029
 
 
1030
 
  /// Regards both *this as a signed quantity and compares it with RHS for
1031
 
  /// the validity of the greater-or-equal relationship.
1032
 
  /// @returns true if *this >= RHS when considered signed.
1033
 
  /// @brief Signed greater or equal comparison
1034
 
  bool sge(uint64_t RHS) const {
1035
 
    return sge(APInt(getBitWidth(), RHS));
1036
 
  }
1037
 
 
1038
 
  
1039
 
  
1040
 
  
 
1029
  ///
 
1030
  /// \returns true if *this < RHS when considered signed.
 
1031
  bool slt(uint64_t RHS) const { return slt(APInt(getBitWidth(), RHS)); }
 
1032
 
 
1033
  /// \brief Unsigned less or equal comparison
 
1034
  ///
 
1035
  /// Regards both *this and RHS as unsigned quantities and compares them for
 
1036
  /// validity of the less-or-equal relationship.
 
1037
  ///
 
1038
  /// \returns true if *this <= RHS when both are considered unsigned.
 
1039
  bool ule(const APInt &RHS) const { return ult(RHS) || eq(RHS); }
 
1040
 
 
1041
  /// \brief Unsigned less or equal comparison
 
1042
  ///
 
1043
  /// Regards both *this as an unsigned quantity and compares it with RHS for
 
1044
  /// the validity of the less-or-equal relationship.
 
1045
  ///
 
1046
  /// \returns true if *this <= RHS when considered unsigned.
 
1047
  bool ule(uint64_t RHS) const { return ule(APInt(getBitWidth(), RHS)); }
 
1048
 
 
1049
  /// \brief Signed less or equal comparison
 
1050
  ///
 
1051
  /// Regards both *this and RHS as signed quantities and compares them for
 
1052
  /// validity of the less-or-equal relationship.
 
1053
  ///
 
1054
  /// \returns true if *this <= RHS when both are considered signed.
 
1055
  bool sle(const APInt &RHS) const { return slt(RHS) || eq(RHS); }
 
1056
 
 
1057
  /// \brief Signed less or equal comparison
 
1058
  ///
 
1059
  /// Regards both *this as a signed quantity and compares it with RHS for the
 
1060
  /// validity of the less-or-equal relationship.
 
1061
  ///
 
1062
  /// \returns true if *this <= RHS when considered signed.
 
1063
  bool sle(uint64_t RHS) const { return sle(APInt(getBitWidth(), RHS)); }
 
1064
 
 
1065
  /// \brief Unsigned greather than comparison
 
1066
  ///
 
1067
  /// Regards both *this and RHS as unsigned quantities and compares them for
 
1068
  /// the validity of the greater-than relationship.
 
1069
  ///
 
1070
  /// \returns true if *this > RHS when both are considered unsigned.
 
1071
  bool ugt(const APInt &RHS) const { return !ult(RHS) && !eq(RHS); }
 
1072
 
 
1073
  /// \brief Unsigned greater than comparison
 
1074
  ///
 
1075
  /// Regards both *this as an unsigned quantity and compares it with RHS for
 
1076
  /// the validity of the greater-than relationship.
 
1077
  ///
 
1078
  /// \returns true if *this > RHS when considered unsigned.
 
1079
  bool ugt(uint64_t RHS) const { return ugt(APInt(getBitWidth(), RHS)); }
 
1080
 
 
1081
  /// \brief Signed greather than comparison
 
1082
  ///
 
1083
  /// Regards both *this and RHS as signed quantities and compares them for the
 
1084
  /// validity of the greater-than relationship.
 
1085
  ///
 
1086
  /// \returns true if *this > RHS when both are considered signed.
 
1087
  bool sgt(const APInt &RHS) const { return !slt(RHS) && !eq(RHS); }
 
1088
 
 
1089
  /// \brief Signed greater than comparison
 
1090
  ///
 
1091
  /// Regards both *this as a signed quantity and compares it with RHS for
 
1092
  /// the validity of the greater-than relationship.
 
1093
  ///
 
1094
  /// \returns true if *this > RHS when considered signed.
 
1095
  bool sgt(uint64_t RHS) const { return sgt(APInt(getBitWidth(), RHS)); }
 
1096
 
 
1097
  /// \brief Unsigned greater or equal comparison
 
1098
  ///
 
1099
  /// Regards both *this and RHS as unsigned quantities and compares them for
 
1100
  /// validity of the greater-or-equal relationship.
 
1101
  ///
 
1102
  /// \returns true if *this >= RHS when both are considered unsigned.
 
1103
  bool uge(const APInt &RHS) const { return !ult(RHS); }
 
1104
 
 
1105
  /// \brief Unsigned greater or equal comparison
 
1106
  ///
 
1107
  /// Regards both *this as an unsigned quantity and compares it with RHS for
 
1108
  /// the validity of the greater-or-equal relationship.
 
1109
  ///
 
1110
  /// \returns true if *this >= RHS when considered unsigned.
 
1111
  bool uge(uint64_t RHS) const { return uge(APInt(getBitWidth(), RHS)); }
 
1112
 
 
1113
  /// \brief Signed greather or equal comparison
 
1114
  ///
 
1115
  /// Regards both *this and RHS as signed quantities and compares them for
 
1116
  /// validity of the greater-or-equal relationship.
 
1117
  ///
 
1118
  /// \returns true if *this >= RHS when both are considered signed.
 
1119
  bool sge(const APInt &RHS) const { return !slt(RHS); }
 
1120
 
 
1121
  /// \brief Signed greater or equal comparison
 
1122
  ///
 
1123
  /// Regards both *this as a signed quantity and compares it with RHS for
 
1124
  /// the validity of the greater-or-equal relationship.
 
1125
  ///
 
1126
  /// \returns true if *this >= RHS when considered signed.
 
1127
  bool sge(uint64_t RHS) const { return sge(APInt(getBitWidth(), RHS)); }
 
1128
 
1041
1129
  /// This operation tests if there are any pairs of corresponding bits
1042
1130
  /// between this APInt and RHS that are both set.
1043
 
  bool intersects(const APInt &RHS) const {
1044
 
    return (*this & RHS) != 0;
1045
 
  }
 
1131
  bool intersects(const APInt &RHS) const { return (*this & RHS) != 0; }
1046
1132
 
1047
1133
  /// @}
1048
 
  /// @name Resizing Operators
 
1134
  /// \name Resizing Operators
1049
1135
  /// @{
 
1136
 
 
1137
  /// \brief Truncate to new width.
 
1138
  ///
1050
1139
  /// Truncate the APInt to a specified width. It is an error to specify a width
1051
1140
  /// that is greater than or equal to the current width.
1052
 
  /// @brief Truncate to new width.
1053
1141
  APInt trunc(unsigned width) const;
1054
1142
 
 
1143
  /// \brief Sign extend to a new width.
 
1144
  ///
1055
1145
  /// This operation sign extends the APInt to a new width. If the high order
1056
1146
  /// bit is set, the fill on the left will be done with 1 bits, otherwise zero.
1057
1147
  /// It is an error to specify a width that is less than or equal to the
1058
1148
  /// current width.
1059
 
  /// @brief Sign extend to a new width.
1060
1149
  APInt sext(unsigned width) const;
1061
1150
 
 
1151
  /// \brief Zero extend to a new width.
 
1152
  ///
1062
1153
  /// This operation zero extends the APInt to a new width. The high order bits
1063
1154
  /// are filled with 0 bits.  It is an error to specify a width that is less
1064
1155
  /// than or equal to the current width.
1065
 
  /// @brief Zero extend to a new width.
1066
1156
  APInt zext(unsigned width) const;
1067
1157
 
 
1158
  /// \brief Sign extend or truncate to width
 
1159
  ///
1068
1160
  /// Make this APInt have the bit width given by \p width. The value is sign
1069
1161
  /// extended, truncated, or left alone to make it that width.
1070
 
  /// @brief Sign extend or truncate to width
1071
1162
  APInt sextOrTrunc(unsigned width) const;
1072
1163
 
 
1164
  /// \brief Zero extend or truncate to width
 
1165
  ///
1073
1166
  /// Make this APInt have the bit width given by \p width. The value is zero
1074
1167
  /// extended, truncated, or left alone to make it that width.
1075
 
  /// @brief Zero extend or truncate to width
1076
1168
  APInt zextOrTrunc(unsigned width) const;
1077
1169
 
 
1170
  /// \brief Sign extend or truncate to width
 
1171
  ///
1078
1172
  /// Make this APInt have the bit width given by \p width. The value is sign
1079
1173
  /// extended, or left alone to make it that width.
1080
 
  /// @brief Sign extend or truncate to width
1081
1174
  APInt sextOrSelf(unsigned width) const;
1082
1175
 
 
1176
  /// \brief Zero extend or truncate to width
 
1177
  ///
1083
1178
  /// Make this APInt have the bit width given by \p width. The value is zero
1084
1179
  /// extended, or left alone to make it that width.
1085
 
  /// @brief Zero extend or truncate to width
1086
1180
  APInt zextOrSelf(unsigned width) const;
1087
1181
 
1088
1182
  /// @}
1089
 
  /// @name Bit Manipulation Operators
 
1183
  /// \name Bit Manipulation Operators
1090
1184
  /// @{
1091
 
  /// @brief Set every bit to 1.
 
1185
 
 
1186
  /// \brief Set every bit to 1.
1092
1187
  void setAllBits() {
1093
1188
    if (isSingleWord())
1094
1189
      VAL = UINT64_MAX;
1101
1196
    clearUnusedBits();
1102
1197
  }
1103
1198
 
 
1199
  /// \brief Set a given bit to 1.
 
1200
  ///
1104
1201
  /// Set the given bit to 1 whose position is given as "bitPosition".
1105
 
  /// @brief Set a given bit to 1.
1106
1202
  void setBit(unsigned bitPosition);
1107
1203
 
1108
 
  /// @brief Set every bit to 0.
 
1204
  /// \brief Set every bit to 0.
1109
1205
  void clearAllBits() {
1110
1206
    if (isSingleWord())
1111
1207
      VAL = 0;
1113
1209
      memset(pVal, 0, getNumWords() * APINT_WORD_SIZE);
1114
1210
  }
1115
1211
 
 
1212
  /// \brief Set a given bit to 0.
 
1213
  ///
1116
1214
  /// Set the given bit to 0 whose position is given as "bitPosition".
1117
 
  /// @brief Set a given bit to 0.
1118
1215
  void clearBit(unsigned bitPosition);
1119
1216
 
1120
 
  /// @brief Toggle every bit to its opposite value.
 
1217
  /// \brief Toggle every bit to its opposite value.
1121
1218
  void flipAllBits() {
1122
1219
    if (isSingleWord())
1123
1220
      VAL ^= UINT64_MAX;
1128
1225
    clearUnusedBits();
1129
1226
  }
1130
1227
 
 
1228
  /// \brief Toggles a given bit to its opposite value.
 
1229
  ///
1131
1230
  /// Toggle a given bit to its opposite value whose position is given
1132
1231
  /// as "bitPosition".
1133
 
  /// @brief Toggles a given bit to its opposite value.
1134
1232
  void flipBit(unsigned bitPosition);
1135
1233
 
1136
1234
  /// @}
1137
 
  /// @name Value Characterization Functions
 
1235
  /// \name Value Characterization Functions
1138
1236
  /// @{
1139
1237
 
1140
 
  /// @returns the total number of bits.
1141
 
  unsigned getBitWidth() const {
1142
 
    return BitWidth;
1143
 
  }
1144
 
 
1145
 
  /// Here one word's bitwidth equals to that of uint64_t.
1146
 
  /// @returns the number of words to hold the integer value of this APInt.
1147
 
  /// @brief Get the number of words.
1148
 
  unsigned getNumWords() const {
1149
 
    return getNumWords(BitWidth);
1150
 
  }
1151
 
 
1152
 
  /// Here one word's bitwidth equals to that of uint64_t.
1153
 
  /// @returns the number of words to hold the integer value with a
1154
 
  /// given bit width.
1155
 
  /// @brief Get the number of words.
 
1238
  /// \brief Return the number of bits in the APInt.
 
1239
  unsigned getBitWidth() const { return BitWidth; }
 
1240
 
 
1241
  /// \brief Get the number of words.
 
1242
  ///
 
1243
  /// Here one word's bitwidth equals to that of uint64_t.
 
1244
  ///
 
1245
  /// \returns the number of words to hold the integer value of this APInt.
 
1246
  unsigned getNumWords() const { return getNumWords(BitWidth); }
 
1247
 
 
1248
  /// \brief Get the number of words.
 
1249
  ///
 
1250
  /// *NOTE* Here one word's bitwidth equals to that of uint64_t.
 
1251
  ///
 
1252
  /// \returns the number of words to hold the integer value with a given bit
 
1253
  /// width.
1156
1254
  static unsigned getNumWords(unsigned BitWidth) {
1157
1255
    return (BitWidth + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD;
1158
1256
  }
1159
1257
 
 
1258
  /// \brief Compute the number of active bits in the value
 
1259
  ///
1160
1260
  /// This function returns the number of active bits which is defined as the
1161
1261
  /// bit width minus the number of leading zeros. This is used in several
1162
1262
  /// computations to see how "wide" the value is.
1163
 
  /// @brief Compute the number of active bits in the value
1164
 
  unsigned getActiveBits() const {
1165
 
    return BitWidth - countLeadingZeros();
1166
 
  }
 
1263
  unsigned getActiveBits() const { return BitWidth - countLeadingZeros(); }
1167
1264
 
1168
 
  /// This function returns the number of active words in the value of this
1169
 
  /// APInt. This is used in conjunction with getActiveData to extract the raw
1170
 
  /// value of the APInt.
 
1265
  /// \brief Compute the number of active words in the value of this APInt.
 
1266
  ///
 
1267
  /// This is used in conjunction with getActiveData to extract the raw value of
 
1268
  /// the APInt.
1171
1269
  unsigned getActiveWords() const {
1172
1270
    unsigned numActiveBits = getActiveBits();
1173
1271
    return numActiveBits ? whichWord(numActiveBits - 1) + 1 : 1;
1174
1272
  }
1175
1273
 
1176
 
  /// Computes the minimum bit width for this APInt while considering it to be
1177
 
  /// a signed (and probably negative) value. If the value is not negative,
1178
 
  /// this function returns the same value as getActiveBits()+1. Otherwise, it
 
1274
  /// \brief Get the minimum bit size for this signed APInt
 
1275
  ///
 
1276
  /// Computes the minimum bit width for this APInt while considering it to be a
 
1277
  /// signed (and probably negative) value. If the value is not negative, this
 
1278
  /// function returns the same value as getActiveBits()+1. Otherwise, it
1179
1279
  /// returns the smallest bit width that will retain the negative value. For
1180
1280
  /// example, -1 can be written as 0b1 or 0xFFFFFFFFFF. 0b1 is shorter and so
1181
1281
  /// for -1, this function will always return 1.
1182
 
  /// @brief Get the minimum bit size for this signed APInt
1183
1282
  unsigned getMinSignedBits() const {
1184
1283
    if (isNegative())
1185
1284
      return BitWidth - countLeadingOnes() + 1;
1186
 
    return getActiveBits()+1;
 
1285
    return getActiveBits() + 1;
1187
1286
  }
1188
1287
 
 
1288
  /// \brief Get zero extended value
 
1289
  ///
1189
1290
  /// This method attempts to return the value of this APInt as a zero extended
1190
1291
  /// uint64_t. The bitwidth must be <= 64 or the value must fit within a
1191
1292
  /// uint64_t. Otherwise an assertion will result.
1192
 
  /// @brief Get zero extended value
1193
1293
  uint64_t getZExtValue() const {
1194
1294
    if (isSingleWord())
1195
1295
      return VAL;
1197
1297
    return pVal[0];
1198
1298
  }
1199
1299
 
 
1300
  /// \brief Get sign extended value
 
1301
  ///
1200
1302
  /// This method attempts to return the value of this APInt as a sign extended
1201
1303
  /// int64_t. The bit width must be <= 64 or the value must fit within an
1202
1304
  /// int64_t. Otherwise an assertion will result.
1203
 
  /// @brief Get sign extended value
1204
1305
  int64_t getSExtValue() const {
1205
1306
    if (isSingleWord())
1206
1307
      return int64_t(VAL << (APINT_BITS_PER_WORD - BitWidth)) >>
1207
 
                     (APINT_BITS_PER_WORD - BitWidth);
 
1308
             (APINT_BITS_PER_WORD - BitWidth);
1208
1309
    assert(getMinSignedBits() <= 64 && "Too many bits for int64_t");
1209
1310
    return int64_t(pVal[0]);
1210
1311
  }
1211
1312
 
 
1313
  /// \brief Get bits required for string value.
 
1314
  ///
1212
1315
  /// This method determines how many bits are required to hold the APInt
1213
1316
  /// equivalent of the string given by \p str.
1214
 
  /// @brief Get bits required for string value.
1215
1317
  static unsigned getBitsNeeded(StringRef str, uint8_t radix);
1216
1318
 
1217
 
  /// countLeadingZeros - This function is an APInt version of the
1218
 
  /// countLeadingZeros_{32,64} functions in MathExtras.h. It counts the number
1219
 
  /// of zeros from the most significant bit to the first one bit.
1220
 
  /// @returns BitWidth if the value is zero, otherwise
1221
 
  /// returns the number of zeros from the most significant bit to the first
1222
 
  /// one bits.
 
1319
  /// \brief The APInt version of the countLeadingZeros functions in
 
1320
  ///   MathExtras.h.
 
1321
  ///
 
1322
  /// It counts the number of zeros from the most significant bit to the first
 
1323
  /// one bit.
 
1324
  ///
 
1325
  /// \returns BitWidth if the value is zero, otherwise returns the number of
 
1326
  ///   zeros from the most significant bit to the first one bits.
1223
1327
  unsigned countLeadingZeros() const {
1224
1328
    if (isSingleWord()) {
1225
1329
      unsigned unusedBits = APINT_BITS_PER_WORD - BitWidth;
1226
 
      return CountLeadingZeros_64(VAL) - unusedBits;
 
1330
      return llvm::countLeadingZeros(VAL) - unusedBits;
1227
1331
    }
1228
1332
    return countLeadingZerosSlowCase();
1229
1333
  }
1230
1334
 
1231
 
  /// countLeadingOnes - This function is an APInt version of the
1232
 
  /// countLeadingOnes_{32,64} functions in MathExtras.h. It counts the number
1233
 
  /// of ones from the most significant bit to the first zero bit.
1234
 
  /// @returns 0 if the high order bit is not set, otherwise
1235
 
  /// returns the number of 1 bits from the most significant to the least
1236
 
  /// @brief Count the number of leading one bits.
 
1335
  /// \brief Count the number of leading one bits.
 
1336
  ///
 
1337
  /// This function is an APInt version of the countLeadingOnes_{32,64}
 
1338
  /// functions in MathExtras.h. It counts the number of ones from the most
 
1339
  /// significant bit to the first zero bit.
 
1340
  ///
 
1341
  /// \returns 0 if the high order bit is not set, otherwise returns the number
 
1342
  /// of 1 bits from the most significant to the least
1237
1343
  unsigned countLeadingOnes() const;
1238
1344
 
1239
1345
  /// Computes the number of leading bits of this APInt that are equal to its
1242
1348
    return isNegative() ? countLeadingOnes() : countLeadingZeros();
1243
1349
  }
1244
1350
 
1245
 
  /// countTrailingZeros - This function is an APInt version of the
1246
 
  /// countTrailingZeros_{32,64} functions in MathExtras.h. It counts
1247
 
  /// the number of zeros from the least significant bit to the first set bit.
1248
 
  /// @returns BitWidth if the value is zero, otherwise
1249
 
  /// returns the number of zeros from the least significant bit to the first
1250
 
  /// one bit.
1251
 
  /// @brief Count the number of trailing zero bits.
 
1351
  /// \brief Count the number of trailing zero bits.
 
1352
  ///
 
1353
  /// This function is an APInt version of the countTrailingZeros_{32,64}
 
1354
  /// functions in MathExtras.h. It counts the number of zeros from the least
 
1355
  /// significant bit to the first set bit.
 
1356
  ///
 
1357
  /// \returns BitWidth if the value is zero, otherwise returns the number of
 
1358
  /// zeros from the least significant bit to the first one bit.
1252
1359
  unsigned countTrailingZeros() const;
1253
1360
 
1254
 
  /// countTrailingOnes - This function is an APInt version of the
1255
 
  /// countTrailingOnes_{32,64} functions in MathExtras.h. It counts
1256
 
  /// the number of ones from the least significant bit to the first zero bit.
1257
 
  /// @returns BitWidth if the value is all ones, otherwise
1258
 
  /// returns the number of ones from the least significant bit to the first
1259
 
  /// zero bit.
1260
 
  /// @brief Count the number of trailing one bits.
 
1361
  /// \brief Count the number of trailing one bits.
 
1362
  ///
 
1363
  /// This function is an APInt version of the countTrailingOnes_{32,64}
 
1364
  /// functions in MathExtras.h. It counts the number of ones from the least
 
1365
  /// significant bit to the first zero bit.
 
1366
  ///
 
1367
  /// \returns BitWidth if the value is all ones, otherwise returns the number
 
1368
  /// of ones from the least significant bit to the first zero bit.
1261
1369
  unsigned countTrailingOnes() const {
1262
1370
    if (isSingleWord())
1263
1371
      return CountTrailingOnes_64(VAL);
1264
1372
    return countTrailingOnesSlowCase();
1265
1373
  }
1266
1374
 
1267
 
  /// countPopulation - This function is an APInt version of the
1268
 
  /// countPopulation_{32,64} functions in MathExtras.h. It counts the number
1269
 
  /// of 1 bits in the APInt value.
1270
 
  /// @returns 0 if the value is zero, otherwise returns the number of set
1271
 
  /// bits.
1272
 
  /// @brief Count the number of bits set.
 
1375
  /// \brief Count the number of bits set.
 
1376
  ///
 
1377
  /// This function is an APInt version of the countPopulation_{32,64} functions
 
1378
  /// in MathExtras.h. It counts the number of 1 bits in the APInt value.
 
1379
  ///
 
1380
  /// \returns 0 if the value is zero, otherwise returns the number of set bits.
1273
1381
  unsigned countPopulation() const {
1274
1382
    if (isSingleWord())
1275
1383
      return CountPopulation_64(VAL);
1277
1385
  }
1278
1386
 
1279
1387
  /// @}
1280
 
  /// @name Conversion Functions
 
1388
  /// \name Conversion Functions
1281
1389
  /// @{
1282
1390
  void print(raw_ostream &OS, bool isSigned) const;
1283
1391
 
1284
 
  /// toString - Converts an APInt to a string and append it to Str.  Str is
1285
 
  /// commonly a SmallString.
 
1392
  /// Converts an APInt to a string and append it to Str.  Str is commonly a
 
1393
  /// SmallString.
1286
1394
  void toString(SmallVectorImpl<char> &Str, unsigned Radix, bool Signed,
1287
1395
                bool formatAsCLiteral = false) const;
1288
1396
 
1298
1406
    toString(Str, Radix, true, false);
1299
1407
  }
1300
1408
 
1301
 
  /// toString - This returns the APInt as a std::string.  Note that this is an
1302
 
  /// inefficient method.  It is better to pass in a SmallVector/SmallString
1303
 
  /// to the methods above to avoid thrashing the heap for the string.
 
1409
  /// \brief Return the APInt as a std::string.
 
1410
  ///
 
1411
  /// Note that this is an inefficient method.  It is better to pass in a
 
1412
  /// SmallVector/SmallString to the methods above to avoid thrashing the heap
 
1413
  /// for the string.
1304
1414
  std::string toString(unsigned Radix, bool Signed) const;
1305
1415
 
1306
 
 
1307
 
  /// @returns a byte-swapped representation of this APInt Value.
 
1416
  /// \returns a byte-swapped representation of this APInt Value.
1308
1417
  APInt byteSwap() const;
1309
1418
 
1310
 
  /// @brief Converts this APInt to a double value.
 
1419
  /// \brief Converts this APInt to a double value.
1311
1420
  double roundToDouble(bool isSigned) const;
1312
1421
 
1313
 
  /// @brief Converts this unsigned APInt to a double value.
1314
 
  double roundToDouble() const {
1315
 
    return roundToDouble(false);
1316
 
  }
1317
 
 
1318
 
  /// @brief Converts this signed APInt to a double value.
1319
 
  double signedRoundToDouble() const {
1320
 
    return roundToDouble(true);
1321
 
  }
1322
 
 
 
1422
  /// \brief Converts this unsigned APInt to a double value.
 
1423
  double roundToDouble() const { return roundToDouble(false); }
 
1424
 
 
1425
  /// \brief Converts this signed APInt to a double value.
 
1426
  double signedRoundToDouble() const { return roundToDouble(true); }
 
1427
 
 
1428
  /// \brief Converts APInt bits to a double
 
1429
  ///
1323
1430
  /// The conversion does not do a translation from integer to double, it just
1324
1431
  /// re-interprets the bits as a double. Note that it is valid to do this on
1325
1432
  /// any bit width. Exactly 64 bits will be translated.
1326
 
  /// @brief Converts APInt bits to a double
1327
1433
  double bitsToDouble() const {
1328
1434
    union {
1329
1435
      uint64_t I;
1333
1439
    return T.D;
1334
1440
  }
1335
1441
 
 
1442
  /// \brief Converts APInt bits to a double
 
1443
  ///
1336
1444
  /// The conversion does not do a translation from integer to float, it just
1337
1445
  /// re-interprets the bits as a float. Note that it is valid to do this on
1338
1446
  /// any bit width. Exactly 32 bits will be translated.
1339
 
  /// @brief Converts APInt bits to a double
1340
1447
  float bitsToFloat() const {
1341
1448
    union {
1342
1449
      unsigned I;
1346
1453
    return T.F;
1347
1454
  }
1348
1455
 
 
1456
  /// \brief Converts a double to APInt bits.
 
1457
  ///
1349
1458
  /// The conversion does not do a translation from double to integer, it just
1350
1459
  /// re-interprets the bits of the double.
1351
 
  /// @brief Converts a double to APInt bits.
1352
1460
  static APInt doubleToBits(double V) {
1353
1461
    union {
1354
1462
      uint64_t I;
1358
1466
    return APInt(sizeof T * CHAR_BIT, T.I);
1359
1467
  }
1360
1468
 
 
1469
  /// \brief Converts a float to APInt bits.
 
1470
  ///
1361
1471
  /// The conversion does not do a translation from float to integer, it just
1362
1472
  /// re-interprets the bits of the float.
1363
 
  /// @brief Converts a float to APInt bits.
1364
1473
  static APInt floatToBits(float V) {
1365
1474
    union {
1366
1475
      unsigned I;
1371
1480
  }
1372
1481
 
1373
1482
  /// @}
1374
 
  /// @name Mathematics Operations
 
1483
  /// \name Mathematics Operations
1375
1484
  /// @{
1376
1485
 
1377
 
  /// @returns the floor log base 2 of this APInt.
1378
 
  unsigned logBase2() const {
1379
 
    return BitWidth - 1 - countLeadingZeros();
1380
 
  }
 
1486
  /// \returns the floor log base 2 of this APInt.
 
1487
  unsigned logBase2() const { return BitWidth - 1 - countLeadingZeros(); }
1381
1488
 
1382
 
  /// @returns the ceil log base 2 of this APInt.
 
1489
  /// \returns the ceil log base 2 of this APInt.
1383
1490
  unsigned ceilLogBase2() const {
1384
1491
    return BitWidth - (*this - 1).countLeadingZeros();
1385
1492
  }
1386
1493
 
1387
 
  /// @returns the log base 2 of this APInt if its an exact power of two, -1
 
1494
  /// \returns the log base 2 of this APInt if its an exact power of two, -1
1388
1495
  /// otherwise
1389
1496
  int32_t exactLogBase2() const {
1390
1497
    if (!isPowerOf2())
1392
1499
    return logBase2();
1393
1500
  }
1394
1501
 
1395
 
  /// @brief Compute the square root
 
1502
  /// \brief Compute the square root
1396
1503
  APInt sqrt() const;
1397
1504
 
 
1505
  /// \brief Get the absolute value;
 
1506
  ///
1398
1507
  /// If *this is < 0 then return -(*this), otherwise *this;
1399
 
  /// @brief Get the absolute value;
1400
1508
  APInt abs() const {
1401
1509
    if (isNegative())
1402
1510
      return -(*this);
1403
1511
    return *this;
1404
1512
  }
1405
1513
 
1406
 
  /// @returns the multiplicative inverse for a given modulo.
1407
 
  APInt multiplicativeInverse(const APInt& modulo) const;
 
1514
  /// \returns the multiplicative inverse for a given modulo.
 
1515
  APInt multiplicativeInverse(const APInt &modulo) const;
1408
1516
 
1409
1517
  /// @}
1410
 
  /// @name Support for division by constant
 
1518
  /// \name Support for division by constant
1411
1519
  /// @{
1412
1520
 
1413
1521
  /// Calculate the magic number for signed division by a constant.
1419
1527
  mu magicu(unsigned LeadingZeros = 0) const;
1420
1528
 
1421
1529
  /// @}
1422
 
  /// @name Building-block Operations for APInt and APFloat
 
1530
  /// \name Building-block Operations for APInt and APFloat
1423
1531
  /// @{
1424
1532
 
1425
 
  // These building block operations operate on a representation of
1426
 
  // arbitrary precision, two's-complement, bignum integer values.
1427
 
  // They should be sufficient to implement APInt and APFloat bignum
1428
 
  // requirements.  Inputs are generally a pointer to the base of an
1429
 
  // array of integer parts, representing an unsigned bignum, and a
1430
 
  // count of how many parts there are.
 
1533
  // These building block operations operate on a representation of arbitrary
 
1534
  // precision, two's-complement, bignum integer values. They should be
 
1535
  // sufficient to implement APInt and APFloat bignum requirements. Inputs are
 
1536
  // generally a pointer to the base of an array of integer parts, representing
 
1537
  // an unsigned bignum, and a count of how many parts there are.
1431
1538
 
1432
 
  /// Sets the least significant part of a bignum to the input value,
1433
 
  /// and zeroes out higher parts.  */
 
1539
  /// Sets the least significant part of a bignum to the input value, and zeroes
 
1540
  /// out higher parts.
1434
1541
  static void tcSet(integerPart *, integerPart, unsigned int);
1435
1542
 
1436
1543
  /// Assign one bignum to another.
1442
1549
  /// Extract the given bit of a bignum; returns 0 or 1.  Zero-based.
1443
1550
  static int tcExtractBit(const integerPart *, unsigned int bit);
1444
1551
 
1445
 
  /// Copy the bit vector of width srcBITS from SRC, starting at bit
1446
 
  /// srcLSB, to DST, of dstCOUNT parts, such that the bit srcLSB
1447
 
  /// becomes the least significant bit of DST.  All high bits above
1448
 
  /// srcBITS in DST are zero-filled.
 
1552
  /// Copy the bit vector of width srcBITS from SRC, starting at bit srcLSB, to
 
1553
  /// DST, of dstCOUNT parts, such that the bit srcLSB becomes the least
 
1554
  /// significant bit of DST.  All high bits above srcBITS in DST are
 
1555
  /// zero-filled.
1449
1556
  static void tcExtract(integerPart *, unsigned int dstCount,
1450
 
                        const integerPart *,
1451
 
                        unsigned int srcBits, unsigned int srcLSB);
 
1557
                        const integerPart *, unsigned int srcBits,
 
1558
                        unsigned int srcLSB);
1452
1559
 
1453
1560
  /// Set the given bit of a bignum.  Zero-based.
1454
1561
  static void tcSetBit(integerPart *, unsigned int bit);
1456
1563
  /// Clear the given bit of a bignum.  Zero-based.
1457
1564
  static void tcClearBit(integerPart *, unsigned int bit);
1458
1565
 
1459
 
  /// Returns the bit number of the least or most significant set bit
1460
 
  /// of a number.  If the input number has no bits set -1U is
1461
 
  /// returned.
 
1566
  /// Returns the bit number of the least or most significant set bit of a
 
1567
  /// number.  If the input number has no bits set -1U is returned.
1462
1568
  static unsigned int tcLSB(const integerPart *, unsigned int);
1463
1569
  static unsigned int tcMSB(const integerPart *parts, unsigned int n);
1464
1570
 
1465
1571
  /// Negate a bignum in-place.
1466
1572
  static void tcNegate(integerPart *, unsigned int);
1467
1573
 
1468
 
  /// DST += RHS + CARRY where CARRY is zero or one.  Returns the
1469
 
  /// carry flag.
 
1574
  /// DST += RHS + CARRY where CARRY is zero or one.  Returns the carry flag.
1470
1575
  static integerPart tcAdd(integerPart *, const integerPart *,
1471
1576
                           integerPart carry, unsigned);
1472
1577
 
1473
 
  /// DST -= RHS + CARRY where CARRY is zero or one.  Returns the
1474
 
  /// carry flag.
 
1578
  /// DST -= RHS + CARRY where CARRY is zero or one. Returns the carry flag.
1475
1579
  static integerPart tcSubtract(integerPart *, const integerPart *,
1476
1580
                                integerPart carry, unsigned);
1477
1581
 
1478
 
  ///  DST += SRC * MULTIPLIER + PART   if add is true
1479
 
  ///  DST  = SRC * MULTIPLIER + PART   if add is false
1480
 
  ///
1481
 
  ///  Requires 0 <= DSTPARTS <= SRCPARTS + 1.  If DST overlaps SRC
1482
 
  ///  they must start at the same point, i.e. DST == SRC.
1483
 
  ///
1484
 
  ///  If DSTPARTS == SRC_PARTS + 1 no overflow occurs and zero is
1485
 
  ///  returned.  Otherwise DST is filled with the least significant
1486
 
  ///  DSTPARTS parts of the result, and if all of the omitted higher
1487
 
  ///  parts were zero return zero, otherwise overflow occurred and
1488
 
  ///  return one.
 
1582
  /// DST += SRC * MULTIPLIER + PART   if add is true
 
1583
  /// DST  = SRC * MULTIPLIER + PART   if add is false
 
1584
  ///
 
1585
  /// Requires 0 <= DSTPARTS <= SRCPARTS + 1.  If DST overlaps SRC they must
 
1586
  /// start at the same point, i.e. DST == SRC.
 
1587
  ///
 
1588
  /// If DSTPARTS == SRC_PARTS + 1 no overflow occurs and zero is returned.
 
1589
  /// Otherwise DST is filled with the least significant DSTPARTS parts of the
 
1590
  /// result, and if all of the omitted higher parts were zero return zero,
 
1591
  /// otherwise overflow occurred and return one.
1489
1592
  static int tcMultiplyPart(integerPart *dst, const integerPart *src,
1490
1593
                            integerPart multiplier, integerPart carry,
1491
1594
                            unsigned int srcParts, unsigned int dstParts,
1492
1595
                            bool add);
1493
1596
 
1494
 
  /// DST = LHS * RHS, where DST has the same width as the operands
1495
 
  /// and is filled with the least significant parts of the result.
1496
 
  /// Returns one if overflow occurred, otherwise zero.  DST must be
1497
 
  /// disjoint from both operands.
1498
 
  static int tcMultiply(integerPart *, const integerPart *,
1499
 
                        const integerPart *, unsigned);
 
1597
  /// DST = LHS * RHS, where DST has the same width as the operands and is
 
1598
  /// filled with the least significant parts of the result.  Returns one if
 
1599
  /// overflow occurred, otherwise zero.  DST must be disjoint from both
 
1600
  /// operands.
 
1601
  static int tcMultiply(integerPart *, const integerPart *, const integerPart *,
 
1602
                        unsigned);
1500
1603
 
1501
 
  /// DST = LHS * RHS, where DST has width the sum of the widths of
1502
 
  /// the operands.  No overflow occurs.  DST must be disjoint from
1503
 
  /// both operands. Returns the number of parts required to hold the
1504
 
  /// result.
 
1604
  /// DST = LHS * RHS, where DST has width the sum of the widths of the
 
1605
  /// operands.  No overflow occurs.  DST must be disjoint from both
 
1606
  /// operands. Returns the number of parts required to hold the result.
1505
1607
  static unsigned int tcFullMultiply(integerPart *, const integerPart *,
1506
1608
                                     const integerPart *, unsigned, unsigned);
1507
1609
 
1508
1610
  /// If RHS is zero LHS and REMAINDER are left unchanged, return one.
1509
 
  /// Otherwise set LHS to LHS / RHS with the fractional part
1510
 
  /// discarded, set REMAINDER to the remainder, return zero.  i.e.
 
1611
  /// Otherwise set LHS to LHS / RHS with the fractional part discarded, set
 
1612
  /// REMAINDER to the remainder, return zero.  i.e.
1511
1613
  ///
1512
1614
  ///  OLD_LHS = RHS * LHS + REMAINDER
1513
1615
  ///
1514
 
  ///  SCRATCH is a bignum of the same size as the operands and result
1515
 
  ///  for use by the routine; its contents need not be initialized
1516
 
  ///  and are destroyed.  LHS, REMAINDER and SCRATCH must be
1517
 
  ///  distinct.
 
1616
  /// SCRATCH is a bignum of the same size as the operands and result for use by
 
1617
  /// the routine; its contents need not be initialized and are destroyed.  LHS,
 
1618
  /// REMAINDER and SCRATCH must be distinct.
1518
1619
  static int tcDivide(integerPart *lhs, const integerPart *rhs,
1519
1620
                      integerPart *remainder, integerPart *scratch,
1520
1621
                      unsigned int parts);
1521
1622
 
1522
 
  /// Shift a bignum left COUNT bits.  Shifted in bits are zero.
1523
 
  /// There are no restrictions on COUNT.
 
1623
  /// Shift a bignum left COUNT bits.  Shifted in bits are zero.  There are no
 
1624
  /// restrictions on COUNT.
1524
1625
  static void tcShiftLeft(integerPart *, unsigned int parts,
1525
1626
                          unsigned int count);
1526
1627
 
1527
 
  /// Shift a bignum right COUNT bits.  Shifted in bits are zero.
1528
 
  /// There are no restrictions on COUNT.
 
1628
  /// Shift a bignum right COUNT bits.  Shifted in bits are zero.  There are no
 
1629
  /// restrictions on COUNT.
1529
1630
  static void tcShiftRight(integerPart *, unsigned int parts,
1530
1631
                           unsigned int count);
1531
1632
 
1536
1637
  static void tcComplement(integerPart *, unsigned int);
1537
1638
 
1538
1639
  /// Comparison (unsigned) of two bignums.
1539
 
  static int tcCompare(const integerPart *, const integerPart *,
1540
 
                       unsigned int);
 
1640
  static int tcCompare(const integerPart *, const integerPart *, unsigned int);
1541
1641
 
1542
1642
  /// Increment a bignum in-place.  Return the carry flag.
1543
1643
  static integerPart tcIncrement(integerPart *, unsigned int);
1546
1646
  static void tcSetLeastSignificantBits(integerPart *, unsigned int,
1547
1647
                                        unsigned int bits);
1548
1648
 
1549
 
  /// @brief debug method
 
1649
  /// \brief debug method
1550
1650
  void dump() const;
1551
1651
 
1552
1652
  /// @}
1554
1654
 
1555
1655
/// Magic data for optimising signed division by a constant.
1556
1656
struct APInt::ms {
1557
 
  APInt m;  ///< magic number
1558
 
  unsigned s;  ///< shift amount
 
1657
  APInt m;    ///< magic number
 
1658
  unsigned s; ///< shift amount
1559
1659
};
1560
1660
 
1561
1661
/// Magic data for optimising unsigned division by a constant.
1562
1662
struct APInt::mu {
1563
 
  APInt m;     ///< magic number
1564
 
  bool a;      ///< add indicator
1565
 
  unsigned s;  ///< shift amount
 
1663
  APInt m;    ///< magic number
 
1664
  bool a;     ///< add indicator
 
1665
  unsigned s; ///< shift amount
1566
1666
};
1567
1667
 
1568
 
inline bool operator==(uint64_t V1, const APInt& V2) {
1569
 
  return V2 == V1;
1570
 
}
 
1668
inline bool operator==(uint64_t V1, const APInt &V2) { return V2 == V1; }
1571
1669
 
1572
 
inline bool operator!=(uint64_t V1, const APInt& V2) {
1573
 
  return V2 != V1;
1574
 
}
 
1670
inline bool operator!=(uint64_t V1, const APInt &V2) { return V2 != V1; }
1575
1671
 
1576
1672
inline raw_ostream &operator<<(raw_ostream &OS, const APInt &I) {
1577
1673
  I.print(OS, true);
1580
1676
 
1581
1677
namespace APIntOps {
1582
1678
 
1583
 
/// @brief Determine the smaller of two APInts considered to be signed.
1584
 
inline APInt smin(const APInt &A, const APInt &B) {
1585
 
  return A.slt(B) ? A : B;
1586
 
}
1587
 
 
1588
 
/// @brief Determine the larger of two APInts considered to be signed.
1589
 
inline APInt smax(const APInt &A, const APInt &B) {
1590
 
  return A.sgt(B) ? A : B;
1591
 
}
1592
 
 
1593
 
/// @brief Determine the smaller of two APInts considered to be signed.
1594
 
inline APInt umin(const APInt &A, const APInt &B) {
1595
 
  return A.ult(B) ? A : B;
1596
 
}
1597
 
 
1598
 
/// @brief Determine the larger of two APInts considered to be unsigned.
1599
 
inline APInt umax(const APInt &A, const APInt &B) {
1600
 
  return A.ugt(B) ? A : B;
1601
 
}
1602
 
 
1603
 
/// @brief Check if the specified APInt has a N-bits unsigned integer value.
1604
 
inline bool isIntN(unsigned N, const APInt& APIVal) {
1605
 
  return APIVal.isIntN(N);
1606
 
}
1607
 
 
1608
 
/// @brief Check if the specified APInt has a N-bits signed integer value.
1609
 
inline bool isSignedIntN(unsigned N, const APInt& APIVal) {
 
1679
/// \brief Determine the smaller of two APInts considered to be signed.
 
1680
inline APInt smin(const APInt &A, const APInt &B) { return A.slt(B) ? A : B; }
 
1681
 
 
1682
/// \brief Determine the larger of two APInts considered to be signed.
 
1683
inline APInt smax(const APInt &A, const APInt &B) { return A.sgt(B) ? A : B; }
 
1684
 
 
1685
/// \brief Determine the smaller of two APInts considered to be signed.
 
1686
inline APInt umin(const APInt &A, const APInt &B) { return A.ult(B) ? A : B; }
 
1687
 
 
1688
/// \brief Determine the larger of two APInts considered to be unsigned.
 
1689
inline APInt umax(const APInt &A, const APInt &B) { return A.ugt(B) ? A : B; }
 
1690
 
 
1691
/// \brief Check if the specified APInt has a N-bits unsigned integer value.
 
1692
inline bool isIntN(unsigned N, const APInt &APIVal) { return APIVal.isIntN(N); }
 
1693
 
 
1694
/// \brief Check if the specified APInt has a N-bits signed integer value.
 
1695
inline bool isSignedIntN(unsigned N, const APInt &APIVal) {
1610
1696
  return APIVal.isSignedIntN(N);
1611
1697
}
1612
1698
 
1613
 
/// @returns true if the argument APInt value is a sequence of ones
1614
 
/// starting at the least significant bit with the remainder zero.
1615
 
inline bool isMask(unsigned numBits, const APInt& APIVal) {
 
1699
/// \returns true if the argument APInt value is a sequence of ones starting at
 
1700
/// the least significant bit with the remainder zero.
 
1701
inline bool isMask(unsigned numBits, const APInt &APIVal) {
1616
1702
  return numBits <= APIVal.getBitWidth() &&
1617
 
    APIVal == APInt::getLowBitsSet(APIVal.getBitWidth(), numBits);
 
1703
         APIVal == APInt::getLowBitsSet(APIVal.getBitWidth(), numBits);
1618
1704
}
1619
1705
 
1620
 
/// @returns true if the argument APInt value contains a sequence of ones
 
1706
/// \brief Return true if the argument APInt value contains a sequence of ones
1621
1707
/// with the remainder zero.
1622
 
inline bool isShiftedMask(unsigned numBits, const APInt& APIVal) {
1623
 
  return isMask(numBits, (APIVal - APInt(numBits,1)) | APIVal);
1624
 
}
1625
 
 
1626
 
/// @returns a byte-swapped representation of the specified APInt Value.
1627
 
inline APInt byteSwap(const APInt& APIVal) {
1628
 
  return APIVal.byteSwap();
1629
 
}
1630
 
 
1631
 
/// @returns the floor log base 2 of the specified APInt value.
1632
 
inline unsigned logBase2(const APInt& APIVal) {
1633
 
  return APIVal.logBase2();
1634
 
}
1635
 
 
1636
 
/// GreatestCommonDivisor - This function returns the greatest common
1637
 
/// divisor of the two APInt values using Euclid's algorithm.
1638
 
/// @returns the greatest common divisor of Val1 and Val2
1639
 
/// @brief Compute GCD of two APInt values.
1640
 
APInt GreatestCommonDivisor(const APInt& Val1, const APInt& Val2);
1641
 
 
 
1708
inline bool isShiftedMask(unsigned numBits, const APInt &APIVal) {
 
1709
  return isMask(numBits, (APIVal - APInt(numBits, 1)) | APIVal);
 
1710
}
 
1711
 
 
1712
/// \brief Returns a byte-swapped representation of the specified APInt Value.
 
1713
inline APInt byteSwap(const APInt &APIVal) { return APIVal.byteSwap(); }
 
1714
 
 
1715
/// \brief Returns the floor log base 2 of the specified APInt value.
 
1716
inline unsigned logBase2(const APInt &APIVal) { return APIVal.logBase2(); }
 
1717
 
 
1718
/// \brief Compute GCD of two APInt values.
 
1719
///
 
1720
/// This function returns the greatest common divisor of the two APInt values
 
1721
/// using Euclid's algorithm.
 
1722
///
 
1723
/// \returns the greatest common divisor of Val1 and Val2
 
1724
APInt GreatestCommonDivisor(const APInt &Val1, const APInt &Val2);
 
1725
 
 
1726
/// \brief Converts the given APInt to a double value.
 
1727
///
1642
1728
/// Treats the APInt as an unsigned value for conversion purposes.
1643
 
/// @brief Converts the given APInt to a double value.
1644
 
inline double RoundAPIntToDouble(const APInt& APIVal) {
 
1729
inline double RoundAPIntToDouble(const APInt &APIVal) {
1645
1730
  return APIVal.roundToDouble();
1646
1731
}
1647
1732
 
 
1733
/// \brief Converts the given APInt to a double value.
 
1734
///
1648
1735
/// Treats the APInt as a signed value for conversion purposes.
1649
 
/// @brief Converts the given APInt to a double value.
1650
 
inline double RoundSignedAPIntToDouble(const APInt& APIVal) {
 
1736
inline double RoundSignedAPIntToDouble(const APInt &APIVal) {
1651
1737
  return APIVal.signedRoundToDouble();
1652
1738
}
1653
1739
 
1654
 
/// @brief Converts the given APInt to a float vlalue.
1655
 
inline float RoundAPIntToFloat(const APInt& APIVal) {
 
1740
/// \brief Converts the given APInt to a float vlalue.
 
1741
inline float RoundAPIntToFloat(const APInt &APIVal) {
1656
1742
  return float(RoundAPIntToDouble(APIVal));
1657
1743
}
1658
1744
 
 
1745
/// \brief Converts the given APInt to a float value.
 
1746
///
1659
1747
/// Treast the APInt as a signed value for conversion purposes.
1660
 
/// @brief Converts the given APInt to a float value.
1661
 
inline float RoundSignedAPIntToFloat(const APInt& APIVal) {
 
1748
inline float RoundSignedAPIntToFloat(const APInt &APIVal) {
1662
1749
  return float(APIVal.signedRoundToDouble());
1663
1750
}
1664
1751
 
1665
 
/// RoundDoubleToAPInt - This function convert a double value to an APInt value.
1666
 
/// @brief Converts the given double value into a APInt.
 
1752
/// \brief Converts the given double value into a APInt.
 
1753
///
 
1754
/// This function convert a double value to an APInt value.
1667
1755
APInt RoundDoubleToAPInt(double Double, unsigned width);
1668
1756
 
1669
 
/// RoundFloatToAPInt - Converts a float value into an APInt value.
1670
 
/// @brief Converts a float value into a APInt.
 
1757
/// \brief Converts a float value into a APInt.
 
1758
///
 
1759
/// Converts a float value into an APInt value.
1671
1760
inline APInt RoundFloatToAPInt(float Float, unsigned width) {
1672
1761
  return RoundDoubleToAPInt(double(Float), width);
1673
1762
}
1674
1763
 
 
1764
/// \brief Arithmetic right-shift function.
 
1765
///
1675
1766
/// Arithmetic right-shift the APInt by shiftAmt.
1676
 
/// @brief Arithmetic right-shift function.
1677
 
inline APInt ashr(const APInt& LHS, unsigned shiftAmt) {
 
1767
inline APInt ashr(const APInt &LHS, unsigned shiftAmt) {
1678
1768
  return LHS.ashr(shiftAmt);
1679
1769
}
1680
1770
 
 
1771
/// \brief Logical right-shift function.
 
1772
///
1681
1773
/// Logical right-shift the APInt by shiftAmt.
1682
 
/// @brief Logical right-shift function.
1683
 
inline APInt lshr(const APInt& LHS, unsigned shiftAmt) {
 
1774
inline APInt lshr(const APInt &LHS, unsigned shiftAmt) {
1684
1775
  return LHS.lshr(shiftAmt);
1685
1776
}
1686
1777
 
 
1778
/// \brief Left-shift function.
 
1779
///
1687
1780
/// Left-shift the APInt by shiftAmt.
1688
 
/// @brief Left-shift function.
1689
 
inline APInt shl(const APInt& LHS, unsigned shiftAmt) {
 
1781
inline APInt shl(const APInt &LHS, unsigned shiftAmt) {
1690
1782
  return LHS.shl(shiftAmt);
1691
1783
}
1692
1784
 
 
1785
/// \brief Signed division function for APInt.
 
1786
///
1693
1787
/// Signed divide APInt LHS by APInt RHS.
1694
 
/// @brief Signed division function for APInt.
1695
 
inline APInt sdiv(const APInt& LHS, const APInt& RHS) {
1696
 
  return LHS.sdiv(RHS);
1697
 
}
 
1788
inline APInt sdiv(const APInt &LHS, const APInt &RHS) { return LHS.sdiv(RHS); }
1698
1789
 
 
1790
/// \brief Unsigned division function for APInt.
 
1791
///
1699
1792
/// Unsigned divide APInt LHS by APInt RHS.
1700
 
/// @brief Unsigned division function for APInt.
1701
 
inline APInt udiv(const APInt& LHS, const APInt& RHS) {
1702
 
  return LHS.udiv(RHS);
1703
 
}
 
1793
inline APInt udiv(const APInt &LHS, const APInt &RHS) { return LHS.udiv(RHS); }
1704
1794
 
 
1795
/// \brief Function for signed remainder operation.
 
1796
///
1705
1797
/// Signed remainder operation on APInt.
1706
 
/// @brief Function for signed remainder operation.
1707
 
inline APInt srem(const APInt& LHS, const APInt& RHS) {
1708
 
  return LHS.srem(RHS);
1709
 
}
 
1798
inline APInt srem(const APInt &LHS, const APInt &RHS) { return LHS.srem(RHS); }
1710
1799
 
 
1800
/// \brief Function for unsigned remainder operation.
 
1801
///
1711
1802
/// Unsigned remainder operation on APInt.
1712
 
/// @brief Function for unsigned remainder operation.
1713
 
inline APInt urem(const APInt& LHS, const APInt& RHS) {
1714
 
  return LHS.urem(RHS);
1715
 
}
 
1803
inline APInt urem(const APInt &LHS, const APInt &RHS) { return LHS.urem(RHS); }
1716
1804
 
 
1805
/// \brief Function for multiplication operation.
 
1806
///
1717
1807
/// Performs multiplication on APInt values.
1718
 
/// @brief Function for multiplication operation.
1719
 
inline APInt mul(const APInt& LHS, const APInt& RHS) {
1720
 
  return LHS * RHS;
1721
 
}
 
1808
inline APInt mul(const APInt &LHS, const APInt &RHS) { return LHS * RHS; }
1722
1809
 
 
1810
/// \brief Function for addition operation.
 
1811
///
1723
1812
/// Performs addition on APInt values.
1724
 
/// @brief Function for addition operation.
1725
 
inline APInt add(const APInt& LHS, const APInt& RHS) {
1726
 
  return LHS + RHS;
1727
 
}
 
1813
inline APInt add(const APInt &LHS, const APInt &RHS) { return LHS + RHS; }
1728
1814
 
 
1815
/// \brief Function for subtraction operation.
 
1816
///
1729
1817
/// Performs subtraction on APInt values.
1730
 
/// @brief Function for subtraction operation.
1731
 
inline APInt sub(const APInt& LHS, const APInt& RHS) {
1732
 
  return LHS - RHS;
1733
 
}
 
1818
inline APInt sub(const APInt &LHS, const APInt &RHS) { return LHS - RHS; }
1734
1819
 
 
1820
/// \brief Bitwise AND function for APInt.
 
1821
///
1735
1822
/// Performs bitwise AND operation on APInt LHS and
1736
1823
/// APInt RHS.
1737
 
/// @brief Bitwise AND function for APInt.
1738
 
inline APInt And(const APInt& LHS, const APInt& RHS) {
1739
 
  return LHS & RHS;
1740
 
}
 
1824
inline APInt And(const APInt &LHS, const APInt &RHS) { return LHS & RHS; }
1741
1825
 
 
1826
/// \brief Bitwise OR function for APInt.
 
1827
///
1742
1828
/// Performs bitwise OR operation on APInt LHS and APInt RHS.
1743
 
/// @brief Bitwise OR function for APInt.
1744
 
inline APInt Or(const APInt& LHS, const APInt& RHS) {
1745
 
  return LHS | RHS;
1746
 
}
 
1829
inline APInt Or(const APInt &LHS, const APInt &RHS) { return LHS | RHS; }
1747
1830
 
 
1831
/// \brief Bitwise XOR function for APInt.
 
1832
///
1748
1833
/// Performs bitwise XOR operation on APInt.
1749
 
/// @brief Bitwise XOR function for APInt.
1750
 
inline APInt Xor(const APInt& LHS, const APInt& RHS) {
1751
 
  return LHS ^ RHS;
1752
 
}
 
1834
inline APInt Xor(const APInt &LHS, const APInt &RHS) { return LHS ^ RHS; }
1753
1835
 
 
1836
/// \brief Bitwise complement function.
 
1837
///
1754
1838
/// Performs a bitwise complement operation on APInt.
1755
 
/// @brief Bitwise complement function.
1756
 
inline APInt Not(const APInt& APIVal) {
1757
 
  return ~APIVal;
1758
 
}
 
1839
inline APInt Not(const APInt &APIVal) { return ~APIVal; }
1759
1840
 
1760
1841
} // End of APIntOps namespace
1761
1842
 
1762
 
  // See friend declaration above. This additional declaration is required in
1763
 
  // order to compile LLVM with IBM xlC compiler.
1764
 
  hash_code hash_value(const APInt &Arg);
 
1843
// See friend declaration above. This additional declaration is required in
 
1844
// order to compile LLVM with IBM xlC compiler.
 
1845
hash_code hash_value(const APInt &Arg);
1765
1846
} // End of llvm namespace
1766
1847
 
1767
1848
#endif