[Mlir-commits] [mlir] [MLIR][Presburger] Implement arithmetic operations (/, +, -) and printing for Fractions (PR #65310)
Arjun P
llvmlistbot at llvm.org
Tue Sep 5 08:41:39 PDT 2023
================
@@ -95,8 +99,44 @@ inline bool operator>=(const Fraction &x, const Fraction &y) {
return compare(x, y) >= 0;
}
+inline Fraction reduce(const Fraction &f) {
+ if (f == Fraction(0))
+ return f;
+ MPInt g = gcd(f.num, f.den);
+ return Fraction(f.num / g, f.den / g);
+}
+
inline Fraction operator*(const Fraction &x, const Fraction &y) {
- return Fraction(x.num * y.num, x.den * y.den);
+ return reduce(Fraction(x.num * y.num, x.den * y.den));
+}
+
+inline Fraction operator/(const Fraction &x, const Fraction &y) {
+ return reduce(Fraction(x.num * y.den, x.den * y.num));
+}
+
+inline Fraction operator+(const Fraction &x, const Fraction &y) {
+ return reduce(Fraction(x.num * y.den + x.den * y.num, x.den * y.den));
+}
+
+inline Fraction operator-(const Fraction &x, const Fraction &y) {
+ return reduce(Fraction(x.num * y.den - x.den * y.num, x.den * y.den));
+}
+
+inline Fraction& operator+=(const Fraction &g, const Fraction &f) {
+ Fraction *r = NULL;
+ *r = g+f;
+ return *r;
+}
----------------
Superty wrote:
`operator+=` should modify the first argument `g`, it can't be const in `g`! Also, please keep the same argument names x, y for uniformity
```
inline Fraction& operator+=(Fraction &x, const Fraction &y) {
x = x + y;
return x;
}
```
https://github.com/llvm/llvm-project/pull/65310
More information about the Mlir-commits
mailing list