Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-17 08:46:45

0001 #ifndef BOOST_HASH2_DETAIL_BYTESWAP_HPP_INCLUDED
0002 #define BOOST_HASH2_DETAIL_BYTESWAP_HPP_INCLUDED
0003 
0004 // Copyright 2025 Christian Mazakas
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/is_constant_evaluated.hpp>
0009 #include <boost/config.hpp>
0010 #include <cstdint>
0011 
0012 #if defined(_MSC_VER)
0013 #include <intrin.h>
0014 #endif
0015 
0016 namespace boost
0017 {
0018 namespace hash2
0019 {
0020 namespace detail
0021 {
0022 
0023 BOOST_CXX14_CONSTEXPR inline std::uint32_t byteswap_impl( std::uint32_t x ) noexcept
0024 {
0025     std::uint32_t step16 = x << 16 | x >> 16;
0026     return ( ( step16 << 8 ) & 0xff00ff00 ) | ( ( step16 >> 8 ) & 0x00ff00ff );
0027 }
0028 
0029 BOOST_CXX14_CONSTEXPR inline std::uint64_t byteswap_impl( std::uint64_t x ) noexcept
0030 {
0031     std::uint64_t step32 = x << 32 | x >> 32;
0032     std::uint64_t step16 = ( step32 & 0x0000ffff0000ffffull ) << 16 | ( step32 & 0xffff0000ffff0000ull ) >> 16;
0033     return ( step16 & 0x00ff00ff00ff00ffull ) << 8 | ( step16 & 0xff00ff00ff00ff00ull ) >> 8;
0034 }
0035 
0036 BOOST_CXX14_CONSTEXPR inline std::uint32_t byteswap( std::uint32_t x ) noexcept
0037 {
0038 #if defined(__GNUC__) || defined(__clang__)
0039 
0040     return __builtin_bswap32( x );
0041 
0042 #elif defined(_MSC_VER)
0043 
0044     if( !detail::is_constant_evaluated() )
0045     {
0046         return _byteswap_ulong( x );
0047     }
0048     else
0049     {
0050         return byteswap_impl( x );
0051     }
0052 
0053 #else
0054 
0055     return byteswap_impl( x );
0056 
0057 #endif
0058 }
0059 
0060 BOOST_CXX14_CONSTEXPR inline std::uint64_t byteswap( std::uint64_t x ) noexcept
0061 {
0062 #if defined(__GNUC__) || defined(__clang__)
0063 
0064     return __builtin_bswap64( x );
0065 
0066 #elif defined(_MSC_VER)
0067 
0068     if( !detail::is_constant_evaluated() )
0069     {
0070         return _byteswap_uint64( x );
0071     }
0072     else
0073     {
0074         return byteswap_impl( x );
0075     }
0076 
0077 #else
0078 
0079     return byteswap_impl( x );
0080 
0081 #endif
0082 }
0083 
0084 } // namespace detail
0085 } // namespace hash2
0086 } // namespace boost
0087 
0088 #endif // #ifndef BOOST_HASH2_DETAIL_BYTESWAP_HPP_INCLUDED