forked from KhronosGroup/SYCL-CTS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
math_helper.cpp
98 lines (77 loc) · 2.78 KB
/
math_helper.cpp
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
/*******************************************************************************
//
// SYCL 1.2.1 Conformance Test Suite
//
// Copyright: (c) 2017 by Codeplay Software LTD. All Rights Reserved.
//
*******************************************************************************/
#include "math_helper.h"
namespace sycl_cts {
/** math utility functions
*/
namespace math {
/* cast an integer to a float */
float int_to_float(uint32_t x) {
static_assert(sizeof(x) == sizeof(float), "incompatible type sizes");
return *reinterpret_cast<float *>(&x);
}
void fill(float &e, float v) { e = v; }
void fill(cl::sycl::float2 &e, float v) {
e.x() = v;
e.y() = v;
}
void fill(cl::sycl::float3 &e, float v) { e.x() = e.y() = e.z() = v; }
void fill(cl::sycl::float4 &e, float v) { e.x() = e.y() = e.z() = e.w() = v; }
void fill(cl::sycl::float8 &e, float v) {
e.s0() = e.s1() = e.s2() = e.s3() = v;
e.s4() = e.s5() = e.s6() = e.s7() = v;
}
void fill(cl::sycl::float16 &e, float v) {
e.s0() = e.s1() = e.s2() = e.s3() = v;
e.s4() = e.s5() = e.s6() = e.s7() = v;
e.s8() = e.s9() = e.sA() = e.sB() = v;
e.sC() = e.sD() = e.sE() = e.sF() = v;
}
/* return number of elements in a type */
int numElements(const float &) { return 1; }
/* return number of elements in a type */
int numElements(const int &) { return 1; }
/* extract an individual elements of a float type */
float getElement(const float &f, int ix) { return f; }
/* extract an individual elements of an int type */
int getElement(const int &f, int ix) { return f; }
/* create random floats with full integer range */
void rand(MTdata &rng, float *buf, int num) {
for (int i = 0; i < num; i++) buf[i] = (float)int32_t(genrand_int32(rng));
}
void rand(MTdata &rng, cl::sycl::float2 *buf, int num) {
const int nDim = int(sizeof(cl::sycl::float2) / sizeof(float));
rand(rng, (float *)buf, num * nDim);
}
void rand(MTdata &rng, cl::sycl::float3 *buf, int num) {
const int nDim = int(sizeof(cl::sycl::float3) / sizeof(float));
rand(rng, (float *)buf, num * nDim);
}
void rand(MTdata &rng, cl::sycl::float4 *buf, int num) {
const int nDim = int(sizeof(cl::sycl::float4) / sizeof(float));
rand(rng, (float *)buf, num * nDim);
}
void rand(MTdata &rng, cl::sycl::float8 *buf, int num) {
const int nDim = int(sizeof(cl::sycl::float8) / sizeof(float));
rand(rng, (float *)buf, num * nDim);
}
void rand(MTdata &rng, cl::sycl::float16 *buf, int num) {
const int nDim = int(sizeof(cl::sycl::float16) / sizeof(float));
rand(rng, (float *)buf, num * nDim);
}
/* generate a stream of random integer data */
void rand(MTdata &rng, uint8_t *buf, int size) {
uint32_t r = 0;
for (int i = 0; i < size; i++) {
if ((i % 4) == 0) r = genrand_int32(rng);
buf[i] = r & 0xff;
r >>= 8;
}
}
} /* namespace math */
} /* namespace sycl_cts */