"ml/git@developer.sourcefind.cn:OpenDAS/ollama.git" did not exist on "df94175a0fb0356c9b9e9a62b73d908633c08810"
math.hpp 4.36 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 "number.hpp"
Chao Liu's avatar
Chao Liu committed
7
#include "type.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
struct minus
{
    __host__ __device__ constexpr T operator()(T a, T b) const { return a - b; }
};

zjing14's avatar
zjing14 committed
30
template <typename T>
Chao Liu's avatar
Chao Liu committed
31
32
33
34
35
struct multiplies
{
    __host__ __device__ constexpr T operator()(T a, T b) const { return a * b; }
};

Chao Liu's avatar
Chao Liu committed
36
37
38
39
40
41
42
43
44
struct multiplies_v2
{
    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
45
template <typename T>
Chao Liu's avatar
Chao Liu committed
46
struct maximize
Chao Liu's avatar
Chao Liu committed
47
48
49
50
{
    __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
struct minimize
{
    __host__ __device__ constexpr T operator()(T a, T b) const { return a <= b ? a : b; }
};

zjing14's avatar
zjing14 committed
57
template <typename T>
Chao Liu's avatar
Chao Liu committed
58
59
60
61
62
63
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
64
        return (a + b - Number<1>{}) / b;
Chao Liu's avatar
Chao Liu committed
65
66
67
    }
};

zjing14's avatar
zjing14 committed
68
template <typename X, typename Y>
69
70
71
72
73
__host__ __device__ constexpr auto integer_divide_floor(X x, Y y)
{
    return x / y;
}

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

zjing14's avatar
zjing14 committed
80
template <typename X, typename Y>
Chao Liu's avatar
Chao Liu committed
81
__host__ __device__ constexpr auto integer_least_multiple(X x, Y y)
Chao Liu's avatar
Chao Liu committed
82
{
Chao Liu's avatar
Chao Liu committed
83
    return y * integer_divide_ceil(x, y);
Chao Liu's avatar
Chao Liu committed
84
85
}

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

zjing14's avatar
zjing14 committed
92
93
template <typename T>
__host__ __device__ constexpr T max(T x, T y)
Chao Liu's avatar
Chao Liu committed
94
{
zjing14's avatar
zjing14 committed
95
96
    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 X>
__host__ __device__ constexpr index_t max(Number<X>, index_t y)
{
    return X > y ? X : y;
}
Chao Liu's avatar
Chao Liu committed
103

zjing14's avatar
zjing14 committed
104
105
106
107
108
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
109

zjing14's avatar
zjing14 committed
110
111
112
113
114
115
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
116
117
}

zjing14's avatar
zjing14 committed
118
template <typename T>
Chao Liu's avatar
Chao Liu committed
119
120
121
122
123
__host__ __device__ constexpr T min(T x)
{
    return x;
}

zjing14's avatar
zjing14 committed
124
125
126
127
128
129
130
131
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
132
{
zjing14's avatar
zjing14 committed
133
134
    return X < y ? X : y;
}
Chao Liu's avatar
Chao Liu committed
135

zjing14's avatar
zjing14 committed
136
137
138
139
140
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
141

zjing14's avatar
zjing14 committed
142
143
144
145
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
146

zjing14's avatar
zjing14 committed
147
    return min(x, min(ys...));
Chao Liu's avatar
Chao Liu committed
148
149
}

150
// greatest common divisor, aka highest common factor
Chao Liu's avatar
Chao Liu committed
151
__host__ __device__ constexpr index_t gcd(index_t x, index_t y)
Chao Liu's avatar
Chao Liu committed
152
{
153
154
155
156
157
158
159
160
161
    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
162
163
164
    {
        return y;
    }
Chao Liu's avatar
Chao Liu committed
165
    else if(y == 0)
Chao Liu's avatar
Chao Liu committed
166
167
168
    {
        return x;
    }
Chao Liu's avatar
Chao Liu committed
169
    else if(x > y)
Chao Liu's avatar
Chao Liu committed
170
    {
171
        return gcd(x % y, y);
Chao Liu's avatar
Chao Liu committed
172
    }
Chao Liu's avatar
Chao Liu committed
173
    else
Chao Liu's avatar
Chao Liu committed
174
    {
175
        return gcd(x, y % x);
Chao Liu's avatar
Chao Liu committed
176
177
178
179
    }
}

template <index_t X, index_t Y>
180
__host__ __device__ constexpr auto gcd(Number<X>, Number<Y>)
Chao Liu's avatar
Chao Liu committed
181
{
Chao Liu's avatar
Chao Liu committed
182
183
184
    constexpr auto r = gcd(X, Y);

    return Number<r>{};
Chao Liu's avatar
Chao Liu committed
185
186
}

Chao Liu's avatar
Chao Liu committed
187
188
189
template <typename X,
          typename... Ys,
          typename std::enable_if<sizeof...(Ys) >= 2, bool>::type = false>
190
__host__ __device__ constexpr auto gcd(X x, Ys... ys)
Chao Liu's avatar
Chao Liu committed
191
{
192
    return gcd(x, gcd(ys...));
Chao Liu's avatar
Chao Liu committed
193
194
195
}

// least common multiple
Chao Liu's avatar
Chao Liu committed
196
197
template <typename X, typename Y>
__host__ __device__ constexpr auto lcm(X x, Y y)
Chao Liu's avatar
Chao Liu committed
198
{
199
    return (x * y) / gcd(x, y);
Chao Liu's avatar
Chao Liu committed
200
201
}

Chao Liu's avatar
Chao Liu committed
202
203
204
template <typename X,
          typename... Ys,
          typename std::enable_if<sizeof...(Ys) >= 2, bool>::type = false>
Chao Liu's avatar
Chao Liu committed
205
__host__ __device__ constexpr auto lcm(X x, Ys... ys)
Chao Liu's avatar
Chao Liu committed
206
{
Chao Liu's avatar
Chao Liu committed
207
    return lcm(x, lcm(ys...));
Chao Liu's avatar
Chao Liu committed
208
209
}

zjing14's avatar
zjing14 committed
210
template <typename T>
Chao Liu's avatar
Chao Liu committed
211
212
213
214
215
struct equal
{
    __host__ __device__ constexpr bool operator()(T x, T y) const { return x == y; }
};

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

Chao Liu's avatar
Chao Liu committed
222
} // namespace math
Chao Liu's avatar
Chao Liu committed
223
} // namespace ck
Chao Liu's avatar
Chao Liu committed
224
225

#endif