MLIR  15.0.0git
Fraction.h
Go to the documentation of this file.
1 //===- Fraction.h - MLIR Fraction Class -------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
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
18
19 namespace mlir {
20 namespace presburger {
21
22 /// A class to represent fractions. The sign of the fraction is represented
23 /// in the sign of the numerator; the denominator is always positive.
24 ///
25 /// Note that overflows may occur if the numerator or denominator are not
26 /// representable by 64-bit integers.
27 struct Fraction {
28  /// Default constructor initializes the represented rational number to zero.
29  Fraction() = default;
30
31  /// Construct a Fraction from a numerator and denominator.
32  Fraction(int64_t oNum, int64_t oDen) : num(oNum), den(oDen) {
33  if (den < 0) {
34  num = -num;
35  den = -den;
36  }
37  }
38
39  // Return the value of the fraction as an integer. This should only be called
40  // when the fraction's value is really an integer.
41  int64_t getAsInteger() const {
42  assert(num % den == 0 && "Get as integer called on non-integral fraction!");
43  return num / den;
44  }
45
46  /// The numerator and denominator, respectively. The denominator is always
47  /// positive.
48  int64_t num{0}, den{1};
49 };
50
51 /// Three-way comparison between two fractions.
52 /// Returns +1, 0, and -1 if the first fraction is greater than, equal to, or
53 /// less than the second fraction, respectively.
54 inline int compare(Fraction x, Fraction y) {
55  int64_t diff = x.num * y.den - y.num * x.den;
56  if (diff > 0)
57  return +1;
58  if (diff < 0)
59  return -1;
60  return 0;
61 }
62
63 inline int64_t floor(Fraction f) { return floorDiv(f.num, f.den); }
64
65 inline int64_t ceil(Fraction f) { return ceilDiv(f.num, f.den); }
66
67 inline Fraction operator-(Fraction x) { return Fraction(-x.num, x.den); }
68
69 inline bool operator<(Fraction x, Fraction y) { return compare(x, y) < 0; }
70
71 inline bool operator<=(Fraction x, Fraction y) { return compare(x, y) <= 0; }
72
73 inline bool operator==(Fraction x, Fraction y) { return compare(x, y) == 0; }
74
75 inline bool operator!=(Fraction x, Fraction y) { return compare(x, y) != 0; }
76
77 inline bool operator>(Fraction x, Fraction y) { return compare(x, y) > 0; }
78
79 inline bool operator>=(Fraction x, Fraction y) { return compare(x, y) >= 0; }
80
82  return Fraction(x.num * y.num, x.den * y.den);
83 }
84
85 } // namespace presburger
86 } // namespace mlir
87
88 #endif // MLIR_ANALYSIS_PRESBURGER_FRACTION_H
Fraction operator*(Fraction x, Fraction y)
Definition: Fraction.h:81
Include the generated interface declarations.
bool operator<(Fraction x, Fraction y)
Definition: Fraction.h:69
Fraction(int64_t oNum, int64_t oDen)
Construct a Fraction from a numerator and denominator.
Definition: Fraction.h:32
bool operator>=(Fraction x, Fraction y)
Definition: Fraction.h:79
Fraction operator-(Fraction x)
Definition: Fraction.h:67
int64_t floor(Fraction f)
Definition: Fraction.h:63
A class to represent fractions.
Definition: Fraction.h:27
int64_t floorDiv(int64_t lhs, int64_t rhs)
Returns the result of MLIR&#39;s floordiv operation on constants.
Definition: MathExtras.h:33
int64_t getAsInteger() const
Definition: Fraction.h:41
int64_t ceilDiv(int64_t lhs, int64_t rhs)
Returns the result of MLIR&#39;s ceildiv operation on constants.
Definition: MathExtras.h:23
bool operator==(Fraction x, Fraction y)
Definition: Fraction.h:73
int64_t ceil(Fraction f)
Definition: Fraction.h:65
bool operator!=(Fraction x, Fraction y)
Definition: Fraction.h:75
int64_t num
The numerator and denominator, respectively.
Definition: Fraction.h:48
Fraction()=default
Default constructor initializes the represented rational number to zero.
bool operator<=(Fraction x, Fraction y)
Definition: Fraction.h:71
int compare(Fraction x, Fraction y)
Three-way comparison between two fractions.
Definition: Fraction.h:54
bool operator>(Fraction x, Fraction y)
Definition: Fraction.h:77