[Mlir-commits] [mlir] 61dd539 - [MLIR][Presburger] Fraction: implement arithmetic operations (/, +, -) and printing (#65310)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue Sep 5 09:15:01 PDT 2023


Author: Abhinav271828
Date: 2023-09-05T21:44:57+05:30
New Revision: 61dd5398368eaa59e4cf86ee33a9b896b83c4d60

URL: https://github.com/llvm/llvm-project/commit/61dd5398368eaa59e4cf86ee33a9b896b83c4d60
DIFF: https://github.com/llvm/llvm-project/commit/61dd5398368eaa59e4cf86ee33a9b896b83c4d60.diff

LOG: [MLIR][Presburger] Fraction: implement arithmetic operations (/, +, -) and printing (#65310)

Added: 
    

Modified: 
    mlir/include/mlir/Analysis/Presburger/Fraction.h

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Analysis/Presburger/Fraction.h b/mlir/include/mlir/Analysis/Presburger/Fraction.h
index c51b6c972bf885..74127a900d53ed 100644
--- a/mlir/include/mlir/Analysis/Presburger/Fraction.h
+++ b/mlir/include/mlir/Analysis/Presburger/Fraction.h
@@ -6,7 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 //
-// This is a simple class to represent fractions. It supports multiplication,
+// This is a simple class to represent fractions. It supports arithmetic,
 // comparison, floor, and ceiling operations.
 //
 //===----------------------------------------------------------------------===//
@@ -30,15 +30,15 @@ struct Fraction {
   Fraction() = default;
 
   /// Construct a Fraction from a numerator and denominator.
-  Fraction(const MPInt &oNum, const MPInt &oDen) : num(oNum), den(oDen) {
+  Fraction(const MPInt &oNum, const MPInt &oDen = MPInt(1)) : num(oNum), den(oDen) {
     if (den < 0) {
       num = -num;
       den = -den;
     }
   }
   /// Overloads for passing literals.
-  Fraction(const MPInt &num, int64_t den) : Fraction(num, MPInt(den)) {}
-  Fraction(int64_t num, const MPInt &den) : Fraction(MPInt(num), den) {}
+  Fraction(const MPInt &num, int64_t den = 1) : Fraction(num, MPInt(den)) {}
+  Fraction(int64_t num, const MPInt &den = MPInt(1)) : Fraction(MPInt(num), den) {}
   Fraction(int64_t num, int64_t den) : Fraction(MPInt(num), MPInt(den)) {}
 
   // Return the value of the fraction as an integer. This should only be called
@@ -48,6 +48,10 @@ struct Fraction {
     return num / den;
   }
 
+  llvm::raw_ostream &print(llvm::raw_ostream &os) const {
+    return os << "(" << num << "/" << den << ")";
+  }
+
   /// The numerator and denominator, respectively. The denominator is always
   /// positive.
   MPInt num{0}, den{1};
@@ -95,8 +99,52 @@ 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 Fraction(0, 1);
+  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+=(Fraction &x, const Fraction &y) {
+  x = x + y;
+  return x;
+}
+
+inline Fraction& operator-=(Fraction &x, const Fraction &y) {
+  x = x - y;
+  return x;
+}
+
+inline Fraction& operator/=(Fraction &x, const Fraction &y) {
+  x = x / y;
+  return x;
+}
+
+inline Fraction& operator*=(Fraction &x, const Fraction &y) {
+  x = x * y;
+  return x;
+}
+
+inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const Fraction &x) {
+  x.print(os);
+  return os;
 }
 
 } // namespace presburger


        


More information about the Mlir-commits mailing list