[Mlir-commits] [mlir] [MLIR][Presburger] Implement arithmetic operations (/, +, -) and printing for Fractions (PR #65310)

Arjun P llvmlistbot at llvm.org
Tue Sep 5 08:31:55 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));
+}
----------------
Superty wrote:

Thanks for adding these here

https://github.com/llvm/llvm-project/pull/65310


More information about the Mlir-commits mailing list