My Project
MathExtras.h
Go to the documentation of this file.
1 //===- MathExtras.h - Math functions relevant to MLIR -----------*- C++ -*-===//
2 //
3 // Part of the MLIR 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 file contains math functions relevant to MLIR.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef MLIR_SUPPORT_MATHEXTRAS_H_
14 #define MLIR_SUPPORT_MATHEXTRAS_H_
15 
16 #include "mlir/Support/LLVM.h"
17 #include "llvm/ADT/APInt.h"
18 
19 namespace mlir {
20 
23 inline int64_t ceilDiv(int64_t lhs, int64_t rhs) {
24  assert(rhs >= 1);
25  // C/C++'s integer division rounds towards 0.
26  return lhs % rhs > 0 ? lhs / rhs + 1 : lhs / rhs;
27 }
28 
31 inline int64_t floorDiv(int64_t lhs, int64_t rhs) {
32  assert(rhs >= 1);
33  // C/C++'s integer division rounds towards 0.
34  return lhs % rhs < 0 ? lhs / rhs - 1 : lhs / rhs;
35 }
36 
41 inline int64_t mod(int64_t lhs, int64_t rhs) {
42  assert(rhs >= 1);
43  return lhs % rhs < 0 ? lhs % rhs + rhs : lhs % rhs;
44 }
45 
47 inline int64_t lcm(int64_t a, int64_t b) {
48  uint64_t x = std::abs(a);
49  uint64_t y = std::abs(b);
50  int64_t lcm = (x * y) / llvm::GreatestCommonDivisor64(x, y);
51  assert((lcm >= a && lcm >= b) && "LCM overflow");
52  return lcm;
53 }
54 } // end namespace mlir
55 
56 #endif // MLIR_SUPPORT_MATHEXTRAS_H_
Definition: InferTypeOpInterface.cpp:20
int64_t lcm(int64_t a, int64_t b)
Returns the least common multiple of &#39;a&#39; and &#39;b&#39;.
Definition: MathExtras.h:47
int64_t floorDiv(int64_t lhs, int64_t rhs)
Definition: MathExtras.h:31
int64_t ceilDiv(int64_t lhs, int64_t rhs)
Definition: MathExtras.h:23
int64_t mod(int64_t lhs, int64_t rhs)
Definition: MathExtras.h:41