math.hpp 3.21 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
2
3
4
#ifndef CK_MATH_HPP
#define CK_MATH_HPP

#include "config.hpp"
Chao Liu's avatar
Chao Liu committed
5
#include "integral_constant.hpp"
Chao Liu's avatar
Chao Liu committed
6
#include "type.hpp"
Chao Liu's avatar
Chao Liu committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

namespace ck {
namespace math {

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; }
};

Chao Liu's avatar
Chao Liu committed
35
36
37
38
39
40
template <class T>
struct maxer
{
    __host__ __device__ constexpr T operator()(T a, T b) const { return a >= b ? a : b; }
};

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

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

52
53
54
55
56
57
template <class X, class Y>
__host__ __device__ constexpr auto integer_divide_floor(X x, Y y)
{
    return x / y;
}

Chao Liu's avatar
Chao Liu committed
58
59
template <class X, class Y>
__host__ __device__ constexpr auto integer_divide_ceil(X x, Y y)
Chao Liu's avatar
Chao Liu committed
60
{
Chao Liu's avatar
Chao Liu committed
61
    return (x + y - 1) / y;
Chao Liu's avatar
Chao Liu committed
62
63
}

Chao Liu's avatar
Chao Liu committed
64
65
template <class X, class Y>
__host__ __device__ constexpr auto integer_least_multiple(X x, Y y)
Chao Liu's avatar
Chao Liu committed
66
{
Chao Liu's avatar
Chao Liu committed
67
    return y * integer_divide_ceil(x, y);
Chao Liu's avatar
Chao Liu committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
}

template <class T>
__host__ __device__ constexpr T max(T x)
{
    return x;
}

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...);

    static_assert(is_same<decltype(y), T>{}, "not the same type");

    return x > y ? x : y;
}

template <class T>
__host__ __device__ constexpr T min(T x)
{
    return x;
}

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...);

    static_assert(is_same<decltype(y), T>{}, "not the same type");

    return x < y ? x : y;
}

106
// greatest common divisor, aka highest common factor
Chao Liu's avatar
Chao Liu committed
107
template <typename T>
108
__host__ __device__ constexpr T gcd(T x, T y)
Chao Liu's avatar
Chao Liu committed
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
{
    if(x == 0)
    {
        return y;
    }

    if(y == 0)
    {
        return x;
    }

    if(x == y)
    {
        return x;
    }

    if(x > y)
    {
127
        return gcd(x - y, y);
Chao Liu's avatar
Chao Liu committed
128
129
    }

130
    return gcd(x, y - x);
Chao Liu's avatar
Chao Liu committed
131
132
133
}

template <index_t X, index_t Y>
134
__host__ __device__ constexpr auto gcd(Number<X>, Number<Y>)
Chao Liu's avatar
Chao Liu committed
135
{
136
    constexpr auto result = gcd(X, Y);
Chao Liu's avatar
Chao Liu committed
137
138
139
140
    return Number<result>{};
}

template <typename X, typename... Ys>
141
__host__ __device__ constexpr auto gcd(X x, Ys... ys)
Chao Liu's avatar
Chao Liu committed
142
{
143
    return gcd(x, ys...);
Chao Liu's avatar
Chao Liu committed
144
145
146
147
148
149
}

// least common multiple
template <typename T>
__host__ __device__ constexpr T lcm(T x, T y)
{
150
    return (x * y) / gcd(x, y);
Chao Liu's avatar
Chao Liu committed
151
152
153
154
}

template <typename X, typename Y, typename... Zs>
__host__ __device__ constexpr auto lcm(X x, Y y, Zs... zs)
Chao Liu's avatar
Chao Liu committed
155
{
Chao Liu's avatar
Chao Liu committed
156
    return lcm(x, lcm(y, zs...));
Chao Liu's avatar
Chao Liu committed
157
158
}

Chao Liu's avatar
Chao Liu committed
159
160
161
162
163
164
165
166
167
168
169
170
template <class T>
struct equal
{
    __host__ __device__ constexpr bool operator()(T x, T y) const { return x == y; }
};

template <class T>
struct less
{
    __host__ __device__ constexpr bool operator()(T x, T y) const { return x < y; }
};

Chao Liu's avatar
Chao Liu committed
171
172
173
174
} // namespace math
} // namspace ck

#endif