Skip to content

[SYCL] Fix bit_cast for half type #4603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion sycl/include/CL/sycl/bit_cast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#pragma once

#include <cstdint>
#include <type_traits>

#if __cpp_lib_bit_cast
Expand All @@ -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 <typename T>
#ifdef __SYCL_DEVICE_ONLY__
using lowering_half_type =
typename std::conditional<std::is_same<T, half>::value, _Float16, T>::type;
#else
using lowering_half_type =
typename std::conditional<std::is_same<T, half>::value, std::uint16_t,
T>::type;
#endif
}

template <typename To, typename From>
Expand All @@ -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<To>::value,
static_assert(std::is_trivially_default_constructible<
typename sycl::detail::lowering_half_type<To>>::value,
"To must be trivially default constructible");
To to;
sycl::detail::memcpy(&to, &from, sizeof(To));
Expand Down
12 changes: 12 additions & 0 deletions sycl/test/regression/bit_cast.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// The purpose of this test is to check that the following code can be
// successfully compiled

#include <CL/sycl.hpp>

int main() {
sycl::half x;
int16_t a = sycl::bit_cast<int16_t>(x);
sycl::bit_cast<sycl::half>(a);

return 0;
}
4 changes: 4 additions & 0 deletions sycl/test/regression/bit_cast_lin.cpp
Original file line number Diff line number Diff line change
@@ -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"
4 changes: 4 additions & 0 deletions sycl/test/regression/bit_cast_win.cpp
Original file line number Diff line number Diff line change
@@ -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"