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 2 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
3 changes: 2 additions & 1 deletion Doc/library/stdtypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,8 @@ made available to Python as the :attr:`modulus` attribute of
.. impl-detail::

Currently, the prime used is ``P = 2**31 - 1`` on machines with 32-bit C
longs and ``P = 2**61 - 1`` on machines with 64-bit C longs.
longs and ``P = 2**61 - 1`` on machines with 64-bit C longs. The exponent
of this Mersenne prime is available as ``_PyHASH_BITS`` macro.

Here are the rules in detail:

Expand Down
9 changes: 6 additions & 3 deletions Doc/library/sys.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1022,19 +1022,22 @@ always available.

.. attribute:: hash_info.modulus

The prime modulus P used for numeric hash scheme
The prime modulus P used for numeric hash scheme. Available also as
``_PyHASH_MODULUS`` macro.
skirpichev marked this conversation as resolved.
Show resolved Hide resolved

.. attribute:: hash_info.inf

The hash value returned for a positive infinity
The hash value returned for a positive infinity. Available also as
``_PyHASH_INF`` macro.

.. attribute:: hash_info.nan

(This attribute is no longer used)

.. attribute:: hash_info.imag

The multiplier used for the imaginary part of a complex number
The multiplier used for the imaginary part of a complex number.
Available also as ``_PyHASH_IMAG`` macro.

.. attribute:: hash_info.algorithm

Expand Down
17 changes: 0 additions & 17 deletions Include/internal/pycore_pyhash.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,6 @@ extern Py_hash_t _Py_HashPointerRaw(const void*);
// Export for '_datetime' shared extension
PyAPI_FUNC(Py_hash_t) _Py_HashBytes(const void*, Py_ssize_t);

/* 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_MODULUS (((size_t)1 << _PyHASH_BITS) - 1)
#define _PyHASH_INF 314159
#define _PyHASH_IMAG _PyHASH_MULTIPLIER

/* Hash secret
*
* memory layout on 64 bit systems
Expand Down
16 changes: 16 additions & 0 deletions Include/pyhash.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ typedef struct {
PyAPI_FUNC(PyHash_FuncDef*) PyHash_GetFuncDef(void);
#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_MODULUS (((size_t)1 << _PyHASH_BITS) - 1)
#define _PyHASH_INF 314159
#define _PyHASH_IMAG _PyHASH_MULTIPLIER

/* Cutoff for small string DJBX33A optimization in range [1, cutoff).
*
Expand Down
Loading