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.
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);
139 // The remaining operators just wrap the logic of APInt, but retain the
140 // signedness information.
142 APSInt operator<<(unsigned Bits) const {
143 return APSInt(static_cast<const APInt&>(*this) << Bits, IsUnsigned);
145 APSInt& operator<<=(unsigned Amt) {
146 *this = *this << Amt;
150 APSInt& operator++() {
151 static_cast<APInt&>(*this)++;
154 APSInt& operator--() {
155 static_cast<APInt&>(*this)--;
158 APSInt operator++(int) {
159 return APSInt(++static_cast<APInt&>(*this), IsUnsigned);
161 APSInt operator--(int) {
162 return APSInt(--static_cast<APInt&>(*this), IsUnsigned);
164 APSInt operator-() const {
165 return APSInt(-static_cast<const APInt&>(*this), IsUnsigned);
167 APSInt& operator+=(const APSInt& RHS) {
168 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
169 static_cast<APInt&>(*this) += RHS;
172 APSInt& operator-=(const APSInt& RHS) {
173 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
174 static_cast<APInt&>(*this) -= RHS;
177 APSInt& operator*=(const APSInt& RHS) {
178 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
179 static_cast<APInt&>(*this) *= RHS;
182 APSInt& operator&=(const APSInt& RHS) {
183 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
184 static_cast<APInt&>(*this) &= RHS;
187 APSInt& operator|=(const APSInt& RHS) {
188 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
189 static_cast<APInt&>(*this) |= RHS;
192 APSInt& operator^=(const APSInt& RHS) {
193 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
194 static_cast<APInt&>(*this) ^= RHS;
198 APSInt operator&(const APSInt& RHS) const {
199 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
200 return APSInt(static_cast<const APInt&>(*this) & RHS, IsUnsigned);
202 APSInt And(const APSInt& RHS) const {
203 return this->operator&(RHS);
206 APSInt operator|(const APSInt& RHS) const {
207 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
208 return APSInt(static_cast<const APInt&>(*this) | RHS, IsUnsigned);
210 APSInt Or(const APSInt& RHS) const {
211 return this->operator|(RHS);
215 APSInt operator^(const APSInt& RHS) const {
216 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
217 return APSInt(static_cast<const APInt&>(*this) ^ RHS, IsUnsigned);
219 APSInt Xor(const APSInt& RHS) const {
220 return this->operator^(RHS);
223 APSInt operator*(const APSInt& RHS) const {
224 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
225 return APSInt(static_cast<const APInt&>(*this) * RHS, IsUnsigned);
227 APSInt operator+(const APSInt& RHS) const {
228 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
229 return APSInt(static_cast<const APInt&>(*this) + RHS, IsUnsigned);
231 APSInt operator-(const APSInt& RHS) const {
232 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
233 return APSInt(static_cast<const APInt&>(*this) - RHS, IsUnsigned);
235 APSInt operator~() const {
236 return APSInt(~static_cast<const APInt&>(*this), IsUnsigned);
239 /// getMaxValue - Return the APSInt representing the maximum integer value
240 /// with the given bit width and signedness.
241 static APSInt getMaxValue(uint32_t numBits, bool Unsigned) {
242 return APSInt(Unsigned ? APInt::getMaxValue(numBits)
243 : APInt::getSignedMaxValue(numBits), Unsigned);
246 /// getMinValue - Return the APSInt representing the minimum integer value
247 /// with the given bit width and signedness.
248 static APSInt getMinValue(uint32_t numBits, bool Unsigned) {
249 return APSInt(Unsigned ? APInt::getMinValue(numBits)
250 : APInt::getSignedMinValue(numBits), Unsigned);
253 /// Profile - Used to insert APSInt objects, or objects that contain APSInt
254 /// objects, into FoldingSets.
255 void Profile(FoldingSetNodeID& ID) const;
258 inline raw_ostream &operator<<(raw_ostream &OS, const APSInt &I) {
259 I.print(OS, I.isSigned());
264 } // end namespace llvm