Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r60805 - in trunk: boost/functional/hash/detail libs/functional/hash/test
From: daniel_james_at_[hidden]
Date: 2010-03-24 04:49:00


Author: danieljames
Date: 2010-03-24 04:49:00 EDT (Wed, 24 Mar 2010)
New Revision: 60805
URL: http://svn.boost.org/trac/boost/changeset/60805

Log:
Remove optimization which causes 0.5 to hash to 0. Refs #4038.

I have an internal requirement the 0 hashes to 0, a better solution
might be to remove that, put the optimization back and hash 0 to another
value. Or alternatively, use the main combine function instead.
Text files modified:
   trunk/boost/functional/hash/detail/hash_float_generic.hpp | 8 +++-----
   trunk/libs/functional/hash/test/hash_float_test.hpp | 17 +++++++++++++++++
   2 files changed, 20 insertions(+), 5 deletions(-)

Modified: trunk/boost/functional/hash/detail/hash_float_generic.hpp
==============================================================================
--- trunk/boost/functional/hash/detail/hash_float_generic.hpp (original)
+++ trunk/boost/functional/hash/detail/hash_float_generic.hpp 2010-03-24 04:49:00 EDT (Wed, 24 Mar 2010)
@@ -51,17 +51,15 @@
                     limits<T>::min_exponent;
             }
 
- // The result of frexp is always between 0.5 and 1, so its
- // top bit will always be 1. Subtract by 0.5 to remove that.
- v -= T(0.5);
- v = ldexp(v, limits<std::size_t>::digits + 1);
+ v = ldexp(v, limits<std::size_t>::digits);
             std::size_t seed = static_cast<std::size_t>(v);
             v -= seed;
 
             // ceiling(digits(T) * log2(radix(T))/ digits(size_t)) - 1;
             std::size_t const length
                 = (limits<T>::digits *
- boost::static_log2<limits<T>::radix>::value - 1)
+ boost::static_log2<limits<T>::radix>::value
+ + limits<std::size_t>::digits - 1)
                 / limits<std::size_t>::digits;
 
             for(std::size_t i = 0; i != length; ++i)

Modified: trunk/libs/functional/hash/test/hash_float_test.hpp
==============================================================================
--- trunk/libs/functional/hash/test/hash_float_test.hpp (original)
+++ trunk/libs/functional/hash/test/hash_float_test.hpp 2010-03-24 04:49:00 EDT (Wed, 24 Mar 2010)
@@ -79,6 +79,11 @@
     BOOST_TEST(x1(minus_zero) == HASH_NAMESPACE::hash_value(minus_zero));
 #endif
 
+ BOOST_TEST(x1(zero) != x1(0.5));
+ BOOST_TEST(x1(minus_zero) != x1(0.5));
+ BOOST_TEST(x1(0.5) != x1(-0.5));
+ BOOST_TEST(x1(1) != x1(-1));
+
     using namespace std;
 
 // Doing anything with infinity causes borland to crash.
@@ -176,6 +181,12 @@
     BOOST_TEST(half_max != three_quarter_max);
     BOOST_TEST(quarter_max != three_quarter_max);
 
+ BOOST_TEST(max != -max);
+ BOOST_TEST(half_max != -half_max);
+ BOOST_TEST(quarter_max != -quarter_max);
+ BOOST_TEST(three_quarter_max != -three_quarter_max);
+
+
 #if defined(TEST_EXTENSIONS)
     BOOST_TEST(x1(max) == HASH_NAMESPACE::hash_value(max));
     BOOST_TEST(x1(half_max) == HASH_NAMESPACE::hash_value(half_max));
@@ -197,6 +208,12 @@
     BOOST_TEST(x1(half_max) != x1(three_quarter_max));
     BOOST_TEST(x1(three_quarter_max) == x1(three_quarter_max));
 
+ BOOST_TEST(x1(max) != x1(-max));
+ BOOST_TEST(x1(half_max) != x1(-half_max));
+ BOOST_TEST(x1(quarter_max) != x1(-quarter_max));
+ BOOST_TEST(x1(three_quarter_max) != x1(-three_quarter_max));
+
+
 // Intel with gcc stdlib sometimes segfaults on calls to asin and acos.
 #if !((defined(__INTEL_COMPILER) || defined(__ICL) || \
         defined(__ICC) || defined(__ECC)) && \


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk