1 //===-- llvm/Support/APSInt.h - Arbitrary Precision Signed Int -*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under the
6 // University of Illinois Open Source 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(unsigned 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; }
55 const APSInt &operator%=(const APSInt &RHS) {
56 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
63 const APSInt &operator/=(const APSInt &RHS) {
64 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
71 APSInt operator%(const APSInt &RHS) const {
72 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
73 return IsUnsigned ? urem(RHS) : srem(RHS);
75 APSInt operator/(const APSInt &RHS) const {
76 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
77 return IsUnsigned ? udiv(RHS) : sdiv(RHS);
80 const APSInt &operator>>=(unsigned Amt) {
85 APSInt operator>>(unsigned Amt) const {
86 return IsUnsigned ? lshr(Amt) : ashr(Amt);
89 inline bool operator<(const APSInt& RHS) const {
90 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
91 return IsUnsigned ? ult(RHS) : slt(RHS);
93 inline bool operator>(const APSInt& RHS) const {
94 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
95 return IsUnsigned ? ugt(RHS) : sgt(RHS);
97 inline bool operator<=(const APSInt& RHS) const {
98 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
99 return IsUnsigned ? ule(RHS) : sle(RHS);
101 inline bool operator>=(const APSInt& RHS) const {
102 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
103 return IsUnsigned ? uge(RHS) : sge(RHS);
107 } // end namespace llvm