Coverage Report

Created: 2020-06-26 05:44

/home/arjun/llvm-project/mlir/include/mlir/Analysis/Presburger/Fraction.h
Line
Count
Source (jump to first uncovered line)
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.
28
40
  Fraction() : num(0), den(1) {}
29
30
  /// Construct a Fraction from a numerator and denominator.
31
320
  Fraction(int64_t oNum, int64_t oDen) : num(oNum), den(oDen) {
32
320
    if (den < 0) {
33
0
      num = -num;
34
0
      den = -den;
35
0
    }
36
320
  }
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.
46
76
inline int compare(Fraction x, Fraction y) {
47
76
  int64_t diff = x.num * y.den - y.num * x.den;
48
76
  if (diff > 0)
49
41
    return +1;
50
35
  if (diff < 0)
51
28
    return -1;
52
7
  return 0;
53
7
}
54
55
51
inline int64_t floor(Fraction f) { return floorDiv(f.num, f.den); }
56
57
51
inline int64_t ceil(Fraction f) { return ceilDiv(f.num, f.den); }
58
59
0
inline Fraction operator-(Fraction x) { return Fraction(-x.num, x.den); }
60
61
76
inline bool operator<(Fraction x, Fraction y) { return compare(x, y) < 0; }
62
63
0
inline bool operator<=(Fraction x, Fraction y) { return compare(x, y) <= 0; }
64
65
0
inline bool operator==(Fraction x, Fraction y) { return compare(x, y) == 0; }
66
67
0
inline bool operator>(Fraction x, Fraction y) { return compare(x, y) > 0; }
68
69
0
inline bool operator>=(Fraction x, Fraction y) { return compare(x, y) >= 0; }
70
71
56
inline Fraction operator*(Fraction x, Fraction y) {
72
56
  return Fraction(x.num * y.num, x.den * y.den);
73
56
}
74
75
} // namespace mlir
76
77
#endif // MLIR_ANALYSIS_PRESBURGER_FRACTION_H