summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qhashfunctions.h
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@qt.io>2020-02-09 15:51:03 +0100
committerLars Knoll <lars.knoll@qt.io>2020-04-09 20:04:00 +0200
commit73ddfa25b41ddf475b70b7089e3b4d2402494f01 (patch)
tree59f3399c54f19b2e772640e126e55620774c6399 /src/corelib/tools/qhashfunctions.h
parent09bdf907cfc41fc20c809c26a5c79cf3b4bf3b14 (diff)
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>
Diffstat (limited to 'src/corelib/tools/qhashfunctions.h')
-rw-r--r--src/corelib/tools/qhashfunctions.h9
1 files changed, 8 insertions, 1 deletions
diff --git a/src/corelib/tools/qhashfunctions.h b/src/corelib/tools/qhashfunctions.h
index f519925798..ef83682433 100644
--- a/src/corelib/tools/qhashfunctions.h
+++ b/src/corelib/tools/qhashfunctions.h
@@ -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);
}
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;
#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;