]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-111389: Add PyHASH_MULTIPLIER constant (#119214)
authorVictor Stinner <vstinner@python.org>
Tue, 21 May 2024 17:51:51 +0000 (19:51 +0200)
committerGitHub <noreply@github.com>
Tue, 21 May 2024 17:51:51 +0000 (19:51 +0200)
Doc/c-api/hash.rst
Include/cpython/pyhash.h
Misc/NEWS.d/next/C API/2024-05-20-10-35-22.gh-issue-111389.a6axBk.rst [new file with mode: 0644]
Objects/codeobject.c
Python/optimizer.c
Python/pyhash.c
Python/tracemalloc.c

index ddf0b3e15dbdbe2884c6602f4245367d0aa79819..7345a048a4128b9e0c7dec8148818073f9b2a682 100644 (file)
@@ -29,6 +29,12 @@ See also the :c:member:`PyTypeObject.tp_hash` member and :ref:`numeric-hash`.
 
    .. versionadded:: 3.13
 
+.. c:macro:: PyHASH_MULTIPLIER
+
+   Prime multiplier used in string and various other hashes.
+
+   .. versionadded:: 3.13
+
 .. c:macro:: PyHASH_INF
 
    The hash value returned for a positive infinity.
index 2f8e12c1423aa1d004a63a6df3037caca43654f5..825c034a8d8474843102de497f4f973125e0674c 100644 (file)
@@ -3,7 +3,7 @@
 #endif
 
 /* Prime multiplier used in string and various other hashes. */
-#define _PyHASH_MULTIPLIER 1000003UL  /* 0xf4243 */
+#define PyHASH_MULTIPLIER 1000003UL  /* 0xf4243 */
 
 /* Parameters used for the numeric hash implementation.  See notes for
    _Py_HashDouble in Python/pyhash.c.  Numeric hashes are based on
 
 #define PyHASH_MODULUS (((size_t)1 << _PyHASH_BITS) - 1)
 #define PyHASH_INF 314159
-#define PyHASH_IMAG _PyHASH_MULTIPLIER
+#define PyHASH_IMAG PyHASH_MULTIPLIER
 
 /* Aliases kept for backward compatibility with Python 3.12 */
+#define _PyHASH_MULTIPLIER PyHASH_MULTIPLIER
 #define _PyHASH_BITS PyHASH_BITS
 #define _PyHASH_MODULUS PyHASH_MODULUS
 #define _PyHASH_INF PyHASH_INF
diff --git a/Misc/NEWS.d/next/C API/2024-05-20-10-35-22.gh-issue-111389.a6axBk.rst b/Misc/NEWS.d/next/C API/2024-05-20-10-35-22.gh-issue-111389.a6axBk.rst
new file mode 100644 (file)
index 0000000..f47662f
--- /dev/null
@@ -0,0 +1,2 @@
+Add :c:macro:`PyHASH_MULTIPLIER` constant: prime multiplier used in string
+and various other hashes. Patch by Victor Stinner.
index 3d804f73a5408802c7a7782b785f9f27b70836b9..1c6b55643fa19ec1a94c76ba8c63cfb81ecaa0fe 100644 (file)
@@ -2020,7 +2020,7 @@ code_hash(PyCodeObject *co)
     Py_uhash_t uhash = 20221211;
     #define SCRAMBLE_IN(H) do {       \
         uhash ^= (Py_uhash_t)(H);     \
-        uhash *= _PyHASH_MULTIPLIER;  \
+        uhash *= PyHASH_MULTIPLIER;  \
     } while (0)
     #define SCRAMBLE_IN_HASH(EXPR) do {     \
         Py_hash_t h = PyObject_Hash(EXPR);  \
index 9ae99ccdaea2e7b2c9ec3f6b0b8935f97ef3d35c..5b4a6ff8cb3dad2ea6a9e1bc8f4d601f12544b77 100644 (file)
@@ -1496,7 +1496,7 @@ address_to_hash(void *ptr) {
     uintptr_t addr = (uintptr_t)ptr;
     for (int i = 0; i < SIZEOF_VOID_P; i++) {
         uhash ^= addr & 255;
-        uhash *= (uint64_t)_PyHASH_MULTIPLIER;
+        uhash *= (uint64_t)PyHASH_MULTIPLIER;
         addr >>= 8;
     }
     return uhash;
index d508d78092a9e768682da00ec813c91d5bd5b045..5263622ff3126d39039923aff47b7ddc728d7840 100644 (file)
@@ -263,12 +263,12 @@ fnv(const void *src, Py_ssize_t len)
     x ^= (Py_uhash_t) *p << 7;
     while (blocks--) {
         PY_UHASH_CPY(block.bytes, p);
-        x = (_PyHASH_MULTIPLIER * x) ^ block.value;
+        x = (PyHASH_MULTIPLIER * x) ^ block.value;
         p += SIZEOF_PY_UHASH_T;
     }
     /* add remainder */
     for (; remainder > 0; remainder--)
-        x = (_PyHASH_MULTIPLIER * x) ^ (Py_uhash_t) *p++;
+        x = (PyHASH_MULTIPLIER * x) ^ (Py_uhash_t) *p++;
     x ^= (Py_uhash_t) len;
     x ^= (Py_uhash_t) _Py_HashSecret.fnv.suffix;
     if (x == (Py_uhash_t) -1) {
index e3ec72062f69313037549d9f1aa7cd63c018c821..fee7dd0e56d96d6ca227306fdadda53382f32de5 100644 (file)
@@ -312,7 +312,7 @@ traceback_hash(traceback_t *traceback)
     /* code based on tuplehash() of Objects/tupleobject.c */
     Py_uhash_t x, y;  /* Unsigned for defined overflow behavior. */
     int len = traceback->nframe;
-    Py_uhash_t mult = _PyHASH_MULTIPLIER;
+    Py_uhash_t mult = PyHASH_MULTIPLIER;
     frame_t *frame;
 
     x = 0x345678UL;