Add a helper class (APSInt) which can represent an APInt along with sign
[oota-llvm.git] / include / llvm / ADT / APSInt.h
1 //===-- llvm/Support/APSInt.h - Arbitrary Precision Signed Int -*- C++ -*--===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the APSInt class, which is a simple class that
11 // represents an arbitrary sized integer that knows its signedness.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_APSINT_H
16 #define LLVM_APSINT_H
17
18 #include "llvm/ADT/APInt.h"
19
20 namespace llvm {
21   
22   
23 class APSInt : public APInt {
24   bool IsUnsigned;
25 public:
26   /// APSInt ctor - Create an APSInt with the specified width, default to
27   /// unsigned.
28   explicit APSInt(unsigned BitWidth) : APInt(BitWidth, 0), IsUnsigned(true) {}
29   APSInt(const APInt &I) : APInt(I), IsUnsigned(true) {}
30
31   APSInt &operator=(const APSInt &RHS) {
32     APInt::operator=(RHS); 
33     IsUnsigned = RHS.IsUnsigned;
34     return *this;
35   }
36
37   APSInt &operator=(const APInt &RHS) {
38     // Retain our current sign.
39     APInt::operator=(RHS); 
40     return *this;
41   }
42
43   APSInt &operator=(uint64_t RHS) {
44     // Retain our current sign.
45     APInt::operator=(RHS); 
46     return *this;
47   }
48
49   // Query sign information.
50   bool isSigned() const { return !IsUnsigned; }
51   bool isUnsigned() const { return IsUnsigned; }
52   void setIsUnsigned(bool Val) { IsUnsigned = Val; }
53   
54   
55   const APSInt &operator%=(const APSInt &RHS) {
56     assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
57     if (IsUnsigned)
58       *this = urem(RHS);
59     else
60       *this = srem(RHS);
61     return *this;
62   }
63   const APSInt &operator/=(const APSInt &RHS) {
64     assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
65     if (IsUnsigned)
66       *this = udiv(RHS);
67     else
68       *this = sdiv(RHS);
69     return *this;
70   }
71   
72   const APSInt &operator>>=(unsigned Amt) {
73     *this = *this >> Amt;
74     return *this;
75   }
76   
77   APSInt operator>>(unsigned Amt) {
78     return IsUnsigned ? lshr(Amt) : ashr(Amt);
79   }
80   
81   inline bool operator<(const APSInt& RHS) const {
82     assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
83     return IsUnsigned ? ult(RHS) : slt(RHS);
84   }
85   inline bool operator>(const APSInt& RHS) const {
86     assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
87     return IsUnsigned ? ugt(RHS) : sgt(RHS);
88   }
89   inline bool operator<=(const APSInt& RHS) const {
90     assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
91     return IsUnsigned ? ule(RHS) : sle(RHS);
92   }
93   inline bool operator>=(const APSInt& RHS) const {
94     assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
95     return IsUnsigned ? uge(RHS) : sge(RHS);
96   }
97 };
98   
99 } // end namespace llvm
100
101 #endif