bitwise_cast.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #ifndef BOOST_ATOMIC_DETAIL_BITWISE_CAST_HPP_INCLUDED_
  16. #define BOOST_ATOMIC_DETAIL_BITWISE_CAST_HPP_INCLUDED_
  17. #include <cstddef>
  18. #include <boost/atomic/detail/config.hpp>
  19. #include <boost/atomic/detail/addressof.hpp>
  20. #include <boost/atomic/detail/string_ops.hpp>
  21. #include <boost/atomic/detail/type_traits/integral_constant.hpp>
  22. #include <boost/atomic/detail/header.hpp>
  23. #ifdef BOOST_HAS_PRAGMA_ONCE
  24. #pragma once
  25. #endif
  26. #if defined(BOOST_GCC) && BOOST_GCC >= 80000
  27. #pragma GCC diagnostic push
  28. // 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.
  29. #pragma GCC diagnostic ignored "-Wclass-memaccess"
  30. #endif
  31. namespace boost {
  32. namespace atomics {
  33. namespace detail {
  34. template< std::size_t FromSize, typename To >
  35. BOOST_FORCEINLINE void clear_tail_padding_bits(To& to, atomics::detail::true_type) BOOST_NOEXCEPT
  36. {
  37. BOOST_ATOMIC_DETAIL_MEMSET(reinterpret_cast< unsigned char* >(atomics::detail::addressof(to)) + FromSize, 0, sizeof(To) - FromSize);
  38. }
  39. template< std::size_t FromSize, typename To >
  40. BOOST_FORCEINLINE void clear_tail_padding_bits(To&, atomics::detail::false_type) BOOST_NOEXCEPT
  41. {
  42. }
  43. template< std::size_t FromSize, typename To >
  44. BOOST_FORCEINLINE void clear_tail_padding_bits(To& to) BOOST_NOEXCEPT
  45. {
  46. atomics::detail::clear_tail_padding_bits< FromSize >(to, atomics::detail::integral_constant< bool, FromSize < sizeof(To) >());
  47. }
  48. template< typename To, std::size_t FromSize, typename From >
  49. BOOST_FORCEINLINE To bitwise_cast(From const& from) BOOST_NOEXCEPT
  50. {
  51. To to;
  52. BOOST_ATOMIC_DETAIL_MEMCPY
  53. (
  54. atomics::detail::addressof(to),
  55. atomics::detail::addressof(from),
  56. (FromSize < sizeof(To) ? FromSize : sizeof(To))
  57. );
  58. atomics::detail::clear_tail_padding_bits< FromSize >(to);
  59. return to;
  60. }
  61. template< typename To, typename From >
  62. BOOST_FORCEINLINE To bitwise_cast(From const& from) BOOST_NOEXCEPT
  63. {
  64. return atomics::detail::bitwise_cast< To, sizeof(From) >(from);
  65. }
  66. } // namespace detail
  67. } // namespace atomics
  68. } // namespace boost
  69. #if defined(BOOST_GCC) && BOOST_GCC >= 80000
  70. #pragma GCC diagnostic pop
  71. #endif
  72. #include <boost/atomic/detail/footer.hpp>
  73. #endif // BOOST_ATOMIC_DETAIL_BITWISE_CAST_HPP_INCLUDED_