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) {}
29 APSInt(const APInt &I) : APInt(I), IsUnsigned(true) {}
31 APSInt &operator=(const APSInt &RHS) {
32 APInt::operator=(RHS);
33 IsUnsigned = RHS.IsUnsigned;
37 APSInt &operator=(const APInt &RHS) {
38 // Retain our current sign.
39 APInt::operator=(RHS);
43 APSInt &operator=(uint64_t RHS) {
44 // Retain our current sign.
45 APInt::operator=(RHS);
49 // Query sign information.
50 bool isSigned() const { return !IsUnsigned; }
51 bool isUnsigned() const { return IsUnsigned; }
52 void setIsUnsigned(bool Val) { IsUnsigned = Val; }
53 void setIsSigned(bool Val) { IsUnsigned = !Val; }
55 /// This is used internally to convert an APInt to a string.
56 /// @brief Converts an APInt to a std::string
57 std::string toString(uint8_t Radix = 10) const {
58 return APInt::toString(Radix, isSigned());
62 const APSInt &operator%=(const APSInt &RHS) {
63 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
70 const APSInt &operator/=(const APSInt &RHS) {
71 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
78 APSInt operator%(const APSInt &RHS) const {
79 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
80 return IsUnsigned ? urem(RHS) : srem(RHS);
82 APSInt operator/(const APSInt &RHS) const {
83 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
84 return IsUnsigned ? udiv(RHS) : sdiv(RHS);
87 const APSInt &operator>>=(unsigned Amt) {
92 APSInt& extend(uint32_t width) {
100 APSInt& extOrTrunc(uint32_t width) {
108 APSInt operator>>(unsigned Amt) const {
109 return IsUnsigned ? lshr(Amt) : ashr(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 /// Profile - Used to insert APSInt objects, or objects that contain APSInt
130 /// objects, into FoldingSets.
131 void Profile(FoldingSetNodeID& ID) const;
134 } // end namespace llvm