2
0
mirror of https://github.com/boostorg/uuid.git synced 2026-01-19 04:42:16 +00:00

Make hash_value constexpr

This commit is contained in:
Peter Dimov
2025-12-25 12:11:08 +02:00
parent bd765b558c
commit 09dd0dd608
5 changed files with 52 additions and 6 deletions

View File

@@ -166,9 +166,19 @@ inline std::uint32_t load_native_u32( void const* p ) noexcept
return tmp;
}
inline std::uint32_t load_little_u32( void const* p ) noexcept
BOOST_CXX14_CONSTEXPR inline std::uint32_t load_little_u32( unsigned char const* p ) noexcept
{
std::uint32_t tmp;
if( is_constant_evaluated() )
{
return
static_cast<std::uint32_t>( p[ 0 ] ) |
static_cast<std::uint32_t>( p[ 1 ] ) << 8 |
static_cast<std::uint32_t>( p[ 2 ] ) << 16 |
static_cast<std::uint32_t>( p[ 3 ] ) << 24;
}
std::uint32_t tmp = {};
std::memcpy( &tmp, p, sizeof( tmp ) );
#if BOOST_UUID_BYTE_ORDER == BOOST_UUID_ORDER_LITTLE_ENDIAN

View File

@@ -5,6 +5,7 @@
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/config.hpp>
#include <cstdint>
namespace boost {
@@ -19,7 +20,7 @@ namespace detail {
// prospector -p mul,xorr -t 1000
// score = 592.20293470138972
inline std::uint64_t hash_mix_mx( std::uint64_t x ) noexcept
BOOST_CXX14_CONSTEXPR inline std::uint64_t hash_mix_mx( std::uint64_t x ) noexcept
{
x *= 0xD96AAA55;
x ^= x >> 16;
@@ -29,7 +30,7 @@ inline std::uint64_t hash_mix_mx( std::uint64_t x ) noexcept
// prospector -p mul:0xD96AAA55,xorr:16,mul,xorr -t 1000
// score = 79.5223047689704
// (with mx prepended)
inline std::uint64_t hash_mix_fmx( std::uint64_t x ) noexcept
BOOST_CXX14_CONSTEXPR inline std::uint64_t hash_mix_fmx( std::uint64_t x ) noexcept
{
x *= 0x7DF954AB;
x ^= x >> 16;

View File

@@ -327,7 +327,7 @@ BOOST_CXX14_CONSTEXPR inline void swap( uuid& lhs, uuid& rhs ) noexcept
// hash_value
inline std::size_t hash_value( uuid const& u ) noexcept
BOOST_CXX14_CONSTEXPR inline std::size_t hash_value( uuid const& u ) noexcept
{
std::uint64_t r = 0;
@@ -363,7 +363,7 @@ namespace std
template<> struct hash<boost::uuids::uuid>
{
std::size_t operator()( boost::uuids::uuid const& value ) const noexcept
BOOST_CXX14_CONSTEXPR std::size_t operator()( boost::uuids::uuid const& value ) const noexcept
{
return boost::uuids::hash_value( value );
}

View File

@@ -221,6 +221,8 @@ run test_uuid_cx3.cpp
: : : <define>BOOST_UUID_REPORT_IMPLEMENTATION ;
run test_uuid_cx3.cpp : : : <define>BOOST_UUID_NO_SIMD <define>BOOST_UUID_REPORT_IMPLEMENTATION : test_uuid_cx3_no_simd ;
run test_hash_value_cx.cpp ;
run test_string_generator_cx.cpp ;
run test_string_generator_cx2.cpp ;

View File

@@ -0,0 +1,33 @@
// Copyright 2024, 2025 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/config.hpp>
#include <cstddef>
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
#if defined(BOOST_NO_CXX14_CONSTEXPR)
# define TEST_EQ(x, y) BOOST_TEST_EQ(x, y)
#else
# define TEST_EQ(x, y) STATIC_ASSERT((x)==(y)); BOOST_TEST_EQ(x, y)
#endif
using namespace boost::uuids;
int main()
{
BOOST_CXX14_CONSTEXPR uuid u1;
TEST_EQ( hash_value( u1 ), 0 );
BOOST_CXX14_CONSTEXPR uuid u2 = {{ 0x01, 0x02, 0x03, 0x04 }};
TEST_EQ( hash_value( u2 ), static_cast<std::size_t>( 7362128010791177377ull ) );
BOOST_CXX14_CONSTEXPR uuid u3 = {{ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10 }};
TEST_EQ( hash_value( u3 ), static_cast<std::size_t>( 18416781058927374793ull ) );
return boost::report_errors();
}