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, bool isUnsigned = true)
29 : APInt(BitWidth, 0), IsUnsigned(isUnsigned) {}
31 explicit APSInt(const APInt &I, bool isUnsigned = true)
32 : APInt(I), IsUnsigned(isUnsigned) {}
34 APSInt &operator=(const APSInt &RHS) {
35 APInt::operator=(RHS);
36 IsUnsigned = RHS.IsUnsigned;
40 APSInt &operator=(const APInt &RHS) {
41 // Retain our current sign.
42 APInt::operator=(RHS);
46 APSInt &operator=(uint64_t RHS) {
47 // Retain our current sign.
48 APInt::operator=(RHS);
52 // Query sign information.
53 bool isSigned() const { return !IsUnsigned; }
54 bool isUnsigned() const { return IsUnsigned; }
55 void setIsUnsigned(bool Val) { IsUnsigned = Val; }
56 void setIsSigned(bool Val) { IsUnsigned = !Val; }
58 /// This is used internally to convert an APInt to a string.
59 /// @brief Converts an APInt to a std::string
60 std::string toString(uint8_t Radix = 10) const {
61 return APInt::toString(Radix, isSigned());
64 APSInt& extend(uint32_t width) {
72 APSInt& extOrTrunc(uint32_t width) {
80 const APSInt &operator%=(const APSInt &RHS) {
81 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
88 const APSInt &operator/=(const APSInt &RHS) {
89 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
96 APSInt operator%(const APSInt &RHS) const {
97 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
98 return IsUnsigned ? APSInt(urem(RHS), true) : APSInt(srem(RHS), false);
100 APSInt operator/(const APSInt &RHS) const {
101 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
102 return IsUnsigned ? APSInt(udiv(RHS), true) : APSInt(sdiv(RHS), false);
105 APSInt operator>>(unsigned Amt) const {
106 return IsUnsigned ? APSInt(lshr(Amt), true) : APSInt(ashr(Amt), false);
108 APSInt& operator>>=(unsigned Amt) {
109 *this = *this >> Amt;
113 inline bool operator<(const APSInt& RHS) const {
114 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
115 return IsUnsigned ? ult(RHS) : slt(RHS);
117 inline bool operator>(const APSInt& RHS) const {
118 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
119 return IsUnsigned ? ugt(RHS) : sgt(RHS);
121 inline bool operator<=(const APSInt& RHS) const {
122 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
123 return IsUnsigned ? ule(RHS) : sle(RHS);
125 inline bool operator>=(const APSInt& RHS) const {
126 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
127 return IsUnsigned ? uge(RHS) : sge(RHS);
130 // The remaining operators just wrap the logic of APInt, but retain the
131 // signedness information.
133 APSInt operator<<(unsigned Bits) {
134 return APSInt(static_cast<APInt&>(*this) << Bits, IsUnsigned);
136 APSInt& operator<<=(unsigned Amt) {
137 *this = *this << Amt;
141 APSInt& operator++() {
142 static_cast<APInt&>(*this)++;
145 APSInt& operator--() {
146 static_cast<APInt&>(*this)++;
149 APSInt operator++(int) {
150 return APSInt(++static_cast<APInt&>(*this), IsUnsigned);
152 APSInt operator--(int) {
153 return APSInt(--static_cast<APInt&>(*this), IsUnsigned);
155 APSInt operator-() const {
156 return APSInt(-static_cast<const APInt&>(*this), IsUnsigned);
158 APSInt& operator+=(const APSInt& RHS) {
159 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
160 static_cast<APInt&>(*this) += RHS;
163 APSInt& operator-=(const APSInt& RHS) {
164 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
165 static_cast<APInt&>(*this) -= RHS;
168 APSInt& operator*=(const APSInt& RHS) {
169 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
170 static_cast<APInt&>(*this) *= RHS;
173 APSInt& operator&=(const APSInt& RHS) {
174 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
175 static_cast<APInt&>(*this) &= RHS;
178 APSInt& operator|=(const APSInt& RHS) {
179 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
180 static_cast<APInt&>(*this) |= RHS;
183 APSInt& operator^=(const APSInt& RHS) {
184 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
185 static_cast<APInt&>(*this) ^= RHS;
189 APSInt operator&(const APSInt& RHS) const {
190 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
191 return APSInt(static_cast<const APInt&>(*this) & RHS, IsUnsigned);
193 APSInt And(const APSInt& RHS) const {
194 return this->operator&(RHS);
197 APSInt operator|(const APSInt& RHS) const {
198 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
199 return APSInt(static_cast<const APInt&>(*this) | RHS, IsUnsigned);
201 APSInt Or(const APSInt& RHS) const {
202 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 Xor(const APSInt& RHS) const {
211 return this->operator^(RHS);
214 APSInt operator*(const APSInt& RHS) const {
215 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
216 return APSInt(static_cast<const APInt&>(*this) * RHS, IsUnsigned);
218 APSInt operator+(const APSInt& RHS) const {
219 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
220 return APSInt(static_cast<const APInt&>(*this) + RHS, IsUnsigned);
222 APSInt operator-(const APSInt& RHS) const {
223 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
224 return APSInt(static_cast<const APInt&>(*this) - RHS, IsUnsigned);
227 return APSInt(~static_cast<const APInt&>(*this), IsUnsigned);
230 /// Profile - Used to insert APSInt objects, or objects that contain APSInt
231 /// objects, into FoldingSets.
232 void Profile(FoldingSetNodeID& ID) const;
235 } // end namespace llvm