Optimize hashing of floating point numbers

Change-Id: Id5e091b135c006b10987f229f45319228edb8675
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Lars Knoll 2020-02-09 15:51:03 +01:00
parent 09bdf907cf
commit 73ddfa25b4
2 changed files with 24 additions and 10 deletions

View File

@ -769,17 +769,12 @@ uint qt_hash(QStringView key, uint chained) noexcept
Returns the hash value for the \a key, using \a seed to seed the calculation. Returns the hash value for the \a key, using \a seed to seed the calculation.
*/ */
/*! \relates QHash /*! \fn size_t qHash(float key, size_t seed) noexcept
\relates QHash
\since 5.3 \since 5.3
Returns the hash value for the \a key, using \a seed to seed the calculation. Returns the hash value for the \a key, using \a seed to seed the calculation.
*/ */
size_t qHash(float key, size_t seed) noexcept
{
// ensure -0 gets mapped to 0
key += 0.0f;
return murmurhash(&key, sizeof(key), seed);
}
/*! \relates QHash /*! \relates QHash
\since 5.3 \since 5.3
@ -790,7 +785,13 @@ size_t qHash(double key, size_t seed) noexcept
{ {
// ensure -0 gets mapped to 0 // ensure -0 gets mapped to 0
key += 0.0; key += 0.0;
if constexpr (sizeof(double) == sizeof(size_t)) {
size_t k;
memcpy(&k, &key, sizeof(double));
return QHashPrivate::hash(k, seed);
} else {
return murmurhash(&key, sizeof(key), seed); return murmurhash(&key, sizeof(key), seed);
}
} }
#if !defined(Q_OS_DARWIN) || defined(Q_CLANG_QDOC) #if !defined(Q_OS_DARWIN) || defined(Q_CLANG_QDOC)
@ -803,7 +804,13 @@ size_t qHash(long double key, size_t seed) noexcept
{ {
// ensure -0 gets mapped to 0 // ensure -0 gets mapped to 0
key += static_cast<long double>(0.0); key += static_cast<long double>(0.0);
if constexpr (sizeof(long double) == sizeof(size_t)) {
size_t k;
memcpy(&k, &key, sizeof(long double));
return QHashPrivate::hash(k, seed);
} else {
return murmurhash(&key, sizeof(key), seed); return murmurhash(&key, sizeof(key), seed);
}
} }
#endif #endif

View File

@ -120,7 +120,14 @@ Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline size_t qHash(quint64 key, size_t s
return QHashPrivate::hash(size_t(key), seed); return QHashPrivate::hash(size_t(key), seed);
} }
Q_DECL_CONST_FUNCTION constexpr inline size_t qHash(qint64 key, size_t seed = 0) noexcept { return qHash(quint64(key), seed); } Q_DECL_CONST_FUNCTION constexpr inline size_t qHash(qint64 key, size_t seed = 0) noexcept { return qHash(quint64(key), seed); }
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION size_t qHash(float key, size_t seed = 0) noexcept; Q_DECL_CONST_FUNCTION inline size_t qHash(float key, size_t seed = 0) noexcept
{
// ensure -0 gets mapped to 0
key += 0.0f;
uint k;
memcpy(&k, &key, sizeof(float));
return QHashPrivate::hash(k, seed);
}
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION size_t qHash(double key, size_t seed = 0) noexcept; Q_CORE_EXPORT Q_DECL_CONST_FUNCTION size_t qHash(double key, size_t seed = 0) noexcept;
#if !defined(Q_OS_DARWIN) || defined(Q_CLANG_QDOC) #if !defined(Q_OS_DARWIN) || defined(Q_CLANG_QDOC)
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION size_t qHash(long double key, size_t seed = 0) noexcept; Q_CORE_EXPORT Q_DECL_CONST_FUNCTION size_t qHash(long double key, size_t seed = 0) noexcept;