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"
23 class APSInt : public APInt {
26 /// APSInt ctor - Create an APSInt with the specified width, default to
28 explicit APSInt(uint32_t BitWidth) : APInt(BitWidth, 0), IsUnsigned(true) {}
30 explicit APSInt(const APInt &I, bool isUnsigned = true)
31 : APInt(I), IsUnsigned(isUnsigned) {}
33 APSInt &operator=(const APSInt &RHS) {
34 APInt::operator=(RHS);
35 IsUnsigned = RHS.IsUnsigned;
39 APSInt &operator=(const APInt &RHS) {
40 // Retain our current sign.
41 APInt::operator=(RHS);
45 APSInt &operator=(uint64_t RHS) {
46 // Retain our current sign.
47 APInt::operator=(RHS);
51 // Query sign information.
52 bool isSigned() const { return !IsUnsigned; }
53 bool isUnsigned() const { return IsUnsigned; }
54 void setIsUnsigned(bool Val) { IsUnsigned = Val; }
55 void setIsSigned(bool Val) { IsUnsigned = !Val; }
57 /// This is used internally to convert an APInt to a string.
58 /// @brief Converts an APInt to a std::string
59 std::string toString(uint8_t Radix = 10) const {
60 return APInt::toString(Radix, isSigned());
63 APSInt& extend(uint32_t width) {
71 APSInt& extOrTrunc(uint32_t width) {
79 const APSInt &operator%=(const APSInt &RHS) {
80 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
87 const APSInt &operator/=(const APSInt &RHS) {
88 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
95 APSInt operator%(const APSInt &RHS) const {
96 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
97 return IsUnsigned ? APSInt(urem(RHS), true) : APSInt(srem(RHS), false);
99 APSInt operator/(const APSInt &RHS) const {
100 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
101 return IsUnsigned ? APSInt(udiv(RHS), true) : APSInt(sdiv(RHS), false);
104 APSInt operator>>(unsigned Amt) const {
105 return IsUnsigned ? APSInt(lshr(Amt), true) : APSInt(ashr(Amt), false);
107 APSInt& operator>>=(unsigned Amt) {
108 *this = *this >> Amt;
112 inline bool operator<(const APSInt& RHS) const {
113 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
114 return IsUnsigned ? ult(RHS) : slt(RHS);
116 inline bool operator>(const APSInt& RHS) const {
117 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
118 return IsUnsigned ? ugt(RHS) : sgt(RHS);
120 inline bool operator<=(const APSInt& RHS) const {
121 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
122 return IsUnsigned ? ule(RHS) : sle(RHS);
124 inline bool operator>=(const APSInt& RHS) const {
125 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
126 return IsUnsigned ? uge(RHS) : sge(RHS);
129 // The remaining operators just wrap the logic of APInt, but retain the
130 // signedness information.
132 APSInt operator<<(unsigned Bits) {
133 return APSInt(static_cast<APInt&>(*this) << Bits, IsUnsigned);
135 APSInt& operator<<=(unsigned Amt) {
136 *this = *this << Amt;
140 APSInt& operator++() {
141 static_cast<APInt&>(*this)++;
144 APSInt& operator--() {
145 static_cast<APInt&>(*this)++;
148 APSInt operator++(int) {
149 return APSInt(++static_cast<APInt&>(*this), IsUnsigned);
151 APSInt operator--(int) {
152 return APSInt(--static_cast<APInt&>(*this), IsUnsigned);
154 APSInt operator-() const {
155 return APSInt(-static_cast<const APInt&>(*this), IsUnsigned);
157 APSInt& operator+=(const APSInt& RHS) {
158 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
159 static_cast<APInt&>(*this) += RHS;
162 APSInt& operator-=(const APSInt& RHS) {
163 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
164 static_cast<APInt&>(*this) -= RHS;
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;
188 APSInt operator&(const APSInt& RHS) const {
189 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
190 return APSInt(static_cast<const APInt&>(*this) & RHS, IsUnsigned);
192 APSInt And(const APSInt& RHS) const {
193 return this->operator&(RHS);
196 APSInt operator|(const APSInt& RHS) const {
197 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
198 return APSInt(static_cast<const APInt&>(*this) | RHS, IsUnsigned);
200 APSInt Or(const APSInt& RHS) const {
201 return this->operator|(RHS);
205 APSInt operator^(const APSInt& RHS) const {
206 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
207 return APSInt(static_cast<const APInt&>(*this) ^ RHS, IsUnsigned);
209 APSInt Xor(const APSInt& RHS) const {
210 return this->operator^(RHS);
213 APSInt operator*(const APSInt& RHS) const {
214 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
215 return APSInt(static_cast<const APInt&>(*this) * RHS, IsUnsigned);
217 APSInt operator+(const APSInt& RHS) const {
218 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
219 return APSInt(static_cast<const APInt&>(*this) + RHS, IsUnsigned);
221 APSInt operator-(const APSInt& RHS) const {
222 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
223 return APSInt(static_cast<const APInt&>(*this) - RHS, IsUnsigned);
226 return APSInt(~static_cast<const APInt&>(*this), IsUnsigned);
229 /// Profile - Used to insert APSInt objects, or objects that contain APSInt
230 /// objects, into FoldingSets.
231 void Profile(FoldingSetNodeID& ID) const;
234 } // end namespace llvm