APInt.h revision a932e3f799cbf2f689e4987fea543d3f8661e7a8
1d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng//===-- llvm/Support/APInt.h - For Arbitrary Precision Integer -*- C++ -*--===// 2d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng// 3d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng// The LLVM Compiler Infrastructure 4d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng// 5d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng// This file was developed by Sheng Zhou and is distributed under the 6d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng// University of Illinois Open Source License. See LICENSE.TXT for details. 7d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng// 8d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng//===----------------------------------------------------------------------===// 9d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng// 10d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng// This file implements a class to represent arbitrary precision integral 11d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng// constant values. 12d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng// 13d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng//===----------------------------------------------------------------------===// 14d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 15d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng#ifndef LLVM_APINT_H 16d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng#define LLVM_APINT_H 17d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 18d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng#include "llvm/Support/DataTypes.h" 1958a0d64fae7ace05b27dad94fd427991d853619bLauro Ramos Venancio#include <cassert> 20d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng#include <string> 21d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 22d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Shengnamespace llvm { 23d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 240b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Forward declaration. 250b706b18bd0a7760d971727460a1f26bff8289b0Zhou Shengclass APInt; 260b706b18bd0a7760d971727460a1f26bff8289b0Zhou Shengnamespace APIntOps { 27e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer APInt udiv(const APInt& LHS, const APInt& RHS); 28e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer APInt urem(const APInt& LHS, const APInt& RHS); 290b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng} 300b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 31d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng//===----------------------------------------------------------------------===// 32d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng// APInt Class 33d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng//===----------------------------------------------------------------------===// 34d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 35d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// APInt - This class represents arbitrary precision constant integral values. 36d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// It is a functional replacement for common case unsigned integer type like 37d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// "unsigned", "unsigned long" or "uint64_t", but also allows non-byte-width 38e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// integer sizes and large integer value types such as 3-bits, 15-bits, or more 39d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// than 64-bits of precision. APInt provides a variety of arithmetic operators 40e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// and methods to manipulate integer values of any bit-width. It supports both 41e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// the typical integer arithmetic and comparison operations as well as bitwise 42e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// manipulation. 43d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// 44e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// The class has several invariants worth noting: 45e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// * All bit, byte, and word positions are zero-based. 46e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// * Once the bit width is set, it doesn't change except by the Truncate, 47e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// SignExtend, or ZeroExtend operations. 48e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// * All binary operators must be on APInt instances of the same bit width. 49e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// Attempting to use these operators on instances with different bit 50e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// widths will yield an assertion. 51e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// * The value is stored canonically as an unsigned value. For operations 52e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// where it makes a difference, there are both signed and unsigned variants 53e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// of the operation. For example, sdiv and udiv. However, because the bit 54e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// widths must be the same, operations such as Mul and Add produce the same 55e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// results regardless of whether the values are interpreted as signed or 56e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// not. 57e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// * In general, the class tries to follow the style of computation that LLVM 58e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// uses in its IR. This simplifies its use for LLVM. 597406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng/// 60e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// @brief Class for arbitrary precision integers. 61d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Shengclass APInt { 62a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencerpublic: 63a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer uint32_t BitWidth; ///< The number of bits in this APInt. 64d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 65d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// This union is used to store the integer value. When the 667406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// integer bit-width <= 64, it uses VAL; 677406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// otherwise it uses the pVal. 68d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng union { 69d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng uint64_t VAL; ///< Used to store the <= 64 bits integer value. 70d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng uint64_t *pVal; ///< Used to store the >64 bits integer value. 71d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng }; 72d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 737406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// This enum is just used to hold a constant we needed for APInt. 74d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng enum { 75d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APINT_BITS_PER_WORD = sizeof(uint64_t) * 8 76d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng }; 77d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 78d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// Here one word's bitwidth equals to that of uint64_t. 797406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @returns the number of words to hold the integer value of this APInt. 807406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Get the number of words. 81a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer inline uint32_t getNumWords() const { 82e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return (BitWidth + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD; 83d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng } 84d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 85d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns true if the number of bits <= 64, false otherwise. 86d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @brief Determine if this APInt just has one word to store value. 87e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer inline bool isSingleWord() const { 88e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return BitWidth <= APINT_BITS_PER_WORD; 89e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 90d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 91d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the word position for the specified bit position. 92a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer static inline uint32_t whichWord(uint32_t bitPosition) { 93e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return bitPosition / APINT_BITS_PER_WORD; 94e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 95d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 96d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the bit position in a word for the specified bit position 97d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// in APInt. 98a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer static inline uint32_t whichBit(uint32_t bitPosition) { 99e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return bitPosition % APINT_BITS_PER_WORD; 100e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 101d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 102d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns a uint64_t type integer with just bit position at 103d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// "whichBit(bitPosition)" setting, others zero. 104a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer static inline uint64_t maskBit(uint32_t bitPosition) { 105e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return (static_cast<uint64_t>(1)) << whichBit(bitPosition); 106e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 107d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 108e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// This method is used internally to clear the to "N" bits that are not used 109e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// by the APInt. This is needed after a word is assigned a value to ensure 110e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// that those bits are zero'd out. 111e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Clear high order bits 112e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer inline void clearUnusedBits() { 113d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng if (isSingleWord()) 114e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer VAL &= ~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - BitWidth); 115d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng else 116f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng pVal[getNumWords() - 1] &= ~uint64_t(0ULL) >> 117e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer (APINT_BITS_PER_WORD - (whichBit(BitWidth - 1) + 1)); 118d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng } 119d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 120d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the corresponding word for the specified bit position. 121d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// This is a constant version. 122a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer inline uint64_t getWord(uint32_t bitPosition) const { 123e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return isSingleWord() ? VAL : pVal[whichWord(bitPosition)]; 124e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 125d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 126f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng /// @brief Converts a char array into an integer. 127a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer void fromString(uint32_t numBits, const char *StrStart, uint32_t slen, 128e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer uint8_t radix); 129f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng 130d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Shengpublic: 1317406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Create a new APInt of numBits bit-width, and initialized as val. 132a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt(uint32_t numBits, uint64_t val); 133d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 1347406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Create a new APInt of numBits bit-width, and initialized as 1357406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// bigVal[]. 136a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt(uint32_t numBits, uint32_t numWords, uint64_t bigVal[]); 137d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 1387406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Create a new APInt by translating the string represented 1397406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// integer value. 140a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt(uint32_t numBits, const std::string& Val, uint8_t radix); 141f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng 142f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng /// @brief Create a new APInt by translating the char array represented 143f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng /// integer value. 144a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt(uint32_t numBits, const char StrStart[], uint32_t slen, uint8_t radix); 145d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 1467406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Copy Constructor. 147d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt(const APInt& API); 148d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 1497406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Destructor. 150d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng ~APInt(); 151d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 1527406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Copy assignment operator. 153d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt& operator=(const APInt& RHS); 154d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 1557406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Assigns an integer value to the APInt. 1567406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Assignment operator. 157d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt& operator=(uint64_t RHS); 158d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 1597406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Increments the APInt by one. 1607406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Postfix increment operator. 161f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng inline const APInt operator++(int) { 162f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng APInt API(*this); 163b04973edfaffb12905f58379d632f0d7e4bb5d9bZhou Sheng ++(*this); 164b04973edfaffb12905f58379d632f0d7e4bb5d9bZhou Sheng return API; 165f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng } 166d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 1677406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Increments the APInt by one. 1687406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Prefix increment operator. 169d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt& operator++(); 170d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 1717406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Decrements the APInt by one. 1727406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Postfix decrement operator. 173f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng inline const APInt operator--(int) { 174f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng APInt API(*this); 175b04973edfaffb12905f58379d632f0d7e4bb5d9bZhou Sheng --(*this); 176b04973edfaffb12905f58379d632f0d7e4bb5d9bZhou Sheng return API; 177f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng } 178d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 1797406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Decrements the APInt by one. 1807406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Prefix decrement operator. 181d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt& operator--(); 182d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 1837406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Performs bitwise AND operation on this APInt and the given APInt& RHS, 1847406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// assigns the result to this APInt. 1857406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Bitwise AND assignment operator. 186d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt& operator&=(const APInt& RHS); 187d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 1887406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Performs bitwise OR operation on this APInt and the given APInt& RHS, 1897406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// assigns the result to this APInt. 1907406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Bitwise OR assignment operator. 191d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt& operator|=(const APInt& RHS); 192d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 1937406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Performs bitwise XOR operation on this APInt and the given APInt& RHS, 1947406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// assigns the result to this APInt. 1957406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Bitwise XOR assignment operator. 196d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt& operator^=(const APInt& RHS); 197d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 1987406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Performs a bitwise complement operation on this APInt. 1997406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Bitwise complement operator. 200d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt operator~() const; 201d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2027406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Multiplies this APInt by the given APInt& RHS and 2037406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// assigns the result to this APInt. 2047406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Multiplication assignment operator. 205d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt& operator*=(const APInt& RHS); 206d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2077406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Adds this APInt by the given APInt& RHS and 2087406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// assigns the result to this APInt. 2097406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Addition assignment operator. 210d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt& operator+=(const APInt& RHS); 211d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2127406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Subtracts this APInt by the given APInt &RHS and 2137406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// assigns the result to this APInt. 2147406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Subtraction assignment operator. 215d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt& operator-=(const APInt& RHS); 216d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2177406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Performs bitwise AND operation on this APInt and 2187406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// the given APInt& RHS. 2197406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Bitwise AND operator. 220d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt operator&(const APInt& RHS) const; 221d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2227406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Performs bitwise OR operation on this APInt and the given APInt& RHS. 2237406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Bitwise OR operator. 224d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt operator|(const APInt& RHS) const; 225d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2267406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Performs bitwise XOR operation on this APInt and the given APInt& RHS. 2277406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Bitwise XOR operator. 228d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt operator^(const APInt& RHS) const; 229d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2307406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Performs logical negation operation on this APInt. 2317406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Logical negation operator. 232d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng bool operator !() const; 233d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2347406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Multiplies this APInt by the given APInt& RHS. 2357406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Multiplication operator. 236d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt operator*(const APInt& RHS) const; 237d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2387406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Adds this APInt by the given APInt& RHS. 2397406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Addition operator. 240d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt operator+(const APInt& RHS) const; 241d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2427406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Subtracts this APInt by the given APInt& RHS 2437406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Subtraction operator. 244d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt operator-(const APInt& RHS) const; 245d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 246e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Unary negation operator 2470b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng inline APInt operator-() const { 248cd6f2bfc268add3dd54cbdf98b2a2c4862fe4ba5Reid Spencer return APInt(BitWidth, 0) - (*this); 2490b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng } 250d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 251d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @brief Array-indexing support. 252a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer bool operator[](uint32_t bitPosition) const; 253d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2547406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Compare this APInt with the given APInt& RHS 255d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// for the validity of the equality relationship. 2567406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Equality operator. 257d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng bool operator==(const APInt& RHS) const; 258d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 259f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng /// Compare this APInt with the given uint64_t value 260f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng /// for the validity of the equality relationship. 261f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng /// @brief Equality operator. 262f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng bool operator==(uint64_t Val) const; 263f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng 2647406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Compare this APInt with the given APInt& RHS 265d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// for the validity of the inequality relationship. 2667406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Inequality operator. 267f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng inline bool operator!=(const APInt& RHS) const { 268f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng return !((*this) == RHS); 269f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng } 270d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 271f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng /// Compare this APInt with the given uint64_t value 272f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng /// for the validity of the inequality relationship. 273f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng /// @brief Inequality operator. 274f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng inline bool operator!=(uint64_t Val) const { 275f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng return !((*this) == Val); 276f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng } 277f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng 278e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Equality comparison 279e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool eq(const APInt &RHS) const { 280e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return (*this) == RHS; 281e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 282e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 283e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Inequality comparison 284e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool ne(const APInt &RHS) const { 285e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return !((*this) == RHS); 286e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 287e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 288e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Unsigned less than comparison 289e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool ult(const APInt& RHS) const; 290e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 291e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Signed less than comparison 292e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool slt(const APInt& RHS) const; 293e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 294e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Unsigned less or equal comparison 295e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool ule(const APInt& RHS) const { 296e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return ult(RHS) || eq(RHS); 297e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 298e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 299e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Signed less or equal comparison 300e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool sle(const APInt& RHS) const { 301e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return slt(RHS) || eq(RHS); 302e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 303e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 304e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Unsigned greather than comparison 305e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool ugt(const APInt& RHS) const { 306e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return !ult(RHS) && !eq(RHS); 307e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 308e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 309e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Signed greather than comparison 310e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool sgt(const APInt& RHS) const { 311e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return !slt(RHS) && !eq(RHS); 312e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 313e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 314e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Unsigned greater or equal comparison 315e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool uge(const APInt& RHS) const { 316e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return !ult(RHS); 317e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 318e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 319e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Signed greather or equal comparison 320e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool sge(const APInt& RHS) const { 321e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return !slt(RHS); 322e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 323e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 324e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Arithmetic right-shift this APInt by shiftAmt. 325e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Arithmetic right-shift function. 326a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt ashr(uint32_t shiftAmt) const; 327e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 328e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Logical right-shift this APInt by shiftAmt. 329e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Logical right-shift function. 330a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt lshr(uint32_t shiftAmt) const; 331e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 332e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Left-shift this APInt by shiftAmt. 333e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Left-shift function. 334a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt shl(uint32_t shiftAmt) const; 335e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 336e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Signed divide this APInt by APInt RHS. 337e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Signed division function for APInt. 338e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer inline APInt sdiv(const APInt& RHS) const { 339e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool isNegativeLHS = (*this)[BitWidth - 1]; 340e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool isNegativeRHS = RHS[RHS.BitWidth - 1]; 341a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt Result = APIntOps::udiv( 342e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer isNegativeLHS ? -(*this) : (*this), isNegativeRHS ? -RHS : RHS); 343a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer return isNegativeLHS != isNegativeRHS ? -Result : Result; 344e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 345e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 346e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Unsigned divide this APInt by APInt RHS. 347e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Unsigned division function for APInt. 348e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer APInt udiv(const APInt& RHS) const; 349e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 350e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Signed remainder operation on APInt. 351e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Function for signed remainder operation. 352e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer inline APInt srem(const APInt& RHS) const { 353e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool isNegativeLHS = (*this)[BitWidth - 1]; 354e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool isNegativeRHS = RHS[RHS.BitWidth - 1]; 355a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt Result = APIntOps::urem( 356e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer isNegativeLHS ? -(*this) : (*this), isNegativeRHS ? -RHS : RHS); 357a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer return isNegativeLHS ? -Result : Result; 358e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 359e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 360e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Unsigned remainder operation on APInt. 361e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Function for unsigned remainder operation. 362e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer APInt urem(const APInt& RHS) const; 363e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 364e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Truncate the APInt to a specified width. It is an error to specify a width 365e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// that is greater than or equal to the current width. 366e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Truncate to new width. 367a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer void trunc(uint32_t width); 368e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 369e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// This operation sign extends the APInt to a new width. If the high order 370e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// bit is set, the fill on the left will be done with 1 bits, otherwise zero. 371e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// It is an error to specify a width that is less than or equal to the 372e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// current width. 373e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Sign extend to a new width. 374a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer void sext(uint32_t width); 375e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 376e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// This operation zero extends the APInt to a new width. Thie high order bits 377e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// are filled with 0 bits. It is an error to specify a width that is less 378e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// than or equal to the current width. 379e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Zero extend to a new width. 380a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer void zext(uint32_t width); 381e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 382e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Set every bit to 1. 383e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer APInt& set(); 384e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 385e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Set the given bit to 1 whose position is given as "bitPosition". 386e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Set a given bit to 1. 387a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt& set(uint32_t bitPosition); 388e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 389e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Set every bit to 0. 390e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer APInt& clear(); 391e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 392e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Set the given bit to 0 whose position is given as "bitPosition". 393e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Set a given bit to 0. 394a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt& clear(uint32_t bitPosition); 395e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 396e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Toggle every bit to its opposite value. 397e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer APInt& flip(); 398e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 399e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Toggle a given bit to its opposite value whose position is given 400e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// as "bitPosition". 401e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Toggles a given bit to its opposite value. 402a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt& flip(uint32_t bitPosition); 403e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 404e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// This function returns the number of active bits which is defined as the 405e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// bit width minus the number of leading zeros. This is used in several 406e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// computations to see how "wide" the value is. 407e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Compute the number of active bits in the value 408a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer inline uint32_t getActiveBits() const { 409e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return getNumWords() * APINT_BITS_PER_WORD - countLeadingZeros(); 410e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 411d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 412d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns a uint64_t value from this APInt. If this APInt contains a single 413d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// word, just returns VAL, otherwise pVal[0]. 414b04973edfaffb12905f58379d632f0d7e4bb5d9bZhou Sheng inline uint64_t getValue(bool isSigned = false) const { 415d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng if (isSingleWord()) 416e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return isSigned ? int64_t(VAL << (64 - BitWidth)) >> 417e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer (64 - BitWidth) : VAL; 418a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer uint32_t n = getActiveBits(); 419d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng if (n <= 64) 420d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng return pVal[0]; 421f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng assert(0 && "This APInt's bitwidth > 64"); 422d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng } 423d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 424d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the largest value for an APInt of the specified bit-width and 425d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// if isSign == true, it should be largest signed value, otherwise largest 426d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// unsigned value. 427d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @brief Gets max value of the APInt with bitwidth <= 64. 428a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer static APInt getMaxValue(uint32_t numBits, bool isSign); 429d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 430d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the smallest value for an APInt of the given bit-width and 431d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// if isSign == true, it should be smallest signed value, otherwise zero. 432d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @brief Gets min value of the APInt with bitwidth <= 64. 433a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer static APInt getMinValue(uint32_t numBits, bool isSign); 434d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 435d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the all-ones value for an APInt of the specified bit-width. 436d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @brief Get the all-ones value. 437a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer static APInt getAllOnesValue(uint32_t numBits); 438d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 439db3faa64ee89d537ca1b1410dd841ef51e957540Reid Spencer /// @returns the '0' value for an APInt of the specified bit-width. 440db3faa64ee89d537ca1b1410dd841ef51e957540Reid Spencer /// @brief Get the '0' value. 441a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer static APInt getNullValue(uint32_t numBits); 442db3faa64ee89d537ca1b1410dd841ef51e957540Reid Spencer 443e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// This converts the APInt to a boolean valy as a test against zero. 444e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Boolean conversion function. 445e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer inline bool getBoolValue() const { 446e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return countLeadingZeros() != BitWidth; 447e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 448d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 449d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns a character interpretation of the APInt. 450443b570149f5756b298de6b63d13bbbf66b4f6fcReid Spencer std::string toString(uint8_t radix = 10, bool wantSigned = true) const; 451d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 452e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Get an APInt with the same BitWidth as this APInt, just zero mask 4537406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// the low bits and right shift to the least significant bit. 454d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the high "numBits" bits of this APInt. 455a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt getHiBits(uint32_t numBits) const; 456d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 457e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Get an APInt with the same BitWidth as this APInt, just zero mask 4587406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// the high bits. 459d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the low "numBits" bits of this APInt. 460a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt getLoBits(uint32_t numBits) const; 461d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 462d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns true if the argument APInt value is a power of two > 0. 463cd6f2bfc268add3dd54cbdf98b2a2c4862fe4ba5Reid Spencer bool isPowerOf2() const; 464d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 465d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the number of zeros from the most significant bit to the first 466d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// one bits. 467a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer uint32_t countLeadingZeros() const; 468d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 469d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the number of zeros from the least significant bit to the first 470d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// one bit. 471a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer uint32_t countTrailingZeros() const; 472d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 473d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the number of set bits. 474a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer uint32_t countPopulation() const; 475d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 476d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the total number of bits. 477a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer inline uint32_t getBitWidth() const { 478e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return BitWidth; 479e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 480d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 481ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng /// @brief Check if this APInt has a N-bits integer value. 482a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer inline bool isIntN(uint32_t N) const { 483b04973edfaffb12905f58379d632f0d7e4bb5d9bZhou Sheng assert(N && "N == 0 ???"); 484ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng if (isSingleWord()) { 485b04973edfaffb12905f58379d632f0d7e4bb5d9bZhou Sheng return VAL == (VAL & (~0ULL >> (64 - N))); 486ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng } else { 487e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer APInt Tmp(N, getNumWords(), pVal); 488ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng return Tmp == (*this); 489ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng } 490ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng } 491ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng 492ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng /// @returns a byte-swapped representation of this APInt Value. 493e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer APInt byteSwap() const; 494ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng 495ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng /// @returns the floor log base 2 of this APInt. 496a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer inline uint32_t logBase2() const { 497e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return getNumWords() * APINT_BITS_PER_WORD - 1 - countLeadingZeros(); 498ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng } 499ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng 500d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng /// @brief Converts this APInt to a double value. 501e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer double roundToDouble(bool isSigned = false) const; 502ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng 503d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng}; 504d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 5050b706b18bd0a7760d971727460a1f26bff8289b0Zhou Shengnamespace APIntOps { 5060b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 507d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// @brief Check if the specified APInt has a N-bits integer value. 508a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencerinline bool isIntN(uint32_t N, const APInt& APIVal) { 509e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return APIVal.isIntN(N); 510d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng} 511d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 512d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// @returns true if the argument APInt value is a sequence of ones 513d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// starting at the least significant bit with the remainder zero. 514a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencerinline const bool isMask(uint32_t numBits, const APInt& APIVal) { 515e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return APIVal.getBoolValue() && ((APIVal + APInt(numBits,1)) & APIVal) == 0; 516d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng} 517d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 518d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// @returns true if the argument APInt value contains a sequence of ones 519d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// with the remainder zero. 520a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencerinline const bool isShiftedMask(uint32_t numBits, const APInt& APIVal) { 521e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return isMask(numBits, (APIVal - APInt(numBits,1)) | APIVal); 522d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng} 523d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 524d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// @returns a byte-swapped representation of the specified APInt Value. 525e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt byteSwap(const APInt& APIVal) { 526e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return APIVal.byteSwap(); 527ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng} 528d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 529d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// @returns the floor log base 2 of the specified APInt value. 530a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencerinline uint32_t logBase2(const APInt& APIVal) { 531e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return APIVal.logBase2(); 532d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng} 533d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 534d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// @returns the greatest common divisor of the two values 535d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// using Euclid's algorithm. 536d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou ShengAPInt GreatestCommonDivisor(const APInt& API1, const APInt& API2); 537d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 538d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng/// @brief Converts the given APInt to a double value. 539e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline double RoundAPIntToDouble(const APInt& APIVal, bool isSigned = false) { 540e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return APIVal.roundToDouble(isSigned); 541d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng} 542d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng 543d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng/// @brief Converts the given APInt to a float vlalue. 544e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline float RoundAPIntToFloat(const APInt& APIVal) { 545e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return float(RoundAPIntToDouble(APIVal)); 546d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng} 547d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng 548d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng/// @brief Converts the given double value into a APInt. 549e81d2dad2c54014d36c73573307db5852c5caf8eReid SpencerAPInt RoundDoubleToAPInt(double Double); 550d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng 551d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng/// @brief Converts the given float value into a APInt. 552e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt RoundFloatToAPInt(float Float) { 553e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return RoundDoubleToAPInt(double(Float)); 554d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng} 555d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng 5560b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Arithmetic right-shift the APInt by shiftAmt. 5570b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Arithmetic right-shift function. 558a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencerinline APInt ashr(const APInt& LHS, uint32_t shiftAmt) { 559e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return LHS.ashr(shiftAmt); 560ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng} 5610b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 5620b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Logical right-shift the APInt by shiftAmt. 5630b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Logical right-shift function. 564a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencerinline APInt lshr(const APInt& LHS, uint32_t shiftAmt) { 565e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return LHS.lshr(shiftAmt); 566ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng} 5670b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 5680b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Left-shift the APInt by shiftAmt. 5690b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Left-shift function. 570a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencerinline APInt shl(const APInt& LHS, uint32_t shiftAmt) { 571e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return LHS.shl(shiftAmt); 572ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng} 5730b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 5740b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Signed divide APInt LHS by APInt RHS. 5750b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Signed division function for APInt. 576e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt sdiv(const APInt& LHS, const APInt& RHS) { 577e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return LHS.sdiv(RHS); 5780b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng} 5790b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 5800b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Unsigned divide APInt LHS by APInt RHS. 5810b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Unsigned division function for APInt. 582e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt udiv(const APInt& LHS, const APInt& RHS) { 583e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return LHS.udiv(RHS); 584ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng} 5850b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 5860b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Signed remainder operation on APInt. 5870b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Function for signed remainder operation. 588e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt srem(const APInt& LHS, const APInt& RHS) { 589e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return LHS.srem(RHS); 5900b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng} 5910b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 5920b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Unsigned remainder operation on APInt. 5930b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Function for unsigned remainder operation. 594e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt urem(const APInt& LHS, const APInt& RHS) { 595e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return LHS.urem(RHS); 596ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng} 5970b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 5980b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Performs multiplication on APInt values. 5990b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Function for multiplication operation. 600e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt mul(const APInt& LHS, const APInt& RHS) { 6010b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng return LHS * RHS; 6020b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng} 6030b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 6040b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Performs addition on APInt values. 6050b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Function for addition operation. 606e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt add(const APInt& LHS, const APInt& RHS) { 6070b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng return LHS + RHS; 6080b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng} 6090b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 6100b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Performs subtraction on APInt values. 6110b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Function for subtraction operation. 612e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt sub(const APInt& LHS, const APInt& RHS) { 6130b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng return LHS - RHS; 6140b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng} 6150b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 616ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// Performs bitwise AND operation on APInt LHS and 617ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// APInt RHS. 618ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// @brief Bitwise AND function for APInt. 619ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Shenginline APInt And(const APInt& LHS, const APInt& RHS) { 620ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng return LHS & RHS; 621ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng} 622ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng 623ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// Performs bitwise OR operation on APInt LHS and APInt RHS. 624ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// @brief Bitwise OR function for APInt. 625ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Shenginline APInt Or(const APInt& LHS, const APInt& RHS) { 626ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng return LHS | RHS; 627ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng} 628ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng 629ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// Performs bitwise XOR operation on APInt. 630ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// @brief Bitwise XOR function for APInt. 631ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Shenginline APInt Xor(const APInt& LHS, const APInt& RHS) { 632ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng return LHS ^ RHS; 633ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng} 634ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng 635ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// Performs a bitwise complement operation on APInt. 636ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// @brief Bitwise complement function. 637ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Shenginline APInt Not(const APInt& APIVal) { 638ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng return ~APIVal; 639ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng} 640ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng 6410b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng} // End of APIntOps namespace 6420b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 643d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng} // End of llvm namespace 644d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 645d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng#endif 646