Skip to content
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

gh-111389: expose _PyHASH_INF/BITS/MODULUS/IMAG macros as public #111418

Merged
merged 18 commits into from Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 17 additions & 1 deletion Doc/c-api/hash.rst
Expand Up @@ -3,7 +3,8 @@
PyHash API
----------

See also the :c:member:`PyTypeObject.tp_hash` member.
See also the :c:member:`PyTypeObject.tp_hash` member and :ref:`numeric-hash`
for more details about hashing of numeric types.

.. c:type:: Py_hash_t

Expand All @@ -17,6 +18,21 @@ See also the :c:member:`PyTypeObject.tp_hash` member.

.. versionadded:: 3.2

.. c:macro:: PyHASH_MODULUS

The Mersenne prime ``P = 2**n -1``, used for numeric hash scheme.
skirpichev marked this conversation as resolved.
Show resolved Hide resolved
skirpichev marked this conversation as resolved.
Show resolved Hide resolved

.. c:macro:: PyHASH_BITS

The exponent ``n`` of ``P``.
skirpichev marked this conversation as resolved.
Show resolved Hide resolved

.. c:macro:: PyHASH_INF

The hash value returned for a positive infinity.

.. c:macro:: PyHASH_IMAG

The multiplier used for the imaginary part of a complex number.

.. c:type:: PyHash_FuncDef

Expand Down
22 changes: 22 additions & 0 deletions Include/pyhash.h
Expand Up @@ -4,6 +4,28 @@
extern "C" {
#endif

/* Prime multiplier used in string and various other hashes. */
#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
reduction modulo the prime 2**_PyHASH_BITS - 1. */

#if SIZEOF_VOID_P >= 8
# define _PyHASH_BITS 61
#else
# define _PyHASH_BITS 31
#endif

#define PyHASH_BITS _PyHASH_BITS

#define _PyHASH_MODULUS (((size_t)1 << _PyHASH_BITS) - 1)
#define PyHASH_MODULUS _PyHASH_MODULUS
#define _PyHASH_INF 314159
#define PyHASH_INF _PyHASH_INF
#define _PyHASH_IMAG _PyHASH_MULTIPLIER
#define PyHASH_IMAG _PyHASH_IMAG

/* Cutoff for small string DJBX33A optimization in range [1, cutoff).
*
* About 50% of the strings in a typical Python application are smaller than
Expand Down
@@ -0,0 +1,2 @@
Add :c:macro:`PyHASH_MODULUS`, :c:macro:`PyHASH_BITS`, :c:macro:`PyHASH_INF`
and :c:macro:`PyHASH_IMAG` C macroses. Patch by Sergey B Kirpichev.
skirpichev marked this conversation as resolved.
Show resolved Hide resolved