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
23namespace dnnl {
24namespace impl {
25namespace gpu {
26namespace jit {
27
28template <typename T>
29static inline constexpr bool equal(T t) {
30 return true;
31}
32template <typename T1, typename T2>
33static inline constexpr bool equal(T1 t1, T2 t2) {
34 return (t1 == t2);
35}
36template <typename T1, typename T2, typename... To>
37static inline constexpr bool equal(T1 t1, T2 t2, To... to) {
38 return (t1 == t2) && equal(t2, to...);
39}
40
41template <typename T>
42static inline constexpr T clamp(T val, T lo, T hi) {
43 return std::min<T>(hi, std::max<T>(lo, val));
44}
45
46static 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.
51static inline int align_down(int value, int factor) {
52 return factor * (value / factor);
53}
54
55// Round value up to a multiple of factor.
56static inline int align_up(int value, int factor) {
57 return factor * div_up(value, factor);
58}
59
60using dnnl::impl::math::gcd;
61
62template <typename T>
63static 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
68static 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