From f86cdfe5022f07e1f6eded4edbd5b765822ae3ce Mon Sep 17 00:00:00 2001 From: Glen Fernandes Date: Mon, 27 Jun 2022 13:25:02 -0400 Subject: [PATCH] Simplify and generalize hash_integral_impl --- include/boost/container_hash/hash.hpp | 41 ++++++++++++--------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/include/boost/container_hash/hash.hpp b/include/boost/container_hash/hash.hpp index b4066b1..f10a7fc 100644 --- a/include/boost/container_hash/hash.hpp +++ b/include/boost/container_hash/hash.hpp @@ -1,5 +1,6 @@ // Copyright 2005-2014 Daniel James. // Copyright 2021 Peter Dimov. +// Copyright 2022 Glen Joseph Fernandes (glenjofe@gmail.com) // Distributed under the Boost Software License, Version 1.0. // https://www.boost.org/LICENSE_1_0.txt @@ -78,11 +79,10 @@ namespace boost template sizeof(std::size_t)), bool is_unsigned = boost::is_unsigned::value, - std::size_t size_t_bits = sizeof(std::size_t) * CHAR_BIT, - std::size_t type_bits = sizeof(T) * CHAR_BIT> + std::size_t blocks = sizeof(T) / sizeof(std::size_t)> struct hash_integral_impl; - template struct hash_integral_impl + template struct hash_integral_impl { static std::size_t fn( T v ) { @@ -90,7 +90,7 @@ namespace boost } }; - template struct hash_integral_impl + template struct hash_integral_impl { static std::size_t fn( T v ) { @@ -107,41 +107,36 @@ namespace boost } }; - template struct hash_integral_impl + template struct hash_integral_impl { static std::size_t fn( T v ) { + enum { + M = sizeof(std::size_t) * CHAR_BIT + }; std::size_t seed = 0; - seed ^= static_cast( v >> 32 ) + ( seed << 6 ) + ( seed >> 2 ); + seed ^= static_cast( v >> M ) + ( seed << 6 ) + ( seed >> 2 ); seed ^= static_cast( v ) + ( seed << 6 ) + ( seed >> 2 ); return seed; } }; - template struct hash_integral_impl + template struct hash_integral_impl { static std::size_t fn( T v ) { + enum { + M1 = sizeof(std::size_t) * CHAR_BIT, + M2 = M1 + M1, + M3 = M2 + M1 + }; std::size_t seed = 0; - seed ^= static_cast( v >> 96 ) + ( seed << 6 ) + ( seed >> 2 ); - seed ^= static_cast( v >> 64 ) + ( seed << 6 ) + ( seed >> 2 ); - seed ^= static_cast( v >> 32 ) + ( seed << 6 ) + ( seed >> 2 ); - seed ^= static_cast( v ) + ( seed << 6 ) + ( seed >> 2 ); - - return seed; - } - }; - - template struct hash_integral_impl - { - static std::size_t fn( T v ) - { - std::size_t seed = 0; - - seed ^= static_cast( v >> 64 ) + ( seed << 6 ) + ( seed >> 2 ); + seed ^= static_cast( v >> M3 ) + ( seed << 6 ) + ( seed >> 2 ); + seed ^= static_cast( v >> M2 ) + ( seed << 6 ) + ( seed >> 2 ); + seed ^= static_cast( v >> M1 ) + ( seed << 6 ) + ( seed >> 2 ); seed ^= static_cast( v ) + ( seed << 6 ) + ( seed >> 2 ); return seed;