forked from intel/llvm-test-suite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbit_cast.cpp
More file actions
86 lines (70 loc) · 2.76 KB
/
Copy pathbit_cast.cpp
File metadata and controls
86 lines (70 loc) · 2.76 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
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
// RUN: %HOST_RUN_PLACEHOLDER %t.out
// RUN: %CPU_RUN_PLACEHOLDER %t.out
// RUN: %GPU_RUN_PLACEHOLDER %t.out
// RUN: %ACC_RUN_PLACEHOLDER %t.out
// UNSUPPORTED: esimd_emulator
#include <CL/sycl.hpp>
#include <iostream>
#include <math.h>
#include <type_traits>
constexpr cl::sycl::access::mode sycl_write = cl::sycl::access::mode::write;
template <typename To, typename From> class BitCastKernel;
template <typename To, typename From> To doBitCast(const From &ValueToConvert) {
std::vector<To> Vec(1);
{
sycl::buffer<To, 1> Buf(Vec.data(), 1);
sycl::queue Queue;
Queue.submit([&](sycl::handler &cgh) {
auto acc = Buf.template get_access<sycl_write>(cgh);
cgh.single_task<class BitCastKernel<To, From>>([=]() {
acc[0] = sycl::bit_cast<To>(ValueToConvert);
});
});
}
return Vec[0];
}
template <typename To, typename From> int test(const From &Value) {
auto ValueConvertedTwoTimes = doBitCast<From>(doBitCast<To>(Value));
bool isOriginalValueEqualsToConvertedTwoTimes = false;
if (std::is_integral<From>::value) {
isOriginalValueEqualsToConvertedTwoTimes = Value == ValueConvertedTwoTimes;
} else if ((std::is_floating_point<From>::value) ||
std::is_same<From, cl::sycl::half>::value) {
static const float Epsilon = 0.0000001f;
isOriginalValueEqualsToConvertedTwoTimes =
fabs(Value - ValueConvertedTwoTimes) < Epsilon;
} else {
std::cerr << "Type " << typeid(From).name()
<< " neither integral nor floating point nor cl::sycl::half\n";
return 1;
}
if (!isOriginalValueEqualsToConvertedTwoTimes) {
std::cerr << "FAIL: Original value which is " << Value
<< " != value converted two times which is "
<< ValueConvertedTwoTimes << "\n";
return 1;
}
std::cout << "PASS\n";
return 0;
}
int main() {
int ReturnCode = 0;
std::cout << "cl::sycl::half to unsigned short ...\n";
ReturnCode += test<unsigned short>(cl::sycl::half(1.0f));
std::cout << "unsigned short to cl::sycl::half ...\n";
ReturnCode += test<cl::sycl::half>(static_cast<unsigned short>(16384));
std::cout << "cl::sycl::half to short ...\n";
ReturnCode += test<short>(cl::sycl::half(1.0f));
std::cout << "short to cl::sycl::half ...\n";
ReturnCode += test<cl::sycl::half>(static_cast<short>(16384));
std::cout << "int to float ...\n";
ReturnCode += test<float>(static_cast<int>(2));
std::cout << "float to int ...\n";
ReturnCode += test<int>(static_cast<float>(-2.4f));
std::cout << "unsigned int to float ...\n";
ReturnCode += test<float>(static_cast<unsigned int>(6));
std::cout << "float to unsigned int ...\n";
ReturnCode += test<unsigned int>(static_cast<float>(-2.4f));
return ReturnCode;
}