DPC++ Runtime
Runtime libraries for oneAPI Data Parallel C++
bit_cast.hpp
Go to the documentation of this file.
1 //==---------------- bit_cast.hpp - SYCL bit_cast --------------------------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #pragma once
10 
11 #include <type_traits>
12 
13 #if __cpp_lib_bit_cast
14 #include <bit>
15 #endif
16 
18 namespace sycl {
19 
20 // forward decl
21 namespace detail {
22 inline void memcpy(void *Dst, const void *Src, std::size_t Size);
23 }
24 
25 template <typename To, typename From>
26 #if __cpp_lib_bit_cast || __has_builtin(__builtin_bit_cast)
27 constexpr
28 #endif
29  To
30  bit_cast(const From &from) noexcept {
31  static_assert(sizeof(To) == sizeof(From),
32  "Sizes of To and From must be equal");
33  static_assert(std::is_trivially_copyable<From>::value,
34  "From must be trivially copyable");
35  static_assert(std::is_trivially_copyable<To>::value,
36  "To must be trivially copyable");
37 #if __cpp_lib_bit_cast
38  return std::bit_cast<To>(from);
39 #else // __cpp_lib_bit_cast
40 
41 #if __has_builtin(__builtin_bit_cast)
42  return __builtin_bit_cast(To, from);
43 #else // __has_builtin(__builtin_bit_cast)
44  static_assert(std::is_trivially_default_constructible<To>::value,
45  "To must be trivially default constructible");
46  To to;
47  sycl::detail::memcpy(&to, &from, sizeof(To));
48  return to;
49 #endif // __has_builtin(__builtin_bit_cast)
50 
51 #endif // __cpp_lib_bit_cast
52 }
53 
54 namespace detail {
55 template <typename To, typename From>
56 __SYCL2020_DEPRECATED("use 'sycl::bit_cast' instead")
57 #if __cpp_lib_bit_cast || __has_builtin(__builtin_bit_cast)
58 constexpr
59 #endif
60  To bit_cast(const From &from) noexcept {
61  return sycl::bit_cast<To>(from);
62 }
63 } // namespace detail
64 
65 } // namespace sycl
66 } // __SYCL_INLINE_NAMESPACE(cl)
__SYCL2020_DEPRECATED
#define __SYCL2020_DEPRECATED(message)
Definition: defines_elementary.hpp:56
detail
Definition: pi_opencl.cpp:86
cl::sycl::detail::memcpy
void memcpy(void *Dst, const void *Src, std::size_t Size)
cl
We provide new interfaces for matrix muliply in this patch:
Definition: access.hpp:13
cl::sycl::detail::bit_cast
To bit_cast(const From &from) noexcept
Definition: bit_cast.hpp:60
__SYCL_INLINE_NAMESPACE
#define __SYCL_INLINE_NAMESPACE(X)
Definition: defines_elementary.hpp:12