diff --git a/hash/doc/ref.xml b/hash/doc/ref.xml index 6e23369..fbbafae 100644 --- a/hash/doc/ref.xml +++ b/hash/doc/ref.xml @@ -261,6 +261,42 @@ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + + + long long + + + std::size_t + + long long + + + Unspecified in TR1, except that equal arguments yield the same result. + hash_value(val) in Boost. + + Doesn't throw + + + + + + + unsigned long long + + + std::size_t + + unsigned long long + + + Unspecified in TR1, except that equal arguments yield the same result. + hash_value(val) in Boost. + + Doesn't throw + + + @@ -534,6 +570,16 @@ for(; first != last; ++first) unsigned long + + std::size_t + long long + + + + std::size_t + unsigned long long + + std::size_t float @@ -699,6 +745,10 @@ for(; first != last; ++first) val + + long long, unsigned long long + val when abs(val) <= std::numeric_limits<std::size_t>::max(). + float, double, long double An unspecified value, except that equal arguments shall yield the same result. diff --git a/hash/test/hash_number_test.cpp b/hash/test/hash_number_test.cpp index 12e9bc8..66448e8 100644 --- a/hash/test/hash_number_test.cpp +++ b/hash/test/hash_number_test.cpp @@ -48,7 +48,8 @@ void numeric_test(T*) if (limits::is_integer) { - BOOST_TEST(HASH_NAMESPACE::hash_value(T(-5)) == (std::size_t)T(-5)); + if(limits::is_signed || limits::digits <= std::numeric_limits::digits) + BOOST_TEST(HASH_NAMESPACE::hash_value(T(-5)) == (std::size_t)T(-5)); BOOST_TEST(HASH_NAMESPACE::hash_value(T(0)) == (std::size_t)T(0u)); BOOST_TEST(HASH_NAMESPACE::hash_value(T(10)) == (std::size_t)T(10u)); BOOST_TEST(HASH_NAMESPACE::hash_value(T(25)) == (std::size_t)T(25u)); @@ -111,6 +112,10 @@ void poor_quality_tests(T*) numeric_test((type*) 0); \ limits_test((type*) 0); \ poor_quality_tests((type*) 0); +#define NUMERIC_TEST_NO_LIMITS(type, name) \ + std::cerr<<"Testing: " BOOST_STRINGIZE(name) "\n"; \ + numeric_test((type*) 0); \ + poor_quality_tests((type*) 0); int main() { @@ -128,6 +133,11 @@ int main() NUMERIC_TEST(long, hash_long) NUMERIC_TEST(unsigned long, ulong) +#if defined(BOOST_HAS_LONG_LONG) + NUMERIC_TEST_NO_LIMITS(long long, hash_longlong) + NUMERIC_TEST_NO_LIMITS(unsigned long long, ulonglong) +#endif + NUMERIC_TEST(float, float) NUMERIC_TEST(double, double) NUMERIC_TEST(long double, ldouble) diff --git a/include/boost/functional/hash/hash.hpp b/include/boost/functional/hash/hash.hpp index 56a922f..3f2b5fc 100644 --- a/include/boost/functional/hash/hash.hpp +++ b/include/boost/functional/hash/hash.hpp @@ -28,11 +28,6 @@ #include #endif -#if defined(BOOST_MSVC) -# pragma warning(push) -# pragma warning(disable:4267) -#endif - namespace boost { std::size_t hash_value(bool); @@ -50,11 +45,7 @@ namespace boost std::size_t hash_value(wchar_t); #endif -#if defined(BOOST_HAS_LONG_LONG) && defined(_M_X64) && defined(_WIN64) - // On 64-bit windows std::size_t is a typedef for unsigned long long, which - // isn't due to be supported until Boost 1.35. So add support here. - // (Technically, Boost.Hash isn't actually documented as supporting - // std::size_t. But it would be pretty silly not to). +#if defined(BOOST_HAS_LONG_LONG) std::size_t hash_value(long long); std::size_t hash_value(unsigned long long); #endif @@ -99,6 +90,50 @@ namespace boost // Implementation + namespace hash_detail + { + template + inline std::size_t hash_value_signed(T val) + { + const int size_t_bits = std::numeric_limits::digits; + // ceiling(std::numeric_limits::digits / size_t_bits) - 1 + const int length = (std::numeric_limits::digits - 1) + / size_t_bits; + + std::size_t seed = 0; + T positive = val < 0 ? -1 - val : val; + + // Hopefully, this loop can be unrolled. + for(unsigned int i = length * size_t_bits; i > 0; i -= size_t_bits) + { + seed ^= (std::size_t) (positive >> i) + (seed<<6) + (seed>>2); + } + seed ^= (std::size_t) val + (seed<<6) + (seed>>2); + + return seed; + } + + template + inline std::size_t hash_value_unsigned(T val) + { + const int size_t_bits = std::numeric_limits::digits; + // ceiling(std::numeric_limits::digits / size_t_bits) - 1 + const int length = (std::numeric_limits::digits - 1) + / size_t_bits; + + std::size_t seed = 0; + + // Hopefully, this loop can be unrolled. + for(unsigned int i = length * size_t_bits; i > 0; i -= size_t_bits) + { + seed ^= (std::size_t) (val >> i) + (seed<<6) + (seed>>2); + } + seed ^= (std::size_t) val + (seed<<6) + (seed>>2); + + return seed; + } + } + inline std::size_t hash_value(bool v) { return static_cast(v); @@ -155,15 +190,15 @@ namespace boost } #endif -#if defined(BOOST_HAS_LONG_LONG) && defined(_M_X64) && defined(_WIN64) +#if defined(BOOST_HAS_LONG_LONG) inline std::size_t hash_value(long long v) { - return v; + return hash_detail::hash_value_signed(v); } inline std::size_t hash_value(unsigned long long v) { - return v; + return hash_detail::hash_value_unsigned(v); } #endif @@ -635,9 +670,5 @@ namespace boost #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION } -#if defined(BOOST_MSVC) -# pragma warning(pop) -#endif - #endif