/src/boost/boost/atomic/detail/bitwise_cast.hpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Distributed under the Boost Software License, Version 1.0. |
3 | | * (See accompanying file LICENSE_1_0.txt or copy at |
4 | | * http://www.boost.org/LICENSE_1_0.txt) |
5 | | * |
6 | | * Copyright (c) 2009 Helge Bahmann |
7 | | * Copyright (c) 2012 Tim Blechmann |
8 | | * Copyright (c) 2013-2018, 2020-2021 Andrey Semashev |
9 | | */ |
10 | | /*! |
11 | | * \file atomic/detail/bitwise_cast.hpp |
12 | | * |
13 | | * This header defines \c bitwise_cast used to convert between storage and value types |
14 | | */ |
15 | | |
16 | | #ifndef BOOST_ATOMIC_DETAIL_BITWISE_CAST_HPP_INCLUDED_ |
17 | | #define BOOST_ATOMIC_DETAIL_BITWISE_CAST_HPP_INCLUDED_ |
18 | | |
19 | | #include <cstddef> |
20 | | #include <boost/atomic/detail/config.hpp> |
21 | | #include <boost/atomic/detail/addressof.hpp> |
22 | | #include <boost/atomic/detail/string_ops.hpp> |
23 | | #include <boost/atomic/detail/type_traits/remove_cv.hpp> |
24 | | #include <boost/atomic/detail/type_traits/integral_constant.hpp> |
25 | | #include <boost/atomic/detail/type_traits/has_unique_object_representations.hpp> |
26 | | #include <boost/atomic/detail/header.hpp> |
27 | | |
28 | | #ifdef BOOST_HAS_PRAGMA_ONCE |
29 | | #pragma once |
30 | | #endif |
31 | | |
32 | | #if !defined(BOOST_ATOMIC_DETAIL_NO_HAS_UNIQUE_OBJECT_REPRESENTATIONS) |
33 | | |
34 | | #if defined(__has_builtin) |
35 | | #if __has_builtin(__builtin_bit_cast) |
36 | 0 | #define BOOST_ATOMIC_DETAIL_BIT_CAST(x, y) __builtin_bit_cast(x, y) |
37 | | #endif |
38 | | #endif |
39 | | |
40 | | #if !defined(BOOST_ATOMIC_DETAIL_BIT_CAST) && defined(BOOST_MSVC) && BOOST_MSVC >= 1926 |
41 | | #define BOOST_ATOMIC_DETAIL_BIT_CAST(x, y) __builtin_bit_cast(x, y) |
42 | | #endif |
43 | | |
44 | | #endif // !defined(BOOST_ATOMIC_DETAIL_NO_HAS_UNIQUE_OBJECT_REPRESENTATIONS) |
45 | | |
46 | | #if defined(BOOST_NO_CXX11_CONSTEXPR) || !defined(BOOST_ATOMIC_DETAIL_BIT_CAST) || !defined(BOOST_ATOMIC_DETAIL_HAS_BUILTIN_ADDRESSOF) |
47 | | #define BOOST_ATOMIC_DETAIL_NO_CXX11_CONSTEXPR_BITWISE_CAST |
48 | | #endif |
49 | | |
50 | | #if !defined(BOOST_ATOMIC_DETAIL_NO_CXX11_CONSTEXPR_BITWISE_CAST) |
51 | | #define BOOST_ATOMIC_DETAIL_CONSTEXPR_BITWISE_CAST BOOST_CONSTEXPR |
52 | | #else |
53 | | #define BOOST_ATOMIC_DETAIL_CONSTEXPR_BITWISE_CAST |
54 | | #endif |
55 | | |
56 | | #if defined(BOOST_GCC) && BOOST_GCC >= 80000 |
57 | | #pragma GCC diagnostic push |
58 | | // copying an object of non-trivial type X from an array of Y. This is benign because we use memcpy to copy trivially copyable objects. |
59 | | #pragma GCC diagnostic ignored "-Wclass-memaccess" |
60 | | #endif |
61 | | |
62 | | namespace boost { |
63 | | namespace atomics { |
64 | | namespace detail { |
65 | | |
66 | | template< std::size_t ValueSize, typename To > |
67 | | BOOST_FORCEINLINE void clear_tail_padding_bits(To& to, atomics::detail::true_type) BOOST_NOEXCEPT |
68 | | { |
69 | | BOOST_ATOMIC_DETAIL_MEMSET(reinterpret_cast< unsigned char* >(atomics::detail::addressof(to)) + ValueSize, 0, sizeof(To) - ValueSize); |
70 | | } |
71 | | |
72 | | template< std::size_t ValueSize, typename To > |
73 | | BOOST_FORCEINLINE void clear_tail_padding_bits(To&, atomics::detail::false_type) BOOST_NOEXCEPT |
74 | | { |
75 | | } |
76 | | |
77 | | template< std::size_t ValueSize, typename To > |
78 | | BOOST_FORCEINLINE void clear_tail_padding_bits(To& to) BOOST_NOEXCEPT |
79 | | { |
80 | | atomics::detail::clear_tail_padding_bits< ValueSize >(to, atomics::detail::integral_constant< bool, ValueSize < sizeof(To) >()); |
81 | | } |
82 | | |
83 | | template< typename To, std::size_t FromValueSize, typename From > |
84 | | BOOST_FORCEINLINE To bitwise_cast_memcpy(From const& from) BOOST_NOEXCEPT |
85 | | { |
86 | | typedef typename atomics::detail::remove_cv< To >::type unqualified_to_t; |
87 | | unqualified_to_t to; |
88 | | #if !defined(BOOST_ATOMIC_NO_CLEAR_PADDING) |
89 | | From from2(from); |
90 | | BOOST_ATOMIC_DETAIL_CLEAR_PADDING(atomics::detail::addressof(from2)); |
91 | | BOOST_ATOMIC_DETAIL_MEMCPY |
92 | | ( |
93 | | atomics::detail::addressof(to), |
94 | | atomics::detail::addressof(from2), |
95 | | (FromValueSize < sizeof(unqualified_to_t) ? FromValueSize : sizeof(unqualified_to_t)) |
96 | | ); |
97 | | #else |
98 | | BOOST_ATOMIC_DETAIL_MEMCPY |
99 | | ( |
100 | | atomics::detail::addressof(to), |
101 | | atomics::detail::addressof(from), |
102 | | (FromValueSize < sizeof(unqualified_to_t) ? FromValueSize : sizeof(unqualified_to_t)) |
103 | | ); |
104 | | #endif |
105 | | atomics::detail::clear_tail_padding_bits< FromValueSize >(to); |
106 | | return to; |
107 | | } |
108 | | |
109 | | #if defined(BOOST_ATOMIC_DETAIL_BIT_CAST) |
110 | | |
111 | | template< typename To, std::size_t FromValueSize, typename From > |
112 | | BOOST_FORCEINLINE BOOST_ATOMIC_DETAIL_CONSTEXPR_BITWISE_CAST To bitwise_cast_impl(From const& from, atomics::detail::true_type) BOOST_NOEXCEPT |
113 | 0 | { |
114 | | // This implementation is only called when the From type has no padding and From and To have the same size |
115 | 0 | return BOOST_ATOMIC_DETAIL_BIT_CAST(typename atomics::detail::remove_cv< To >::type, from); |
116 | 0 | } Unexecuted instantiation: std::__1::locale* boost::atomics::detail::bitwise_cast_impl<std::__1::locale*, 8ul, unsigned long>(unsigned long const&, std::__1::integral_constant<bool, true>) Unexecuted instantiation: unsigned long boost::atomics::detail::bitwise_cast_impl<unsigned long, 8ul, std::__1::locale*>(std::__1::locale* const&, std::__1::integral_constant<bool, true>) |
117 | | |
118 | | template< typename To, std::size_t FromValueSize, typename From > |
119 | | BOOST_FORCEINLINE To bitwise_cast_impl(From const& from, atomics::detail::false_type) BOOST_NOEXCEPT |
120 | | { |
121 | | return atomics::detail::bitwise_cast_memcpy< To, FromValueSize >(from); |
122 | | } |
123 | | |
124 | | template< typename To, std::size_t FromValueSize, typename From > |
125 | | BOOST_FORCEINLINE BOOST_ATOMIC_DETAIL_CONSTEXPR_BITWISE_CAST To bitwise_cast(From const& from) BOOST_NOEXCEPT |
126 | 0 | { |
127 | 0 | return atomics::detail::bitwise_cast_impl< To, FromValueSize >(from, atomics::detail::integral_constant< bool, |
128 | 0 | FromValueSize == sizeof(To) && atomics::detail::has_unique_object_representations< From >::value >()); |
129 | 0 | } Unexecuted instantiation: std::__1::locale* boost::atomics::detail::bitwise_cast<std::__1::locale*, 8ul, unsigned long>(unsigned long const&) Unexecuted instantiation: unsigned long boost::atomics::detail::bitwise_cast<unsigned long, 8ul, std::__1::locale*>(std::__1::locale* const&) |
130 | | |
131 | | #else // defined(BOOST_ATOMIC_DETAIL_BIT_CAST) |
132 | | |
133 | | template< typename To, std::size_t FromValueSize, typename From > |
134 | | BOOST_FORCEINLINE To bitwise_cast(From const& from) BOOST_NOEXCEPT |
135 | | { |
136 | | return atomics::detail::bitwise_cast_memcpy< To, FromValueSize >(from); |
137 | | } |
138 | | |
139 | | #endif // defined(BOOST_ATOMIC_DETAIL_BIT_CAST) |
140 | | |
141 | | //! Converts the source object to the target type, possibly by padding or truncating it on the right, and clearing any padding bits (if supported by compiler). Preserves value bits unchanged. |
142 | | template< typename To, typename From > |
143 | | BOOST_FORCEINLINE BOOST_ATOMIC_DETAIL_CONSTEXPR_BITWISE_CAST To bitwise_cast(From const& from) BOOST_NOEXCEPT |
144 | 0 | { |
145 | 0 | return atomics::detail::bitwise_cast< To, sizeof(From) >(from); |
146 | 0 | } Unexecuted instantiation: std::__1::locale* boost::atomics::detail::bitwise_cast<std::__1::locale*, unsigned long>(unsigned long const&) Unexecuted instantiation: unsigned long boost::atomics::detail::bitwise_cast<unsigned long, std::__1::locale*>(std::__1::locale* const&) |
147 | | |
148 | | } // namespace detail |
149 | | } // namespace atomics |
150 | | } // namespace boost |
151 | | |
152 | | #if defined(BOOST_GCC) && BOOST_GCC >= 80000 |
153 | | #pragma GCC diagnostic pop |
154 | | #endif |
155 | | |
156 | | #include <boost/atomic/detail/footer.hpp> |
157 | | |
158 | | #endif // BOOST_ATOMIC_DETAIL_BITWISE_CAST_HPP_INCLUDED_ |