1 //===-- llvm/ADT/APSInt.h - Arbitrary Precision Signed Int -----*- C++ -*--===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the APSInt class, which is a simple class that
10 // represents an arbitrary sized integer that knows its signedness.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ADT_APSINT_H
15 #define LLVM_ADT_APSINT_H
17 #include "llvm/ADT/APInt.h"
21 class LLVM_NODISCARD 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(APInt I
, bool isUnsigned
= true)
34 : APInt(std::move(I
)), IsUnsigned(isUnsigned
) {}
36 /// Construct an APSInt from a string representation.
38 /// This constructor interprets the string \p Str using the radix of 10.
39 /// The interpretation stops at the end of the string. The bit width of the
40 /// constructed APSInt is determined automatically.
42 /// \param Str the string to be interpreted.
43 explicit APSInt(StringRef Str
);
45 /// Determine sign of this APSInt.
47 /// \returns true if this APSInt is negative, false otherwise
48 bool isNegative() const { return isSigned() && APInt::isNegative(); }
50 /// Determine if this APSInt Value is non-negative (>= 0)
52 /// \returns true if this APSInt is non-negative, false otherwise
53 bool isNonNegative() const { return !isNegative(); }
55 /// Determine if this APSInt Value is positive.
57 /// This tests if the value of this APSInt is positive (> 0). Note
58 /// that 0 is not a positive value.
60 /// \returns true if this APSInt is positive.
61 bool isStrictlyPositive() const { return isNonNegative() && !isNullValue(); }
63 APSInt
&operator=(APInt RHS
) {
64 // Retain our current sign.
65 APInt::operator=(std::move(RHS
));
69 APSInt
&operator=(uint64_t RHS
) {
70 // Retain our current sign.
71 APInt::operator=(RHS
);
75 // Query sign information.
76 bool isSigned() const { return !IsUnsigned
; }
77 bool isUnsigned() const { return IsUnsigned
; }
78 void setIsUnsigned(bool Val
) { IsUnsigned
= Val
; }
79 void setIsSigned(bool Val
) { IsUnsigned
= !Val
; }
81 /// toString - Append this APSInt to the specified SmallString.
82 void toString(SmallVectorImpl
<char> &Str
, unsigned Radix
= 10) const {
83 APInt::toString(Str
, Radix
, isSigned());
85 /// toString - Converts an APInt to a std::string. This is an inefficient
86 /// method; you should prefer passing in a SmallString instead.
87 std::string
toString(unsigned Radix
) const {
88 return APInt::toString(Radix
, isSigned());
90 using APInt::toString
;
92 /// Get the correctly-extended \c int64_t value.
93 int64_t getExtValue() const {
94 assert(getMinSignedBits() <= 64 && "Too many bits for int64_t");
95 return isSigned() ? getSExtValue() : getZExtValue();
98 APSInt
trunc(uint32_t width
) const {
99 return APSInt(APInt::trunc(width
), IsUnsigned
);
102 APSInt
extend(uint32_t width
) const {
104 return APSInt(zext(width
), IsUnsigned
);
106 return APSInt(sext(width
), IsUnsigned
);
109 APSInt
extOrTrunc(uint32_t width
) const {
111 return APSInt(zextOrTrunc(width
), IsUnsigned
);
113 return APSInt(sextOrTrunc(width
), IsUnsigned
);
116 const APSInt
&operator%=(const APSInt
&RHS
) {
117 assert(IsUnsigned
== RHS
.IsUnsigned
&& "Signedness mismatch!");
124 const APSInt
&operator/=(const APSInt
&RHS
) {
125 assert(IsUnsigned
== RHS
.IsUnsigned
&& "Signedness mismatch!");
132 APSInt
operator%(const APSInt
&RHS
) const {
133 assert(IsUnsigned
== RHS
.IsUnsigned
&& "Signedness mismatch!");
134 return IsUnsigned
? APSInt(urem(RHS
), true) : APSInt(srem(RHS
), false);
136 APSInt
operator/(const APSInt
&RHS
) const {
137 assert(IsUnsigned
== RHS
.IsUnsigned
&& "Signedness mismatch!");
138 return IsUnsigned
? APSInt(udiv(RHS
), true) : APSInt(sdiv(RHS
), false);
141 APSInt
operator>>(unsigned Amt
) const {
142 return IsUnsigned
? APSInt(lshr(Amt
), true) : APSInt(ashr(Amt
), false);
144 APSInt
& operator>>=(unsigned Amt
) {
152 inline bool operator<(const APSInt
& RHS
) const {
153 assert(IsUnsigned
== RHS
.IsUnsigned
&& "Signedness mismatch!");
154 return IsUnsigned
? ult(RHS
) : slt(RHS
);
156 inline bool operator>(const APSInt
& RHS
) const {
157 assert(IsUnsigned
== RHS
.IsUnsigned
&& "Signedness mismatch!");
158 return IsUnsigned
? ugt(RHS
) : sgt(RHS
);
160 inline bool operator<=(const APSInt
& RHS
) const {
161 assert(IsUnsigned
== RHS
.IsUnsigned
&& "Signedness mismatch!");
162 return IsUnsigned
? ule(RHS
) : sle(RHS
);
164 inline bool operator>=(const APSInt
& RHS
) const {
165 assert(IsUnsigned
== RHS
.IsUnsigned
&& "Signedness mismatch!");
166 return IsUnsigned
? uge(RHS
) : sge(RHS
);
168 inline bool operator==(const APSInt
& RHS
) const {
169 assert(IsUnsigned
== RHS
.IsUnsigned
&& "Signedness mismatch!");
172 inline bool operator!=(const APSInt
& RHS
) const {
173 return !((*this) == RHS
);
176 bool operator==(int64_t RHS
) const {
177 return compareValues(*this, get(RHS
)) == 0;
179 bool operator!=(int64_t RHS
) const {
180 return compareValues(*this, get(RHS
)) != 0;
182 bool operator<=(int64_t RHS
) const {
183 return compareValues(*this, get(RHS
)) <= 0;
185 bool operator>=(int64_t RHS
) const {
186 return compareValues(*this, get(RHS
)) >= 0;
188 bool operator<(int64_t RHS
) const {
189 return compareValues(*this, get(RHS
)) < 0;
191 bool operator>(int64_t RHS
) const {
192 return compareValues(*this, get(RHS
)) > 0;
195 // The remaining operators just wrap the logic of APInt, but retain the
196 // signedness information.
198 APSInt
operator<<(unsigned Bits
) const {
199 return APSInt(static_cast<const APInt
&>(*this) << Bits
, IsUnsigned
);
201 APSInt
& operator<<=(unsigned Amt
) {
202 static_cast<APInt
&>(*this) <<= Amt
;
206 APSInt
& operator++() {
207 ++(static_cast<APInt
&>(*this));
210 APSInt
& operator--() {
211 --(static_cast<APInt
&>(*this));
214 APSInt
operator++(int) {
215 return APSInt(++static_cast<APInt
&>(*this), IsUnsigned
);
217 APSInt
operator--(int) {
218 return APSInt(--static_cast<APInt
&>(*this), IsUnsigned
);
220 APSInt
operator-() const {
221 return APSInt(-static_cast<const APInt
&>(*this), IsUnsigned
);
223 APSInt
& operator+=(const APSInt
& RHS
) {
224 assert(IsUnsigned
== RHS
.IsUnsigned
&& "Signedness mismatch!");
225 static_cast<APInt
&>(*this) += RHS
;
228 APSInt
& operator-=(const APSInt
& RHS
) {
229 assert(IsUnsigned
== RHS
.IsUnsigned
&& "Signedness mismatch!");
230 static_cast<APInt
&>(*this) -= RHS
;
233 APSInt
& operator*=(const APSInt
& RHS
) {
234 assert(IsUnsigned
== RHS
.IsUnsigned
&& "Signedness mismatch!");
235 static_cast<APInt
&>(*this) *= RHS
;
238 APSInt
& operator&=(const APSInt
& RHS
) {
239 assert(IsUnsigned
== RHS
.IsUnsigned
&& "Signedness mismatch!");
240 static_cast<APInt
&>(*this) &= RHS
;
243 APSInt
& operator|=(const APSInt
& RHS
) {
244 assert(IsUnsigned
== RHS
.IsUnsigned
&& "Signedness mismatch!");
245 static_cast<APInt
&>(*this) |= RHS
;
248 APSInt
& operator^=(const APSInt
& RHS
) {
249 assert(IsUnsigned
== RHS
.IsUnsigned
&& "Signedness mismatch!");
250 static_cast<APInt
&>(*this) ^= RHS
;
254 APSInt
operator&(const APSInt
& RHS
) const {
255 assert(IsUnsigned
== RHS
.IsUnsigned
&& "Signedness mismatch!");
256 return APSInt(static_cast<const APInt
&>(*this) & RHS
, IsUnsigned
);
259 APSInt
operator|(const APSInt
& RHS
) const {
260 assert(IsUnsigned
== RHS
.IsUnsigned
&& "Signedness mismatch!");
261 return APSInt(static_cast<const APInt
&>(*this) | RHS
, IsUnsigned
);
264 APSInt
operator^(const APSInt
&RHS
) const {
265 assert(IsUnsigned
== RHS
.IsUnsigned
&& "Signedness mismatch!");
266 return APSInt(static_cast<const APInt
&>(*this) ^ RHS
, IsUnsigned
);
269 APSInt
operator*(const APSInt
& RHS
) const {
270 assert(IsUnsigned
== RHS
.IsUnsigned
&& "Signedness mismatch!");
271 return APSInt(static_cast<const APInt
&>(*this) * RHS
, IsUnsigned
);
273 APSInt
operator+(const APSInt
& RHS
) const {
274 assert(IsUnsigned
== RHS
.IsUnsigned
&& "Signedness mismatch!");
275 return APSInt(static_cast<const APInt
&>(*this) + RHS
, IsUnsigned
);
277 APSInt
operator-(const APSInt
& RHS
) const {
278 assert(IsUnsigned
== RHS
.IsUnsigned
&& "Signedness mismatch!");
279 return APSInt(static_cast<const APInt
&>(*this) - RHS
, IsUnsigned
);
281 APSInt
operator~() const {
282 return APSInt(~static_cast<const APInt
&>(*this), IsUnsigned
);
285 /// getMaxValue - Return the APSInt representing the maximum integer value
286 /// with the given bit width and signedness.
287 static APSInt
getMaxValue(uint32_t numBits
, bool Unsigned
) {
288 return APSInt(Unsigned
? APInt::getMaxValue(numBits
)
289 : APInt::getSignedMaxValue(numBits
), Unsigned
);
292 /// getMinValue - Return the APSInt representing the minimum integer value
293 /// with the given bit width and signedness.
294 static APSInt
getMinValue(uint32_t numBits
, bool Unsigned
) {
295 return APSInt(Unsigned
? APInt::getMinValue(numBits
)
296 : APInt::getSignedMinValue(numBits
), Unsigned
);
299 /// Determine if two APSInts have the same value, zero- or
300 /// sign-extending as needed.
301 static bool isSameValue(const APSInt
&I1
, const APSInt
&I2
) {
302 return !compareValues(I1
, I2
);
305 /// Compare underlying values of two numbers.
306 static int compareValues(const APSInt
&I1
, const APSInt
&I2
) {
307 if (I1
.getBitWidth() == I2
.getBitWidth() && I1
.isSigned() == I2
.isSigned())
308 return I1
.IsUnsigned
? I1
.compare(I2
) : I1
.compareSigned(I2
);
310 // Check for a bit-width mismatch.
311 if (I1
.getBitWidth() > I2
.getBitWidth())
312 return compareValues(I1
, I2
.extend(I1
.getBitWidth()));
313 if (I2
.getBitWidth() > I1
.getBitWidth())
314 return compareValues(I1
.extend(I2
.getBitWidth()), I2
);
316 // We have a signedness mismatch. Check for negative values and do an
317 // unsigned compare if both are positive.
319 assert(!I2
.isSigned() && "Expected signed mismatch");
323 assert(I2
.isSigned() && "Expected signed mismatch");
328 return I1
.compare(I2
);
331 static APSInt
get(int64_t X
) { return APSInt(APInt(64, X
), false); }
332 static APSInt
getUnsigned(uint64_t X
) { return APSInt(APInt(64, X
), true); }
334 /// Profile - Used to insert APSInt objects, or objects that contain APSInt
335 /// objects, into FoldingSets.
336 void Profile(FoldingSetNodeID
& ID
) const;
339 inline bool operator==(int64_t V1
, const APSInt
&V2
) { return V2
== V1
; }
340 inline bool operator!=(int64_t V1
, const APSInt
&V2
) { return V2
!= V1
; }
341 inline bool operator<=(int64_t V1
, const APSInt
&V2
) { return V2
>= V1
; }
342 inline bool operator>=(int64_t V1
, const APSInt
&V2
) { return V2
<= V1
; }
343 inline bool operator<(int64_t V1
, const APSInt
&V2
) { return V2
> V1
; }
344 inline bool operator>(int64_t V1
, const APSInt
&V2
) { return V2
< V1
; }
346 inline raw_ostream
&operator<<(raw_ostream
&OS
, const APSInt
&I
) {
347 I
.print(OS
, I
.isSigned());
351 } // end namespace llvm