mirror of git://gcc.gnu.org/git/gcc.git
PR libstdc++/21193 (float, double, long double)
2005-07-14 Paolo Carlini <pcarlini@suse.de> PR libstdc++/21193 (float, double, long double) * include/tr1/functional (hash<float>, hash<double>): Reimplement exploiting the Fnv_hash<>::hash helper. (hash<long double>): Reimplement using frexp (in this case, due to random padding bits, the former approach is not generally viable). From-SVN: r102043
This commit is contained in:
parent
06277571f4
commit
dbd160bff8
|
|
@ -1,3 +1,12 @@
|
||||||
|
2005-07-14 Paolo Carlini <pcarlini@suse.de>
|
||||||
|
|
||||||
|
PR libstdc++/21193 (float, double, long double)
|
||||||
|
* include/tr1/functional (hash<float>, hash<double>):
|
||||||
|
Reimplement exploiting the Fnv_hash<>::hash helper.
|
||||||
|
(hash<long double>): Reimplement using frexp (in this
|
||||||
|
case, due to random padding bits, the former approach
|
||||||
|
is not generally viable).
|
||||||
|
|
||||||
2005-07-13 Paolo Carlini <pcarlini@suse.de>
|
2005-07-13 Paolo Carlini <pcarlini@suse.de>
|
||||||
|
|
||||||
PR libstdc++/21193 (string & wstring)
|
PR libstdc++/21193 (string & wstring)
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,7 @@
|
||||||
#include <bits/cpp_type_traits.h>
|
#include <bits/cpp_type_traits.h>
|
||||||
#include <string> // for std::tr1::hash
|
#include <string> // for std::tr1::hash
|
||||||
#include <cstdlib> // for std::abort
|
#include <cstdlib> // for std::abort
|
||||||
|
#include <cmath> // for std::frexp
|
||||||
#include <tr1/tuple>
|
#include <tr1/tuple>
|
||||||
|
|
||||||
namespace std
|
namespace std
|
||||||
|
|
@ -1116,10 +1117,6 @@ namespace tr1
|
||||||
tr1_hashtable_define_trivial_hash(unsigned int);
|
tr1_hashtable_define_trivial_hash(unsigned int);
|
||||||
tr1_hashtable_define_trivial_hash(unsigned long);
|
tr1_hashtable_define_trivial_hash(unsigned long);
|
||||||
|
|
||||||
tr1_hashtable_define_trivial_hash(float);
|
|
||||||
tr1_hashtable_define_trivial_hash(double);
|
|
||||||
tr1_hashtable_define_trivial_hash(long double);
|
|
||||||
|
|
||||||
#undef tr1_hashtable_define_trivial_hash
|
#undef tr1_hashtable_define_trivial_hash
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
|
@ -1133,7 +1130,7 @@ namespace tr1
|
||||||
// Fowler / Noll / Vo (FNV) Hash (type FNV-1a)
|
// Fowler / Noll / Vo (FNV) Hash (type FNV-1a)
|
||||||
// (used by the next specializations of std::tr1::hash<>)
|
// (used by the next specializations of std::tr1::hash<>)
|
||||||
|
|
||||||
// Dummy generic implementation (for sizeof(size_t) != 4,8).
|
// Dummy generic implementation (for sizeof(size_t) != 4, 8).
|
||||||
template<std::size_t = sizeof(std::size_t)>
|
template<std::size_t = sizeof(std::size_t)>
|
||||||
struct Fnv_hash
|
struct Fnv_hash
|
||||||
{
|
{
|
||||||
|
|
@ -1179,9 +1176,9 @@ namespace tr1
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// XXX String hash probably shouldn't be an inline member function,
|
// XXX String and floating point hashes probably shouldn't be inline
|
||||||
// since it's nontrivial. Once we have the framework for TR1 .cc
|
// member functions, since are nontrivial. Once we have the framework
|
||||||
// files, this should go in one.
|
// for TR1 .cc files, these should go in one.
|
||||||
template<>
|
template<>
|
||||||
struct hash<std::string>
|
struct hash<std::string>
|
||||||
{
|
{
|
||||||
|
|
@ -1203,6 +1200,69 @@ namespace tr1
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct hash<float>
|
||||||
|
{
|
||||||
|
std::size_t
|
||||||
|
operator()(float fval) const
|
||||||
|
{
|
||||||
|
std::size_t result = 0;
|
||||||
|
|
||||||
|
// 0 and -0 both hash to zero.
|
||||||
|
if (fval != 0.0f)
|
||||||
|
result = Fnv_hash<>::hash(reinterpret_cast<const char*>(&fval),
|
||||||
|
sizeof(fval));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct hash<double>
|
||||||
|
{
|
||||||
|
std::size_t
|
||||||
|
operator()(double dval) const
|
||||||
|
{
|
||||||
|
std::size_t result = 0;
|
||||||
|
|
||||||
|
// 0 and -0 both hash to zero.
|
||||||
|
if (dval != 0.0)
|
||||||
|
result = Fnv_hash<>::hash(reinterpret_cast<const char*>(&dval),
|
||||||
|
sizeof(dval));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// For long double, careful with random padding bits (e.g., on x86,
|
||||||
|
// 10 bytes -> 12 bytes) and resort to frexp.
|
||||||
|
template<>
|
||||||
|
struct hash<long double>
|
||||||
|
{
|
||||||
|
std::size_t
|
||||||
|
operator()(long double ldval) const
|
||||||
|
{
|
||||||
|
std::size_t result = 0;
|
||||||
|
|
||||||
|
int exponent;
|
||||||
|
ldval = std::frexp(ldval, &exponent);
|
||||||
|
ldval = ldval < 0.0l ? -(ldval + 0.5l) : ldval;
|
||||||
|
|
||||||
|
const long double mult = std::numeric_limits<std::size_t>::max() + 1.0l;
|
||||||
|
ldval *= mult;
|
||||||
|
|
||||||
|
// Try to use all the bits of the mantissa (really necessary only
|
||||||
|
// on 32-bit targets, at least for 80-bit floating point formats).
|
||||||
|
const std::size_t hibits = (std::size_t)ldval;
|
||||||
|
ldval = (ldval - (long double)hibits) * mult;
|
||||||
|
|
||||||
|
const std::size_t coeff =
|
||||||
|
(std::numeric_limits<std::size_t>::max()
|
||||||
|
/ std::numeric_limits<long double>::max_exponent);
|
||||||
|
|
||||||
|
result = hibits + (std::size_t)ldval + coeff * exponent;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue