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