bitwise_fp_cast.hpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 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. #include <boost/atomic/detail/header.hpp>
  20. #ifdef BOOST_HAS_PRAGMA_ONCE
  21. #pragma once
  22. #endif
  23. namespace boost {
  24. namespace atomics {
  25. namespace detail {
  26. /*!
  27. * \brief The type trait returns the size of the value of the specified floating point type
  28. *
  29. * This size may be less than <tt>sizeof(T)</tt> if the implementation uses padding bytes for a particular FP type. This is
  30. * often the case with 80-bit extended double, which is stored in 12 or 16 bytes with padding filled with garbage.
  31. */
  32. template< typename T >
  33. struct value_sizeof
  34. {
  35. static BOOST_CONSTEXPR_OR_CONST std::size_t value = sizeof(T);
  36. };
  37. #if defined(BOOST_ATOMIC_DETAIL_SIZEOF_FLOAT_VALUE)
  38. template< >
  39. struct value_sizeof< float >
  40. {
  41. static BOOST_CONSTEXPR_OR_CONST std::size_t value = BOOST_ATOMIC_DETAIL_SIZEOF_FLOAT_VALUE;
  42. };
  43. #endif
  44. #if defined(BOOST_ATOMIC_DETAIL_SIZEOF_DOUBLE_VALUE)
  45. template< >
  46. struct value_sizeof< double >
  47. {
  48. static BOOST_CONSTEXPR_OR_CONST std::size_t value = BOOST_ATOMIC_DETAIL_SIZEOF_DOUBLE_VALUE;
  49. };
  50. #endif
  51. #if defined(BOOST_ATOMIC_DETAIL_SIZEOF_LONG_DOUBLE_VALUE)
  52. template< >
  53. struct value_sizeof< long double >
  54. {
  55. static BOOST_CONSTEXPR_OR_CONST std::size_t value = BOOST_ATOMIC_DETAIL_SIZEOF_LONG_DOUBLE_VALUE;
  56. };
  57. #endif
  58. template< typename T >
  59. struct value_sizeof< const T > : value_sizeof< T > {};
  60. template< typename T >
  61. struct value_sizeof< volatile T > : value_sizeof< T > {};
  62. template< typename T >
  63. struct value_sizeof< const volatile T > : value_sizeof< T > {};
  64. template< typename To, typename From >
  65. BOOST_FORCEINLINE To bitwise_fp_cast(From const& from) BOOST_NOEXCEPT
  66. {
  67. return atomics::detail::bitwise_cast< To, atomics::detail::value_sizeof< From >::value >(from);
  68. }
  69. } // namespace detail
  70. } // namespace atomics
  71. } // namespace boost
  72. #include <boost/atomic/detail/footer.hpp>
  73. #endif // BOOST_ATOMIC_DETAIL_BITWISE_FP_CAST_HPP_INCLUDED_