Skip to content

Commit b667c2e

Browse files
committed
use public Py_HASH_* macros in sys.hash_info
1 parent 87942d9 commit b667c2e

File tree

6 files changed

+41
-41
lines changed

6 files changed

+41
-41
lines changed

Include/cpython/pyhash.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77

88
/* Parameters used for the numeric hash implementation. See notes for
99
_Py_HashDouble in Python/pyhash.c. Numeric hashes are based on
10-
reduction modulo the prime 2**_PyHASH_BITS - 1. */
10+
reduction modulo the prime 2**PyHASH_BITS - 1. */
1111

1212
#if SIZEOF_VOID_P >= 8
1313
# define PyHASH_BITS 61
1414
#else
1515
# define PyHASH_BITS 31
1616
#endif
1717

18-
#define PyHASH_MODULUS (((size_t)1 << _PyHASH_BITS) - 1)
18+
#define PyHASH_MODULUS (((size_t)1 << PyHASH_BITS) - 1)
1919
#define PyHASH_INF 314159
2020
#define PyHASH_IMAG PyHASH_MULTIPLIER
2121

Modules/_decimal/_decimal.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5799,15 +5799,15 @@ _decimal_Decimal___floor___impl(PyObject *self, PyTypeObject *cls)
57995799
static Py_hash_t
58005800
_dec_hash(PyDecObject *v)
58015801
{
5802-
#if defined(CONFIG_64) && _PyHASH_BITS == 61
5802+
#if defined(CONFIG_64) && PyHASH_BITS == 61
58035803
/* 2**61 - 1 */
58045804
mpd_uint_t p_data[1] = {2305843009213693951ULL};
58055805
mpd_t p = {MPD_POS|MPD_STATIC|MPD_CONST_DATA, 0, 19, 1, 1, p_data};
58065806
/* Inverse of 10 modulo p */
58075807
mpd_uint_t inv10_p_data[1] = {2075258708292324556ULL};
58085808
mpd_t inv10_p = {MPD_POS|MPD_STATIC|MPD_CONST_DATA,
58095809
0, 19, 1, 1, inv10_p_data};
5810-
#elif defined(CONFIG_32) && _PyHASH_BITS == 31
5810+
#elif defined(CONFIG_32) && PyHASH_BITS == 31
58115811
/* 2**31 - 1 */
58125812
mpd_uint_t p_data[2] = {147483647UL, 2};
58135813
mpd_t p = {MPD_POS|MPD_STATIC|MPD_CONST_DATA, 0, 10, 2, 2, p_data};
@@ -5816,7 +5816,7 @@ _dec_hash(PyDecObject *v)
58165816
mpd_t inv10_p = {MPD_POS|MPD_STATIC|MPD_CONST_DATA,
58175817
0, 10, 2, 2, inv10_p_data};
58185818
#else
5819-
#error "No valid combination of CONFIG_64, CONFIG_32 and _PyHASH_BITS"
5819+
#error "No valid combination of CONFIG_64, CONFIG_32 and PyHASH_BITS"
58205820
#endif
58215821
const Py_hash_t py_hash_inf = 314159;
58225822
mpd_uint_t ten_data[1] = {10};

Objects/complexobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ complex_hash(PyObject *op)
644644
* compare equal must have the same hash value, so that
645645
* hash(x + 0*j) must equal hash(x).
646646
*/
647-
combined = hashreal + _PyHASH_IMAG * hashimag;
647+
combined = hashreal + PyHASH_IMAG * hashimag;
648648
if (combined == (Py_uhash_t)-1)
649649
combined = (Py_uhash_t)-2;
650650
return (Py_hash_t)combined;

Objects/longobject.c

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3703,36 +3703,36 @@ long_hash(PyObject *obj)
37033703
#endif
37043704

37053705
while (--i >= 0) {
3706-
/* Here x is a quantity in the range [0, _PyHASH_MODULUS); we
3706+
/* Here x is a quantity in the range [0, PyHASH_MODULUS); we
37073707
want to compute x * 2**PyLong_SHIFT + v->long_value.ob_digit[i] modulo
3708-
_PyHASH_MODULUS.
3708+
PyHASH_MODULUS.
37093709
3710-
The computation of x * 2**PyLong_SHIFT % _PyHASH_MODULUS
3710+
The computation of x * 2**PyLong_SHIFT % PyHASH_MODULUS
37113711
amounts to a rotation of the bits of x. To see this, write
37123712
3713-
x * 2**PyLong_SHIFT = y * 2**_PyHASH_BITS + z
3713+
x * 2**PyLong_SHIFT = y * 2**PyHASH_BITS + z
37143714
3715-
where y = x >> (_PyHASH_BITS - PyLong_SHIFT) gives the top
3715+
where y = x >> (PyHASH_BITS - PyLong_SHIFT) gives the top
37163716
PyLong_SHIFT bits of x (those that are shifted out of the
3717-
original _PyHASH_BITS bits, and z = (x << PyLong_SHIFT) &
3718-
_PyHASH_MODULUS gives the bottom _PyHASH_BITS - PyLong_SHIFT
3719-
bits of x, shifted up. Then since 2**_PyHASH_BITS is
3720-
congruent to 1 modulo _PyHASH_MODULUS, y*2**_PyHASH_BITS is
3721-
congruent to y modulo _PyHASH_MODULUS. So
3717+
original PyHASH_BITS bits, and z = (x << PyLong_SHIFT) &
3718+
PyHASH_MODULUS gives the bottom PyHASH_BITS - PyLong_SHIFT
3719+
bits of x, shifted up. Then since 2**PyHASH_BITS is
3720+
congruent to 1 modulo PyHASH_MODULUS, y*2**PyHASH_BITS is
3721+
congruent to y modulo PyHASH_MODULUS. So
37223722
3723-
x * 2**PyLong_SHIFT = y + z (mod _PyHASH_MODULUS).
3723+
x * 2**PyLong_SHIFT = y + z (mod PyHASH_MODULUS).
37243724
37253725
The right-hand side is just the result of rotating the
3726-
_PyHASH_BITS bits of x left by PyLong_SHIFT places; since
3727-
not all _PyHASH_BITS bits of x are 1s, the same is true
3728-
after rotation, so 0 <= y+z < _PyHASH_MODULUS and y + z is
3726+
PyHASH_BITS bits of x left by PyLong_SHIFT places; since
3727+
not all PyHASH_BITS bits of x are 1s, the same is true
3728+
after rotation, so 0 <= y+z < PyHASH_MODULUS and y + z is
37293729
the reduction of x*2**PyLong_SHIFT modulo
3730-
_PyHASH_MODULUS. */
3731-
x = ((x << PyLong_SHIFT) & _PyHASH_MODULUS) |
3732-
(x >> (_PyHASH_BITS - PyLong_SHIFT));
3730+
PyHASH_MODULUS. */
3731+
x = ((x << PyLong_SHIFT) & PyHASH_MODULUS) |
3732+
(x >> (PyHASH_BITS - PyLong_SHIFT));
37333733
x += v->long_value.ob_digit[i];
3734-
if (x >= _PyHASH_MODULUS)
3735-
x -= _PyHASH_MODULUS;
3734+
if (x >= PyHASH_MODULUS)
3735+
x -= PyHASH_MODULUS;
37363736
}
37373737
x = x * sign;
37383738
if (x == (Py_uhash_t)-1)

Python/pyhash.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static Py_ssize_t hashstats[Py_HASH_STATS_MAX + 1] = {0};
2929
#endif
3030

3131
/* For numeric types, the hash of a number x is based on the reduction
32-
of x modulo the prime P = 2**_PyHASH_BITS - 1. It's designed so that
32+
of x modulo the prime P = 2**PyHASH_BITS - 1. It's designed so that
3333
hash(x) == hash(y) whenever x and y are numerically equal, even if
3434
x and y have different types.
3535
@@ -52,8 +52,8 @@ static Py_ssize_t hashstats[Py_HASH_STATS_MAX + 1] = {0};
5252
5353
If the result of the reduction is infinity (this is impossible for
5454
integers, floats and Decimals) then use the predefined hash value
55-
_PyHASH_INF for x >= 0, or -_PyHASH_INF for x < 0, instead.
56-
_PyHASH_INF and -_PyHASH_INF are also used for the
55+
PyHASH_INF for x >= 0, or -PyHASH_INF for x < 0, instead.
56+
PyHASH_INF and -PyHASH_INF are also used for the
5757
hashes of float and Decimal infinities.
5858
5959
NaNs hash with a pointer hash. Having distinct hash values prevents
@@ -65,16 +65,16 @@ static Py_ssize_t hashstats[Py_HASH_STATS_MAX + 1] = {0};
6565
efficiently, even if the exponent of the binary or decimal number
6666
is large. The key point is that
6767
68-
reduce(x * y) == reduce(x) * reduce(y) (modulo _PyHASH_MODULUS)
68+
reduce(x * y) == reduce(x) * reduce(y) (modulo PyHASH_MODULUS)
6969
7070
provided that {reduce(x), reduce(y)} != {0, infinity}. The reduction of a
7171
binary or decimal float is never infinity, since the denominator is a power
7272
of 2 (for binary) or a divisor of a power of 10 (for decimal). So we have,
7373
for nonnegative x,
7474
75-
reduce(x * 2**e) == reduce(x) * reduce(2**e) % _PyHASH_MODULUS
75+
reduce(x * 2**e) == reduce(x) * reduce(2**e) % PyHASH_MODULUS
7676
77-
reduce(x * 10**e) == reduce(x) * reduce(10**e) % _PyHASH_MODULUS
77+
reduce(x * 10**e) == reduce(x) * reduce(10**e) % PyHASH_MODULUS
7878
7979
and reduce(10**e) can be computed efficiently by the usual modular
8080
exponentiation algorithm. For reduce(2**e) it's even better: since
@@ -92,7 +92,7 @@ _Py_HashDouble(PyObject *inst, double v)
9292

9393
if (!isfinite(v)) {
9494
if (isinf(v))
95-
return v > 0 ? _PyHASH_INF : -_PyHASH_INF;
95+
return v > 0 ? PyHASH_INF : -PyHASH_INF;
9696
else
9797
return PyObject_GenericHash(inst);
9898
}
@@ -109,19 +109,19 @@ _Py_HashDouble(PyObject *inst, double v)
109109
and hexadecimal floating point. */
110110
x = 0;
111111
while (m) {
112-
x = ((x << 28) & _PyHASH_MODULUS) | x >> (_PyHASH_BITS - 28);
112+
x = ((x << 28) & PyHASH_MODULUS) | x >> (PyHASH_BITS - 28);
113113
m *= 268435456.0; /* 2**28 */
114114
e -= 28;
115115
y = (Py_uhash_t)m; /* pull out integer part */
116116
m -= y;
117117
x += y;
118-
if (x >= _PyHASH_MODULUS)
119-
x -= _PyHASH_MODULUS;
118+
if (x >= PyHASH_MODULUS)
119+
x -= PyHASH_MODULUS;
120120
}
121121

122-
/* adjust for the exponent; first reduce it modulo _PyHASH_BITS */
123-
e = e >= 0 ? e % _PyHASH_BITS : _PyHASH_BITS-1-((-1-e) % _PyHASH_BITS);
124-
x = ((x << e) & _PyHASH_MODULUS) | x >> (_PyHASH_BITS - e);
122+
/* adjust for the exponent; first reduce it modulo PyHASH_BITS */
123+
e = e >= 0 ? e % PyHASH_BITS : PyHASH_BITS-1-((-1-e) % PyHASH_BITS);
124+
x = ((x << e) & PyHASH_MODULUS) | x >> (PyHASH_BITS - e);
125125

126126
x = x * sign;
127127
if (x == (Py_uhash_t)-1)

Python/sysmodule.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,10 +1587,10 @@ get_hash_info(PyThreadState *tstate)
15871587
} while(0)
15881588

15891589
SET_HASH_INFO_ITEM(PyLong_FromLong(8 * sizeof(Py_hash_t)));
1590-
SET_HASH_INFO_ITEM(PyLong_FromSsize_t(_PyHASH_MODULUS));
1591-
SET_HASH_INFO_ITEM(PyLong_FromLong(_PyHASH_INF));
1590+
SET_HASH_INFO_ITEM(PyLong_FromSsize_t(PyHASH_MODULUS));
1591+
SET_HASH_INFO_ITEM(PyLong_FromLong(PyHASH_INF));
15921592
SET_HASH_INFO_ITEM(PyLong_FromLong(0)); // This is no longer used
1593-
SET_HASH_INFO_ITEM(PyLong_FromLong(_PyHASH_IMAG));
1593+
SET_HASH_INFO_ITEM(PyLong_FromLong(PyHASH_IMAG));
15941594
SET_HASH_INFO_ITEM(PyUnicode_FromString(hashfunc->name));
15951595
SET_HASH_INFO_ITEM(PyLong_FromLong(hashfunc->hash_bits));
15961596
SET_HASH_INFO_ITEM(PyLong_FromLong(hashfunc->seed_bits));

0 commit comments

Comments
 (0)