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 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_ADT_APSINT_H
16 #define LLVM_ADT_APSINT_H
18 #include "llvm/ADT/APInt.h"
22 class LLVM_NODISCARD APSInt : public APInt {
26 /// Default constructor that creates an uninitialized APInt.
27 explicit APSInt() : IsUnsigned(false) {}
29 /// APSInt ctor - Create an APSInt with the specified width, default to
31 explicit APSInt(uint32_t BitWidth, bool isUnsigned = true)
32 : APInt(BitWidth, 0), IsUnsigned(isUnsigned) {}
34 explicit APSInt(APInt I, bool isUnsigned = true)
35 : APInt(std::move(I)), IsUnsigned(isUnsigned) {}
37 /// Construct an APSInt from a string representation.
39 /// This constructor interprets the string \p Str using the radix of 10.
40 /// The interpretation stops at the end of the string. The bit width of the
41 /// constructed APSInt is determined automatically.
43 /// \param Str the string to be interpreted.
44 explicit APSInt(StringRef Str);
46 APSInt &operator=(APInt RHS) {
47 // Retain our current sign.
48 APInt::operator=(std::move(RHS));
52 APSInt &operator=(uint64_t RHS) {
53 // Retain our current sign.
54 APInt::operator=(RHS);
58 // Query sign information.
59 bool isSigned() const { return !IsUnsigned; }
60 bool isUnsigned() const { return IsUnsigned; }
61 void setIsUnsigned(bool Val) { IsUnsigned = Val; }
62 void setIsSigned(bool Val) { IsUnsigned = !Val; }
64 /// toString - Append this APSInt to the specified SmallString.
65 void toString(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
66 APInt::toString(Str, Radix, isSigned());
68 /// toString - Converts an APInt to a std::string. This is an inefficient
69 /// method; you should prefer passing in a SmallString instead.
70 std::string toString(unsigned Radix) const {
71 return APInt::toString(Radix, isSigned());
73 using APInt::toString;
75 /// Get the correctly-extended \c int64_t value.
76 int64_t getExtValue() const {
77 assert(getMinSignedBits() <= 64 && "Too many bits for int64_t");
78 return isSigned() ? getSExtValue() : getZExtValue();
81 APSInt trunc(uint32_t width) const {
82 return APSInt(APInt::trunc(width), IsUnsigned);
85 APSInt extend(uint32_t width) const {
87 return APSInt(zext(width), IsUnsigned);
89 return APSInt(sext(width), IsUnsigned);
92 APSInt extOrTrunc(uint32_t width) const {
94 return APSInt(zextOrTrunc(width), IsUnsigned);
96 return APSInt(sextOrTrunc(width), IsUnsigned);
99 const APSInt &operator%=(const APSInt &RHS) {
100 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
107 const APSInt &operator/=(const APSInt &RHS) {
108 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
115 APSInt operator%(const APSInt &RHS) const {
116 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
117 return IsUnsigned ? APSInt(urem(RHS), true) : APSInt(srem(RHS), false);
119 APSInt operator/(const APSInt &RHS) const {
120 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
121 return IsUnsigned ? APSInt(udiv(RHS), true) : APSInt(sdiv(RHS), false);
124 APSInt operator>>(unsigned Amt) const {
125 return IsUnsigned ? APSInt(lshr(Amt), true) : APSInt(ashr(Amt), false);
127 APSInt& operator>>=(unsigned Amt) {
135 inline bool operator<(const APSInt& RHS) const {
136 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
137 return IsUnsigned ? ult(RHS) : slt(RHS);
139 inline bool operator>(const APSInt& RHS) const {
140 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
141 return IsUnsigned ? ugt(RHS) : sgt(RHS);
143 inline bool operator<=(const APSInt& RHS) const {
144 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
145 return IsUnsigned ? ule(RHS) : sle(RHS);
147 inline bool operator>=(const APSInt& RHS) const {
148 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
149 return IsUnsigned ? uge(RHS) : sge(RHS);
151 inline bool operator==(const APSInt& RHS) const {
152 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
155 inline bool operator!=(const APSInt& RHS) const {
156 return !((*this) == RHS);
159 bool operator==(int64_t RHS) const {
160 return compareValues(*this, get(RHS)) == 0;
162 bool operator!=(int64_t RHS) const {
163 return compareValues(*this, get(RHS)) != 0;
165 bool operator<=(int64_t RHS) const {
166 return compareValues(*this, get(RHS)) <= 0;
168 bool operator>=(int64_t RHS) const {
169 return compareValues(*this, get(RHS)) >= 0;
171 bool operator<(int64_t RHS) const {
172 return compareValues(*this, get(RHS)) < 0;
174 bool operator>(int64_t RHS) const {
175 return compareValues(*this, get(RHS)) > 0;
178 // The remaining operators just wrap the logic of APInt, but retain the
179 // signedness information.
181 APSInt operator<<(unsigned Bits) const {
182 return APSInt(static_cast<const APInt&>(*this) << Bits, IsUnsigned);
184 APSInt& operator<<=(unsigned Amt) {
185 static_cast<APInt&>(*this) <<= Amt;
189 APSInt& operator++() {
190 ++(static_cast<APInt&>(*this));
193 APSInt& operator--() {
194 --(static_cast<APInt&>(*this));
197 APSInt operator++(int) {
198 return APSInt(++static_cast<APInt&>(*this), IsUnsigned);
200 APSInt operator--(int) {
201 return APSInt(--static_cast<APInt&>(*this), IsUnsigned);
203 APSInt operator-() const {
204 return APSInt(-static_cast<const APInt&>(*this), IsUnsigned);
206 APSInt& operator+=(const APSInt& RHS) {
207 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
208 static_cast<APInt&>(*this) += RHS;
211 APSInt& operator-=(const APSInt& RHS) {
212 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
213 static_cast<APInt&>(*this) -= RHS;
216 APSInt& operator*=(const APSInt& RHS) {
217 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
218 static_cast<APInt&>(*this) *= RHS;
221 APSInt& operator&=(const APSInt& RHS) {
222 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
223 static_cast<APInt&>(*this) &= RHS;
226 APSInt& operator|=(const APSInt& RHS) {
227 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
228 static_cast<APInt&>(*this) |= RHS;
231 APSInt& operator^=(const APSInt& RHS) {
232 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
233 static_cast<APInt&>(*this) ^= RHS;
237 APSInt operator&(const APSInt& RHS) const {
238 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
239 return APSInt(static_cast<const APInt&>(*this) & RHS, IsUnsigned);
242 APSInt operator|(const APSInt& RHS) const {
243 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
244 return APSInt(static_cast<const APInt&>(*this) | RHS, IsUnsigned);
247 APSInt operator^(const APSInt &RHS) const {
248 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
249 return APSInt(static_cast<const APInt&>(*this) ^ RHS, IsUnsigned);
252 APSInt operator*(const APSInt& RHS) const {
253 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
254 return APSInt(static_cast<const APInt&>(*this) * RHS, IsUnsigned);
256 APSInt operator+(const APSInt& RHS) const {
257 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
258 return APSInt(static_cast<const APInt&>(*this) + RHS, IsUnsigned);
260 APSInt operator-(const APSInt& RHS) const {
261 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
262 return APSInt(static_cast<const APInt&>(*this) - RHS, IsUnsigned);
264 APSInt operator~() const {
265 return APSInt(~static_cast<const APInt&>(*this), IsUnsigned);
268 /// getMaxValue - Return the APSInt representing the maximum integer value
269 /// with the given bit width and signedness.
270 static APSInt getMaxValue(uint32_t numBits, bool Unsigned) {
271 return APSInt(Unsigned ? APInt::getMaxValue(numBits)
272 : APInt::getSignedMaxValue(numBits), Unsigned);
275 /// getMinValue - Return the APSInt representing the minimum integer value
276 /// with the given bit width and signedness.
277 static APSInt getMinValue(uint32_t numBits, bool Unsigned) {
278 return APSInt(Unsigned ? APInt::getMinValue(numBits)
279 : APInt::getSignedMinValue(numBits), Unsigned);
282 /// Determine if two APSInts have the same value, zero- or
283 /// sign-extending as needed.
284 static bool isSameValue(const APSInt &I1, const APSInt &I2) {
285 return !compareValues(I1, I2);
288 /// Compare underlying values of two numbers.
289 static int compareValues(const APSInt &I1, const APSInt &I2) {
290 if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
291 return I1.IsUnsigned ? I1.compare(I2) : I1.compareSigned(I2);
293 // Check for a bit-width mismatch.
294 if (I1.getBitWidth() > I2.getBitWidth())
295 return compareValues(I1, I2.extend(I1.getBitWidth()));
296 if (I2.getBitWidth() > I1.getBitWidth())
297 return compareValues(I1.extend(I2.getBitWidth()), I2);
299 // We have a signedness mismatch. Check for negative values and do an
300 // unsigned compare if both are positive.
302 assert(!I2.isSigned() && "Expected signed mismatch");
306 assert(I2.isSigned() && "Expected signed mismatch");
311 return I1.compare(I2);
314 static APSInt get(int64_t X) { return APSInt(APInt(64, X), false); }
315 static APSInt getUnsigned(uint64_t X) { return APSInt(APInt(64, X), true); }
317 /// Profile - Used to insert APSInt objects, or objects that contain APSInt
318 /// objects, into FoldingSets.
319 void Profile(FoldingSetNodeID& ID) const;
322 inline bool operator==(int64_t V1, const APSInt &V2) { return V2 == V1; }
323 inline bool operator!=(int64_t V1, const APSInt &V2) { return V2 != V1; }
324 inline bool operator<=(int64_t V1, const APSInt &V2) { return V2 >= V1; }
325 inline bool operator>=(int64_t V1, const APSInt &V2) { return V2 <= V1; }
326 inline bool operator<(int64_t V1, const APSInt &V2) { return V2 > V1; }
327 inline bool operator>(int64_t V1, const APSInt &V2) { return V2 < V1; }
329 inline raw_ostream &operator<<(raw_ostream &OS, const APSInt &I) {
330 I.print(OS, I.isSigned());
334 } // end namespace llvm