]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/atomic/detail/bitwise_cast.hpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / boost / atomic / detail / bitwise_cast.hpp
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 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/integral_constant.hpp>
24 #include <boost/atomic/detail/header.hpp>
25
26 #ifdef BOOST_HAS_PRAGMA_ONCE
27 #pragma once
28 #endif
29
30 #if defined(BOOST_GCC) && BOOST_GCC >= 80000
31 #pragma GCC diagnostic push
32 // 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.
33 #pragma GCC diagnostic ignored "-Wclass-memaccess"
34 #endif
35
36 namespace boost {
37 namespace atomics {
38 namespace detail {
39
40 template< std::size_t FromSize, typename To >
41 BOOST_FORCEINLINE void clear_tail_padding_bits(To& to, atomics::detail::true_type) BOOST_NOEXCEPT
42 {
43 BOOST_ATOMIC_DETAIL_MEMSET(reinterpret_cast< unsigned char* >(atomics::detail::addressof(to)) + FromSize, 0, sizeof(To) - FromSize);
44 }
45
46 template< std::size_t FromSize, typename To >
47 BOOST_FORCEINLINE void clear_tail_padding_bits(To&, atomics::detail::false_type) BOOST_NOEXCEPT
48 {
49 }
50
51 template< std::size_t FromSize, typename To >
52 BOOST_FORCEINLINE void clear_tail_padding_bits(To& to) BOOST_NOEXCEPT
53 {
54 atomics::detail::clear_tail_padding_bits< FromSize >(to, atomics::detail::integral_constant< bool, FromSize < sizeof(To) >());
55 }
56
57 template< typename To, std::size_t FromSize, typename From >
58 BOOST_FORCEINLINE To bitwise_cast(From const& from) BOOST_NOEXCEPT
59 {
60 To to;
61 BOOST_ATOMIC_DETAIL_MEMCPY
62 (
63 atomics::detail::addressof(to),
64 atomics::detail::addressof(from),
65 (FromSize < sizeof(To) ? FromSize : sizeof(To))
66 );
67 atomics::detail::clear_tail_padding_bits< FromSize >(to);
68 return to;
69 }
70
71 template< typename To, typename From >
72 BOOST_FORCEINLINE To bitwise_cast(From const& from) BOOST_NOEXCEPT
73 {
74 return atomics::detail::bitwise_cast< To, sizeof(From) >(from);
75 }
76
77 } // namespace detail
78 } // namespace atomics
79 } // namespace boost
80
81 #if defined(BOOST_GCC) && BOOST_GCC >= 80000
82 #pragma GCC diagnostic pop
83 #endif
84
85 #include <boost/atomic/detail/footer.hpp>
86
87 #endif // BOOST_ATOMIC_DETAIL_BITWISE_CAST_HPP_INCLUDED_