forked from KhronosGroup/SYCL-CTS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalue_operations.h
322 lines (285 loc) · 11.3 KB
/
value_operations.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
/*******************************************************************************
//
// SYCL 2020 Conformance Test Suite
//
// Copyright (c) 2017-2022 Codeplay Software LTD. All Rights Reserved.
// Copyright (c) 2020-2022 The Khronos Group Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// This file contains helper functions for modifying and comparing values,
// arrays, and objects that implements operator[]
//
*******************************************************************************/
#ifndef __SYCLCTS_TESTS_COMMON_VALUE_OPERATIONS_H
#define __SYCLCTS_TESTS_COMMON_VALUE_OPERATIONS_H
#include "../../util/type_traits.h"
#include <cassert>
#include <tuple>
#include <utility>
#include <variant>
namespace value_operations {
namespace detail {
template <typename T, size_t N>
using ArrayT = T[N];
/**
* @brief Helper function assigns value in general case
* and assigns true if value is even number and false otherwise
* in case of bool types
*
* @tparam T Type of variable for assignment
* @tparam U Type of right operand for assignment operation
* @param left Variable for assignment
* @param right New value to assign
*/
template <typename T, typename U>
void assign_value_or_even(T& left, const U& right) {
if constexpr ((std::is_same_v<T, bool> ||
std::is_same_v<
T, std::optional<bool>>)&&(!std::is_same_v<U, bool> &&
!std::is_same_v<
U, std::optional<bool>>))
left = (right % 2 != 0);
else
left = right;
}
template <typename T, typename U,
typename = std::enable_if_t<std::is_integral_v<U>>>
void assign_value_or_even(T*& left, const U& right) {
left = reinterpret_cast<T*>(right);
}
/**
* @brief Helper function checks if values are equal in general case
* and checks if right operand is even number in case of bool types
*
* @tparam T Type of left operand for comparison
* @tparam U Type of right operand for comparison
* @param left Left operand for comparison
* @param right Right operand for comparison
*/
template <typename T, typename U>
bool are_equal_value_or_even(const T& left, const U& right) {
if constexpr ((std::is_same_v<T, bool> ||
std::is_same_v<
T, std::optional<bool>>)&&(!std::is_same_v<U, bool> &&
!std::is_same_v<
U, std::optional<bool>>))
return left == (right % 2 != 0);
else
return left == right;
}
/**
* @brief Function allows to make assignment operations to elements of the
* container using std::get and std::index_sequence
*
* @tparam ContainerT Type of container for assignment
* @tparam U Right operand for assignment operation
* @tparam I Indexes for assignment
* @param left Container for assignment
* @param right New value to assign
*/
template <typename ContainerT, typename U, std::size_t... I>
void assign_by_index_sequence(ContainerT& left, const U& right,
std::index_sequence<I...>) {
((assign_value_or_even(std::get<I>(left), right)), ...);
}
/**
* @brief Function allows to compare elements of the
* container using std::get and std::index_sequence
*
* @tparam ContainerT Type of container for assignment
* @tparam U Right operand for assignment operation
* @tparam I Indexes for assignment
* @param left Left operand of comparison
* @param right Right operand of comparison
* @return Function returns true if all elements of the left operand are equal
* to the right operand. False otherwise
*/
template <typename ContainerT, typename U, std::size_t... I>
bool are_equal_by_index_sequence(const ContainerT& left, const U& right,
std::index_sequence<I...>) {
bool result = true;
((result &= are_equal_value_or_even(std::get<I>(left), right)), ...);
return result;
}
template <typename T>
struct is_array : std::false_type {};
template <typename T, size_t N>
struct is_array<ArrayT<T, N>> : std::true_type {};
template <typename T>
constexpr bool is_array_v = is_array<T>::value;
template <typename T>
struct is_non_array_with_subscript_and_size {
static constexpr bool value =
!is_array<T>::value && has_subscript_and_size_v<T>;
};
template <typename T>
constexpr bool is_non_array_with_subscript_and_size_v =
is_non_array_with_subscript_and_size<T>::value;
} // namespace detail
// Modify functions
template <typename T, size_t N, typename RightNonArrT = T>
inline void assign(detail::ArrayT<T, N>& left, const RightNonArrT& right) {
for (size_t i = 0; i < N; ++i) {
detail::assign_value_or_even(left[i], right);
}
}
template <typename LeftArrT, size_t LeftArrN, typename RightArrT,
size_t RightArrN>
inline void assign(detail::ArrayT<LeftArrT, LeftArrN>& left,
const detail::ArrayT<RightArrT, RightArrN>& right) {
static_assert(LeftArrN == RightArrN, "Arrays have to be the same size");
for (size_t i = 0; i < LeftArrN; ++i) {
detail::assign_value_or_even(left[i], right[i]);
}
}
template <typename LeftArrT, typename RightNonArrT>
inline typename std::enable_if_t<
detail::is_non_array_with_subscript_and_size_v<LeftArrT> &&
!has_subscript_and_size_v<RightNonArrT>>
assign(LeftArrT& left, const RightNonArrT& right) {
for (size_t i = 0; i < left.size(); ++i) {
detail::assign_value_or_even(left[i], right);
}
}
template <typename LeftArrT, typename RightArrT>
inline typename std::enable_if_t<
detail::is_non_array_with_subscript_and_size_v<LeftArrT> &&
detail::is_non_array_with_subscript_and_size_v<RightArrT>>
assign(LeftArrT& left, const RightArrT& right) {
assert((left.size() == right.size()) && "Arrays have to be the same size");
for (size_t i = 0; i < left.size(); ++i) {
detail::assign_value_or_even(left[i], right[i]);
}
}
template <typename LeftNonArrT, typename RightNonArrT = LeftNonArrT>
inline typename std::enable_if_t<!has_subscript_and_size_v<LeftNonArrT> &&
!has_subscript_and_size_v<RightNonArrT>>
assign(LeftNonArrT& left, const RightNonArrT& right) {
detail::assign_value_or_even(left, right);
}
template <typename... Types, typename U>
void assign(std::tuple<Types...>& left, const U& right) {
using tuple_t = std::remove_reference_t<decltype(left)>;
using indexes = std::make_index_sequence<std::tuple_size<tuple_t>::value>;
detail::assign_by_index_sequence(left, right, indexes());
}
template <typename FirstT, typename SecondT = FirstT, typename U>
void assign(std::pair<FirstT, SecondT>& left, const U& right) {
constexpr size_t pair_size = 2;
using indexes = std::make_index_sequence<pair_size>;
detail::assign_by_index_sequence(left, right, indexes());
}
#if defined(SYCL_EXT_ONEAPI_PROPERTIES) && \
defined(SYCL_EXT_ONEAPI_DEVICE_GLOBAL)
template <typename T, typename Props, typename RightNonArrT>
void assign(sycl::ext::oneapi::experimental::device_global<T, Props>& left,
const RightNonArrT& right) {
assign(left.get(), right);
}
#endif
/////////////////////////// Modify functions
// Compare functions
template <typename T, size_t N>
inline bool are_equal(const detail::ArrayT<T, N>& left, const T& right) {
for (size_t i = 0; i < N; ++i) {
if (!detail::are_equal_value_or_even(left[i], right)) return false;
}
return true;
}
template <typename LeftArrT, size_t LeftArrN, typename RightArrT,
size_t RightArrN>
inline bool are_equal(const detail::ArrayT<LeftArrT, LeftArrN>& left,
const detail::ArrayT<RightArrT, RightArrN>& right) {
static_assert(LeftArrN == RightArrN, "Arrays have to be the same size");
for (size_t i = 0; i < LeftArrN; ++i) {
if (!detail::are_equal_value_or_even(left[i], right[i])) return false;
}
return true;
}
template <typename LeftArrT, typename RightNonArrT>
inline typename std::enable_if_t<
detail::is_non_array_with_subscript_and_size_v<LeftArrT> &&
!has_subscript_and_size_v<RightNonArrT>,
bool>
are_equal(const LeftArrT& left, const RightNonArrT& right) {
for (size_t i = 0; i < left.size(); ++i) {
if (!detail::are_equal_value_or_even(left[i], right)) return false;
}
return true;
}
template <typename LeftArrT, typename RightArrT>
inline typename std::enable_if_t<
detail::is_non_array_with_subscript_and_size_v<LeftArrT> &&
detail::is_non_array_with_subscript_and_size_v<RightArrT>,
bool>
are_equal(const LeftArrT& left, const RightArrT& right) {
assert((left.size() == right.size()) && "Arrays have to be the same size");
for (size_t i = 0; i < left.size(); ++i) {
if (!detail::are_equal_value_or_even(left[i], right[i])) return false;
}
return true;
}
template <typename LeftNonArrT, typename RightNonArrT = LeftNonArrT>
inline typename std::enable_if_t<!has_subscript_and_size_v<LeftNonArrT> &&
!has_subscript_and_size_v<RightNonArrT>,
bool>
are_equal(const LeftNonArrT& left, const RightNonArrT& right) {
return detail::are_equal_value_or_even(left, right);
}
template <typename... Types, typename U>
std::enable_if_t<!std::is_same_v<std::tuple<Types...>, U>, bool> are_equal(
const std::tuple<Types...>& left, const U& right) {
using tuple_t = std::remove_reference_t<decltype(left)>;
using indexes = std::make_index_sequence<std::tuple_size<tuple_t>::value>;
return detail::are_equal_by_index_sequence(left, right, indexes());
}
template <typename FirstT, typename SecondT = FirstT, typename U>
typename std::enable_if_t<!std::is_same_v<std::pair<FirstT, SecondT>, U>, bool>
are_equal(const std::pair<FirstT, SecondT>& left, const U& right) {
constexpr size_t pair_size = 2;
using indexes = std::make_index_sequence<pair_size>;
return detail::are_equal_by_index_sequence(left, right, indexes());
}
template <typename... Types, typename U>
typename std::enable_if_t<!std::is_same_v<std::variant<Types...>, U>, bool>
are_equal(const std::variant<Types...>& left, const U& right) {
return std::get<U>(left) == right;
}
#if defined(SYCL_EXT_ONEAPI_PROPERTIES) && \
defined(SYCL_EXT_ONEAPI_DEVICE_GLOBAL)
template <typename T, typename Props, typename RightT>
bool are_equal(
const sycl::ext::oneapi::experimental::device_global<T, Props>& left,
const RightT& right) {
return are_equal(left.get(), right);
}
template <typename LeftT, typename LeftProps, typename RightT,
typename RightProps>
bool are_equal(
const sycl::ext::oneapi::experimental::device_global<LeftT, LeftProps>&
left,
const sycl::ext::oneapi::experimental::device_global<RightT, RightProps>&
right) {
return are_equal(left.get(), right.get());
}
#endif
//////////////////////////// Compare functions
template <typename T>
inline constexpr auto init(int val) {
std::remove_const_t<T> data;
assign(data, val);
return data;
}
} // namespace value_operations
#endif //__SYCLCTS_TESTS_COMMON_VALUE_OPERATIONS_H