MLIR  19.0.0git
SlowMPInt.h
Go to the documentation of this file.
1 //===- SlowMPInt.h - MLIR SlowMPInt Class -----------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This is a simple class to represent arbitrary precision signed integers.
10 // Unlike APInt, one does not have to specify a fixed maximum size, and the
11 // integer can take on any arbitrary values.
12 //
13 // This class is to be used as a fallback slow path for the MPInt class, and
14 // is not intended to be used directly.
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #ifndef MLIR_ANALYSIS_PRESBURGER_SLOWMPINT_H
19 #define MLIR_ANALYSIS_PRESBURGER_SLOWMPINT_H
20 
22 #include "llvm/ADT/APInt.h"
23 #include "llvm/ADT/Hashing.h"
24 #include "llvm/Support/raw_ostream.h"
25 
26 namespace mlir {
27 namespace presburger {
28 namespace detail {
29 
30 /// A simple class providing multi-precision arithmetic. Internally, it stores
31 /// an APInt, whose width is doubled whenever an overflow occurs at a certain
32 /// width. The default constructor sets the initial width to 64. SlowMPInt is
33 /// primarily intended to be used as a slow fallback path for the upcoming MPInt
34 /// class.
35 class SlowMPInt {
36 private:
37  llvm::APInt val;
38 
39 public:
40  explicit SlowMPInt(int64_t val);
41  SlowMPInt();
42  explicit SlowMPInt(const llvm::APInt &val);
43  SlowMPInt &operator=(int64_t val);
44  explicit operator int64_t() const;
45  SlowMPInt operator-() const;
46  bool operator==(const SlowMPInt &o) const;
47  bool operator!=(const SlowMPInt &o) const;
48  bool operator>(const SlowMPInt &o) const;
49  bool operator<(const SlowMPInt &o) const;
50  bool operator<=(const SlowMPInt &o) const;
51  bool operator>=(const SlowMPInt &o) const;
52  SlowMPInt operator+(const SlowMPInt &o) const;
53  SlowMPInt operator-(const SlowMPInt &o) const;
54  SlowMPInt operator*(const SlowMPInt &o) const;
55  SlowMPInt operator/(const SlowMPInt &o) const;
56  SlowMPInt operator%(const SlowMPInt &o) const;
57  SlowMPInt &operator+=(const SlowMPInt &o);
58  SlowMPInt &operator-=(const SlowMPInt &o);
59  SlowMPInt &operator*=(const SlowMPInt &o);
60  SlowMPInt &operator/=(const SlowMPInt &o);
61  SlowMPInt &operator%=(const SlowMPInt &o);
62 
65 
66  friend SlowMPInt abs(const SlowMPInt &x);
67  friend SlowMPInt ceilDiv(const SlowMPInt &lhs, const SlowMPInt &rhs);
68  friend SlowMPInt floorDiv(const SlowMPInt &lhs, const SlowMPInt &rhs);
69  /// The operands must be non-negative for gcd.
70  friend SlowMPInt gcd(const SlowMPInt &a, const SlowMPInt &b);
71 
72  /// Overload to compute a hash_code for a SlowMPInt value.
73  friend llvm::hash_code hash_value(const SlowMPInt &x); // NOLINT
74 
75  void print(llvm::raw_ostream &os) const;
76  void dump() const;
77 
78  unsigned getBitWidth() const { return val.getBitWidth(); }
79 };
80 
81 llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const SlowMPInt &x);
82 
83 /// Returns the remainder of dividing LHS by RHS.
84 ///
85 /// The RHS is always expected to be positive, and the result
86 /// is always non-negative.
87 SlowMPInt mod(const SlowMPInt &lhs, const SlowMPInt &rhs);
88 
89 /// Returns the least common multiple of 'a' and 'b'.
90 SlowMPInt lcm(const SlowMPInt &a, const SlowMPInt &b);
91 
92 /// Redeclarations of friend declarations above to
93 /// make it discoverable by lookups.
95 SlowMPInt ceilDiv(const SlowMPInt &lhs, const SlowMPInt &rhs);
96 SlowMPInt floorDiv(const SlowMPInt &lhs, const SlowMPInt &rhs);
97 SlowMPInt gcd(const SlowMPInt &a, const SlowMPInt &b);
98 llvm::hash_code hash_value(const SlowMPInt &x); // NOLINT
99 
100 /// ---------------------------------------------------------------------------
101 /// Convenience operator overloads for int64_t.
102 /// ---------------------------------------------------------------------------
108 
109 bool operator==(const SlowMPInt &a, int64_t b);
110 bool operator!=(const SlowMPInt &a, int64_t b);
111 bool operator>(const SlowMPInt &a, int64_t b);
112 bool operator<(const SlowMPInt &a, int64_t b);
113 bool operator<=(const SlowMPInt &a, int64_t b);
114 bool operator>=(const SlowMPInt &a, int64_t b);
115 SlowMPInt operator+(const SlowMPInt &a, int64_t b);
116 SlowMPInt operator-(const SlowMPInt &a, int64_t b);
117 SlowMPInt operator*(const SlowMPInt &a, int64_t b);
118 SlowMPInt operator/(const SlowMPInt &a, int64_t b);
119 SlowMPInt operator%(const SlowMPInt &a, int64_t b);
120 
121 bool operator==(int64_t a, const SlowMPInt &b);
122 bool operator!=(int64_t a, const SlowMPInt &b);
123 bool operator>(int64_t a, const SlowMPInt &b);
124 bool operator<(int64_t a, const SlowMPInt &b);
125 bool operator<=(int64_t a, const SlowMPInt &b);
126 bool operator>=(int64_t a, const SlowMPInt &b);
127 SlowMPInt operator+(int64_t a, const SlowMPInt &b);
128 SlowMPInt operator-(int64_t a, const SlowMPInt &b);
129 SlowMPInt operator*(int64_t a, const SlowMPInt &b);
130 SlowMPInt operator/(int64_t a, const SlowMPInt &b);
131 SlowMPInt operator%(int64_t a, const SlowMPInt &b);
132 } // namespace detail
133 } // namespace presburger
134 } // namespace mlir
135 
136 #endif // MLIR_ANALYSIS_PRESBURGER_SLOWMPINT_H
A simple class providing multi-precision arithmetic.
Definition: SlowMPInt.h:35
SlowMPInt operator+(const SlowMPInt &o) const
Definition: SlowMPInt.cpp:192
friend SlowMPInt gcd(const SlowMPInt &a, const SlowMPInt &b)
The operands must be non-negative for gcd.
bool operator<=(const SlowMPInt &o) const
Definition: SlowMPInt.cpp:159
SlowMPInt & operator-=(const SlowMPInt &o)
Definition: SlowMPInt.cpp:266
SlowMPInt & operator/=(const SlowMPInt &o)
Definition: SlowMPInt.cpp:274
friend llvm::hash_code hash_value(const SlowMPInt &x)
Overload to compute a hash_code for a SlowMPInt value.
bool operator==(const SlowMPInt &o) const
Definition: SlowMPInt.cpp:143
friend SlowMPInt abs(const SlowMPInt &x)
Redeclarations of friend declarations above to make it discoverable by lookups.
SlowMPInt operator/(const SlowMPInt &o) const
Definition: SlowMPInt.cpp:204
SlowMPInt & operator+=(const SlowMPInt &o)
Definition: SlowMPInt.cpp:262
friend SlowMPInt ceilDiv(const SlowMPInt &lhs, const SlowMPInt &rhs)
bool operator>(const SlowMPInt &o) const
Definition: SlowMPInt.cpp:151
SlowMPInt operator*(const SlowMPInt &o) const
Definition: SlowMPInt.cpp:200
SlowMPInt operator%(const SlowMPInt &o) const
This operation cannot overflow.
Definition: SlowMPInt.cpp:245
bool operator>=(const SlowMPInt &o) const
Definition: SlowMPInt.cpp:163
SlowMPInt & operator=(int64_t val)
Definition: SlowMPInt.cpp:27
friend SlowMPInt floorDiv(const SlowMPInt &lhs, const SlowMPInt &rhs)
void print(llvm::raw_ostream &os) const
Definition: SlowMPInt.cpp:37
bool operator!=(const SlowMPInt &o) const
Definition: SlowMPInt.cpp:147
SlowMPInt & operator%=(const SlowMPInt &o)
Definition: SlowMPInt.cpp:278
bool operator<(const SlowMPInt &o) const
Definition: SlowMPInt.cpp:155
SlowMPInt & operator*=(const SlowMPInt &o)
Definition: SlowMPInt.cpp:270
bool operator==(const SlowMPInt &a, int64_t b)
SlowMPInt operator/(const SlowMPInt &a, int64_t b)
SlowMPInt gcd(const SlowMPInt &a, const SlowMPInt &b)
llvm::hash_code hash_value(const SlowMPInt &x)
SlowMPInt & operator*=(SlowMPInt &a, int64_t b)
SlowMPInt & operator+=(SlowMPInt &a, int64_t b)
SlowMPInt & operator%=(SlowMPInt &a, int64_t b)
bool operator<(const SlowMPInt &a, int64_t b)
SlowMPInt abs(const SlowMPInt &x)
Redeclarations of friend declarations above to make it discoverable by lookups.
SlowMPInt mod(const SlowMPInt &lhs, const SlowMPInt &rhs)
Returns the remainder of dividing LHS by RHS.
SlowMPInt operator+(const SlowMPInt &a, int64_t b)
SlowMPInt operator*(const SlowMPInt &a, int64_t b)
SlowMPInt lcm(const SlowMPInt &a, const SlowMPInt &b)
Returns the least common multiple of 'a' and 'b'.
bool operator>(const SlowMPInt &a, int64_t b)
bool operator>=(const SlowMPInt &a, int64_t b)
SlowMPInt floorDiv(const SlowMPInt &lhs, const SlowMPInt &rhs)
llvm::raw_ostream & operator<<(llvm::raw_ostream &os, const SlowMPInt &x)
SlowMPInt operator%(const SlowMPInt &a, int64_t b)
SlowMPInt operator-(const SlowMPInt &a, int64_t b)
bool operator!=(const SlowMPInt &a, int64_t b)
SlowMPInt ceilDiv(const SlowMPInt &lhs, const SlowMPInt &rhs)
bool operator<=(const SlowMPInt &a, int64_t b)
SlowMPInt & operator-=(SlowMPInt &a, int64_t b)
SlowMPInt & operator/=(SlowMPInt &a, int64_t b)
Include the generated interface declarations.