DPC++ Runtime
Runtime libraries for oneAPI DPC++
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 <sycl/detail/memcpy.hpp>
12 
13 #include <type_traits>
14 
15 #if __cpp_lib_bit_cast
16 #include <bit>
17 #endif
18 
19 namespace sycl {
21 
22 template <typename To, typename From>
23 #if __cpp_lib_bit_cast || __has_builtin(__builtin_bit_cast)
24 constexpr
25 #endif
26  To
27  bit_cast(const From &from) noexcept {
28  static_assert(sizeof(To) == sizeof(From),
29  "Sizes of To and From must be equal");
30  static_assert(std::is_trivially_copyable<From>::value,
31  "From must be trivially copyable");
32  static_assert(std::is_trivially_copyable<To>::value,
33  "To must be trivially copyable");
34 #if __cpp_lib_bit_cast
35  return std::bit_cast<To>(from);
36 #else // __cpp_lib_bit_cast
37 
38 #if __has_builtin(__builtin_bit_cast)
39  return __builtin_bit_cast(To, from);
40 #else // __has_builtin(__builtin_bit_cast)
41  static_assert(std::is_trivially_default_constructible<To>::value,
42  "To must be trivially default constructible");
43  To to;
44  sycl::detail::memcpy(&to, &from, sizeof(To));
45  return to;
46 #endif // __has_builtin(__builtin_bit_cast)
47 
48 #endif // __cpp_lib_bit_cast
49 }
50 
51 namespace detail {
52 template <typename To, typename From>
53 __SYCL2020_DEPRECATED("use 'sycl::bit_cast' instead")
54 #if __cpp_lib_bit_cast || __has_builtin(__builtin_bit_cast)
55 constexpr
56 #endif
57  To bit_cast(const From &from) noexcept {
58  return sycl::bit_cast<To>(from);
59 }
60 } // namespace detail
61 
62 } // __SYCL_INLINE_VER_NAMESPACE(_V1)
63 } // namespace sycl
memcpy.hpp
__SYCL_INLINE_VER_NAMESPACE
#define __SYCL_INLINE_VER_NAMESPACE(X)
Definition: defines_elementary.hpp:11
sycl::_V1::detail::memcpy
void memcpy(void *Dst, const void *Src, size_t Size)
Definition: memcpy.hpp:16
sycl
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14
sycl::_V1::detail::bit_cast
To bit_cast(const From &from) noexcept
Definition: bit_cast.hpp:57
__SYCL2020_DEPRECATED
#define __SYCL2020_DEPRECATED(message)
Definition: defines_elementary.hpp:57