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; }
53 void setIsSigned(bool Val) { IsUnsigned = !Val; }
56 const APSInt &operator%=(const APSInt &RHS) {
57 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
64 const APSInt &operator/=(const APSInt &RHS) {
65 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
72 APSInt operator%(const APSInt &RHS) const {
73 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
74 return IsUnsigned ? urem(RHS) : srem(RHS);
76 APSInt operator/(const APSInt &RHS) const {
77 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
78 return IsUnsigned ? udiv(RHS) : sdiv(RHS);
81 const APSInt &operator>>=(unsigned Amt) {
86 APSInt& extend(uint32_t width) {
94 APSInt& extOrTrunc(uint32_t width) {
102 APSInt operator>>(unsigned Amt) const {
103 return IsUnsigned ? lshr(Amt) : ashr(Amt);
106 inline bool operator<(const APSInt& RHS) const {
107 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
108 return IsUnsigned ? ult(RHS) : slt(RHS);
110 inline bool operator>(const APSInt& RHS) const {
111 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
112 return IsUnsigned ? ugt(RHS) : sgt(RHS);
114 inline bool operator<=(const APSInt& RHS) const {
115 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
116 return IsUnsigned ? ule(RHS) : sle(RHS);
118 inline bool operator>=(const APSInt& RHS) const {
119 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
120 return IsUnsigned ? uge(RHS) : sge(RHS);
124 } // end namespace llvm