Skip to content

gh-111389: Add PyHASH_MULTIPLIER constant #119214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Doc/c-api/hash.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions Include/cpython/pyhash.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add :c:macro:`PyHASH_MULTIPLIER` constant: prime multiplier used in string
and various other hashes. Patch by Victor Stinner.
2 changes: 1 addition & 1 deletion Objects/codeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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); \
Expand Down
2 changes: 1 addition & 1 deletion Python/optimizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions Python/pyhash.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion Python/tracemalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading