]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Consolidate the occurrances of the prime used as the multiplier when hashing
authorGregory P. Smith <greg@krypto.org>
Sat, 14 Jan 2012 23:31:34 +0000 (15:31 -0800)
committerGregory P. Smith <greg@krypto.org>
Sat, 14 Jan 2012 23:31:34 +0000 (15:31 -0800)
to a single #define instead of having several copies in several files.

This excludes the Modules/ tree (datetime and expat both have a copy
for their own purposes with no need for it to be the same).

Include/pyport.h
Objects/bytesobject.c
Objects/tupleobject.c
Objects/unicodeobject.c

index 62aa53ae21618aeb8f50e8a8923e45636ef09503..269ba68e68f1918130904f51dd1f3f250976e637 100644 (file)
@@ -131,6 +131,9 @@ Used in:  PY_LONG_LONG
 #endif
 #endif
 
+/* Prime multiplier used in string and various other hashes. */
+#define _PyHASH_MULTIPLIER 1000003  /* 0xf4243 */
+
 /* Parameters used for the numeric hash implementation.  See notes for
    _PyHash_Double in Objects/object.c.  Numeric hashes are based on
    reduction modulo the prime 2**_PyHASH_BITS - 1. */
@@ -143,7 +146,7 @@ Used in:  PY_LONG_LONG
 #define _PyHASH_MODULUS (((size_t)1 << _PyHASH_BITS) - 1)
 #define _PyHASH_INF 314159
 #define _PyHASH_NAN 0
-#define _PyHASH_IMAG 1000003UL
+#define _PyHASH_IMAG _PyHASH_MULTIPLIER
 
 /* uintptr_t is the C9X name for an unsigned integral type such that a
  * legitimate void* can be cast to uintptr_t and then back to void* again
index b10cb5d3de5acbb9194173fddcc21c9f89e286c6..8249e5f6df56902332e9bd64bcf293ff8e726df2 100644 (file)
@@ -881,7 +881,7 @@ bytes_hash(PyBytesObject *a)
     p = (unsigned char *) a->ob_sval;
     x = *p << 7;
     while (--len >= 0)
-        x = (1000003*x) ^ *p++;
+        x = (_PyHASH_MULTIPLIER*x) ^ *p++;
     x ^= Py_SIZE(a);
     if (x == -1)
         x = -2;
index 8aacd121143fdcc4ed7557759a5b40ccd11500c1..f6dbc315d90a7e0b74a6d4cff177454a2c4d0bc7 100644 (file)
@@ -315,7 +315,7 @@ tuplehash(PyTupleObject *v)
     register Py_hash_t x, y;
     register Py_ssize_t len = Py_SIZE(v);
     register PyObject **p;
-    Py_hash_t mult = 1000003L;
+    Py_hash_t mult = _PyHASH_MULTIPLIER;
     x = 0x345678L;
     p = v->ob_item;
     while (--len >= 0) {
index c4cfe1bca05cf7a4da8838e8cea812ae77db7067..20528b931d02afc802fbabfb2a3c7c48df692372 100644 (file)
@@ -7666,7 +7666,7 @@ unicode_hash(PyUnicodeObject *self)
     p = self->str;
     x = *p << 7;
     while (--len >= 0)
-        x = (1000003*x) ^ *p++;
+        x = (_PyHASH_MULTIPLIER*x) ^ *p++;
     x ^= Py_SIZE(self);
     if (x == -1)
         x = -2;