Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit 1d6a703

Browse files
authored
[SYCL] Add tests for constexpr half type (#289)
Signed-off-by: mdimakov <[email protected]>
1 parent 0a032c5 commit 1d6a703

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

SYCL/Basic/half_type.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,52 @@ inline bool bitwise_comparison_fp32(const half val, const uint32_t exp) {
192192
return reinterpret_cast<const uint32_t &>(fp32) == exp;
193193
}
194194

195+
constexpr void constexpr_verify_add() {
196+
constexpr half a{5.0}, b{2.0}, ref{7.0};
197+
constexpr half result = a + b;
198+
constexpr half diff = result - ref;
199+
constexpr auto sign = diff < 0 ? -1 : 1;
200+
static_assert(sign * static_cast<float>(diff) <
201+
std::numeric_limits<cl::sycl::half>::epsilon(),
202+
"Constexpr add is wrong");
203+
}
204+
205+
constexpr void constexpr_verify_sub() {
206+
constexpr half a{5.0f}, b{2.0}, ref{3.0};
207+
constexpr half result = a - b;
208+
constexpr half diff = result - ref;
209+
constexpr auto sign = diff < 0 ? -1 : 1;
210+
static_assert(sign * static_cast<float>(diff) <
211+
std::numeric_limits<cl::sycl::half>::epsilon(),
212+
"Constexpr sub is wrong");
213+
}
214+
215+
constexpr void constexpr_verify_mul() {
216+
constexpr half a{5.0f}, b{2.0}, ref{10.0};
217+
constexpr half result = a * b;
218+
constexpr half diff = result - ref;
219+
constexpr auto sign = diff < 0 ? -1 : 1;
220+
static_assert(sign * static_cast<float>(diff) <
221+
std::numeric_limits<cl::sycl::half>::epsilon(),
222+
"Constexpr mul is wrong");
223+
}
224+
225+
constexpr void constexpr_verify_div() {
226+
constexpr half a{5.0f}, b{2.0}, ref{2.5};
227+
constexpr half result = a / b;
228+
constexpr half diff = result - ref;
229+
constexpr auto sign = diff < 0 ? -1 : 1;
230+
static_assert(sign * static_cast<float>(diff) <
231+
std::numeric_limits<cl::sycl::half>::epsilon(),
232+
"Constexpr div is wrong");
233+
}
234+
195235
int main() {
236+
constexpr_verify_add();
237+
constexpr_verify_sub();
238+
constexpr_verify_mul();
239+
constexpr_verify_div();
240+
196241
device dev{default_selector()};
197242
if (!dev.is_host() && !dev.has_extension("cl_khr_fp16")) {
198243
std::cout << "This device doesn't support the extension cl_khr_fp16"

0 commit comments

Comments
 (0)