bitwise_cast.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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-2025 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. #ifndef BOOST_ATOMIC_DETAIL_BITWISE_CAST_HPP_INCLUDED_
  16. #define BOOST_ATOMIC_DETAIL_BITWISE_CAST_HPP_INCLUDED_
  17. #include <cstddef>
  18. #include <type_traits>
  19. #include <boost/atomic/detail/config.hpp>
  20. #include <boost/atomic/detail/addressof.hpp>
  21. #include <boost/atomic/detail/string_ops.hpp>
  22. #include <boost/atomic/detail/type_traits/is_trivially_copyable.hpp>
  23. #include <boost/atomic/detail/type_traits/has_unique_object_representations.hpp>
  24. #include <boost/atomic/detail/header.hpp>
  25. #ifdef BOOST_HAS_PRAGMA_ONCE
  26. #pragma once
  27. #endif
  28. #if !defined(BOOST_ATOMIC_DETAIL_NO_HAS_UNIQUE_OBJECT_REPRESENTATIONS)
  29. #if defined(__has_builtin)
  30. #if __has_builtin(__builtin_bit_cast)
  31. #define BOOST_ATOMIC_DETAIL_BIT_CAST(x, y) __builtin_bit_cast(x, y)
  32. #endif
  33. #endif
  34. #if !defined(BOOST_ATOMIC_DETAIL_BIT_CAST) && defined(BOOST_MSVC) && BOOST_MSVC >= 1926
  35. #define BOOST_ATOMIC_DETAIL_BIT_CAST(x, y) __builtin_bit_cast(x, y)
  36. #endif
  37. #endif // !defined(BOOST_ATOMIC_DETAIL_NO_HAS_UNIQUE_OBJECT_REPRESENTATIONS)
  38. #if !defined(BOOST_ATOMIC_DETAIL_BIT_CAST) || !defined(BOOST_ATOMIC_DETAIL_HAS_BUILTIN_ADDRESSOF)
  39. #define BOOST_ATOMIC_DETAIL_NO_CXX11_CONSTEXPR_BITWISE_CAST
  40. #endif
  41. #if !defined(BOOST_ATOMIC_DETAIL_NO_CXX11_CONSTEXPR_BITWISE_CAST)
  42. #define BOOST_ATOMIC_DETAIL_CONSTEXPR_BITWISE_CAST constexpr
  43. #else
  44. #define BOOST_ATOMIC_DETAIL_CONSTEXPR_BITWISE_CAST
  45. #endif
  46. #if defined(BOOST_GCC) && BOOST_GCC >= 80000
  47. #pragma GCC diagnostic push
  48. // 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.
  49. #pragma GCC diagnostic ignored "-Wclass-memaccess"
  50. #endif
  51. namespace boost {
  52. namespace atomics {
  53. namespace detail {
  54. template< std::size_t ValueSize, typename To >
  55. BOOST_FORCEINLINE void clear_tail_padding_bits(To& to, std::true_type) noexcept
  56. {
  57. BOOST_ATOMIC_DETAIL_MEMSET(reinterpret_cast< unsigned char* >(atomics::detail::addressof(to)) + ValueSize, 0, sizeof(To) - ValueSize);
  58. }
  59. template< std::size_t ValueSize, typename To >
  60. BOOST_FORCEINLINE void clear_tail_padding_bits(To&, std::false_type) noexcept
  61. {
  62. }
  63. template< std::size_t ValueSize, typename To >
  64. BOOST_FORCEINLINE void clear_tail_padding_bits(To& to) noexcept
  65. {
  66. atomics::detail::clear_tail_padding_bits< ValueSize >(to, std::integral_constant< bool, ValueSize < sizeof(To) >());
  67. }
  68. template< typename To, std::size_t FromValueSize, typename From >
  69. BOOST_FORCEINLINE To bitwise_cast_memcpy(From const& from) noexcept
  70. {
  71. using unqualified_to_t = typename std::remove_cv< To >::type;
  72. static_assert(atomics::detail::is_trivially_copyable< unqualified_to_t >::value, "bitwise_cast target type must be trivially copyable");
  73. static_assert(atomics::detail::is_trivially_copyable< From >::value, "bitwise_cast source type must be trivially copyable");
  74. // Suppress default constructor of To as it may potentially be a non-trivial throwing constructor
  75. union cast_helper
  76. {
  77. unsigned char as_bytes[sizeof(unqualified_to_t)];
  78. unqualified_to_t as_to;
  79. BOOST_FORCEINLINE cast_helper() noexcept {}
  80. }
  81. storage;
  82. #if !defined(BOOST_ATOMIC_NO_CLEAR_PADDING)
  83. From from2(from);
  84. BOOST_ATOMIC_DETAIL_CLEAR_PADDING(atomics::detail::addressof(from2));
  85. BOOST_ATOMIC_DETAIL_MEMCPY
  86. (
  87. atomics::detail::addressof(storage.as_to),
  88. atomics::detail::addressof(from2),
  89. (FromValueSize < sizeof(unqualified_to_t) ? FromValueSize : sizeof(unqualified_to_t))
  90. );
  91. #else
  92. BOOST_ATOMIC_DETAIL_MEMCPY
  93. (
  94. atomics::detail::addressof(storage.as_to),
  95. atomics::detail::addressof(from),
  96. (FromValueSize < sizeof(unqualified_to_t) ? FromValueSize : sizeof(unqualified_to_t))
  97. );
  98. #endif
  99. atomics::detail::clear_tail_padding_bits< FromValueSize >(storage.as_to);
  100. return storage.as_to;
  101. }
  102. #if defined(BOOST_ATOMIC_DETAIL_BIT_CAST)
  103. template< typename To, std::size_t FromValueSize, typename From >
  104. BOOST_FORCEINLINE BOOST_ATOMIC_DETAIL_CONSTEXPR_BITWISE_CAST To bitwise_cast_impl(From const& from, std::true_type) noexcept
  105. {
  106. // This implementation is only called when the From type has no padding and From and To have the same size
  107. return BOOST_ATOMIC_DETAIL_BIT_CAST(typename std::remove_cv< To >::type, from);
  108. }
  109. template< typename To, std::size_t FromValueSize, typename From >
  110. BOOST_FORCEINLINE To bitwise_cast_impl(From const& from, std::false_type) noexcept
  111. {
  112. return atomics::detail::bitwise_cast_memcpy< To, FromValueSize >(from);
  113. }
  114. template< typename To, std::size_t FromValueSize, typename From >
  115. BOOST_FORCEINLINE BOOST_ATOMIC_DETAIL_CONSTEXPR_BITWISE_CAST To bitwise_cast(From const& from) noexcept
  116. {
  117. return atomics::detail::bitwise_cast_impl< To, FromValueSize >(from, std::integral_constant< bool,
  118. FromValueSize == sizeof(To) && atomics::detail::has_unique_object_representations< From >::value >());
  119. }
  120. #else // defined(BOOST_ATOMIC_DETAIL_BIT_CAST)
  121. template< typename To, std::size_t FromValueSize, typename From >
  122. BOOST_FORCEINLINE To bitwise_cast(From const& from) noexcept
  123. {
  124. return atomics::detail::bitwise_cast_memcpy< To, FromValueSize >(from);
  125. }
  126. #endif // defined(BOOST_ATOMIC_DETAIL_BIT_CAST)
  127. //! 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).
  128. //! Preserves value bits unchanged.
  129. template< typename To, typename From >
  130. BOOST_FORCEINLINE BOOST_ATOMIC_DETAIL_CONSTEXPR_BITWISE_CAST To bitwise_cast(From const& from) noexcept
  131. {
  132. return atomics::detail::bitwise_cast< To, sizeof(From) >(from);
  133. }
  134. } // namespace detail
  135. } // namespace atomics
  136. } // namespace boost
  137. #if defined(BOOST_GCC) && BOOST_GCC >= 80000
  138. #pragma GCC diagnostic pop
  139. #endif
  140. #include <boost/atomic/detail/footer.hpp>
  141. #endif // BOOST_ATOMIC_DETAIL_BITWISE_CAST_HPP_INCLUDED_