math.hpp 4.4 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
#pragma once
Chao Liu's avatar
Chao Liu committed
2

Chao Liu's avatar
Chao Liu committed
3
#include "ck/ck.hpp"
Chao Liu's avatar
Chao Liu committed
4
#include "integral_constant.hpp"
Chao Liu's avatar
Chao Liu committed
5
#include "number.hpp"
Chao Liu's avatar
Chao Liu committed
6
#include "type.hpp"
Chao Liu's avatar
Chao Liu committed
7
#include "enable_if.hpp"
Chao Liu's avatar
Chao Liu committed
8
9
10
11

namespace ck {
namespace math {

zjing14's avatar
zjing14 committed
12
template <typename T, T s>
Chao Liu's avatar
Chao Liu committed
13
14
15
16
17
struct scales
{
    __host__ __device__ constexpr T operator()(T a) const { return s * a; }
};

zjing14's avatar
zjing14 committed
18
template <typename T>
Chao Liu's avatar
Chao Liu committed
19
20
21
22
23
struct plus
{
    __host__ __device__ constexpr T operator()(T a, T b) const { return a + b; }
};

zjing14's avatar
zjing14 committed
24
template <typename T>
Chao Liu's avatar
Chao Liu committed
25
26
27
28
29
30
struct minus
{
    __host__ __device__ constexpr T operator()(T a, T b) const { return a - b; }
};

struct multiplies
Chao Liu's avatar
Chao Liu committed
31
32
33
34
35
36
37
38
{
    template <typename A, typename B>
    __host__ __device__ constexpr auto operator()(const A& a, const B& b) const
    {
        return a * b;
    }
};

zjing14's avatar
zjing14 committed
39
template <typename T>
Chao Liu's avatar
Chao Liu committed
40
struct maximize
Chao Liu's avatar
Chao Liu committed
41
42
43
44
{
    __host__ __device__ constexpr T operator()(T a, T b) const { return a >= b ? a : b; }
};

zjing14's avatar
zjing14 committed
45
template <typename T>
Chao Liu's avatar
Chao Liu committed
46
47
48
49
50
struct minimize
{
    __host__ __device__ constexpr T operator()(T a, T b) const { return a <= b ? a : b; }
};

zjing14's avatar
zjing14 committed
51
template <typename T>
Chao Liu's avatar
Chao Liu committed
52
53
54
55
56
57
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");

zjing14's avatar
zjing14 committed
58
        return (a + b - Number<1>{}) / b;
Chao Liu's avatar
Chao Liu committed
59
60
61
    }
};

zjing14's avatar
zjing14 committed
62
template <typename X, typename Y>
63
64
65
66
67
__host__ __device__ constexpr auto integer_divide_floor(X x, Y y)
{
    return x / y;
}

zjing14's avatar
zjing14 committed
68
template <typename X, typename Y>
Chao Liu's avatar
Chao Liu committed
69
__host__ __device__ constexpr auto integer_divide_ceil(X x, Y y)
Chao Liu's avatar
Chao Liu committed
70
{
71
    return (x + y - Number<1>{}) / y;
Chao Liu's avatar
Chao Liu committed
72
73
}

zjing14's avatar
zjing14 committed
74
template <typename X, typename Y>
Chao Liu's avatar
Chao Liu committed
75
__host__ __device__ constexpr auto integer_least_multiple(X x, Y y)
Chao Liu's avatar
Chao Liu committed
76
{
Chao Liu's avatar
Chao Liu committed
77
    return y * integer_divide_ceil(x, y);
Chao Liu's avatar
Chao Liu committed
78
79
}

zjing14's avatar
zjing14 committed
80
template <typename T>
Chao Liu's avatar
Chao Liu committed
81
82
83
84
85
__host__ __device__ constexpr T max(T x)
{
    return x;
}

zjing14's avatar
zjing14 committed
86
87
template <typename T>
__host__ __device__ constexpr T max(T x, T y)
Chao Liu's avatar
Chao Liu committed
88
{
zjing14's avatar
zjing14 committed
89
90
    return x > y ? x : y;
}
Chao Liu's avatar
Chao Liu committed
91

zjing14's avatar
zjing14 committed
92
93
94
95
96
template <index_t X>
__host__ __device__ constexpr index_t max(Number<X>, index_t y)
{
    return X > y ? X : y;
}
Chao Liu's avatar
Chao Liu committed
97

zjing14's avatar
zjing14 committed
98
99
100
101
102
template <index_t Y>
__host__ __device__ constexpr index_t max(index_t x, Number<Y>)
{
    return x > Y ? x : Y;
}
Chao Liu's avatar
Chao Liu committed
103

zjing14's avatar
zjing14 committed
104
105
106
107
108
109
template <typename X, typename... Ys>
__host__ __device__ constexpr auto max(X x, Ys... ys)
{
    static_assert(sizeof...(Ys) > 0, "not enough argument");

    return max(x, max(ys...));
Chao Liu's avatar
Chao Liu committed
110
111
}

zjing14's avatar
zjing14 committed
112
template <typename T>
Chao Liu's avatar
Chao Liu committed
113
114
115
116
117
__host__ __device__ constexpr T min(T x)
{
    return x;
}

zjing14's avatar
zjing14 committed
118
119
120
121
122
123
124
125
template <typename T>
__host__ __device__ constexpr T min(T x, T y)
{
    return x < y ? x : y;
}

template <index_t X>
__host__ __device__ constexpr index_t min(Number<X>, index_t y)
Chao Liu's avatar
Chao Liu committed
126
{
zjing14's avatar
zjing14 committed
127
128
    return X < y ? X : y;
}
Chao Liu's avatar
Chao Liu committed
129

zjing14's avatar
zjing14 committed
130
131
132
133
134
template <index_t Y>
__host__ __device__ constexpr index_t min(index_t x, Number<Y>)
{
    return x < Y ? x : Y;
}
Chao Liu's avatar
Chao Liu committed
135

zjing14's avatar
zjing14 committed
136
137
138
139
template <typename X, typename... Ys>
__host__ __device__ constexpr auto min(X x, Ys... ys)
{
    static_assert(sizeof...(Ys) > 0, "not enough argument");
Chao Liu's avatar
Chao Liu committed
140

zjing14's avatar
zjing14 committed
141
    return min(x, min(ys...));
Chao Liu's avatar
Chao Liu committed
142
143
}

144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// disallow implicit type casting
template <typename T>
__device__ T exp(T x);

template <>
__device__ float exp<float>(float x)
{
    return __expf(x);
}

template <>
__device__ double exp<double>(double x)
{
    return exp(x);
}

160
// greatest common divisor, aka highest common factor
Chao Liu's avatar
Chao Liu committed
161
__host__ __device__ constexpr index_t gcd(index_t x, index_t y)
Chao Liu's avatar
Chao Liu committed
162
{
163
164
165
166
167
168
169
170
171
    if(x < 0)
    {
        return gcd(-x, y);
    }
    else if(y < 0)
    {
        return gcd(x, -y);
    }
    else if(x == y || x == 0)
Chao Liu's avatar
Chao Liu committed
172
173
174
    {
        return y;
    }
Chao Liu's avatar
Chao Liu committed
175
    else if(y == 0)
Chao Liu's avatar
Chao Liu committed
176
177
178
    {
        return x;
    }
Chao Liu's avatar
Chao Liu committed
179
    else if(x > y)
Chao Liu's avatar
Chao Liu committed
180
    {
181
        return gcd(x % y, y);
Chao Liu's avatar
Chao Liu committed
182
    }
Chao Liu's avatar
Chao Liu committed
183
    else
Chao Liu's avatar
Chao Liu committed
184
    {
185
        return gcd(x, y % x);
Chao Liu's avatar
Chao Liu committed
186
187
188
189
    }
}

template <index_t X, index_t Y>
190
__host__ __device__ constexpr auto gcd(Number<X>, Number<Y>)
Chao Liu's avatar
Chao Liu committed
191
{
Chao Liu's avatar
Chao Liu committed
192
193
194
    constexpr auto r = gcd(X, Y);

    return Number<r>{};
Chao Liu's avatar
Chao Liu committed
195
196
}

Chao Liu's avatar
Chao Liu committed
197
template <typename X, typename... Ys, typename enable_if<sizeof...(Ys) >= 2, bool>::type = false>
198
__host__ __device__ constexpr auto gcd(X x, Ys... ys)
Chao Liu's avatar
Chao Liu committed
199
{
200
    return gcd(x, gcd(ys...));
Chao Liu's avatar
Chao Liu committed
201
202
203
}

// least common multiple
Chao Liu's avatar
Chao Liu committed
204
205
template <typename X, typename Y>
__host__ __device__ constexpr auto lcm(X x, Y y)
Chao Liu's avatar
Chao Liu committed
206
{
207
    return (x * y) / gcd(x, y);
Chao Liu's avatar
Chao Liu committed
208
209
}

Chao Liu's avatar
Chao Liu committed
210
template <typename X, typename... Ys, typename enable_if<sizeof...(Ys) >= 2, bool>::type = false>
Chao Liu's avatar
Chao Liu committed
211
__host__ __device__ constexpr auto lcm(X x, Ys... ys)
Chao Liu's avatar
Chao Liu committed
212
{
Chao Liu's avatar
Chao Liu committed
213
    return lcm(x, lcm(ys...));
Chao Liu's avatar
Chao Liu committed
214
215
}

zjing14's avatar
zjing14 committed
216
template <typename T>
Chao Liu's avatar
Chao Liu committed
217
218
219
220
221
struct equal
{
    __host__ __device__ constexpr bool operator()(T x, T y) const { return x == y; }
};

zjing14's avatar
zjing14 committed
222
template <typename T>
Chao Liu's avatar
Chao Liu committed
223
224
225
226
227
struct less
{
    __host__ __device__ constexpr bool operator()(T x, T y) const { return x < y; }
};

Chao Liu's avatar
Chao Liu committed
228
} // namespace math
Chao Liu's avatar
Chao Liu committed
229
} // namespace ck