summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2024-06-04 07:26:25 (GMT)
committerGitHub <noreply@github.com>2024-06-04 07:26:25 (GMT)
commit1177897551066c1e8c394809152614a6483f9ce7 (patch)
tree62a6fbb373e0bf83932bc192caaaa8cf321bd3de
parent6be55f1bedcd076c12d00bf061bbcffc4ed8073a (diff)
downloadcpython-1177897551066c1e8c394809152614a6483f9ce7.zip
cpython-1177897551066c1e8c394809152614a6483f9ce7.tar.gz
cpython-1177897551066c1e8c394809152614a6483f9ce7.tar.bz2
[3.13] gh-111389: Add PyHASH_MULTIPLIER constant (GH-119214) (#119334)
gh-111389: Add PyHASH_MULTIPLIER constant (GH-119214) (cherry picked from commit f6da790122fdae1a28f444edfbb55202d6829cd1) Co-authored-by: Victor Stinner <vstinner@python.org>
-rw-r--r--Doc/c-api/hash.rst6
-rw-r--r--Include/cpython/pyhash.h5
-rw-r--r--Misc/NEWS.d/next/C API/2024-05-20-10-35-22.gh-issue-111389.a6axBk.rst2
-rw-r--r--Objects/codeobject.c2
-rw-r--r--Python/optimizer.c2
-rw-r--r--Python/pyhash.c4
-rw-r--r--Python/tracemalloc.c2
7 files changed, 16 insertions, 7 deletions
diff --git a/Doc/c-api/hash.rst b/Doc/c-api/hash.rst
index ddf0b3e..7345a04 100644
--- a/Doc/c-api/hash.rst
+++ b/Doc/c-api/hash.rst
@@ -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.
diff --git a/Include/cpython/pyhash.h b/Include/cpython/pyhash.h
index 2f8e12c..825c034 100644
--- a/Include/cpython/pyhash.h
+++ b/Include/cpython/pyhash.h
@@ -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
@@ -17,9 +17,10 @@
#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
index 0000000..f47662f
--- /dev/null
+++ b/Misc/NEWS.d/next/C API/2024-05-20-10-35-22.gh-issue-111389.a6axBk.rst
@@ -0,0 +1,2 @@
+Add :c:macro:`PyHASH_MULTIPLIER` constant: prime multiplier used in string
+and various other hashes. Patch by Victor Stinner.
diff --git a/Objects/codeobject.c b/Objects/codeobject.c
index 23fc8a7..55b512b 100644
--- a/Objects/codeobject.c
+++ b/Objects/codeobject.c
@@ -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); \
diff --git a/Python/optimizer.c b/Python/optimizer.c
index 8be2c0f..9dddc43 100644
--- a/Python/optimizer.c
+++ b/Python/optimizer.c
@@ -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;
diff --git a/Python/pyhash.c b/Python/pyhash.c
index d508d78..5263622 100644
--- a/Python/pyhash.c
+++ b/Python/pyhash.c
@@ -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) {
diff --git a/Python/tracemalloc.c b/Python/tracemalloc.c
index e3ec720..fee7dd0 100644
--- a/Python/tracemalloc.c
+++ b/Python/tracemalloc.c
@@ -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;