1 //===- Fraction.h - MLIR Fraction 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 fractions. It supports multiplication,
10 // comparison, floor, and ceiling operations.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef MLIR_ANALYSIS_PRESBURGER_FRACTION_H
15 #define MLIR_ANALYSIS_PRESBURGER_FRACTION_H
16
17 #include "mlir/Support/MathExtras.h"
18
19 namespace mlir {
20
21 /// A class to represent fractions. The sign of the fraction is represented
22 /// in the sign of the numerator; the denominator is always positive.
23 ///
24 /// Note that overflows may occur if the numerator or denominator are not
25 /// representable by 64-bit integers.
26 struct Fraction {
27 /// Default constructor initializes the represented rational number to zero.
FractionFraction28 Fraction() : num(0), den(1) {}
29
30 /// Construct a Fraction from a numerator and denominator.
FractionFraction31 Fraction(int64_t oNum, int64_t oDen) : num(oNum), den(oDen) {
32 if (den < 0) {
33 num = -num;
34 den = -den;
35 }
36 }
37
38 /// The numerator and denominator, respectively. The denominator is always
39 /// positive.
40 int64_t num, den;
41 };
42
43 /// Three-way comparison between two fractions.
44 /// Returns +1, 0, and -1 if the first fraction is greater than, equal to, or
45 /// less than the second fraction, respectively.
compare(Fraction x,Fraction y)46 inline int compare(Fraction x, Fraction y) {
47 int64_t diff = x.num * y.den - y.num * x.den;
48 if (diff > 0)
49 return +1;
50 if (diff < 0)
51 return -1;
52 return 0;
53 }
54
floor(Fraction f)55 inline int64_t floor(Fraction f) { return floorDiv(f.num, f.den); }
56
ceil(Fraction f)57 inline int64_t ceil(Fraction f) { return ceilDiv(f.num, f.den); }
58
59 inline Fraction operator-(Fraction x) { return Fraction(-x.num, x.den); }
60
61 inline bool operator<(Fraction x, Fraction y) { return compare(x, y) < 0; }
62
63 inline bool operator<=(Fraction x, Fraction y) { return compare(x, y) <= 0; }
64
65 inline bool operator==(Fraction x, Fraction y) { return compare(x, y) == 0; }
66
67 inline bool operator>(Fraction x, Fraction y) { return compare(x, y) > 0; }
68
69 inline bool operator>=(Fraction x, Fraction y) { return compare(x, y) >= 0; }
70
71 inline Fraction operator*(Fraction x, Fraction y) {
72 return Fraction(x.num * y.num, x.den * y.den);
73 }
74
75 } // namespace mlir
76
77 #endif // MLIR_ANALYSIS_PRESBURGER_FRACTION_H
78