integral_constant.hpp 1.74 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
// SPDX-License-Identifier: MIT
arai713's avatar
arai713 committed
2
// Copyright (c) 2018-2025, Advanced Micro Devices, Inc. All rights reserved.
Chao Liu's avatar
Chao Liu committed
3

4
#pragma once
5

Chao Liu's avatar
Chao Liu committed
6
namespace ck {
7

Chao Liu's avatar
Chao Liu committed
8
template <class T, T v>
Chao Liu's avatar
Chao Liu committed
9
10
11
12
struct integral_constant
{
    static constexpr T value = v;
    typedef T value_type;
Chao Liu's avatar
Chao Liu committed
13
    typedef integral_constant type;
Chao Liu's avatar
Chao Liu committed
14
    __host__ __device__ constexpr operator value_type() const noexcept { return value; }
Chao Liu's avatar
Chao Liu committed
15
    __host__ __device__ constexpr value_type operator()() const noexcept { return value; }
Chao Liu's avatar
Chao Liu committed
16
};
17

Jianfeng Yan's avatar
Jianfeng Yan committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
template <typename TX, TX X, typename TY, TY Y>
__host__ __device__ constexpr auto operator+(integral_constant<TX, X>, integral_constant<TY, Y>)
{
    return integral_constant<decltype(X + Y), X + Y>{};
}

template <typename TX, TX X, typename TY, TY Y>
__host__ __device__ constexpr auto operator-(integral_constant<TX, X>, integral_constant<TY, Y>)
{
    static_assert(Y <= X, "wrong!");
    return integral_constant<decltype(X - Y), X - Y>{};
}

template <typename TX, TX X, typename TY, TY Y>
__host__ __device__ constexpr auto operator*(integral_constant<TX, X>, integral_constant<TY, Y>)
{
    return integral_constant<decltype(X * Y), X * Y>{};
}

template <typename TX, TX X, typename TY, TY Y>
__host__ __device__ constexpr auto operator/(integral_constant<TX, X>, integral_constant<TY, Y>)
{
    static_assert(Y > 0, "wrong!");
    return integral_constant<decltype(X / Y), X / Y>{};
}

template <typename TX, TX X, typename TY, TY Y>
__host__ __device__ constexpr auto operator%(integral_constant<TX, X>, integral_constant<TY, Y>)
{
    static_assert(Y > 0, "wrong!");
    return integral_constant<decltype(X % Y), X % Y>{};
}

arai713's avatar
arai713 committed
51
52
53
54
55
template <bool B>
using bool_constant = integral_constant<bool, B>;

using true_type  = bool_constant<true>;
using false_type = bool_constant<false>;
56
} // namespace ck