8000 follow up to #9778: define and use an unsigned hash type · python/cpython@8035bc5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8035bc5

Browse files
committed
follow up to #9778: define and use an unsigned hash type
1 parent 2b9af63 commit 8035bc5

File tree

7 files changed

+22
-20
lines changed

7 files changed

+22
-20
lines changed

Include/pyport.h

Lines changed: 3 additions & 1 deletion
139
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ Used in: PY_LONG_LONG
135135
#else
136136
#define _PyHASH_BITS 31
137137
#endif
138-
#define _PyHASH_MODULUS ((1UL << _PyHASH_BITS) - 1)
138+
#define _PyHASH_MODULUS (((size_t)1 << _PyHASH_BITS) - 1)
139
#define _PyHASH_INF 314159
140140
#define _PyHASH_NAN 0
141141
#define _PyHASH_IMAG 1000003UL
@@ -179,6 +179,8 @@ typedef Py_intptr_t Py_ssize_t;
179179

180180
/* Py_hash_t is the same size as a pointer. */
181181
typedef Py_ssize_t Py_hash_t;
182+
/* Py_uhash_t is the unsigned equivalent needed to calculate numeric hash. */
183+
typedef size_t Py_uhash_t;
182184

183185
/* Largest possible value of size_t.
184186
SIZE_MAX is part of C99, so it might be defined on some

Objects/complexobject.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -397,12 +397,12 @@ complex_repr(PyComplexObject *v)
397397
static Py_hash_t
398398
complex_hash(PyComplexObject *v)
399399
{
400-
unsigned long hashreal, hashimag, combined;
401-
hashreal = (unsigned long)_Py_HashDouble(v->cval.real);
402-
if (hashreal == (unsigned long)-1)
400+
Py_uhash_t hashreal, hashimag, combined;
401+
hashreal = (Py_uhash_t)_Py_HashDouble(v->cval.real);
402+
if (hashreal == (Py_uhash_t)-1)
403403
return -1;
404-
hashimag = (unsigned long)_Py_HashDouble(v->cval.imag);
405-
if (hashimag == (unsigned long)-1)
404+
hashimag = (Py_uhash_t)_Py_HashDouble(v->cval.imag);
405+
if (hashimag == (Py_uhash_t)-1)
406406
return -1;
407407
/* Note: if the imaginary part is 0, hashimag is 0 now,
408408
* so the following returns hashreal unchanged. This is
@@ -411,8 +411,8 @@ complex_hash(PyComplexObject *v)
411411
* hash(x + 0*j) must equal hash(x).
412412
*/
413413
combined = hashreal + _PyHASH_IMAG * hashimag;
414-
if (combined == (unsigned long)-1)
415-
combined = (unsigned long)-2;
414+
if (combined == (Py_uhash_t)-1)
415+
combined = (Py_uhash_t)-2;
416416
return (Py_hash_t)combined;
417417
}
418418

Objects/longobject.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2555,7 +2555,7 @@ long_richcompare(PyObject *self, PyObject *other, int op)
25552555
static Py_hash_t
25562556
long_hash(PyLongObject *v)
25572557
{
2558-
unsigned long x;
2558+
Py_uhash_t x;
25592559
Py_ssize_t i;
25602560
int sign;
25612561

@@ -2604,8 +2604,8 @@ long_hash(PyLongObject *v)
26042604
x -= _PyHASH_MODULUS;
26052605
}
26062606
x = x * sign;
2607-
if (x == (unsigned long)-1)
2608-
x = (unsigned long)-2;
2607+
if (x == (Py_uhash_t)-1)
2608+
x = (Py_uhash_t)-2;
26092609
return (Py_hash_t)x;
26102610
}
26112611

Objects/object.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ _Py_HashDouble(double v)
692692
{
693693
int e, sign;
694694
double m;
695-
unsigned long x, y;
695+
Py_uhash_t x, y;
696696

697697
if (!Py_IS_FINITE(v)) {
698698
if (Py_IS_INFINITY(v))
@@ -716,7 +716,7 @@ _Py_HashDouble(double v)
716716
x = ((x << 28) & _PyHASH_MODULUS) | x >> (_PyHASH_BITS - 28);
717717
m *= 268435456.0; /* 2**28 */
718718
e -= 28;
719-
y = (unsigned long)m; /* pull out integer part */
719+
y = (Py_uhash_t)m; /* pull out integer part */
720720
m -= y;
721721
x += y;
722722
if (x >= _PyHASH_MODULUS)
@@ -728,8 +728,8 @@ _Py_HashDouble(double v)
728728
x = ((x << e) & _PyHASH_MODULUS) | x >> (_PyHASH_BITS - e);
729729

730730
x = x * sign;
731-
if (x == (unsigned long)-1)
732-
x = (unsigned long)-2;
731+
if (x == (Py_uhash_t)-1)
732+
x = (Py_uhash_t)-2;
733733
return (Py_hash_t)x;
734734
}
735735

Objects/tupleobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ tuplehash(PyTupleObject *v)
318318
register Py_hash_t x, y;
319319
register Py_ssize_t len = Py_SIZE(v);
320320
register PyObject **p;
321-
long mult = 1000003L;
321+
Py_hash_t mult = 1000003L;
322322
x = 0x345678L;
323323
p = v->ob_item;
324324
while (--len >= 0) {
@@ -327,7 +327,7 @@ tuplehash(PyTupleObject *v)
327327
return -1;
328328
x = (x ^ y) * mult;
329329
/* the cast might truncate len; that doesn't change hash stability */
330-
mult += (long)(82520L + len + len);
330+
mult += (Py_hash_t)(82520L + len + len);
331331
}
332332
x += 97531L;
333333
if (x == -1)

Objects/typeobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4314,14 +4314,14 @@ static PyObject *
43144314
wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
43154315
{
43164316
hashfunc func = (hashfunc)wrapped;
4317-
long res;
4317+
Py_hash_t res;
43184318

43194319
if (!check_num_args(args, 0))
43204320
return NULL;
43214321
res = (*func)(self);
43224322
if (res == -1 && PyErr_Occurred())
43234323
return NULL;
4324-
return PyLong_FromLong(res);
4324+
return PyLong_FromSsize_t(res);
43254325
}
43264326

43274327
static PyObject *

Python/sysmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ get_hash_info(void)
569569
PyStructSequence_SET_ITEM(hash_info, field++,
570570
PyLong_FromLong(8*sizeof(Py_hash_t)));
571571
PyStructSequence_SET_ITEM(hash_info, field++,
572-
PyLong_FromLong(_PyHASH_MODULUS));
572+
PyLong_FromSsize_t(_PyHASH_MODULUS));
573573
PyStructSequence_SET_ITEM(hash_info, field++,
574574
PyLong_FromLong(_PyHASH_INF));
575575
PyStructSequence_SET_ITEM(hash_info, field++,

0 commit comments

Comments
 (0)
0