QHash benchmark: improve Java's hash

Added a bit of documentation to the Java-like hashing function.

Change-Id: I3f44eee305d91b76f0f89cd1acf21f6430b9482b
Reviewed-by: João Abecasis <joao.abecasis@nokia.com>
Reviewed-by: Robin Burchell <robin+qt@viroteck.net>
This commit is contained in:
Giuseppe D'Angelo 2012-01-25 22:12:43 +00:00 committed by Qt by Nokia
parent 071561f0eb
commit e1149349c1

View File

@ -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();