llvm.org GIT mirror llvm / db8918a
Add a helper class (APSInt) which can represent an APInt along with sign information. This is useful when a value does have a sign associated with it. This shouldn't be used generally in LLVM for mid-level optimizer stuff. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@35681 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 13 years ago
1 changed file(s) with 101 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 //===-- llvm/Support/APSInt.h - Arbitrary Precision Signed Int -*- C++ -*--===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by Chris Lattner and is distributed under the
5 // University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the APSInt class, which is a simple class that
10 // represents an arbitrary sized integer that knows its signedness.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_APSINT_H
15 #define LLVM_APSINT_H
16
17 #include "llvm/ADT/APInt.h"
18
19 namespace llvm {
20
21
22 class APSInt : public APInt {
23 bool IsUnsigned;
24 public:
25 /// APSInt ctor - Create an APSInt with the specified width, default to
26 /// unsigned.
27 explicit APSInt(unsigned BitWidth) : APInt(BitWidth, 0), IsUnsigned(true) {}
28 APSInt(const APInt &I) : APInt(I), IsUnsigned(true) {}
29
30 APSInt &operator=(const APSInt &RHS) {
31 APInt::operator=(RHS);
32 IsUnsigned = RHS.IsUnsigned;
33 return *this;
34 }
35
36 APSInt &operator=(const APInt &RHS) {
37 // Retain our current sign.
38 APInt::operator=(RHS);
39 return *this;
40 }
41
42 APSInt &operator=(uint64_t RHS) {
43 // Retain our current sign.
44 APInt::operator=(RHS);
45 return *this;
46 }
47
48 // Query sign information.
49 bool isSigned() const { return !IsUnsigned; }
50 bool isUnsigned() const { return IsUnsigned; }
51 void setIsUnsigned(bool Val) { IsUnsigned = Val; }
52
53
54 const APSInt &operator%=(const APSInt &RHS) {
55 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
56 if (IsUnsigned)
57 *this = urem(RHS);
58 else
59 *this = srem(RHS);
60 return *this;
61 }
62 const APSInt &operator/=(const APSInt &RHS) {
63 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
64 if (IsUnsigned)
65 *this = udiv(RHS);
66 else
67 *this = sdiv(RHS);
68 return *this;
69 }
70
71 const APSInt &operator>>=(unsigned Amt) {
72 *this = *this >> Amt;
73 return *this;
74 }
75
76 APSInt operator>>(unsigned Amt) {
77 return IsUnsigned ? lshr(Amt) : ashr(Amt);
78 }
79
80 inline bool operator<(const APSInt& RHS) const {
81 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
82 return IsUnsigned ? ult(RHS) : slt(RHS);
83 }
84 inline bool operator>(const APSInt& RHS) const {
85 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
86 return IsUnsigned ? ugt(RHS) : sgt(RHS);
87 }
88 inline bool operator<=(const APSInt& RHS) const {
89 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
90 return IsUnsigned ? ule(RHS) : sle(RHS);
91 }
92 inline bool operator>=(const APSInt& RHS) const {
93 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
94 return IsUnsigned ? uge(RHS) : sge(RHS);
95 }
96 };
97
98 } // end namespace llvm
99
100 #endif