From e1149349c158364854bcfece3c6d80ebccb26f28 Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Wed, 25 Jan 2012 22:12:43 +0000 Subject: QHash benchmark: improve Java's hash MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added a bit of documentation to the Java-like hashing function. Change-Id: I3f44eee305d91b76f0f89cd1acf21f6430b9482b Reviewed-by: João Abecasis Reviewed-by: Robin Burchell --- tests/benchmarks/corelib/tools/qhash/outofline.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'tests/benchmarks/corelib') diff --git a/tests/benchmarks/corelib/tools/qhash/outofline.cpp b/tests/benchmarks/corelib/tools/qhash/outofline.cpp index 88fd9c144d..21740536dd 100644 --- a/tests/benchmarks/corelib/tools/qhash/outofline.cpp +++ b/tests/benchmarks/corelib/tools/qhash/outofline.cpp @@ -87,6 +87,17 @@ uint qHash(const String &str) return h; } +// The Java's hashing algorithm for strings is a variation of D. J. Bernstein +// hashing algorithm appeared here http://cr.yp.to/cdb/cdb.txt +// and informally known as DJB33XX - DJB's 33 Times Xor. +// Java uses DJB31XA, that is, 31 Times Add. +// The original algorithm was a loop around "(h << 5) + h ^ c", +// which is indeed "h * 33 ^ c"; it was then changed to +// "(h << 5) - h ^ c", so "h * 31 ^ c", and the XOR changed to a sum: +// "(h << 5) - h + c", which can save some assembly instructions. +// Still, we can avoid writing the multiplication as "(h << 5) - h" +// -- the compiler will turn it into a shift and an addition anyway +// (for instance, gcc 4.4 does that even at -O0). uint qHash(const JavaString &str) { const unsigned short *p = (unsigned short *)str.constData(); -- cgit v1.2.3