forked from intel/llvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvec_convert.cpp
More file actions
155 lines (141 loc) · 6.05 KB
/
Copy pathvec_convert.cpp
File metadata and controls
155 lines (141 loc) · 6.05 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
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
// XFAIL: cuda
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
// RUN: env SYCL_DEVICE_TYPE=HOST %t.out
// RUN: %CPU_RUN_PLACEHOLDER %t.out
// RUN: %GPU_RUN_PLACEHOLDER %t.out
// RUN: %ACC_RUN_PLACEHOLDER %t.out
//==------------ vec_convert.cpp - SYCL vec class convert method test ------==//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include <CL/sycl.hpp>
#include <cassert>
// TODO uncomment run lines on non-host devices when the rounding modes will
// be implemented.
// TODO make the test to pass on cuda
using namespace cl::sycl;
template <typename T, typename convertT, int roundingMode> class kernel_name;
template <int N> struct helper;
template <> struct helper<0> {
template <typename T, int NumElements>
static void compare(const vec<T, NumElements> &x,
const vec<T, NumElements> &y) {
const T xs = x.template swizzle<0>();
const T ys = y.template swizzle<0>();
assert(xs == ys);
}
};
template <int N> struct helper {
template <typename T, int NumElements>
static void compare(const vec<T, NumElements> &x,
const vec<T, NumElements> &y) {
const T xs = x.template swizzle<N>();
const T ys = y.template swizzle<N>();
helper<N - 1>::compare(x, y);
assert(xs == ys);
}
};
template <typename T, typename convertT, int NumElements,
rounding_mode roundingMode>
void test(const vec<T, NumElements> &ToConvert,
const vec<convertT, NumElements> &Expected) {
vec<convertT, NumElements> Converted{0};
{
buffer<vec<convertT, NumElements>, 1> Buffer{&Converted, range<1>{1}};
queue Queue;
Queue.submit([&](handler &CGH) {
accessor<vec<convertT, NumElements>, 1, access::mode::write> Accessor(
Buffer, CGH);
CGH.single_task<class kernel_name<T, convertT, static_cast<int>(roundingMode)>>([=]() {
Accessor[0] = ToConvert.template convert<convertT, roundingMode>();
});
});
}
helper<NumElements - 1>::compare(Converted, Expected);
}
int main() {
// automatic
test<int, int, 8, rounding_mode::automatic>(
int8{2, 3, 3, -2, -3, -3, 0, 0},
int8{2, 3, 3, -2, -3, -3, 0, 0});
test<float, int, 8, rounding_mode::automatic>(
float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f},
int8{2, 2, 3, -2, -2, -3, 0, 0});
test<int, float, 8, rounding_mode::automatic>(
int8{2, 3, 3, -2, -3, -3, 0, 0},
float8{2.f, 3.f, 3.f, -2.f, -3.f, -3.f, 0.f, 0.f});
test<float, float, 8, rounding_mode::automatic>(
float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f},
float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f});
test<float, half, 8, rounding_mode::automatic>(
float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f},
half8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f});
// rte
test<int, int, 8, rounding_mode::rte>(
int8{2, 3, 3, -2, -3, -3, 0, 0},
int8{2, 3, 3, -2, -3, -3, 0, 0});
test<float, int, 8, rounding_mode::rte>(
float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f},
int8{2, 2, 3, -2, -2, -3, 0, 0});
test<int, float, 8, rounding_mode::rte>(
int8{2, 3, 3, -2, -3, -3, 0, 0},
float8{2.f, 3.f, 3.f, -2.f, -3.f, -3.f, 0.f, 0.f});
test<float, float, 8, rounding_mode::rte>(
float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f},
float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f});
test<float, half, 8, rounding_mode::rte>(
float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f},
half8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f});
// rtz
test<int, int, 8, rounding_mode::rtz>(
int8{2, 3, 3, -2, -3, -3, 0, 0},
int8{2, 3, 3, -2, -3, -3, 0, 0});
test<float, int, 8, rounding_mode::rtz>(
float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f},
int8{2, 2, 2, -2, -2, -2, 0, 0});
test<int, float, 8, rounding_mode::rtz>(
int8{2, 3, 3, -2, -3, -3, 0, 0},
float8{2.f, 3.f, 3.f, -2.f, -3.f, -3.f, 0.f, 0.f});
test<float, float, 8, rounding_mode::rtz>(
float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f},
float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f});
test<float, half, 8, rounding_mode::rtz>(
float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f},
half8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f});
// rtp
test<int, int, 8, rounding_mode::rtp>(
int8{2, 3, 3, -2, -3, -3, 0, 0},
int8{2, 3, 3, -2, -3, -3, 0, 0});
test<float, int, 8, rounding_mode::rtp>(
float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f},
int8{3, 3, 3, -2, -2, -2, 0, 0});
test<int, float, 8, rounding_mode::rtp>(
int8{2, 3, 3, -2, -3, -3, 0, 0},
float8{2.f, 3.f, 3.f, -2.f, -3.f, -3.f, 0.f, 0.f});
test<float, float, 8, rounding_mode::rtp>(
float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f},
float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f});
test<float, half, 8, rounding_mode::rtp>(
float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f},
half8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f});
// rtn
test<int, int, 8, rounding_mode::rtn>(
int8{2, 3, 3, -2, -3, -3, 0, 0},
int8{2, 3, 3, -2, -3, -3, 0, 0});
test<float, int, 8, rounding_mode::rtn>(
float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f},
int8{2, 2, 2, -3, -3, -3, 0, 0});
test<int, float, 8, rounding_mode::rtn>(
int8{2, 3, 3, -2, -3, -3, 0, 0},
float8{2.f, 3.f, 3.f, -2.f, -3.f, -3.f, 0.f, 0.f});
test<float, float, 8, rounding_mode::rtn>(
float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f},
float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f});
test<float, half, 8, rounding_mode::rtn>(
float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f},
half8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f});
return 0;
}