-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvector.h
More file actions
387 lines (338 loc) · 11 KB
/
Copy pathvector.h
File metadata and controls
387 lines (338 loc) · 11 KB
1
2
3
4
5
6
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#ifndef KERNEL_FLOAT_VECTOR_H
#define KERNEL_FLOAT_VECTOR_H
#include "base.h"
#include "conversion.h"
#include "iterate.h"
#include "macros.h"
#include "reduce.h"
#include "triops.h"
#include "unops.h"
namespace kernel_float {
/**
* Container that store fixed number of elements of type ``T``.
*
* It is not recommended to use this class directly, instead, use the type `vec<T, N>` which is an alias for
* `vector<T, extent<N>, vector_storage<T, E>>`.
*
* @tparam T The type of the values stored within the vector.
* @tparam E The size of this vector. Should be of type `extent<N>`.
* @tparam S The object's storage class. Should be the type `vector_storage<T, E>`
*/
template<typename T, typename E, class S>
struct vector: public S {
using self_type = vector<T, E, S>;
using value_type = T;
using extent_type = E;
using storage_type = S;
// Copy another `vector<T, E>`
vector(const vector&) = default;
// Copy anything of type `storage_type`
KERNEL_FLOAT_INLINE
vector(const storage_type& storage) : storage_type(storage) {}
// Copy anything of type `storage_type`
KERNEL_FLOAT_INLINE
vector(const value_type& input = {}) :
storage_type(detail::broadcast_impl<T, kernel_float::extent<1>, E>::call(input)) {}
// For all other arguments, we convert it using `convert_storage` according to broadcast rules
template<typename U, enable_if_t<is_implicit_convertible<vector_value_type<U>, T>, int> = 0>
KERNEL_FLOAT_INLINE vector(U&& input) :
storage_type(convert_storage<T>(input, extent_type {})) {}
template<typename U, enable_if_t<!is_implicit_convertible<vector_value_type<U>, T>, int> = 0>
KERNEL_FLOAT_INLINE explicit vector(U&& input) :
storage_type(convert_storage<T>(input, extent_type {})) {}
// List of `N` (where N >= 2), simply pass forward to the storage
template<
typename A,
typename B,
typename... Rest,
typename = enable_if_t<sizeof...(Rest) + 2 == extent_size<E>>>
KERNEL_FLOAT_INLINE vector(const A& a, const B& b, const Rest&... rest) :
storage_type {T(a), T(b), T(rest)...} {}
/**
* Returns the number of elements in this vector.
*/
KERNEL_FLOAT_INLINE
static constexpr size_t size() {
return extent_size<E>;
}
/**
* Returns a reference to the underlying storage type.
*/
KERNEL_FLOAT_INLINE
storage_type& storage() {
return *this;
}
/**
* Returns a reference to the underlying storage type.
*/
KERNEL_FLOAT_INLINE
const storage_type& storage() const {
return *this;
}
/**
* Returns an instance of the `extent_type` for this vector.
*/
KERNEL_FLOAT_INLINE
extent_type extent() const {
return {};
}
/**
* Returns a pointer to the underlying storage data.
*/
KERNEL_FLOAT_INLINE
T* data() {
return storage().data();
}
/**
* Returns a pointer to the underlying storage data.
*/
KERNEL_FLOAT_INLINE
const T* data() const {
return storage().data();
}
KERNEL_FLOAT_INLINE
const T* cdata() const {
return this->data();
}
/**
* Returns a reference to the item at index `i`.
*/
KERNEL_FLOAT_INLINE
T& at(size_t i) {
return *(this->data() + i);
}
/**
* Returns a constant reference to the item at index `i`.
*/
KERNEL_FLOAT_INLINE
const T& at(size_t i) const {
return *(this->data() + i);
}
/**
* Returns a reference to the item at index `i`.
*/
KERNEL_FLOAT_INLINE
T& operator[](size_t i) {
return at(i);
}
/**
* Returns a constant reference to the item at index `i`.
*/
KERNEL_FLOAT_INLINE
const T& operator[](size_t i) const {
return at(i);
}
KERNEL_FLOAT_INLINE
T& operator()(size_t i) {
return at(i);
}
KERNEL_FLOAT_INLINE
const T& operator()(size_t i) const {
return at(i);
}
/**
* Returns a pointer to the first element.
*/
KERNEL_FLOAT_INLINE
T* begin() {
return this->data();
}
/**
* Returns a pointer to the first element.
*/
KERNEL_FLOAT_INLINE
const T* begin() const {
return this->data();
}
/**
* Returns a pointer to the first element.
*/
KERNEL_FLOAT_INLINE
const T* cbegin() const {
return this->data();
}
/**
* Returns a pointer to one past the last element.
*/
KERNEL_FLOAT_INLINE
T* end() {
return this->data() + size();
}
/**
* Returns a pointer to one past the last element.
*/
KERNEL_FLOAT_INLINE
const T* end() const {
return this->data() + size();
}
/**
* Returns a pointer to one past the last element.
*/
KERNEL_FLOAT_INLINE
const T* cend() const {
return this->data() + size();
}
/**
* Copy the element at index `i`.
*/
KERNEL_FLOAT_INLINE
T get(size_t x) const {
return at(x);
}
/**
* Set the element at index `i`.
*/
KERNEL_FLOAT_INLINE
void set(size_t x, T value) {
at(x) = static_cast<T&&>(value);
}
/**
* Selects elements from the this vector based on the specified indices.
*
* Example
* =======
* ```
* vec<float, 6> input = {0, 10, 20, 30, 40, 50};
* vec<float, 4> vec1 = select(input, 0, 4, 4, 2); // [0, 40, 40, 20]
*
* vec<int, 4> indices = {0, 4, 4, 2};
* vec<float, 4> vec2 = select(input, indices); // [0, 40, 40, 20]
* ```
*/
template<typename... Is>
KERNEL_FLOAT_INLINE select_type<self_type, Is...> select(const Is&... indices) {
return kernel_float::select(*this, indices...);
}
/**
* Cast the elements of this vector to type `R` and returns a new vector.
*/
template<typename R, RoundingMode Mode = RoundingMode::ANY>
KERNEL_FLOAT_INLINE vector<R, extent_type> cast() const {
return kernel_float::cast<R, Mode>(*this);
}
/**
* Broadcast this vector into a new size `(Ns...)`.
*/
template<size_t... Ns>
KERNEL_FLOAT_INLINE vector<T, kernel_float::extent<Ns...>>
broadcast(kernel_float::extent<Ns...> new_size = {}) const {
return kernel_float::broadcast(*this, new_size);
}
/**
* Apply the given function `F` to each element of this vector and returns a new vector with the results.
*/
template<typename F>
KERNEL_FLOAT_INLINE vector<result_t<F, T>, E> map(F fun) const {
return kernel_float::map(fun, *this);
}
/**
* Reduce the elements of the given vector input into a single value using the function `F`.
*
* This function should be a binary function that takes two elements and returns one element. The order in which
* the elements are reduced is not specified and depends on the reduction function and the vector type.
*/
template<typename F>
KERNEL_FLOAT_INLINE T reduce(F fun) const {
return kernel_float::reduce(fun, *this);
}
/**
* Flattens the elements of this vector. For example, this turns a `vec<vec<int, 2>, 3>` into a `vec<int, 6>`.
*/
KERNEL_FLOAT_INLINE flatten_type<vector> flatten() const {
return kernel_float::flatten(*this);
}
/**
* Apply the given function `F` to each element of this vector.
*/
template<typename F>
KERNEL_FLOAT_INLINE void for_each(F fun) const {
return kernel_float::for_each(*this, fun);
}
/**
* Returns the result of `this + lhs * rhs`.
*
* The operation is performed using a single `kernel_float::fma` call, which may be faster then perform
* the addition and multiplication separately.
*/
template<
typename L,
typename R,
typename T2 = promote_t<T, vector_value_type<L>, vector_value_type<R>>,
typename E2 = broadcast_extent<E, vector_extent_type<L>, vector_extent_type<R>>>
KERNEL_FLOAT_INLINE vector<T2, E2> add_mul(const L& lhs, const R& rhs) const {
return ::kernel_float::fma(lhs, rhs, *this);
}
template<
typename L,
typename R,
typename T2 = promote_t<T, vector_value_type<L>, vector_value_type<R>>,
typename E2 = broadcast_extent<E, vector_extent_type<L>, vector_extent_type<R>>>
KERNEL_FLOAT_INLINE vector<T2, E2> fma(const L& lhs, const R& rhs) const {
return ::kernel_float::fma(lhs, rhs, *this);
}
};
/**
* Convert the given `input` into a vector. This function can perform one of the following actions:
*
* - For vectors `vec<T, N>`, it simply returns the original vector.
* - For primitive types `T` (e.g., `int`, `float`, `double`), it returns a `vec<T, 1>`.
* - For array-like types (e.g., `std::array<T, N>`, `T[N]`), it returns `vec<T, N>`.
* - For vector-like types (e.g., `int2`, `dim3`), it returns `vec<T, N>`.
*/
template<typename V>
KERNEL_FLOAT_INLINE into_vector_type<V> into_vec(V&& input) {
return into_vector_impl<V>::call(static_cast<V&&>(input));
}
template<typename T>
using scalar = vector<T, extent<1>>;
template<typename T, size_t N>
using vec = vector<T, extent<N>>;
// clang-format off
template<typename T> using vec1 = vec<T, 1>;
template<typename T> using vec2 = vec<T, 2>;
template<typename T> using vec3 = vec<T, 3>;
template<typename T> using vec4 = vec<T, 4>;
template<typename T> using vec5 = vec<T, 5>;
template<typename T> using vec6 = vec<T, 6>;
template<typename T> using vec7 = vec<T, 7>;
template<typename T> using vec8 = vec<T, 8>;
// clang-format on
#define KERNEL_FLOAT_VECTOR_ALIAS(NAME, T) \
template<size_t N> \
using v##NAME = vec<T, N>; \
using NAME##1 = vec<T, 1>; \
using NAME##2 = vec<T, 2>; \
using NAME##3 = vec<T, 3>; \
using NAME##4 = vec<T, 4>; \
using NAME##5 = vec<T, 5>; \
using NAME##6 = vec<T, 6>; \
using NAME##7 = vec<T, 7>; \
using NAME##8 = vec<T, 8>;
KERNEL_FLOAT_VECTOR_ALIAS(int, int)
KERNEL_FLOAT_VECTOR_ALIAS(float, float)
KERNEL_FLOAT_VECTOR_ALIAS(double, double)
/**
* Create a vector from a variable number of input values.
*
* The resulting vector type is determined by promoting the types of the input values into a common type.
* The number of input values determines the dimension of the resulting vector.
*
* Example
* =======
* ```
* auto v1 = make_vec(1.0f, 2.0f, 3.0f); // Creates a vec<float, 3> [1.0f, 2.0f, 3.0f]
* auto v2 = make_vec(1, 2, 3, 4); // Creates a vec<int, 4> [1, 2, 3, 4]
* ```
*/
template<typename... Args>
KERNEL_FLOAT_INLINE vec<promote_t<Args...>, sizeof...(Args)> make_vec(Args&&... args) {
using T = promote_t<Args...>;
return vector_storage<T, sizeof...(Args)> {T(args)...};
};
#if defined(__cpp_deduction_guides)
// Deduction guide for `vector`
template<typename... Args>
vector(Args&&... args) -> vector<promote_t<Args...>, extent<sizeof...(Args)>>;
#endif
} // namespace kernel_float
#endif