Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:33:42

0001 #ifndef BOOST_HASH2_DETAIL_BIT_CAST_HPP_INCLUDED
0002 #define BOOST_HASH2_DETAIL_BIT_CAST_HPP_INCLUDED
0003 
0004 // Copyright 2024 Peter Dimov
0005 // Distributed under the Boost Software License, Version 1.0.
0006 // https://www.boost.org/LICENSE_1_0.txt
0007 
0008 #include <boost/hash2/detail/config.hpp>
0009 #include <boost/config.hpp>
0010 #include <type_traits>
0011 #include <cstring>
0012 
0013 namespace boost
0014 {
0015 namespace hash2
0016 {
0017 namespace detail
0018 {
0019 
0020 template<class To, class From> BOOST_CXX14_CONSTEXPR To bit_cast( From const& from ) noexcept
0021 {
0022     static_assert( sizeof(From) == sizeof(To), "Types must be the same size" );
0023 
0024 #if defined(BOOST_LIBSTDCXX_VERSION) && BOOST_LIBSTDCXX_VERSION < 50000
0025 
0026     // std::is_trivially_copyable doesn't exist in libstdc++ 4.x
0027 
0028 #else
0029 
0030     static_assert( std::is_trivially_copyable<From>::value, "Types must be trivially copyable" );
0031     static_assert( std::is_trivially_copyable<To>::value, "Types must be trivially copyable" );
0032 
0033 #endif
0034 
0035 #if defined(BOOST_HASH2_HAS_BUILTIN_BIT_CAST)
0036 
0037     return __builtin_bit_cast( To, from );
0038 
0039 #else
0040 
0041     To to{};
0042     std::memcpy( &to, &from, sizeof(From) );
0043     return to;
0044 
0045 #endif
0046 }
0047 
0048 } // namespace detail
0049 } // namespace hash2
0050 } // namespace boost
0051 
0052 #endif // #ifndef BOOST_HASH2_DETAIL_BIT_CAST_HPP_INCLUDED