Skip to content

Commit 42a347d

Browse files
authored
[SYCL] Fix bit_cast for half type (#4603)
After removing of half type trivial default constructor the bit_cast to sycl::half became impossible. Added helper struct for that case Signed-off-by: mdimakov maxim.dimakov@intel.com
1 parent 266515a commit 42a347d

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

sycl/include/CL/sycl/bit_cast.hpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#pragma once
1010

11+
#include <cstdint>
1112
#include <type_traits>
1213

1314
#if __cpp_lib_bit_cast
@@ -20,6 +21,21 @@ namespace sycl {
2021
// forward decl
2122
namespace detail {
2223
inline void memcpy(void *Dst, const void *Src, std::size_t Size);
24+
25+
namespace half_impl {
26+
class half;
27+
}
28+
using half = cl::sycl::detail::half_impl::half;
29+
30+
template <typename T>
31+
#ifdef __SYCL_DEVICE_ONLY__
32+
using lowering_half_type =
33+
typename std::conditional<std::is_same<T, half>::value, _Float16, T>::type;
34+
#else
35+
using lowering_half_type =
36+
typename std::conditional<std::is_same<T, half>::value, std::uint16_t,
37+
T>::type;
38+
#endif
2339
}
2440

2541
template <typename To, typename From>
@@ -41,7 +57,8 @@ constexpr
4157
#if __has_builtin(__builtin_bit_cast)
4258
return __builtin_bit_cast(To, from);
4359
#else // __has_builtin(__builtin_bit_cast)
44-
static_assert(std::is_trivially_default_constructible<To>::value,
60+
static_assert(std::is_trivially_default_constructible<
61+
typename sycl::detail::lowering_half_type<To>>::value,
4562
"To must be trivially default constructible");
4663
To to;
4764
sycl::detail::memcpy(&to, &from, sizeof(To));

sycl/test/regression/bit_cast.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// The purpose of this test is to check that the following code can be
2+
// successfully compiled
3+
4+
#include <CL/sycl.hpp>
5+
6+
int main() {
7+
sycl::half x;
8+
int16_t a = sycl::bit_cast<int16_t>(x);
9+
sycl::bit_cast<sycl::half>(a);
10+
11+
return 0;
12+
}

sycl/test/regression/bit_cast_lin.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// RUN: %clangxx -fsycl -fsycl-host-compiler=g++ -fsycl-host-compiler-options='-std=c++17' %s -o %t.out
2+
// UNSUPPORTED: windows
3+
4+
#include "bit_cast.hpp"

sycl/test/regression/bit_cast_win.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// RUN: %clangxx -fsycl -fsycl-host-compiler=cl -fsycl-host-compiler-options='/std:c++17' %s -o %t.out
2+
// UNSUPPORTED: linux
3+
4+
#include "bit_cast.hpp"

0 commit comments

Comments
 (0)