"src/vscode:/vscode.git/clone" did not exist on "079d63a788f7d39381ce1513b7c9090d3ac3f439"
integral_constant.hpp 1.74 KB
Newer Older
1
2
3
#ifndef CK_INTEGRAL_CONSTANT_HPP
#define CK_INTEGRAL_CONSTANT_HPP

Chao Liu's avatar
Chao Liu committed
4
namespace ck {
5

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

Chao Liu's avatar
Chao Liu committed
16
17
template <class X, class Y>
struct is_same : public integral_constant<bool, false>
Chao Liu's avatar
Chao Liu committed
18
{
Chao Liu's avatar
Chao Liu committed
19
};
Chao Liu's avatar
Chao Liu committed
20

Chao Liu's avatar
Chao Liu committed
21
22
template <class X>
struct is_same<X, X> : public integral_constant<bool, true>
Chao Liu's avatar
Chao Liu committed
23
{
Chao Liu's avatar
Chao Liu committed
24
};
Chao Liu's avatar
Chao Liu committed
25

Chao Liu's avatar
Chao Liu committed
26
27
template <index_t N>
using Number = integral_constant<index_t, N>;
28

Chao Liu's avatar
Chao Liu committed
29
30
template <index_t X, index_t Y>
__host__ __device__ constexpr auto operator+(Number<X>, Number<Y>)
Chao Liu's avatar
Chao Liu committed
31
{
Chao Liu's avatar
Chao Liu committed
32
33
    return Number<X + Y>{};
}
Chao Liu's avatar
Chao Liu committed
34

Chao Liu's avatar
Chao Liu committed
35
36
template <index_t X, index_t Y>
__host__ __device__ constexpr auto operator-(Number<X>, Number<Y>)
Chao Liu's avatar
Chao Liu committed
37
{
Chao Liu's avatar
Chao Liu committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
    static_assert(Y <= X, "wrong!");
    return Number<X - Y>{};
}

template <index_t X, index_t Y>
__host__ __device__ constexpr auto operator*(Number<X>, Number<Y>)
{
    return Number<X * Y>{};
}

template <index_t X, index_t Y>
__host__ __device__ constexpr auto operator/(Number<X>, Number<Y>)
{
    static_assert(Y > 0, "wrong!");
    return Number<X / Y>{};
}

template <index_t X, index_t Y>
__host__ __device__ constexpr auto operator%(Number<X>, Number<Y>)
{
    static_assert(Y > 0, "wrong!");
    return Number<X % Y>{};
}

#if 0
static constexpr Number<0> 0_c;
static constexpr Number<1> 1_c;
static constexpr Number<2> 2_c;
static constexpr Number<3> 3_c;
static constexpr Number<4> 4_c;
static constexpr Number<5> 5_c;
static constexpr Number<6> 6_c;
static constexpr Number<7> 7_c;
static constexpr Number<8> 8_c;
static constexpr Number<9> 9_c;
#endif
Chao Liu's avatar
Chao Liu committed
74

75
76
} // namespace ck
#endif