bitwise_fp_cast.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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) 2018, 2021 Andrey Semashev
  7. */
  8. /*!
  9. * \file atomic/detail/bitwise_fp_cast.hpp
  10. *
  11. * This header defines \c bitwise_fp_cast used to convert between storage and floating point value types
  12. */
  13. #ifndef BOOST_ATOMIC_DETAIL_BITWISE_FP_CAST_HPP_INCLUDED_
  14. #define BOOST_ATOMIC_DETAIL_BITWISE_FP_CAST_HPP_INCLUDED_
  15. #include <cstddef>
  16. #include <boost/atomic/detail/config.hpp>
  17. #include <boost/atomic/detail/float_sizes.hpp>
  18. #include <boost/atomic/detail/bitwise_cast.hpp>
  19. #if defined(BOOST_ATOMIC_DETAIL_BIT_CAST)
  20. #include <boost/atomic/detail/type_traits/integral_constant.hpp>
  21. #endif
  22. #include <boost/atomic/detail/header.hpp>
  23. #ifdef BOOST_HAS_PRAGMA_ONCE
  24. #pragma once
  25. #endif
  26. namespace boost {
  27. namespace atomics {
  28. namespace detail {
  29. /*!
  30. * \brief The type trait returns the size of the value of the specified floating point type
  31. *
  32. * This size may be less than <tt>sizeof(T)</tt> if the implementation uses padding bytes for a particular FP type. This is
  33. * often the case with 80-bit extended double, which is stored in 12 or 16 initial bytes with tail padding filled with garbage.
  34. */
  35. template< typename T >
  36. struct value_size_of
  37. {
  38. static BOOST_CONSTEXPR_OR_CONST std::size_t value = sizeof(T);
  39. };
  40. #if defined(BOOST_ATOMIC_DETAIL_SIZEOF_FLOAT_VALUE)
  41. template< >
  42. struct value_size_of< float >
  43. {
  44. static BOOST_CONSTEXPR_OR_CONST std::size_t value = BOOST_ATOMIC_DETAIL_SIZEOF_FLOAT_VALUE;
  45. };
  46. #endif
  47. #if defined(BOOST_ATOMIC_DETAIL_SIZEOF_DOUBLE_VALUE)
  48. template< >
  49. struct value_size_of< double >
  50. {
  51. static BOOST_CONSTEXPR_OR_CONST std::size_t value = BOOST_ATOMIC_DETAIL_SIZEOF_DOUBLE_VALUE;
  52. };
  53. #endif
  54. #if defined(BOOST_ATOMIC_DETAIL_SIZEOF_LONG_DOUBLE_VALUE)
  55. template< >
  56. struct value_size_of< long double >
  57. {
  58. static BOOST_CONSTEXPR_OR_CONST std::size_t value = BOOST_ATOMIC_DETAIL_SIZEOF_LONG_DOUBLE_VALUE;
  59. };
  60. #endif
  61. template< typename T >
  62. struct value_size_of< const T > : value_size_of< T > {};
  63. template< typename T >
  64. struct value_size_of< volatile T > : value_size_of< T > {};
  65. template< typename T >
  66. struct value_size_of< const volatile T > : value_size_of< T > {};
  67. #if !defined(BOOST_ATOMIC_NO_CLEAR_PADDING)
  68. // BOOST_ATOMIC_DETAIL_CLEAR_PADDING, which is used in bitwise_cast, will clear the tail padding bits in the source object.
  69. // We don't need to specify the actual value size to avoid redundant zeroing of the tail padding.
  70. #define BOOST_ATOMIC_DETAIL_BITWISE_FP_CAST_VALUE_SIZE_OF(x) sizeof(x)
  71. #else
  72. #define BOOST_ATOMIC_DETAIL_BITWISE_FP_CAST_VALUE_SIZE_OF(x) atomics::detail::value_size_of< x >::value
  73. #endif
  74. #if defined(BOOST_ATOMIC_DETAIL_BIT_CAST)
  75. //! Similar to bitwise_cast, but either \c From or \c To is expected to be a floating point type. Attempts to detect the actual value size in the source object and considers the rest of the object as padding.
  76. template< typename To, typename From >
  77. BOOST_FORCEINLINE BOOST_ATOMIC_DETAIL_CONSTEXPR_BITWISE_CAST To bitwise_fp_cast(From const& from) BOOST_NOEXCEPT
  78. {
  79. // For floating point types, has_unique_object_representations is typically false even if the type contains no padding bits.
  80. // Here, we rely on our detection of the actual value size to select constexpr bit_cast implementation when possible. We assume
  81. // here that floating point value bits are contiguous.
  82. return atomics::detail::bitwise_cast_impl< To, BOOST_ATOMIC_DETAIL_BITWISE_FP_CAST_VALUE_SIZE_OF(From) >(from, atomics::detail::integral_constant< bool,
  83. atomics::detail::value_size_of< From >::value == sizeof(From) && atomics::detail::value_size_of< From >::value == sizeof(To) >());
  84. }
  85. #else // defined(BOOST_ATOMIC_DETAIL_BIT_CAST)
  86. //! Similar to bitwise_cast, but either \c From or \c To is expected to be a floating point type. Attempts to detect the actual value size in the source object and considers the rest of the object as padding.
  87. template< typename To, typename From >
  88. BOOST_FORCEINLINE BOOST_ATOMIC_DETAIL_CONSTEXPR_BITWISE_CAST To bitwise_fp_cast(From const& from) BOOST_NOEXCEPT
  89. {
  90. return atomics::detail::bitwise_cast< To, BOOST_ATOMIC_DETAIL_BITWISE_FP_CAST_VALUE_SIZE_OF(From) >(from);
  91. }
  92. #endif // defined(BOOST_ATOMIC_DETAIL_BIT_CAST)
  93. } // namespace detail
  94. } // namespace atomics
  95. } // namespace boost
  96. #include <boost/atomic/detail/footer.hpp>
  97. #endif // BOOST_ATOMIC_DETAIL_BITWISE_FP_CAST_HPP_INCLUDED_