MLIR: include/mlir/Analysis/Presburger/Fraction.h Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

14 #ifndef MLIR_ANALYSIS_PRESBURGER_FRACTION_H

15 #define MLIR_ANALYSIS_PRESBURGER_FRACTION_H

16

17 #include "llvm/ADT/DynamicAPInt.h"

18 #include "llvm/Support/raw_ostream.h"

19

20 namespace mlir {

21 namespace presburger {

22 using llvm::DynamicAPInt;

23

24

25

26

27

28

30

32

33

34 Fraction(const DynamicAPInt &oNum, const DynamicAPInt &oDen = DynamicAPInt(1))

35 : num(oNum), den(oDen) {

36 if (den < 0) {

39 }

40 }

41

44 Fraction(int64_t num, const DynamicAPInt &den = DynamicAPInt(1))

48

49

50

52 assert(num % den == 0 && "Get as integer called on non-integral fraction!");

54 }

55

56 llvm::raw_ostream &print(llvm::raw_ostream &os) const {

57 return os << "(" << num << "/" << den << ")";

58 }

59

60

61

63 };

64

65

66

67

69 DynamicAPInt diff = x.num * y.den - y.num * x.den;

70 if (diff > 0)

71 return +1;

72 if (diff < 0)

73 return -1;

74 return 0;

75 }

76

78

80

82

85 }

86

88 return compare(x, y) <= 0;

89 }

90

92 return compare(x, y) == 0;

93 }

94

96 return compare(x, y) != 0;

97 }

98

100 return compare(x, y) > 0;

101 }

102

104 return compare(x, y) >= 0;

105 }

106

108 assert(f.den > 0 && "denominator of fraction must be positive!");

110 }

111

115 DynamicAPInt g = gcd(abs(f.num), abs(f.den));

117 }

118

121 }

122

125 }

126

129 }

130

133 }

134

135

137 DynamicAPInt rem = f.num % f.den;

138 return (f.num / f.den) + (rem > f.den / 2);

139 }

140

142 x = x + y;

143 return x;

144 }

145

147 x = x - y;

148 return x;

149 }

150

152 x = x / y;

153 return x;

154 }

155

157 x = x * y;

158 return x;

159 }

160

163 return os;

164 }

165

166 }

167 }

168

169 #endif

llvm::raw_ostream & operator<<(llvm::raw_ostream &os, const Fraction &x)

Fraction & operator-=(Fraction &x, const Fraction &y)

Fraction operator+(const Fraction &x, const Fraction &y)

bool operator==(const Fraction &x, const Fraction &y)

bool operator>(const Fraction &x, const Fraction &y)

bool operator<(const Fraction &x, const Fraction &y)

int compare(const Fraction &x, const Fraction &y)

Three-way comparison between two fractions.

DynamicAPInt floor(const Fraction &f)

bool operator>=(const Fraction &x, const Fraction &y)

DynamicAPInt ceil(const Fraction &f)

Fraction reduce(const Fraction &f)

Fraction operator-(const Fraction &x)

bool operator<=(const Fraction &x, const Fraction &y)

DynamicAPInt round(const Fraction &f)

Fraction & operator*=(Fraction &x, const Fraction &y)

Fraction abs(const Fraction &f)

Fraction & operator/=(Fraction &x, const Fraction &y)

bool operator!=(const Fraction &x, const Fraction &y)

Fraction & operator+=(Fraction &x, const Fraction &y)

Fraction operator*(const Fraction &x, const Fraction &y)

Fraction operator/(const Fraction &x, const Fraction &y)

Include the generated interface declarations.

A class to represent fractions.

Fraction()=default

Default constructor initializes the represented rational number to zero.

Fraction(int64_t num, const DynamicAPInt &den=DynamicAPInt(1))

Fraction(const DynamicAPInt &oNum, const DynamicAPInt &oDen=DynamicAPInt(1))

Construct a Fraction from a numerator and denominator.

DynamicAPInt getAsInteger() const

Fraction(const DynamicAPInt &num, int64_t den)

Overloads for passing literals.

Fraction(int64_t num, int64_t den)

DynamicAPInt num

The numerator and denominator, respectively.

llvm::raw_ostream & print(llvm::raw_ostream &os) const