math.hpp 5.38 KB
Newer Older
Umang Yadav's avatar
Umang Yadav committed
1
2
3

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Weverything"
Chao Liu's avatar
Chao Liu committed
4
// SPDX-License-Identifier: MIT
Illia Silin's avatar
Illia Silin committed
5
// Copyright (c) 2018-2023, Advanced Micro Devices, Inc. All rights reserved.
Chao Liu's avatar
Chao Liu committed
6

Chao Liu's avatar
Chao Liu committed
7
#pragma once
Chao Liu's avatar
Chao Liu committed
8

Chao Liu's avatar
Chao Liu committed
9
#include "ck/ck.hpp"
Umang Yadav's avatar
Umang Yadav committed
10
#include "enable_if.hpp"
Chao Liu's avatar
Chao Liu committed
11
#include "integral_constant.hpp"
Chao Liu's avatar
Chao Liu committed
12
#include "number.hpp"
Chao Liu's avatar
Chao Liu committed
13
#include "type.hpp"
Chao Liu's avatar
Chao Liu committed
14
15
16
17

namespace ck {
namespace math {

Umang Yadav's avatar
Umang Yadav committed
18
19
template <typename T, T s> struct scales {
  __host__ __device__ constexpr T operator()(T a) const { return s * a; }
Chao Liu's avatar
Chao Liu committed
20
21
};

Umang Yadav's avatar
Umang Yadav committed
22
23
template <typename T> struct plus {
  __host__ __device__ constexpr T operator()(T a, T b) const { return a + b; }
Chao Liu's avatar
Chao Liu committed
24
25
};

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

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

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

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

Umang Yadav's avatar
Umang Yadav committed
49
50
51
template <typename 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");
Chao Liu's avatar
Chao Liu committed
52

Umang Yadav's avatar
Umang Yadav committed
53
54
    return (a + b - Number<1>{}) / b;
  }
Chao Liu's avatar
Chao Liu committed
55
56
};

zjing14's avatar
zjing14 committed
57
template <typename X, typename Y>
Umang Yadav's avatar
Umang Yadav committed
58
59
__host__ __device__ constexpr auto integer_divide_floor(X x, Y y) {
  return x / y;
60
61
}

zjing14's avatar
zjing14 committed
62
template <typename X, typename Y>
Umang Yadav's avatar
Umang Yadav committed
63
64
__host__ __device__ constexpr auto integer_divide_ceil(X x, Y y) {
  return (x + y - Number<1>{}) / y;
Chao Liu's avatar
Chao Liu committed
65
66
}

zjing14's avatar
zjing14 committed
67
template <typename X, typename Y>
Umang Yadav's avatar
Umang Yadav committed
68
69
__host__ __device__ constexpr auto integer_least_multiple(X x, Y y) {
  return y * integer_divide_ceil(x, y);
Chao Liu's avatar
Chao Liu committed
70
71
}

Umang Yadav's avatar
Umang Yadav committed
72
template <typename T> __host__ __device__ constexpr T max(T x) { return x; }
Chao Liu's avatar
Chao Liu committed
73

Umang Yadav's avatar
Umang Yadav committed
74
75
template <typename T> __host__ __device__ constexpr T max(T x, T y) {
  return x > y ? x : y;
zjing14's avatar
zjing14 committed
76
}
Chao Liu's avatar
Chao Liu committed
77

zjing14's avatar
zjing14 committed
78
template <index_t X>
Umang Yadav's avatar
Umang Yadav committed
79
80
__host__ __device__ constexpr index_t max(Number<X>, index_t y) {
  return X > y ? X : y;
zjing14's avatar
zjing14 committed
81
}
Chao Liu's avatar
Chao Liu committed
82

zjing14's avatar
zjing14 committed
83
template <index_t Y>
Umang Yadav's avatar
Umang Yadav committed
84
85
__host__ __device__ constexpr index_t max(index_t x, Number<Y>) {
  return x > Y ? x : Y;
zjing14's avatar
zjing14 committed
86
}
Chao Liu's avatar
Chao Liu committed
87

zjing14's avatar
zjing14 committed
88
template <typename X, typename... Ys>
Umang Yadav's avatar
Umang Yadav committed
89
90
__host__ __device__ constexpr auto max(X x, Ys... ys) {
  static_assert(sizeof...(Ys) > 0, "not enough argument");
zjing14's avatar
zjing14 committed
91

Umang Yadav's avatar
Umang Yadav committed
92
  return max(x, max(ys...));
Chao Liu's avatar
Chao Liu committed
93
94
}

Umang Yadav's avatar
Umang Yadav committed
95
template <typename T> __host__ __device__ constexpr T min(T x) { return x; }
Chao Liu's avatar
Chao Liu committed
96

Umang Yadav's avatar
Umang Yadav committed
97
98
template <typename T> __host__ __device__ constexpr T min(T x, T y) {
  return x < y ? x : y;
zjing14's avatar
zjing14 committed
99
100
101
}

template <index_t X>
Umang Yadav's avatar
Umang Yadav committed
102
103
__host__ __device__ constexpr index_t min(Number<X>, index_t y) {
  return X < y ? X : y;
zjing14's avatar
zjing14 committed
104
}
Chao Liu's avatar
Chao Liu committed
105

zjing14's avatar
zjing14 committed
106
template <index_t Y>
Umang Yadav's avatar
Umang Yadav committed
107
108
__host__ __device__ constexpr index_t min(index_t x, Number<Y>) {
  return x < Y ? x : Y;
zjing14's avatar
zjing14 committed
109
}
Chao Liu's avatar
Chao Liu committed
110

zjing14's avatar
zjing14 committed
111
template <typename X, typename... Ys>
Umang Yadav's avatar
Umang Yadav committed
112
113
__host__ __device__ constexpr auto min(X x, Ys... ys) {
  static_assert(sizeof...(Ys) > 0, "not enough argument");
Chao Liu's avatar
Chao Liu committed
114

Umang Yadav's avatar
Umang Yadav committed
115
  return min(x, min(ys...));
Chao Liu's avatar
Chao Liu committed
116
117
}

rocking5566's avatar
rocking5566 committed
118
template <typename T>
Umang Yadav's avatar
Umang Yadav committed
119
120
121
__host__ __device__ constexpr T clamp(const T &x, const T &lowerbound,
                                      const T &upperbound) {
  return min(max(x, lowerbound), upperbound);
rocking5566's avatar
rocking5566 committed
122
123
}

124
// disallow implicit type casting
Umang Yadav's avatar
Umang Yadav committed
125
template <typename T> __device__ T exp(T x);
126

127
128
// TODO: add f16 support using v_exp_f16

Umang Yadav's avatar
Umang Yadav committed
129
template <> __device__ float exp<float>(float x) { return __expf(x); }
130

Umang Yadav's avatar
Umang Yadav committed
131
template <> __device__ double exp<double>(double x) { return exp(x); }
132

Umang Yadav's avatar
Umang Yadav committed
133
// static inline __host__ float exp(float x) { return ::expf(x); }
134

Umang Yadav's avatar
Umang Yadav committed
135
// static inline __host__ double exp(double x) { return std::exp(x); }
136

137
// greatest common divisor, aka highest common factor
Umang Yadav's avatar
Umang Yadav committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
__host__ __device__ constexpr index_t gcd(index_t x, index_t y) {
  if (x < 0) {
    return gcd(-x, y);
  } else if (y < 0) {
    return gcd(x, -y);
  } else if (x == y || x == 0) {
    return y;
  } else if (y == 0) {
    return x;
  } else if (x > y) {
    return gcd(x % y, y);
  } else {
    return gcd(x, y % x);
  }
Chao Liu's avatar
Chao Liu committed
152
153
154
}

template <index_t X, index_t Y>
Umang Yadav's avatar
Umang Yadav committed
155
156
__host__ __device__ constexpr auto gcd(Number<X>, Number<Y>) {
  constexpr auto r = gcd(X, Y);
Chao Liu's avatar
Chao Liu committed
157

Umang Yadav's avatar
Umang Yadav committed
158
  return Number<r>{};
Chao Liu's avatar
Chao Liu committed
159
160
}

Umang Yadav's avatar
Umang Yadav committed
161
162
163
164
template <typename X, typename... Ys,
          typename enable_if<sizeof...(Ys) >= 2, bool>::type = false>
__host__ __device__ constexpr auto gcd(X x, Ys... ys) {
  return gcd(x, gcd(ys...));
Chao Liu's avatar
Chao Liu committed
165
166
167
}

// least common multiple
Chao Liu's avatar
Chao Liu committed
168
template <typename X, typename Y>
Umang Yadav's avatar
Umang Yadav committed
169
170
__host__ __device__ constexpr auto lcm(X x, Y y) {
  return (x * y) / gcd(x, y);
Chao Liu's avatar
Chao Liu committed
171
172
}

Umang Yadav's avatar
Umang Yadav committed
173
174
175
176
template <typename X, typename... Ys,
          typename enable_if<sizeof...(Ys) >= 2, bool>::type = false>
__host__ __device__ constexpr auto lcm(X x, Ys... ys) {
  return lcm(x, lcm(ys...));
Chao Liu's avatar
Chao Liu committed
177
178
}

Umang Yadav's avatar
Umang Yadav committed
179
180
181
182
template <typename T> struct equal {
  __host__ __device__ constexpr bool operator()(T x, T y) const {
    return x == y;
  }
Chao Liu's avatar
Chao Liu committed
183
184
};

Umang Yadav's avatar
Umang Yadav committed
185
186
187
188
template <typename T> struct less {
  __host__ __device__ constexpr bool operator()(T x, T y) const {
    return x < y;
  }
Chao Liu's avatar
Chao Liu committed
189
190
};

191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
template <index_t X>
__host__ __device__ constexpr auto next_power_of_two()
{
    // TODO: X need to be 2 ~ 0x7fffffff. 0, 1, or larger than 0x7fffffff will compile fail
    constexpr index_t Y = 1 << (32 - __builtin_clz(X - 1));
    return Y;
}

template <index_t X>
__host__ __device__ constexpr auto next_power_of_two(Number<X> x)
{
    // TODO: X need to be 2 ~ 0x7fffffff. 0, 1, or larger than 0x7fffffff will compile fail
    constexpr index_t Y = 1 << (32 - __builtin_clz(x.value - 1));
    return Number<Y>{};
}

Chao Liu's avatar
Chao Liu committed
207
} // namespace math
Chao Liu's avatar
Chao Liu committed
208
} // namespace ck
Umang Yadav's avatar
Umang Yadav committed
209
210

#pragma clang diagnostic pop