1 //===-- llvm/ADT/APSInt.h - Arbitrary Precision Signed Int -----*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the APSInt class, which is a simple class that
11 // represents an arbitrary sized integer that knows its signedness.
13 //===----------------------------------------------------------------------===//
18 #include "llvm/ADT/APInt.h"
22 class APSInt : public APInt {
25 /// Default constructor that creates an uninitialized APInt.
26 explicit APSInt() : IsUnsigned(false) {}
28 /// APSInt ctor - Create an APSInt with the specified width, default to
30 explicit APSInt(uint32_t BitWidth, bool isUnsigned = true)
31 : APInt(BitWidth, 0), IsUnsigned(isUnsigned) {}
33 explicit APSInt(const APInt &I, bool isUnsigned = true)
34 : APInt(I), IsUnsigned(isUnsigned) {}
36 APSInt &operator=(const APSInt &RHS) {
37 APInt::operator=(RHS);
38 IsUnsigned = RHS.IsUnsigned;
42 APSInt &operator=(const APInt &RHS) {
43 // Retain our current sign.
44 APInt::operator=(RHS);
48 APSInt &operator=(uint64_t RHS) {
49 // Retain our current sign.
50 APInt::operator=(RHS);
54 // Query sign information.
55 bool isSigned() const { return !IsUnsigned; }
56 bool isUnsigned() const { return IsUnsigned; }
57 void setIsUnsigned(bool Val) { IsUnsigned = Val; }
58 void setIsSigned(bool Val) { IsUnsigned = !Val; }
60 /// toString - Append this APSInt to the specified SmallString.
61 void toString(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
62 APInt::toString(Str, Radix, isSigned());
64 /// toString - Converts an APInt to a std::string. This is an inefficient
65 /// method, your should prefer passing in a SmallString instead.
66 std::string toString(unsigned Radix) const {
67 return APInt::toString(Radix, isSigned());
69 using APInt::toString;
71 APSInt trunc(uint32_t width) const {
72 return APSInt(APInt::trunc(width), IsUnsigned);
75 APSInt extend(uint32_t width) const {
77 return APSInt(zext(width), IsUnsigned);
79 return APSInt(sext(width), IsUnsigned);
82 APSInt extOrTrunc(uint32_t width) const {
84 return APSInt(zextOrTrunc(width), IsUnsigned);
86 return APSInt(sextOrTrunc(width), IsUnsigned);
89 const APSInt &operator%=(const APSInt &RHS) {
90 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
97 const APSInt &operator/=(const APSInt &RHS) {
98 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
105 APSInt operator%(const APSInt &RHS) const {
106 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
107 return IsUnsigned ? APSInt(urem(RHS), true) : APSInt(srem(RHS), false);
109 APSInt operator/(const APSInt &RHS) const {
110 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
111 return IsUnsigned ? APSInt(udiv(RHS), true) : APSInt(sdiv(RHS), false);
114 APSInt operator>>(unsigned Amt) const {
115 return IsUnsigned ? APSInt(lshr(Amt), true) : APSInt(ashr(Amt), false);
117 APSInt& operator>>=(unsigned Amt) {
118 *this = *this >> Amt;
122 inline bool operator<(const APSInt& RHS) const {
123 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
124 return IsUnsigned ? ult(RHS) : slt(RHS);
126 inline bool operator>(const APSInt& RHS) const {
127 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
128 return IsUnsigned ? ugt(RHS) : sgt(RHS);
130 inline bool operator<=(const APSInt& RHS) const {
131 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
132 return IsUnsigned ? ule(RHS) : sle(RHS);
134 inline bool operator>=(const APSInt& RHS) const {
135 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
136 return IsUnsigned ? uge(RHS) : sge(RHS);
138 inline bool operator==(const APSInt& RHS) const {
139 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
142 inline bool operator==(int64_t RHS) const {
143 return isSameValue(*this, APSInt(APInt(64, RHS), true));
145 inline bool operator!=(const APSInt& RHS) const {
146 return !((*this) == RHS);
148 inline bool operator!=(int64_t RHS) const {
149 return !((*this) == RHS);
152 // The remaining operators just wrap the logic of APInt, but retain the
153 // signedness information.
155 APSInt operator<<(unsigned Bits) const {
156 return APSInt(static_cast<const APInt&>(*this) << Bits, IsUnsigned);
158 APSInt& operator<<=(unsigned Amt) {
159 *this = *this << Amt;
163 APSInt& operator++() {
164 static_cast<APInt&>(*this)++;
167 APSInt& operator--() {
168 static_cast<APInt&>(*this)--;
171 APSInt operator++(int) {
172 return APSInt(++static_cast<APInt&>(*this), IsUnsigned);
174 APSInt operator--(int) {
175 return APSInt(--static_cast<APInt&>(*this), IsUnsigned);
177 APSInt operator-() const {
178 return APSInt(-static_cast<const APInt&>(*this), IsUnsigned);
180 APSInt& operator+=(const APSInt& RHS) {
181 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
182 static_cast<APInt&>(*this) += RHS;
185 APSInt& operator-=(const APSInt& RHS) {
186 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
187 static_cast<APInt&>(*this) -= RHS;
190 APSInt& operator*=(const APSInt& RHS) {
191 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
192 static_cast<APInt&>(*this) *= RHS;
195 APSInt& operator&=(const APSInt& RHS) {
196 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
197 static_cast<APInt&>(*this) &= RHS;
200 APSInt& operator|=(const APSInt& RHS) {
201 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
202 static_cast<APInt&>(*this) |= RHS;
205 APSInt& operator^=(const APSInt& RHS) {
206 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
207 static_cast<APInt&>(*this) ^= RHS;
211 APSInt operator&(const APSInt& RHS) const {
212 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
213 return APSInt(static_cast<const APInt&>(*this) & RHS, IsUnsigned);
215 APSInt And(const APSInt& RHS) const {
216 return this->operator&(RHS);
219 APSInt operator|(const APSInt& RHS) const {
220 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
221 return APSInt(static_cast<const APInt&>(*this) | RHS, IsUnsigned);
223 APSInt Or(const APSInt& RHS) const {
224 return this->operator|(RHS);
228 APSInt operator^(const APSInt& RHS) const {
229 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
230 return APSInt(static_cast<const APInt&>(*this) ^ RHS, IsUnsigned);
232 APSInt Xor(const APSInt& RHS) const {
233 return this->operator^(RHS);
236 APSInt operator*(const APSInt& RHS) const {
237 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
238 return APSInt(static_cast<const APInt&>(*this) * RHS, IsUnsigned);
240 APSInt operator+(const APSInt& RHS) const {
241 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
242 return APSInt(static_cast<const APInt&>(*this) + RHS, IsUnsigned);
244 APSInt operator-(const APSInt& RHS) const {
245 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
246 return APSInt(static_cast<const APInt&>(*this) - RHS, IsUnsigned);
248 APSInt operator~() const {
249 return APSInt(~static_cast<const APInt&>(*this), IsUnsigned);
252 /// getMaxValue - Return the APSInt representing the maximum integer value
253 /// with the given bit width and signedness.
254 static APSInt getMaxValue(uint32_t numBits, bool Unsigned) {
255 return APSInt(Unsigned ? APInt::getMaxValue(numBits)
256 : APInt::getSignedMaxValue(numBits), Unsigned);
259 /// getMinValue - Return the APSInt representing the minimum integer value
260 /// with the given bit width and signedness.
261 static APSInt getMinValue(uint32_t numBits, bool Unsigned) {
262 return APSInt(Unsigned ? APInt::getMinValue(numBits)
263 : APInt::getSignedMinValue(numBits), Unsigned);
266 /// \brief Determine if two APSInts have the same value, zero- or
267 /// sign-extending as needed.
268 static bool isSameValue(const APSInt &I1, const APSInt &I2) {
269 if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
272 // Check for a bit-width mismatch.
273 if (I1.getBitWidth() > I2.getBitWidth())
274 return isSameValue(I1, I2.extend(I1.getBitWidth()));
275 else if (I2.getBitWidth() > I1.getBitWidth())
276 return isSameValue(I1.extend(I2.getBitWidth()), I2);
278 // We have a signedness mismatch. Turn the signed value into an unsigned
284 return APSInt(I1, true) == I2;
290 return I1 == APSInt(I2, true);
293 /// Profile - Used to insert APSInt objects, or objects that contain APSInt
294 /// objects, into FoldingSets.
295 void Profile(FoldingSetNodeID& ID) const;
298 inline bool operator==(int64_t V1, const APSInt& V2) {
301 inline bool operator!=(int64_t V1, const APSInt& V2) {
305 inline raw_ostream &operator<<(raw_ostream &OS, const APSInt &I) {
306 I.print(OS, I.isSigned());
310 } // end namespace llvm