bitwise_fp_cast.hpp 2.4 KB

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