Boost logo

Boost-Commit :

From: daniel_james_at_[hidden]
Date: 2008-01-13 14:55:01


Author: danieljames
Date: 2008-01-13 14:55:00 EST (Sun, 13 Jan 2008)
New Revision: 42732
URL: http://svn.boost.org/trac/boost/changeset/42732

Log:
Workaround Open BSD's incorrect numeric_limits.

Text files modified:
   branches/unordered/dev/boost/functional/detail/hash_float.hpp | 48 +++++++++++++++++++---
   branches/unordered/dev/libs/functional/hash/test/hash_float_test.hpp | 84 ++++++++++++++++++++--------------------
   branches/unordered/dev/libs/functional/hash/test/hash_number_test.cpp | 8 +-
   3 files changed, 86 insertions(+), 54 deletions(-)

Modified: branches/unordered/dev/boost/functional/detail/hash_float.hpp
==============================================================================
--- branches/unordered/dev/boost/functional/detail/hash_float.hpp (original)
+++ branches/unordered/dev/boost/functional/detail/hash_float.hpp 2008-01-13 14:55:00 EST (Sun, 13 Jan 2008)
@@ -54,10 +54,43 @@
 
 #endif
 
+// On OpenBSD, numeric_limits is not reliable for long doubles, but
+// the macros defined in <float.h> are.
+
+#if defined(__OpenBSD__)
+#include <float.h>
+#endif
+
 namespace boost
 {
     namespace hash_detail
     {
+ template <class T>
+ struct limits : std::numeric_limits<T> {};
+
+#if defined(__OpenBSD__)
+ template <>
+ struct limits<long double>
+ : std::numeric_limits<long double>
+ {
+ static long double epsilon() {
+ return LDBL_EPSILON;
+ }
+
+ static long double (max)() {
+ return LDBL_MAX;
+ }
+
+ static long double (min)() {
+ return LDBL_MIN;
+ }
+
+ BOOST_STATIC_CONSTANT(int, digits = LDBL_MANT_DIG);
+ BOOST_STATIC_CONSTANT(int, max_exponent = LDBL_MAX_EXP);
+ BOOST_STATIC_CONSTANT(int, min_exponent = LDBL_MIN_EXP);
+ };
+#endif // __OpenBSD__
+
         inline void hash_float_combine(std::size_t& seed, std::size_t value)
         {
             seed ^= value + (seed<<6) + (seed>>2);
@@ -102,29 +135,28 @@
             // sign with the exponent.
             if(v < 0) {
                 v = -v;
- exp += std::numeric_limits<T>::max_exponent -
- std::numeric_limits<T>::min_exponent;
+ exp += limits<T>::max_exponent -
+ 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 = boost::hash_detail::call_ldexp(v,
- std::numeric_limits<std::size_t>::digits + 1);
+ limits<std::size_t>::digits + 1);
             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
- = (std::numeric_limits<T>::digits *
- boost::static_log2<std::numeric_limits<T>::radix>::value
- - 1)
- / std::numeric_limits<std::size_t>::digits;
+ = (limits<T>::digits *
+ boost::static_log2<limits<T>::radix>::value - 1)
+ / limits<std::size_t>::digits;
 
             for(std::size_t i = 0; i != length; ++i)
             {
                 v = boost::hash_detail::call_ldexp(v,
- std::numeric_limits<std::size_t>::digits);
+ limits<std::size_t>::digits);
                 std::size_t part = static_cast<std::size_t>(v);
                 v -= part;
                 hash_float_combine(seed, part);

Modified: branches/unordered/dev/libs/functional/hash/test/hash_float_test.hpp
==============================================================================
--- branches/unordered/dev/libs/functional/hash/test/hash_float_test.hpp (original)
+++ branches/unordered/dev/libs/functional/hash/test/hash_float_test.hpp 2008-01-13 14:55:00 EST (Sun, 13 Jan 2008)
@@ -29,12 +29,12 @@
     std::cerr<<"\n"
         <<"Testing " BOOST_STRINGIZE(HASH_NAMESPACE) "::hash<"<<name<<">\n"
         <<"\n"
- <<"std::numeric_limits<T>::digits = "
- <<std::numeric_limits<T>::digits<<"\n"
- <<"std::numeric_limits<int>::digits = "
- <<std::numeric_limits<int>::digits<<"\n"
- <<"std::numeric_limits<std::size_t>::digits = "
- <<std::numeric_limits<std::size_t>::digits<<"\n"
+ <<"boost::hash_detail::limits<T>::digits = "
+ <<boost::hash_detail::limits<T>::digits<<"\n"
+ <<"boost::hash_detail::limits<int>::digits = "
+ <<boost::hash_detail::limits<int>::digits<<"\n"
+ <<"boost::hash_detail::limits<std::size_t>::digits = "
+ <<boost::hash_detail::limits<std::size_t>::digits<<"\n"
         <<"\n"
         ;
 
@@ -55,11 +55,11 @@
 #if defined(__BORLANDC__)
     std::cerr<<"Not running infinity checks on Borland, as it causes it to crash.\n";
 #else
- if(std::numeric_limits<T>::has_infinity) {
+ if(boost::hash_detail::limits<T>::has_infinity) {
         T infinity = -log(zero);
         T infinity2 = (T) 1. / zero;
         T infinity3 = (T) -1. / minus_zero;
- T infinity4 = std::numeric_limits<T>::infinity();
+ T infinity4 = boost::hash_detail::limits<T>::infinity();
 
         T minus_infinity = log(zero);
         T minus_infinity2 = (T) -1. / zero;
@@ -89,26 +89,26 @@
 
         // This should really be 'has_denorm == denorm_present' but some
         // compilers don't have 'denorm_present'. See also a later use.
- if(std::numeric_limits<T>::has_denorm) {
- if(x1(std::numeric_limits<T>::denorm_min()) == x1(infinity)) {
+ if(boost::hash_detail::limits<T>::has_denorm) {
+ if(x1(boost::hash_detail::limits<T>::denorm_min()) == x1(infinity)) {
                 std::cerr<<"x1(denorm_min) == x1(infinity) == "<<x1(infinity)<<"\n";
             }
- if(x1(std::numeric_limits<T>::denorm_min()) == x1(minus_infinity)) {
+ if(x1(boost::hash_detail::limits<T>::denorm_min()) == x1(minus_infinity)) {
                 std::cerr<<"x1(denorm_min) == x1(-infinity) == "<<x1(minus_infinity)<<"\n";
             }
         }
- if(std::numeric_limits<T>::has_quiet_NaN) {
- if(x1(std::numeric_limits<T>::quiet_NaN()) == x1(infinity)) {
+ if(boost::hash_detail::limits<T>::has_quiet_NaN) {
+ if(x1(boost::hash_detail::limits<T>::quiet_NaN()) == x1(infinity)) {
                 std::cerr<<"x1(quiet_NaN) == x1(infinity) == "<<x1(infinity)<<"\n";
             }
- if(x1(std::numeric_limits<T>::quiet_NaN()) == x1(minus_infinity)) {
+ if(x1(boost::hash_detail::limits<T>::quiet_NaN()) == x1(minus_infinity)) {
                 std::cerr<<"x1(quiet_NaN) == x1(-infinity) == "<<x1(minus_infinity)<<"\n";
             }
         }
     }
 #endif
 
- T max = (std::numeric_limits<T>::max)();
+ T max = (boost::hash_detail::limits<T>::max)();
     T half_max = max / 2;
     T quarter_max = max / 4;
     T three_quarter_max = max - quarter_max;
@@ -142,50 +142,50 @@
     BOOST_TEST(x1(v2) == HASH_NAMESPACE::hash_value(v2));
 #endif
 
- BOOST_TEST(x1(std::numeric_limits<T>::epsilon()) ==
- HASH_NAMESPACE::hash_value(std::numeric_limits<T>::epsilon()));
+ BOOST_TEST(x1(boost::hash_detail::limits<T>::epsilon()) ==
+ HASH_NAMESPACE::hash_value(boost::hash_detail::limits<T>::epsilon()));
 
- BOOST_TEST(std::numeric_limits<T>::epsilon() != (T) 0);
- if(x1(std::numeric_limits<T>::epsilon()) == x1((T) 0))
+ BOOST_TEST(boost::hash_detail::limits<T>::epsilon() != (T) 0);
+ if(x1(boost::hash_detail::limits<T>::epsilon()) == x1((T) 0))
         std::cerr<<"x1(epsilon) == x1(0) == "<<x1((T) 0)<<"\n";
 
- BOOST_TEST(-std::numeric_limits<T>::epsilon() != (T) 0);
- if(x1(-std::numeric_limits<T>::epsilon()) == x1((T) 0))
+ BOOST_TEST(-boost::hash_detail::limits<T>::epsilon() != (T) 0);
+ if(x1(-boost::hash_detail::limits<T>::epsilon()) == x1((T) 0))
         std::cerr<<"x1(-epsilon) == x1(0) == "<<x1((T) 0)<<"\n";
 
- BOOST_TEST((T) 1 + std::numeric_limits<T>::epsilon() != (T) 1);
- if(x1((T) 1 + std::numeric_limits<T>::epsilon()) == x1((T) 1))
+ BOOST_TEST((T) 1 + boost::hash_detail::limits<T>::epsilon() != (T) 1);
+ if(x1((T) 1 + boost::hash_detail::limits<T>::epsilon()) == x1((T) 1))
         std::cerr<<"x1(1 + epsilon) == x1(1) == "<<x1((T) 1)<<"\n";
 
- BOOST_TEST((T) 1 - std::numeric_limits<T>::epsilon() != (T) 1);
- if(x1((T) 1 - std::numeric_limits<T>::epsilon()) == x1((T) 1))
+ BOOST_TEST((T) 1 - boost::hash_detail::limits<T>::epsilon() != (T) 1);
+ if(x1((T) 1 - boost::hash_detail::limits<T>::epsilon()) == x1((T) 1))
         std::cerr<<"x1(1 - epsilon) == x1(1) == "<<x1((T) 1)<<"\n";
 
- BOOST_TEST((T) -1 + std::numeric_limits<T>::epsilon() != (T) -1);
- if(x1((T) -1 + std::numeric_limits<T>::epsilon()) == x1((T) -1))
+ BOOST_TEST((T) -1 + boost::hash_detail::limits<T>::epsilon() != (T) -1);
+ if(x1((T) -1 + boost::hash_detail::limits<T>::epsilon()) == x1((T) -1))
         std::cerr<<"x1(-1 + epsilon) == x1(-1) == "<<x1((T) -1)<<"\n";
 
- BOOST_TEST((T) -1 - std::numeric_limits<T>::epsilon() != (T) -1);
- if(x1((T) -1 - std::numeric_limits<T>::epsilon()) == x1((T) -1))
+ BOOST_TEST((T) -1 - boost::hash_detail::limits<T>::epsilon() != (T) -1);
+ if(x1((T) -1 - boost::hash_detail::limits<T>::epsilon()) == x1((T) -1))
         std::cerr<<"x1(-1 - epsilon) == x1(-1) == "<<x1((T) -1)<<"\n";
 
     // As before.
- if(std::numeric_limits<T>::has_denorm) {
- if(x1(std::numeric_limits<T>::denorm_min()) == x1(zero)) {
+ if(boost::hash_detail::limits<T>::has_denorm) {
+ if(x1(boost::hash_detail::limits<T>::denorm_min()) == x1(zero)) {
             std::cerr<<"x1(denorm_min) == x1(zero) == "<<x1(zero)<<"\n";
         }
 #if !BOOST_WORKAROUND(__DECCXX_VER,<70190006)
         // The Tru64/CXX standard library prior to 7.1 contains a bug in the
- // specialization of std::numeric_limits::denorm_min() for long
+ // specialization of boost::hash_detail::limits::denorm_min() for long
         // doubles which causes this test to fail.
- if(x1(std::numeric_limits<T>::denorm_min()) !=
- HASH_NAMESPACE::hash_value(std::numeric_limits<T>::denorm_min()))
+ if(x1(boost::hash_detail::limits<T>::denorm_min()) !=
+ HASH_NAMESPACE::hash_value(boost::hash_detail::limits<T>::denorm_min()))
         {
- std::cerr<<"x1(std::numeric_limits<T>::denorm_min()) = "
- << x1(std::numeric_limits<T>::denorm_min())
- << "\nhash_value(std::numeric_limits<T>::denorm_min()) = "
+ std::cerr<<"x1(boost::hash_detail::limits<T>::denorm_min()) = "
+ << x1(boost::hash_detail::limits<T>::denorm_min())
+ << "\nhash_value(boost::hash_detail::limits<T>::denorm_min()) = "
                 << HASH_NAMESPACE::hash_value(
- std::numeric_limits<T>::denorm_min())
+ boost::hash_detail::limits<T>::denorm_min())
                 << "\nx1(0) = "<<x1(0)<<"\n";
         }
 #endif
@@ -193,12 +193,12 @@
 
 // NaN also causes borland to crash.
 #if !defined(__BORLANDC__)
- if(std::numeric_limits<T>::has_quiet_NaN) {
- if(x1(std::numeric_limits<T>::quiet_NaN()) == x1(1.0)) {
+ if(boost::hash_detail::limits<T>::has_quiet_NaN) {
+ if(x1(boost::hash_detail::limits<T>::quiet_NaN()) == x1(1.0)) {
             std::cerr<<"x1(quiet_NaN) == x1(1.0) == "<<x1(1.0)<<"\n";
         }
- BOOST_TEST(x1(std::numeric_limits<T>::quiet_NaN()) ==
- HASH_NAMESPACE::hash_value(std::numeric_limits<T>::quiet_NaN()));
+ BOOST_TEST(x1(boost::hash_detail::limits<T>::quiet_NaN()) ==
+ HASH_NAMESPACE::hash_value(boost::hash_detail::limits<T>::quiet_NaN()));
     }
 #endif
 }

Modified: branches/unordered/dev/libs/functional/hash/test/hash_number_test.cpp
==============================================================================
--- branches/unordered/dev/libs/functional/hash/test/hash_number_test.cpp (original)
+++ branches/unordered/dev/libs/functional/hash/test/hash_number_test.cpp 2008-01-13 14:55:00 EST (Sun, 13 Jan 2008)
@@ -31,7 +31,7 @@
 template <class T>
 void numeric_test(T*)
 {
- typedef std::numeric_limits<T> limits;
+ typedef boost::hash_detail::limits<T> limits;
 
     compile_time_tests((T*) 0);
 
@@ -55,7 +55,7 @@
 
     if (limits::is_integer)
     {
- if(limits::is_signed || limits::digits <= std::numeric_limits<std::size_t>::digits)
+ if(limits::is_signed || limits::digits <= boost::hash_detail::limits<std::size_t>::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));
@@ -67,7 +67,7 @@
 template <class T>
 void limits_test(T*)
 {
- typedef std::numeric_limits<T> limits;
+ typedef boost::hash_detail::limits<T> limits;
 
     if(limits::is_specialized)
     {
@@ -98,7 +98,7 @@
 template <class T>
 void poor_quality_tests(T*)
 {
- typedef std::numeric_limits<T> limits;
+ typedef boost::hash_detail::limits<T> limits;
 
     HASH_NAMESPACE::hash<T> x1;
     HASH_NAMESPACE::hash<T> x2;


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