Fix bug spotted by Chris.
[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   void setIsSigned(bool Val) { IsUnsigned = !Val; }
54   
55   
56   const APSInt &operator%=(const APSInt &RHS) {
57     assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
58     if (IsUnsigned)
59       *this = urem(RHS);
60     else
61       *this = srem(RHS);
62     return *this;
63   }
64   const APSInt &operator/=(const APSInt &RHS) {
65     assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
66     if (IsUnsigned)
67       *this = udiv(RHS);
68     else
69       *this = sdiv(RHS);
70     return *this;
71   }
72   APSInt operator%(const APSInt &RHS) const {
73     assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
74     return IsUnsigned ? urem(RHS) : srem(RHS);
75   }
76   APSInt operator/(const APSInt &RHS) const {
77     assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
78     return IsUnsigned ? udiv(RHS) : sdiv(RHS);
79   }
80   
81   const APSInt &operator>>=(unsigned Amt) {
82     *this = *this >> Amt;
83     return *this;
84   }
85   
86   APSInt& extend(uint32_t width) {
87     if (IsUnsigned)
88       zext(width);
89     else
90       sext(width);
91     return *this;
92   }
93   
94   APSInt& extOrTrunc(uint32_t width) {
95       if (IsUnsigned)
96         zextOrTrunc(width);
97       else
98         sextOrTrunc(width);
99       return *this;
100   }
101   
102   APSInt operator>>(unsigned Amt) const {
103     return IsUnsigned ? lshr(Amt) : ashr(Amt);
104   }
105   
106   inline bool operator<(const APSInt& RHS) const {
107     assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
108     return IsUnsigned ? ult(RHS) : slt(RHS);
109   }
110   inline bool operator>(const APSInt& RHS) const {
111     assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
112     return IsUnsigned ? ugt(RHS) : sgt(RHS);
113   }
114   inline bool operator<=(const APSInt& RHS) const {
115     assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
116     return IsUnsigned ? ule(RHS) : sle(RHS);
117   }
118   inline bool operator>=(const APSInt& RHS) const {
119     assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
120     return IsUnsigned ? uge(RHS) : sge(RHS);
121   }
122 };
123   
124 } // end namespace llvm
125
126 #endif