utility.hpp 2.21 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
2
#ifndef CK_UTILITY_HPP
#define CK_UTILITY_HPP
3

Chao Liu's avatar
Chao Liu committed
4
#include <type_traits>
Chao Liu's avatar
Chao Liu committed
5
6
#include "config.hpp"

7
namespace ck {
Chao Liu's avatar
Chao Liu committed
8
9

template <class X, class Y>
Chao Liu's avatar
Chao Liu committed
10
using is_same = std::is_same<X, Y>;
Chao Liu's avatar
Chao Liu committed
11

Chao Liu's avatar
Chao Liu committed
12
13
namespace math {

Chao Liu's avatar
Chao Liu committed
14
15
16
17
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
template <class T, T s>
struct scales
{
    __host__ __device__ constexpr T operator()(T a) const { return s * a; }
};

template <class T>
struct plus
{
    __host__ __device__ constexpr T operator()(T a, T b) const { return a + b; }
};

template <class T>
struct minus
{
    __host__ __device__ constexpr T operator()(T a, T b) const { return a - b; }
};

template <class T>
struct multiplies
{
    __host__ __device__ constexpr T operator()(T a, T b) const { return a * b; }
};

template <class T>
struct integer_divide_ceiler
{
    __host__ __device__ constexpr T operator()(T a, T b) const
    {
Chao Liu's avatar
Chao Liu committed
43
        static_assert(is_same<T, index_t>{} || is_same<T, int>{}, "wrong type");
Chao Liu's avatar
Chao Liu committed
44
45
46
47
48
49
50
51

        return (a + b - 1) / b;
    }
};

template <class T>
__host__ __device__ constexpr T integer_divide_ceil(T a, T b)
{
Chao Liu's avatar
Chao Liu committed
52
    static_assert(is_same<T, index_t>{} || is_same<T, int>{}, "wrong type");
Chao Liu's avatar
Chao Liu committed
53
54
55
56

    return (a + b - 1) / b;
}

Chao Liu's avatar
Chao Liu committed
57
58
59
60
61
62
63
64
template <class T>
__host__ __device__ constexpr T integer_least_multiple(T a, T b)
{
    static_assert(is_same<T, index_t>{} || is_same<T, int>{}, "wrong type");

    return b * integer_divide_ceil(a, b);
}

Chao Liu's avatar
Chao Liu committed
65
template <class T>
Chao Liu's avatar
Chao Liu committed
66
__host__ __device__ constexpr T max(T x)
Chao Liu's avatar
Chao Liu committed
67
{
Chao Liu's avatar
Chao Liu committed
68
    return x;
Chao Liu's avatar
Chao Liu committed
69
70
71
72
73
74
75
76
77
}

template <class T, class... Ts>
__host__ __device__ constexpr T max(T x, Ts... xs)
{
    static_assert(sizeof...(xs) > 0, "not enough argument");

    auto y = max(xs...);

Chao Liu's avatar
Chao Liu committed
78
    static_assert(is_same<decltype(y), T>{}, "not the same type");
Chao Liu's avatar
Chao Liu committed
79
80
81
82
83

    return x > y ? x : y;
}

template <class T>
Chao Liu's avatar
Chao Liu committed
84
__host__ __device__ constexpr T min(T x)
Chao Liu's avatar
Chao Liu committed
85
{
Chao Liu's avatar
Chao Liu committed
86
    return x;
Chao Liu's avatar
Chao Liu committed
87
88
89
90
91
92
93
94
95
}

template <class T, class... Ts>
__host__ __device__ constexpr T min(T x, Ts... xs)
{
    static_assert(sizeof...(xs) > 0, "not enough argument");

    auto y = min(xs...);

Chao Liu's avatar
Chao Liu committed
96
    static_assert(is_same<decltype(y), T>{}, "not the same type");
Chao Liu's avatar
Chao Liu committed
97
98
99
100

    return x < y ? x : y;
}

Chao Liu's avatar
Chao Liu committed
101
// this is WRONG
Chao Liu's avatar
Chao Liu committed
102
// TODO: implement least common multiple properly, instead of calling max()
Chao Liu's avatar
Chao Liu committed
103
104
105
106
107
108
template <class T, class... Ts>
__host__ __device__ constexpr T lcm(T x, Ts... xs)
{
    return max(x, xs...);
}

109
110
111
112
} // namespace math
} // namspace ck

#endif