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