[Mlir-commits] [mlir] [MLIR][Presburger] Implement arithmetic operations (/, +, -) and printing for Fractions (PR #65310)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Tue Sep 5 03:15:42 PDT 2023
https://github.com/Abhinav271828 updated https://github.com/llvm/llvm-project/pull/65310:
>From 8d3e0dae92f131d54aadf15046145f026d6907a8 Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Tue, 5 Sep 2023 10:42:03 +0100
Subject: [PATCH 1/2] Shift changes to Fraction
---
.../mlir/Analysis/Presburger/Fraction.h | 28 +++++++++++++++++--
1 file changed, 25 insertions(+), 3 deletions(-)
diff --git a/mlir/include/mlir/Analysis/Presburger/Fraction.h b/mlir/include/mlir/Analysis/Presburger/Fraction.h
index c51b6c972bf8851..2cb90b708435353 100644
--- a/mlir/include/mlir/Analysis/Presburger/Fraction.h
+++ b/mlir/include/mlir/Analysis/Presburger/Fraction.h
@@ -15,6 +15,7 @@
#define MLIR_ANALYSIS_PRESBURGER_FRACTION_H
#include "mlir/Analysis/Presburger/MPInt.h"
+#include "mlir/Analysis/Presburger/Utils.h"
#include "mlir/Support/MathExtras.h"
namespace mlir {
@@ -30,15 +31,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 +49,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};
@@ -99,6 +104,23 @@ inline Fraction operator*(const Fraction &x, const Fraction &y) {
return Fraction(x.num * y.num, x.den * y.den);
}
+inline Fraction operator/(const Fraction &x, const Fraction &y) {
+ return Fraction(x.num * y.den, x.den * y.num);
+}
+
+inline Fraction operator+(const Fraction &x, const Fraction &y) {
+ return Fraction(x.num * y.den + x.den * y.num, x.den * y.den);
+}
+
+inline Fraction operator-(const Fraction &x, const Fraction &y) {
+ return Fraction(x.num * y.den - x.den * y.num, x.den * y.den);
+}
+
+inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const Fraction &x) {
+ x.print(os);
+ return os;
+}
+
} // namespace presburger
} // namespace mlir
>From 8a050875adb48d4bc9b220052f36f959e2bad73c Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Tue, 5 Sep 2023 11:15:05 +0100
Subject: [PATCH 2/2] Update documentation and remove extraneous include
---
mlir/include/mlir/Analysis/Presburger/Fraction.h | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/mlir/include/mlir/Analysis/Presburger/Fraction.h b/mlir/include/mlir/Analysis/Presburger/Fraction.h
index 2cb90b708435353..e88f5d768f0c40f 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.
//
//===----------------------------------------------------------------------===//
@@ -15,7 +15,6 @@
#define MLIR_ANALYSIS_PRESBURGER_FRACTION_H
#include "mlir/Analysis/Presburger/MPInt.h"
-#include "mlir/Analysis/Presburger/Utils.h"
#include "mlir/Support/MathExtras.h"
namespace mlir {
More information about the Mlir-commits
mailing list