Skip to content

Commit 20fd8d3

Browse files
vstinnerestyxx
authored andcommitted
pythongh-111389: Add PyHASH_MULTIPLIER constant (python#119214)
1 parent e6d705d commit 20fd8d3

File tree

7 files changed

+16
-7
lines changed

7 files changed

+16
-7
lines changed

Doc/c-api/hash.rst

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ See also the :c:member:`PyTypeObject.tp_hash` member and :ref:`numeric-hash`.
2929

3030
.. versionadded:: 3.13
3131

32+
.. c:macro:: PyHASH_MULTIPLIER
33+
34+
Prime multiplier used in string and various other hashes.
35+
36+
.. versionadded:: 3.13
37+
3238
.. c:macro:: PyHASH_INF
3339
3440
The hash value returned for a positive infinity.

Include/cpython/pyhash.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#endif
44

55
/* Prime multiplier used in string and various other hashes. */
6-
#define _PyHASH_MULTIPLIER 1000003UL /* 0xf4243 */
6+
#define PyHASH_MULTIPLIER 1000003UL /* 0xf4243 */
77

88
/* Parameters used for the numeric hash implementation. See notes for
99
_Py_HashDouble in Python/pyhash.c. Numeric hashes are based on
@@ -17,9 +17,10 @@
1717

1818
#define PyHASH_MODULUS (((size_t)1 << _PyHASH_BITS) - 1)
1919
#define PyHASH_INF 314159
20-
#define PyHASH_IMAG _PyHASH_MULTIPLIER
20+
#define PyHASH_IMAG PyHASH_MULTIPLIER
2121

2222
/* Aliases kept for backward compatibility with Python 3.12 */
23+
#define _PyHASH_MULTIPLIER PyHASH_MULTIPLIER
2324
#define _PyHASH_BITS PyHASH_BITS
2425
#define _PyHASH_MODULUS PyHASH_MODULUS
2526
#define _PyHASH_INF PyHASH_INF
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add :c:macro:`PyHASH_MULTIPLIER` constant: prime multiplier used in string
2+
and various other hashes. Patch by Victor Stinner.

Objects/codeobject.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2020,7 +2020,7 @@ code_hash(PyCodeObject *co)
20202020
Py_uhash_t uhash = 20221211;
20212021
#define SCRAMBLE_IN(H) do { \
20222022
uhash ^= (Py_uhash_t)(H); \
2023-
uhash *= _PyHASH_MULTIPLIER; \
2023+
uhash *= PyHASH_MULTIPLIER; \
20242024
} while (0)
20252025
#define SCRAMBLE_IN_HASH(EXPR) do { \
20262026
Py_hash_t h = PyObject_Hash(EXPR); \

Python/optimizer.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1496,7 +1496,7 @@ address_to_hash(void *ptr) {
14961496
uintptr_t addr = (uintptr_t)ptr;
14971497
for (int i = 0; i < SIZEOF_VOID_P; i++) {
14981498
uhash ^= addr & 255;
1499-
uhash *= (uint64_t)_PyHASH_MULTIPLIER;
1499+
uhash *= (uint64_t)PyHASH_MULTIPLIER;
15001500
addr >>= 8;
15011501
}
15021502
return uhash;

Python/pyhash.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,12 @@ fnv(const void *src, Py_ssize_t len)
263263
x ^= (Py_uhash_t) *p << 7;
264264
while (blocks--) {
265265
PY_UHASH_CPY(block.bytes, p);
266-
x = (_PyHASH_MULTIPLIER * x) ^ block.value;
266+
x = (PyHASH_MULTIPLIER * x) ^ block.value;
267267
p += SIZEOF_PY_UHASH_T;
268268
}
269269
/* add remainder */
270270
for (; remainder > 0; remainder--)
271-
x = (_PyHASH_MULTIPLIER * x) ^ (Py_uhash_t) *p++;
271+
x = (PyHASH_MULTIPLIER * x) ^ (Py_uhash_t) *p++;
272272
x ^= (Py_uhash_t) len;
273273
x ^= (Py_uhash_t) _Py_HashSecret.fnv.suffix;
274274
if (x == (Py_uhash_t) -1) {

Python/tracemalloc.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ traceback_hash(traceback_t *traceback)
312312
/* code based on tuplehash() of Objects/tupleobject.c */
313313
Py_uhash_t x, y; /* Unsigned for defined overflow behavior. */
314314
int len = traceback->nframe;
315-
Py_uhash_t mult = _PyHASH_MULTIPLIER;
315+
Py_uhash_t mult = PyHASH_MULTIPLIER;
316316
frame_t *frame;
317317

318318
x = 0x345678UL;

0 commit comments

Comments
 (0)