summaryrefslogtreecommitdiff
path: root/include/llvm/ADT/APSInt.h
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2007-04-05 05:20:11 +0000
committerChris Lattner <sabre@nondot.org>2007-04-05 05:20:11 +0000
commitdb8918a98ff001d0c37abf41bc62364595340035 (patch)
treef191b6e18e7dca5dfb0b59d5e9100541cfa2adad /include/llvm/ADT/APSInt.h
parent1984a32867dc65e5731c5c3b330b4b46f6d73bee (diff)
downloadllvm-db8918a98ff001d0c37abf41bc62364595340035.tar.gz
llvm-db8918a98ff001d0c37abf41bc62364595340035.tar.bz2
llvm-db8918a98ff001d0c37abf41bc62364595340035.tar.xz
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
Diffstat (limited to 'include/llvm/ADT/APSInt.h')
-rw-r--r--include/llvm/ADT/APSInt.h101
1 files changed, 101 insertions, 0 deletions
diff --git a/include/llvm/ADT/APSInt.h b/include/llvm/ADT/APSInt.h
new file mode 100644
index 0000000000..d75c7094cd
--- /dev/null
+++ b/include/llvm/ADT/APSInt.h
@@ -0,0 +1,101 @@
+//===-- llvm/Support/APSInt.h - Arbitrary Precision Signed Int -*- C++ -*--===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file was developed by Chris Lattner and is distributed under the
+// University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements the APSInt class, which is a simple class that
+// represents an arbitrary sized integer that knows its signedness.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_APSINT_H
+#define LLVM_APSINT_H
+
+#include "llvm/ADT/APInt.h"
+
+namespace llvm {
+
+
+class APSInt : public APInt {
+ bool IsUnsigned;
+public:
+ /// APSInt ctor - Create an APSInt with the specified width, default to
+ /// unsigned.
+ explicit APSInt(unsigned BitWidth) : APInt(BitWidth, 0), IsUnsigned(true) {}
+ APSInt(const APInt &I) : APInt(I), IsUnsigned(true) {}
+
+ APSInt &operator=(const APSInt &RHS) {
+ APInt::operator=(RHS);
+ IsUnsigned = RHS.IsUnsigned;
+ return *this;
+ }
+
+ APSInt &operator=(const APInt &RHS) {
+ // Retain our current sign.
+ APInt::operator=(RHS);
+ return *this;
+ }
+
+ APSInt &operator=(uint64_t RHS) {
+ // Retain our current sign.
+ APInt::operator=(RHS);
+ return *this;
+ }
+
+ // Query sign information.
+ bool isSigned() const { return !IsUnsigned; }
+ bool isUnsigned() const { return IsUnsigned; }
+ void setIsUnsigned(bool Val) { IsUnsigned = Val; }
+
+
+ const APSInt &operator%=(const APSInt &RHS) {
+ assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
+ if (IsUnsigned)
+ *this = urem(RHS);
+ else
+ *this = srem(RHS);
+ return *this;
+ }
+ const APSInt &operator/=(const APSInt &RHS) {
+ assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
+ if (IsUnsigned)
+ *this = udiv(RHS);
+ else
+ *this = sdiv(RHS);
+ return *this;
+ }
+
+ const APSInt &operator>>=(unsigned Amt) {
+ *this = *this >> Amt;
+ return *this;
+ }
+
+ APSInt operator>>(unsigned Amt) {
+ return IsUnsigned ? lshr(Amt) : ashr(Amt);
+ }
+
+ inline bool operator<(const APSInt& RHS) const {
+ assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
+ return IsUnsigned ? ult(RHS) : slt(RHS);
+ }
+ inline bool operator>(const APSInt& RHS) const {
+ assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
+ return IsUnsigned ? ugt(RHS) : sgt(RHS);
+ }
+ inline bool operator<=(const APSInt& RHS) const {
+ assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
+ return IsUnsigned ? ule(RHS) : sle(RHS);
+ }
+ inline bool operator>=(const APSInt& RHS) const {
+ assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
+ return IsUnsigned ? uge(RHS) : sge(RHS);
+ }
+};
+
+} // end namespace llvm
+
+#endif