-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathmath_helper.h
291 lines (250 loc) · 8.6 KB
/
math_helper.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
/*******************************************************************************
//
// SYCL 2020 Conformance Test Suite
//
// Copyright (c) 2017-2022 Codeplay Software LTD. All Rights Reserved.
// Copyright (c) 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.
//
*******************************************************************************/
#ifndef __SYCLCTS_UTIL_MATH_HELPER_H
#define __SYCLCTS_UTIL_MATH_HELPER_H
#include <map>
#include <climits>
#include <sycl/sycl.hpp>
#include "../util/stl.h"
#include "./../oclmath/mt19937.h"
namespace sycl_cts {
/** math utility functions
*/
template <typename returnT> struct resultRef {
returnT res;
std::map<int, bool> undefined;
template <typename U>
resultRef(U res_t, std::map<int, bool> und_t)
: res(res_t), undefined(und_t) {}
template <typename U>
resultRef(U res_t, bool und_t)
: res(res_t), undefined({{0, und_t}}) {}
template <typename U> resultRef(U res_t) : res(res_t) {}
template <class U>
resultRef(const resultRef<U> &other)
: res(other.res), undefined(other.undefined) {}
};
namespace math {
/* Generic function for both scalar and vector types to
* return the number of elements in a type. */
int numElements(const float &);
/* Generic function for both scalar and vector types to
* return the number of elements in a type. */
int numElements(const int &);
/* Generic function for both scalar and vector types to
* return the number of elements in a type. */
template <typename T, int numElems>
int numElements(const sycl::vec<T, numElems> &) {
return numElems;
}
/* Generic function for both scalar and marray types to
* return the number of elements in a type. */
// FIXME: AdaptiveCpp does not support marray
#ifndef SYCL_CTS_COMPILING_WITH_ADAPTIVECPP
template <typename T, size_t numElems>
int numElements(const sycl::marray<T, numElems> &) {
return numElems;
}
#endif
/* Generic function for both scalar and vector types to
* extract an individual element. */
template <typename T>
T getElement(const T &f, int) {
return f;
}
/* Generic function for both scalar and vector types to
* extract an individual element. */
template <typename T, int dim>
T getElement(sycl::vec<T, dim> &f, int ix) {
return f[ix];
}
// FIXME: AdaptiveCpp does not support marray
#ifndef SYCL_CTS_COMPILING_WITH_ADAPTIVECPP
/* Generic function for both scalar and vector types to
* extract an individual element. */
template <typename T, size_t dim>
T getElement(sycl::marray<T, dim> &f, size_t ix) {
return f[ix];
}
#endif
template <typename T, int dim>
void setElement(sycl::vec<T, dim> &f, int ix, T value) {
f[ix] = value;
}
template <typename R, typename T, int N, typename funT, typename... Args>
sycl::vec<R, N> run_func_on_vector(funT fun, Args... args) {
sycl::vec<R, N> res;
for (int i = 0; i < N; i++) {
setElement<R, N>(res, i, fun(getElement(args, i)...));
}
return res;
}
// FIXME: AdaptiveCpp does not support marray
#ifndef SYCL_CTS_COMPILING_WITH_ADAPTIVECPP
template <typename R, typename T, size_t N, typename funT, typename... Args>
sycl::marray<R, N> run_func_on_marray(funT fun, Args... args) {
sycl::marray<R, N> res;
for (size_t i = 0; i < N; i++) {
res[i] = fun(getElement(args, i)...);
}
return res;
}
#endif
/* helper for relational functions where true result gives 1 for scalar
and -1 for vector argument types */
template <typename R, typename T, int N, typename funT, typename... Args>
sycl::vec<R, N> run_rel_func_on_vector(funT fun, Args... args) {
sycl::vec<R, N> res;
for (int i = 0; i < N; i++) {
if (fun(getElement<T, N>(args, i)...))
setElement<R, N>(res, i, -1);
else
setElement<R, N>(res, i, 0);
}
return res;
}
// FIXME: AdaptiveCpp does not support marray
#ifndef SYCL_CTS_COMPILING_WITH_ADAPTIVECPP
template <typename T, size_t N, typename funT, typename... Args>
sycl::marray<bool, N> run_rel_func_on_marray(funT fun, Args... args) {
sycl::marray<bool, N> res;
for (size_t i = 0; i < N; i++) {
res[i] = (fun(getElement<T, N>(args, i)...));
}
return res;
}
#endif
template <typename T, int N, typename funT, typename... Args>
sycl_cts::resultRef<sycl::vec<T, N>>
run_func_on_vector_result_ref(funT fun, Args... args) {
sycl::vec<T, N> res;
std::map<int, bool> undefined;
for (int i = 0; i < N; i++) {
undefined[i] = false;
resultRef<T> element = fun(getElement(args, i)...);
if (element.undefined.empty())
setElement<T, N>(res, i, element.res);
else
undefined[i] = true;
}
return sycl_cts::resultRef<sycl::vec<T, N>>(res, undefined);
}
// FIXME: AdaptiveCpp does not support marray
#ifndef SYCL_CTS_COMPILING_WITH_ADAPTIVECPP
template <typename T, size_t N, typename funT, typename... Args>
sycl_cts::resultRef<sycl::marray<T, N>> run_func_on_marray_result_ref(
funT fun, Args... args) {
sycl::marray<T, N> res;
std::map<int, bool> undefined;
for (size_t i = 0; i < N; i++) {
resultRef<T> element = fun(getElement(args, i)...);
if (element.undefined.empty())
res[i] = element.res;
else
undefined[i] = true;
}
return sycl_cts::resultRef<sycl::marray<T, N>>(res, undefined);
}
#endif
template <typename T>
struct rel_funcs_return;
#if SYCL_CTS_ENABLE_HALF_TESTS
template <>
struct rel_funcs_return<sycl::half> {
using type = int16_t;
};
#endif
template <>
struct rel_funcs_return<float> {
using type = int32_t;
};
template <>
struct rel_funcs_return<double> {
using type = int64_t;
};
template <template <class> class funT, typename T, typename... Args>
bool rel_func_dispatcher(T a, Args... args) {
return funT<T>()(a, args...);
}
template <template <class> class funT, typename T, int N, typename... Args>
typename sycl::vec<typename rel_funcs_return<T>::type, N>
rel_func_dispatcher(sycl::vec<T, N> a, Args... args) {
return run_rel_func_on_vector<typename rel_funcs_return<T>::type, T, N>(
funT<T>{}, a, args...);
}
// FIXME: AdaptiveCpp does not support marray
#ifndef SYCL_CTS_COMPILING_WITH_ADAPTIVECPP
template <template <class> class funT, typename T, size_t N, typename... Args>
sycl::marray<bool, N> rel_func_dispatcher(sycl::marray<T, N> a, Args... args) {
return run_rel_func_on_marray<T, N>(funT<T>{}, a, args...);
}
#endif
template <typename funT, typename T, typename... Args>
typename rel_funcs_return<T>::type rel_func_dispatcher(funT fun, T a,
Args... args) {
return fun(a, args...);
}
template <typename funT, typename T, int N, typename... Args>
typename sycl::vec<typename rel_funcs_return<T>::type, N>
rel_func_dispatcher(funT fun, sycl::vec<T, N> a, Args... args) {
return run_rel_func_on_vector<typename rel_funcs_return<T>::type, T, N>(
fun, a, args...);
}
// FIXME: AdaptiveCpp does not support marray
#ifndef SYCL_CTS_COMPILING_WITH_ADAPTIVECPP
template <typename funT, typename T, size_t N, typename... Args>
sycl::marray<bool, N> rel_func_dispatcher(funT fun, sycl::marray<T, N> a,
Args... args) {
return run_rel_func_on_marray<T, N>(fun, a, args...);
}
#endif
template <typename T>
int32_t num_bits(T) {
return int32_t(sizeof(T) * CHAR_BIT);
}
template <typename T> bool if_bit_set(T num, int bit) {
return (num >> bit) & 1;
}
template <typename T> bool if_msb_set(T x) {
return if_bit_set(x, num_bits(x) - 1);
}
/* cast an integer to a float */
float int_to_float(uint32_t x);
void fill(float &e, float v);
void fill(sycl::float2 &e, float v);
void fill(sycl::float3 &e, float v);
void fill(sycl::float4 &e, float v);
void fill(sycl::float8 &e, float v);
void fill(sycl::float16 &e, float v);
/* create random floats with an integer range [-0x7fffffff to 0x7fffffff]
*/
void rand(MTdata &rng, float *buf, int num);
void rand(MTdata &rng, sycl::float2 *buf, int num);
void rand(MTdata &rng, sycl::float3 *buf, int num);
void rand(MTdata &rng, sycl::float4 *buf, int num);
void rand(MTdata &rng, sycl::float8 *buf, int num);
void rand(MTdata &rng, sycl::float16 *buf, int num);
/* generate a stream of random integer data
*/
void rand(MTdata &rng, uint8_t *buf, int size);
} /* namespace math */
} /* namespace sycl_cts */
#endif // __SYCLCTS_UTIL_MATH_HELPER_H