1 | /******************************************************************************* |
2 | * Copyright 2019-2022 Intel Corporation |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | *******************************************************************************/ |
16 | |
17 | #ifndef GPU_JIT_GEMM_UTILS_HPP |
18 | #define GPU_JIT_GEMM_UTILS_HPP |
19 | |
20 | #include "common/math_utils.hpp" |
21 | #include "common/utils.hpp" |
22 | |
23 | namespace dnnl { |
24 | namespace impl { |
25 | namespace gpu { |
26 | namespace jit { |
27 | |
28 | template <typename T> |
29 | static inline constexpr bool equal(T t) { |
30 | return true; |
31 | } |
32 | template <typename T1, typename T2> |
33 | static inline constexpr bool equal(T1 t1, T2 t2) { |
34 | return (t1 == t2); |
35 | } |
36 | template <typename T1, typename T2, typename... To> |
37 | static inline constexpr bool equal(T1 t1, T2 t2, To... to) { |
38 | return (t1 == t2) && equal(t2, to...); |
39 | } |
40 | |
41 | template <typename T> |
42 | static inline constexpr T clamp(T val, T lo, T hi) { |
43 | return std::min<T>(hi, std::max<T>(lo, val)); |
44 | } |
45 | |
46 | static inline int div_up(int value, int divisor) { |
47 | return (value + divisor - 1) / divisor; |
48 | } |
49 | |
50 | // Round value down to a multiple of factor. |
51 | static inline int align_down(int value, int factor) { |
52 | return factor * (value / factor); |
53 | } |
54 | |
55 | // Round value up to a multiple of factor. |
56 | static inline int align_up(int value, int factor) { |
57 | return factor * div_up(value, factor); |
58 | } |
59 | |
60 | using dnnl::impl::math::gcd; |
61 | |
62 | template <typename T> |
63 | static inline T lcm(T x, T y) { |
64 | if (x == 0 || y == 0) return 0; |
65 | return dnnl::impl::math::lcm(x, y); |
66 | } |
67 | |
68 | static inline int largest_pow2_divisor(int x) { |
69 | return x & ~(x - 1); |
70 | } |
71 | |
72 | } // namespace jit |
73 | } // namespace gpu |
74 | } // namespace impl |
75 | } // namespace dnnl |
76 | |
77 | #endif /* GPU_JIT_GEMM_UTILS_HPP */ |
78 | |