diff --git a/sycl/include/CL/sycl/bit_cast.hpp b/sycl/include/CL/sycl/bit_cast.hpp index 6fe0b85dfe637..26a65840e64bc 100644 --- a/sycl/include/CL/sycl/bit_cast.hpp +++ b/sycl/include/CL/sycl/bit_cast.hpp @@ -8,6 +8,7 @@ #pragma once +#include #include #if __cpp_lib_bit_cast @@ -20,6 +21,21 @@ namespace sycl { // forward decl namespace detail { inline void memcpy(void *Dst, const void *Src, std::size_t Size); + +namespace half_impl { +class half; +} +using half = cl::sycl::detail::half_impl::half; + +template +#ifdef __SYCL_DEVICE_ONLY__ +using lowering_half_type = + typename std::conditional::value, _Float16, T>::type; +#else +using lowering_half_type = + typename std::conditional::value, std::uint16_t, + T>::type; +#endif } template @@ -41,7 +57,8 @@ constexpr #if __has_builtin(__builtin_bit_cast) return __builtin_bit_cast(To, from); #else // __has_builtin(__builtin_bit_cast) - static_assert(std::is_trivially_default_constructible::value, + static_assert(std::is_trivially_default_constructible< + typename sycl::detail::lowering_half_type>::value, "To must be trivially default constructible"); To to; sycl::detail::memcpy(&to, &from, sizeof(To)); diff --git a/sycl/test/regression/bit_cast.hpp b/sycl/test/regression/bit_cast.hpp new file mode 100644 index 0000000000000..49e4a8e9a2b7b --- /dev/null +++ b/sycl/test/regression/bit_cast.hpp @@ -0,0 +1,12 @@ +// The purpose of this test is to check that the following code can be +// successfully compiled + +#include + +int main() { + sycl::half x; + int16_t a = sycl::bit_cast(x); + sycl::bit_cast(a); + + return 0; +} diff --git a/sycl/test/regression/bit_cast_lin.cpp b/sycl/test/regression/bit_cast_lin.cpp new file mode 100644 index 0000000000000..9b456900a1f0b --- /dev/null +++ b/sycl/test/regression/bit_cast_lin.cpp @@ -0,0 +1,4 @@ +// RUN: %clangxx -fsycl -fsycl-host-compiler=g++ -fsycl-host-compiler-options='-std=c++17' %s -o %t.out +// UNSUPPORTED: windows + +#include "bit_cast.hpp" diff --git a/sycl/test/regression/bit_cast_win.cpp b/sycl/test/regression/bit_cast_win.cpp new file mode 100644 index 0000000000000..9dd8747a4e8a1 --- /dev/null +++ b/sycl/test/regression/bit_cast_win.cpp @@ -0,0 +1,4 @@ +// RUN: %clangxx -fsycl -fsycl-host-compiler=cl -fsycl-host-compiler-options='/std:c++17' %s -o %t.out +// UNSUPPORTED: linux + +#include "bit_cast.hpp"