mirror of
https://github.com/boostorg/functional.git
synced 2025-08-02 14:04:27 +02:00
Attempting to work round the dodgy std::numeric_limits<long double> on
some BSDs. [SVN r35345]
This commit is contained in:
@@ -24,12 +24,12 @@ void float_tests(char const* name, T* = 0)
|
|||||||
std::cerr<<"\n"
|
std::cerr<<"\n"
|
||||||
<<"Testing " BOOST_STRINGIZE(HASH_NAMESPACE) "::hash<"<<name<<">\n"
|
<<"Testing " BOOST_STRINGIZE(HASH_NAMESPACE) "::hash<"<<name<<">\n"
|
||||||
<<"\n"
|
<<"\n"
|
||||||
<<"std::numeric_limits<T>::digits = "
|
<<"boost::hash_detail::float_limits<T>::digits = "
|
||||||
<<std::numeric_limits<T>::digits<<"\n"
|
<<boost::hash_detail::float_limits<T>::digits<<"\n"
|
||||||
<<"std::numeric_limits<int>::digits = "
|
<<"boost::hash_detail::float_limits<int>::digits = "
|
||||||
<<std::numeric_limits<int>::digits<<"\n"
|
<<boost::hash_detail::float_limits<int>::digits<<"\n"
|
||||||
<<"std::numeric_limits<std::size_t>::digits = "
|
<<"boost::hash_detail::float_limits<std::size_t>::digits = "
|
||||||
<<std::numeric_limits<std::size_t>::digits<<"\n"
|
<<boost::hash_detail::float_limits<std::size_t>::digits<<"\n"
|
||||||
<<"\n"
|
<<"\n"
|
||||||
;
|
;
|
||||||
|
|
||||||
@@ -50,11 +50,11 @@ void float_tests(char const* name, T* = 0)
|
|||||||
#if defined(__BORLANDC__)
|
#if defined(__BORLANDC__)
|
||||||
std::cerr<<"Not running infinity checks on Borland, as it causes it to crash.\n";
|
std::cerr<<"Not running infinity checks on Borland, as it causes it to crash.\n";
|
||||||
#else
|
#else
|
||||||
if(std::numeric_limits<T>::has_infinity) {
|
if(boost::hash_detail::float_limits<T>::has_infinity) {
|
||||||
T infinity = -log(zero);
|
T infinity = -log(zero);
|
||||||
T infinity2 = (T) 1. / zero;
|
T infinity2 = (T) 1. / zero;
|
||||||
T infinity3 = (T) -1. / minus_zero;
|
T infinity3 = (T) -1. / minus_zero;
|
||||||
T infinity4 = std::numeric_limits<T>::infinity();
|
T infinity4 = boost::hash_detail::float_limits<T>::infinity();
|
||||||
|
|
||||||
T minus_infinity = log(zero);
|
T minus_infinity = log(zero);
|
||||||
T minus_infinity2 = (T) -1. / zero;
|
T minus_infinity2 = (T) -1. / zero;
|
||||||
@@ -84,26 +84,26 @@ void float_tests(char const* name, T* = 0)
|
|||||||
|
|
||||||
// This should really be 'has_denorm == denorm_present' but some
|
// This should really be 'has_denorm == denorm_present' but some
|
||||||
// compilers don't have 'denorm_present'. See also a later use.
|
// compilers don't have 'denorm_present'. See also a later use.
|
||||||
if(std::numeric_limits<T>::has_denorm) {
|
if(boost::hash_detail::float_limits<T>::has_denorm) {
|
||||||
if(x1(std::numeric_limits<T>::denorm_min()) == x1(infinity)) {
|
if(x1(boost::hash_detail::float_limits<T>::denorm_min()) == x1(infinity)) {
|
||||||
std::cerr<<"x1(denorm_min) == x1(infinity) == "<<x1(infinity)<<"\n";
|
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::float_limits<T>::denorm_min()) == x1(minus_infinity)) {
|
||||||
std::cerr<<"x1(denorm_min) == x1(-infinity) == "<<x1(minus_infinity)<<"\n";
|
std::cerr<<"x1(denorm_min) == x1(-infinity) == "<<x1(minus_infinity)<<"\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(std::numeric_limits<T>::has_quiet_NaN) {
|
if(boost::hash_detail::float_limits<T>::has_quiet_NaN) {
|
||||||
if(x1(std::numeric_limits<T>::quiet_NaN()) == x1(infinity)) {
|
if(x1(boost::hash_detail::float_limits<T>::quiet_NaN()) == x1(infinity)) {
|
||||||
std::cerr<<"x1(quiet_NaN) == x1(infinity) == "<<x1(infinity)<<"\n";
|
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::float_limits<T>::quiet_NaN()) == x1(minus_infinity)) {
|
||||||
std::cerr<<"x1(quiet_NaN) == x1(-infinity) == "<<x1(minus_infinity)<<"\n";
|
std::cerr<<"x1(quiet_NaN) == x1(-infinity) == "<<x1(minus_infinity)<<"\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
T max = (std::numeric_limits<T>::max)();
|
T max = (boost::hash_detail::float_limits<T>::max)();
|
||||||
T half_max = max / 2;
|
T half_max = max / 2;
|
||||||
T quarter_max = max / 4;
|
T quarter_max = max / 4;
|
||||||
T three_quarter_max = max - quarter_max;
|
T three_quarter_max = max - quarter_max;
|
||||||
@@ -132,50 +132,50 @@ void float_tests(char const* name, T* = 0)
|
|||||||
BOOST_TEST(x1(v1) == HASH_NAMESPACE::hash_value(v1));
|
BOOST_TEST(x1(v1) == HASH_NAMESPACE::hash_value(v1));
|
||||||
BOOST_TEST(x1(v2) == HASH_NAMESPACE::hash_value(v2));
|
BOOST_TEST(x1(v2) == HASH_NAMESPACE::hash_value(v2));
|
||||||
|
|
||||||
BOOST_TEST(x1(std::numeric_limits<T>::epsilon()) ==
|
BOOST_TEST(x1(boost::hash_detail::float_limits<T>::epsilon()) ==
|
||||||
HASH_NAMESPACE::hash_value(std::numeric_limits<T>::epsilon()));
|
HASH_NAMESPACE::hash_value(boost::hash_detail::float_limits<T>::epsilon()));
|
||||||
|
|
||||||
BOOST_TEST(std::numeric_limits<T>::epsilon() != (T) 0);
|
BOOST_TEST(boost::hash_detail::float_limits<T>::epsilon() != (T) 0);
|
||||||
if(x1(std::numeric_limits<T>::epsilon()) == x1((T) 0))
|
if(x1(boost::hash_detail::float_limits<T>::epsilon()) == x1((T) 0))
|
||||||
std::cerr<<"x1(epsilon) == x1(0) == "<<x1((T) 0)<<"\n";
|
std::cerr<<"x1(epsilon) == x1(0) == "<<x1((T) 0)<<"\n";
|
||||||
|
|
||||||
BOOST_TEST(-std::numeric_limits<T>::epsilon() != (T) 0);
|
BOOST_TEST(-boost::hash_detail::float_limits<T>::epsilon() != (T) 0);
|
||||||
if(x1(-std::numeric_limits<T>::epsilon()) == x1((T) 0))
|
if(x1(-boost::hash_detail::float_limits<T>::epsilon()) == x1((T) 0))
|
||||||
std::cerr<<"x1(-epsilon) == x1(0) == "<<x1((T) 0)<<"\n";
|
std::cerr<<"x1(-epsilon) == x1(0) == "<<x1((T) 0)<<"\n";
|
||||||
|
|
||||||
BOOST_TEST((T) 1 + std::numeric_limits<T>::epsilon() != (T) 1);
|
BOOST_TEST((T) 1 + boost::hash_detail::float_limits<T>::epsilon() != (T) 1);
|
||||||
if(x1((T) 1 + std::numeric_limits<T>::epsilon()) == x1((T) 1))
|
if(x1((T) 1 + boost::hash_detail::float_limits<T>::epsilon()) == x1((T) 1))
|
||||||
std::cerr<<"x1(1 + epsilon) == x1(1) == "<<x1((T) 1)<<"\n";
|
std::cerr<<"x1(1 + epsilon) == x1(1) == "<<x1((T) 1)<<"\n";
|
||||||
|
|
||||||
BOOST_TEST((T) 1 - std::numeric_limits<T>::epsilon() != (T) 1);
|
BOOST_TEST((T) 1 - boost::hash_detail::float_limits<T>::epsilon() != (T) 1);
|
||||||
if(x1((T) 1 - std::numeric_limits<T>::epsilon()) == x1((T) 1))
|
if(x1((T) 1 - boost::hash_detail::float_limits<T>::epsilon()) == x1((T) 1))
|
||||||
std::cerr<<"x1(1 - epsilon) == x1(1) == "<<x1((T) 1)<<"\n";
|
std::cerr<<"x1(1 - epsilon) == x1(1) == "<<x1((T) 1)<<"\n";
|
||||||
|
|
||||||
BOOST_TEST((T) -1 + std::numeric_limits<T>::epsilon() != (T) -1);
|
BOOST_TEST((T) -1 + boost::hash_detail::float_limits<T>::epsilon() != (T) -1);
|
||||||
if(x1((T) -1 + std::numeric_limits<T>::epsilon()) == x1((T) -1))
|
if(x1((T) -1 + boost::hash_detail::float_limits<T>::epsilon()) == x1((T) -1))
|
||||||
std::cerr<<"x1(-1 + epsilon) == x1(-1) == "<<x1((T) -1)<<"\n";
|
std::cerr<<"x1(-1 + epsilon) == x1(-1) == "<<x1((T) -1)<<"\n";
|
||||||
|
|
||||||
BOOST_TEST((T) -1 - std::numeric_limits<T>::epsilon() != (T) -1);
|
BOOST_TEST((T) -1 - boost::hash_detail::float_limits<T>::epsilon() != (T) -1);
|
||||||
if(x1((T) -1 - std::numeric_limits<T>::epsilon()) == x1((T) -1))
|
if(x1((T) -1 - boost::hash_detail::float_limits<T>::epsilon()) == x1((T) -1))
|
||||||
std::cerr<<"x1(-1 - epsilon) == x1(-1) == "<<x1((T) -1)<<"\n";
|
std::cerr<<"x1(-1 - epsilon) == x1(-1) == "<<x1((T) -1)<<"\n";
|
||||||
|
|
||||||
// As before.
|
// As before.
|
||||||
if(std::numeric_limits<T>::has_denorm) {
|
if(boost::hash_detail::float_limits<T>::has_denorm) {
|
||||||
if(x1(std::numeric_limits<T>::denorm_min()) == x1(zero)) {
|
if(x1(boost::hash_detail::float_limits<T>::denorm_min()) == x1(zero)) {
|
||||||
std::cerr<<"x1(denorm_min) == x1(zero) == "<<x1(zero)<<"\n";
|
std::cerr<<"x1(denorm_min) == x1(zero) == "<<x1(zero)<<"\n";
|
||||||
}
|
}
|
||||||
#if !BOOST_WORKAROUND(__DECCXX_VER,<70190006)
|
#if !BOOST_WORKAROUND(__DECCXX_VER,<70190006)
|
||||||
// The Tru64/CXX standard library prior to 7.1 contains a bug in the
|
// 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::float_limits::denorm_min() for long
|
||||||
// doubles which causes this test to fail.
|
// doubles which causes this test to fail.
|
||||||
if(x1(std::numeric_limits<T>::denorm_min()) !=
|
if(x1(boost::hash_detail::float_limits<T>::denorm_min()) !=
|
||||||
HASH_NAMESPACE::hash_value(std::numeric_limits<T>::denorm_min()))
|
HASH_NAMESPACE::hash_value(boost::hash_detail::float_limits<T>::denorm_min()))
|
||||||
{
|
{
|
||||||
std::cerr<<"x1(std::numeric_limits<T>::denorm_min()) = "
|
std::cerr<<"x1(boost::hash_detail::float_limits<T>::denorm_min()) = "
|
||||||
<< x1(std::numeric_limits<T>::denorm_min())
|
<< x1(boost::hash_detail::float_limits<T>::denorm_min())
|
||||||
<< "\nhash_value(std::numeric_limits<T>::denorm_min()) = "
|
<< "\nhash_value(boost::hash_detail::float_limits<T>::denorm_min()) = "
|
||||||
<< HASH_NAMESPACE::hash_value(
|
<< HASH_NAMESPACE::hash_value(
|
||||||
std::numeric_limits<T>::denorm_min())
|
boost::hash_detail::float_limits<T>::denorm_min())
|
||||||
<< "\nx1(0) = "<<x1(0)<<"\n";
|
<< "\nx1(0) = "<<x1(0)<<"\n";
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -183,12 +183,12 @@ void float_tests(char const* name, T* = 0)
|
|||||||
|
|
||||||
// NaN also causes borland to crash.
|
// NaN also causes borland to crash.
|
||||||
#if !defined(__BORLANDC__)
|
#if !defined(__BORLANDC__)
|
||||||
if(std::numeric_limits<T>::has_quiet_NaN) {
|
if(boost::hash_detail::float_limits<T>::has_quiet_NaN) {
|
||||||
if(x1(std::numeric_limits<T>::quiet_NaN()) == x1(1.0)) {
|
if(x1(boost::hash_detail::float_limits<T>::quiet_NaN()) == x1(1.0)) {
|
||||||
std::cerr<<"x1(quiet_NaN) == x1(1.0) == "<<x1(1.0)<<"\n";
|
std::cerr<<"x1(quiet_NaN) == x1(1.0) == "<<x1(1.0)<<"\n";
|
||||||
}
|
}
|
||||||
BOOST_TEST(x1(std::numeric_limits<T>::quiet_NaN()) ==
|
BOOST_TEST(x1(boost::hash_detail::float_limits<T>::quiet_NaN()) ==
|
||||||
HASH_NAMESPACE::hash_value(std::numeric_limits<T>::quiet_NaN()));
|
HASH_NAMESPACE::hash_value(boost::hash_detail::float_limits<T>::quiet_NaN()));
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
@@ -35,10 +35,61 @@
|
|||||||
# endif
|
# endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// On FreeBSD and OpenBSD, numeric_limits is not reliable for long doubles, but
|
||||||
|
// the macros defined in <float.h> are. I don't know if this is also be the case for
|
||||||
|
// other BSDs, but using the macros if they're available seems like the best
|
||||||
|
// choice.
|
||||||
|
|
||||||
|
#if defined(__FreeBSD__) || defined(__NetBSD__) && \
|
||||||
|
defined(__OpenBSD__) || defined(__DragonFly__)
|
||||||
|
#include <float.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace boost
|
namespace boost
|
||||||
{
|
{
|
||||||
namespace hash_detail
|
namespace hash_detail
|
||||||
{
|
{
|
||||||
|
template <class T>
|
||||||
|
struct float_limits : std::numeric_limits<T> {};
|
||||||
|
|
||||||
|
#if defined(__FreeBSD__) || defined(__NetBSD__) && \
|
||||||
|
defined(__OpenBSD__) || defined(__DragonFly__)
|
||||||
|
template <>
|
||||||
|
struct float_limits<long double>
|
||||||
|
: std::numeric_limits<long double>
|
||||||
|
{
|
||||||
|
#if defined(LDBL_EPSILON)
|
||||||
|
static long double epsilon() {
|
||||||
|
return LDBL_EPSILON;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(LDBL_MANT_DIG)
|
||||||
|
BOOST_STATIC_CONSTANT(int, digits = LDBL_MANT_DIG);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(LDBL_MAX)
|
||||||
|
static long double (max)() {
|
||||||
|
return LDBL_MAX;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(LDBL_MIN)
|
||||||
|
static long double (min)() {
|
||||||
|
return LDBL_MIN;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(LDBL_MAX_EXP)
|
||||||
|
BOOST_STATIC_CONSTANT(int, max_exponent = LDBL_MAX_EXP);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(LDBL_MIN_EXP)
|
||||||
|
BOOST_STATIC_CONSTANT(int, min_exponent = LDBL_MIN_EXP);
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
#endif // __FreeBSD__/__NetBSD__/__OpenBSD__/__DragonFly__
|
||||||
|
|
||||||
inline void hash_float_combine(std::size_t& seed, std::size_t value)
|
inline void hash_float_combine(std::size_t& seed, std::size_t value)
|
||||||
{
|
{
|
||||||
seed ^= value + (seed<<6) + (seed>>2);
|
seed ^= value + (seed<<6) + (seed>>2);
|
||||||
@@ -63,19 +114,19 @@ namespace boost
|
|||||||
//BOOST_ASSERT(0 <= v && v < 0.5);
|
//BOOST_ASSERT(0 <= v && v < 0.5);
|
||||||
|
|
||||||
v = boost::hash_detail::call_ldexp(v,
|
v = boost::hash_detail::call_ldexp(v,
|
||||||
std::numeric_limits<std::size_t>::digits + 1);
|
float_limits<std::size_t>::digits + 1);
|
||||||
std::size_t seed = static_cast<std::size_t>(v);
|
std::size_t seed = static_cast<std::size_t>(v);
|
||||||
v -= seed;
|
v -= seed;
|
||||||
|
|
||||||
// ceiling(digits(T) * log2(radix(T))/ digits(size_t)) - 1;
|
// ceiling(digits(T) * log2(radix(T))/ digits(size_t)) - 1;
|
||||||
std::size_t const length
|
std::size_t const length
|
||||||
= (std::numeric_limits<T>::digits *
|
= (float_limits<T>::digits *
|
||||||
boost::static_log2<std::numeric_limits<T>::radix>::value - 1)
|
boost::static_log2<float_limits<T>::radix>::value - 1)
|
||||||
/ std::numeric_limits<std::size_t>::digits;
|
/ float_limits<std::size_t>::digits;
|
||||||
|
|
||||||
for(std::size_t i = 0; i < length; ++i)
|
for(std::size_t i = 0; i < length; ++i)
|
||||||
{
|
{
|
||||||
v = boost::hash_detail::call_ldexp(v, std::numeric_limits<std::size_t>::digits);
|
v = boost::hash_detail::call_ldexp(v, float_limits<std::size_t>::digits);
|
||||||
std::size_t part = static_cast<std::size_t>(v);
|
std::size_t part = static_cast<std::size_t>(v);
|
||||||
v -= part;
|
v -= part;
|
||||||
hash_float_combine(seed, part);
|
hash_float_combine(seed, part);
|
||||||
|
Reference in New Issue
Block a user