-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathtype_traits.h
426 lines (332 loc) · 13.1 KB
/
type_traits.h
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*******************************************************************************
//
// SYCL 2020 Conformance Test Suite
//
// Common type traits support
//
*******************************************************************************/
#ifndef __SYCLCTS_UTIL_TYPE_TRAITS_H
#define __SYCLCTS_UTIL_TYPE_TRAITS_H
#include <climits>
#include <sycl/sycl.hpp>
#include <type_traits>
namespace {
// Common type traits functions
template <typename... T>
struct contains;
template <typename T>
struct contains<T> : std::false_type {};
/**
* @brief Verify type is within the list of types
*/
template <typename T, typename headT, typename... tailT>
struct contains<T, headT, tailT...>
: std::conditional<std::is_same<T, headT>::value, std::true_type,
contains<T, tailT...>>::type {};
/**
* @brief Verify type has the given number of bits
*/
template <typename T, size_t bits>
using bits_eq = std::bool_constant<(sizeof(T) * CHAR_BIT) == bits>;
// Specific type traits functions
/**
* @brief Verify type is within the list of types with atomic support
* Note that different implementation-defined aliases can actually fall
* into this set
*/
template <typename T>
using has_atomic_support = contains<T, int, unsigned int, long, unsigned long,
long long, unsigned long long, float>;
/**
* @brief Checks whether T is a floating-point sycl type
*/
template <typename T>
using is_sycl_scalar_floating_point =
#if SYCL_CTS_ENABLE_HALF_TESTS
std::bool_constant<std::is_floating_point_v<T> ||
std::is_same_v<std::remove_cv_t<T>, sycl::half>>;
#else
std::is_floating_point<T>;
#endif
template <typename T>
inline constexpr bool is_sycl_scalar_floating_point_v{
is_sycl_scalar_floating_point<T>::value};
template <typename T>
using is_nonconst_rvalue_reference =
std::bool_constant<std::is_rvalue_reference_v<T> &&
!std::is_const_v<typename std::remove_reference_t<T>>>;
template <typename T>
inline constexpr bool is_nonconst_rvalue_reference_v{
is_nonconst_rvalue_reference<T>::value};
namespace has_static_member {
template <typename, typename = void>
struct to_string : std::false_type {};
template <typename T>
struct to_string<T, std::void_t<decltype(T::to_string())>> : std::true_type {};
} // namespace has_static_member
/**
* @brief Verify \c T has subscript subscript operator
*/
template <typename T, typename = void>
struct has_subscript_operator : std::false_type {};
template <typename T>
struct has_subscript_operator<
T, std::void_t<decltype(std::declval<T>()[std::declval<size_t>()])>>
: std::true_type {};
/**
* @brief Shortcut for has_subscript_operator::type
*/
template <typename T>
using has_subscript_operator_t = typename has_subscript_operator<T>::type;
/**
* @brief Shortcut for has_subscript_operator::value
*/
template <typename T>
inline constexpr bool has_subscript_operator_v =
has_subscript_operator_t<T>::value;
/**
* @brief Verify \c T has implemented operator*()
*/
template <typename T, typename = void>
struct is_dereferenceable : std::false_type {};
template <typename T>
struct is_dereferenceable<T, std::void_t<decltype(*std::declval<T>())>>
: std::true_type {};
/**
* @brief Shortcut for is_dereferenceable::value
*/
template <typename T>
inline constexpr bool is_dereferenceable_v = is_dereferenceable<T>::value;
/**
* @brief Verify \c T has size member function
*/
template <typename T, typename = void>
struct has_size : std::false_type {};
template <typename T>
struct has_size<T, std::void_t<decltype(std::declval<T>().size())>>
: std::true_type {};
/**
* @brief Shortcut for has_size::type
*/
template <typename T>
using has_size_t = typename has_size<T>::type;
/**
* @brief Shortcut for has_size::value
*/
template <typename T>
constexpr inline bool has_size_v = has_size_t<T>::value;
/**
* @brief Verify \c T has both subscript operator and size member function
*/
template <typename T>
using has_subscript_and_size =
std::conjunction<has_subscript_operator<T>, has_size<T>>;
/**
* @brief Shortcut for has_subscript_and_size::value
*/
template <typename T>
constexpr inline bool has_subscript_and_size_v =
has_subscript_and_size<T>::value;
} // namespace
// type_traits for specifying that provided type have one of the following
// things:
// - Some member types (e.g. value_type or difference_type)
// - Implemented operators (e.g. operator++(), operator+=(), operator>())
namespace type_traits {
// Provide code to verify that provided datatype has different fields
namespace has_field {
template <typename T, typename = void>
struct value_type : std::false_type {};
template <typename T>
struct value_type<T, std::void_t<typename std::iterator_traits<T>::value_type>>
: std::true_type {};
template <typename T>
inline constexpr bool value_type_v = value_type<T>::value;
template <typename T, typename = void>
struct difference_type : std::false_type {};
template <typename T>
struct difference_type<
T, std::void_t<typename std::iterator_traits<T>::difference_type>>
: std::true_type {};
template <typename T>
inline constexpr bool difference_type_v = difference_type<T>::value;
template <typename T, typename = void>
struct reference : std::false_type {};
template <typename T>
struct reference<T, std::void_t<typename std::iterator_traits<T>::reference>>
: std::true_type {};
template <typename T>
inline constexpr bool reference_v = reference<T>::value;
template <typename T, typename = void>
struct pointer : std::false_type {};
template <typename T>
struct pointer<T, std::void_t<typename std::iterator_traits<T>::pointer>>
: std::true_type {};
template <typename T>
inline constexpr bool pointer_v = pointer<T>::value;
template <typename T, typename = void>
struct iterator_category : std::false_type {};
template <typename T>
struct iterator_category<T,
std::void_t<typename std::iterator_traits<T>::pointer>>
: std::true_type {};
template <typename T>
inline constexpr bool iterator_category_v = iterator_category<T>::value;
} // namespace has_field
// Provide code to verify that provided datatype has compound assignment
namespace compound_assignment {
template <typename T, typename RightOperandT = int, typename = void>
struct addition : std::false_type {};
template <typename T, typename RightOperandT>
struct addition<
T, RightOperandT,
// Need reference here to handle rval of native C++ types like int
std::void_t<decltype(std::declval<T&>() += std::declval<RightOperandT>())>>
: std::true_type {};
template <typename T, typename RightOperandT = int>
inline constexpr bool addition_v = addition<T, RightOperandT>::value;
template <typename T, typename RightOperandT = int, typename = void>
struct subtraction : std::false_type {};
template <typename T, typename RightOperandT>
struct subtraction<
T, RightOperandT,
// Need reference here to handle rval of native C++ types like int
std::void_t<decltype(std::declval<T&>() -= std::declval<RightOperandT>())>>
: std::true_type {};
template <typename T, typename RightOperandT = int>
inline constexpr bool subtraction_v = subtraction<T>::value;
} // namespace compound_assignment
// Provide code to verify that provided datatype has arithmetic operators
namespace has_arithmetic {
template <typename LeftOperand, typename RightOperand, typename = void>
struct addition : std::false_type {};
template <typename LeftOperand, typename RightOperand>
struct addition<LeftOperand, RightOperand,
std::void_t<decltype(std::declval<LeftOperand>() +
std::declval<RightOperand>())>>
: std::true_type {};
template <typename LeftOperand, typename RightOperand>
inline constexpr bool addition_v = addition<LeftOperand, RightOperand>::value;
template <typename LeftOperand, typename RightOperand, typename = void>
struct subtraction : std::false_type {};
template <typename LeftOperand, typename RightOperand>
struct subtraction<LeftOperand, RightOperand,
std::void_t<decltype(std::declval<LeftOperand>() -
std::declval<RightOperand>())>>
: std::true_type {};
template <typename LeftOperand, typename RightOperand>
inline constexpr bool subtraction_v =
subtraction<LeftOperand, RightOperand>::value;
template <typename T, typename = void>
struct pre_increment : std::false_type {};
template <typename T>
// Need reference here to handle rval of native C++ types like int
struct pre_increment<T, std::void_t<decltype(++std::declval<T&>())>>
: std::true_type {};
template <typename T>
inline constexpr bool pre_increment_v = pre_increment<T>::value;
template <typename T, typename = void>
struct post_increment : std::false_type {};
template <typename T>
// Need reference here to handle rval of native C++ types like int
struct post_increment<T, std::void_t<decltype(std::declval<T&>()++)>>
: std::true_type {};
template <typename T>
inline constexpr bool post_increment_v = post_increment<T>::value;
template <typename T, typename = void>
struct post_decrement : std::false_type {};
template <typename T>
// Need reference here to handle rval of native C++ types like int
struct post_decrement<T, std::void_t<decltype(std::declval<T&>()--)>>
: std::true_type {};
template <typename T>
inline constexpr bool post_decrement_v = post_decrement<T>::value;
template <typename T, typename = void>
struct pre_decrement : std::false_type {};
template <typename T>
// Need reference here to handle rval of native C++ types like int
struct pre_decrement<T, std::void_t<decltype(--std::declval<T&>())>>
: std::true_type {};
template <typename T>
inline constexpr bool pre_decrement_v = pre_decrement<T>::value;
} // namespace has_arithmetic
// Provide code to verify that provided datatype has comparison operators
namespace has_comparison {
template <typename T, typename = void>
struct is_equal : std::false_type {};
template <typename T>
struct is_equal<T,
std::void_t<decltype(std::declval<T>() == std::declval<T>())>>
: std::true_type {};
template <typename T>
inline constexpr bool is_equal_v = is_equal<T>::value;
template <typename T, typename = void>
struct not_equal : std::false_type {};
template <typename T>
struct not_equal<T,
std::void_t<decltype(std::declval<T>() != std::declval<T>())>>
: std::true_type {};
template <typename T>
inline constexpr bool not_equal_v = not_equal<T>::value;
template <typename T, typename = void>
struct greater_than : std::false_type {};
template <typename T>
struct greater_than<
T, std::void_t<decltype(std::declval<T>() > std::declval<T>())>>
: std::true_type {};
template <typename T>
inline constexpr bool greater_than_v = greater_than<T>::value;
template <typename T, typename = void>
struct less_than : std::false_type {};
template <typename T>
struct less_than<T,
std::void_t<decltype(std::declval<T>() < std::declval<T>())>>
: std::true_type {};
template <typename T>
inline constexpr bool less_than_v = less_than<T>::value;
template <typename T, typename = void>
struct greater_or_equal : std::false_type {};
template <typename T>
struct greater_or_equal<
T, std::void_t<decltype(std::declval<T>() >= std::declval<T>())>>
: std::true_type {};
template <typename T>
inline constexpr bool greater_or_equal_v = greater_or_equal<T>::value;
template <typename T, typename = void>
struct less_or_equal : std::false_type {};
template <typename T>
struct less_or_equal<
T, std::void_t<decltype(std::declval<T>() <= std::declval<T>())>>
: std::true_type {};
template <typename T>
inline constexpr bool less_or_equal_v = less_or_equal<T>::value;
} // namespace has_comparison
namespace group_algorithms {
/**
Checks whether \p T and \p OperatorT form a valid SYCL operator. */
template <typename T, typename OperatorT>
using is_legal_operator = std::bool_constant<
(std::is_same_v<OperatorT, sycl::plus<T>> && std::is_arithmetic_v<T>) ||
(std::is_same_v<OperatorT, sycl::multiplies<T>> &&
std::is_arithmetic_v<T>) ||
(std::is_same_v<OperatorT, sycl::bit_and<T>> && std::is_integral_v<T>) ||
(std::is_same_v<OperatorT, sycl::bit_or<T>> && std::is_integral_v<T>) ||
(std::is_same_v<OperatorT, sycl::bit_xor<T>> && std::is_integral_v<T>) ||
(std::is_same_v<OperatorT, sycl::logical_and<T>> &&
std::is_same_v<std::remove_cv_t<T>, bool>) ||
(std::is_same_v<OperatorT, sycl::logical_or<T>> &&
std::is_same_v<std::remove_cv_t<T>, bool>) ||
(std::is_same_v<OperatorT, sycl::minimum<T>> && std::is_integral_v<T>) ||
(std::is_same_v<OperatorT, sycl::minimum<T>> &&
is_sycl_scalar_floating_point_v<T>) ||
(std::is_same_v<OperatorT, sycl::maximum<T>> && std::is_integral_v<T>) ||
(std::is_same_v<OperatorT, sycl::maximum<T>> &&
is_sycl_scalar_floating_point_v<T>)>;
/**
Checks whether \p T and \p OperatorT form a valid SYCL operator. */
template <typename T, typename OperatorT>
inline constexpr bool is_legal_operator_v{
is_legal_operator<T, OperatorT>::value};
} // namespace group_algorithms
} // namespace type_traits
#endif // __SYCLCTS_UTIL_TYPE_TRAITS_H