• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- MathExtras.h - Math functions relevant to MLIR -----------*- 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 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 
21 /// Returns the result of MLIR's ceildiv operation on constants. The RHS is
22 /// expected to be non-zero.
ceilDiv(int64_t lhs,int64_t rhs)23 inline int64_t ceilDiv(int64_t lhs, int64_t rhs) {
24   assert(rhs != 0);
25   // C/C++'s integer division rounds towards 0.
26   int64_t x = (rhs > 0) ? -1 : 1;
27   return (lhs * rhs > 0) ? ((lhs + x) / rhs) + 1 : -(-lhs / rhs);
28 }
29 
30 /// Returns the result of MLIR's floordiv operation on constants. The RHS is
31 /// expected to be non-zero.
floorDiv(int64_t lhs,int64_t rhs)32 inline int64_t floorDiv(int64_t lhs, int64_t rhs) {
33   assert(rhs != 0);
34   // C/C++'s integer division rounds towards 0.
35   int64_t x = (rhs < 0) ? 1 : -1;
36   return (lhs * rhs < 0) ? -((-lhs + x) / rhs) - 1 : lhs / rhs;
37 }
38 
39 /// Returns MLIR's mod operation on constants. MLIR's mod operation yields the
40 /// remainder of the Euclidean division of 'lhs' by 'rhs', and is therefore not
41 /// C's % operator.  The RHS is always expected to be positive, and the result
42 /// is always non-negative.
mod(int64_t lhs,int64_t rhs)43 inline int64_t mod(int64_t lhs, int64_t rhs) {
44   assert(rhs >= 1);
45   return lhs % rhs < 0 ? lhs % rhs + rhs : lhs % rhs;
46 }
47 
48 /// Returns the least common multiple of 'a' and 'b'.
lcm(int64_t a,int64_t b)49 inline int64_t lcm(int64_t a, int64_t b) {
50   uint64_t x = std::abs(a);
51   uint64_t y = std::abs(b);
52   int64_t lcm = (x * y) / llvm::GreatestCommonDivisor64(x, y);
53   assert((lcm >= a && lcm >= b) && "LCM overflow");
54   return lcm;
55 }
56 } // end namespace mlir
57 
58 #endif // MLIR_SUPPORT_MATHEXTRAS_H_
59