Skip to content

Commit 224bef2

Browse files
committed
Fix for 32-bit platforms
1 parent edcc03d commit 224bef2

File tree

3 files changed

+27
-24
lines changed

3 files changed

+27
-24
lines changed

Include/internal/pycore_runtime_init.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ extern "C" {
2727
#include "pycore_runtime_init_generated.h" // _Py_bytes_characters_INIT
2828
#include "pycore_signal.h" // _signals_RUNTIME_INIT
2929
#include "pycore_tracemalloc.h" // _tracemalloc_runtime_state_INIT
30+
#include "pycore_tuple.h" // _PyTuple_HASH_EMPTY
3031

3132

3233
extern PyTypeObject _PyExc_MemoryError;
@@ -106,9 +107,7 @@ extern PyTypeObject _PyExc_MemoryError;
106107
}, \
107108
.tuple_empty = { \
108109
.ob_base = _PyVarObject_HEAD_INIT(&PyTuple_Type, 0), \
109-
/* Using the hash constants in tupleobject.c
110-
_PyHASH_XXPRIME_5 + (_PyHASH_XXPRIME_5 ^ 3527539UL) */ \
111-
.ob_hash = 5740354900026072187, \
110+
.ob_hash = _PyTuple_HASH_EMPTY, \
112111
}, \
113112
.hamt_bitmap_node_empty = { \
114113
.ob_base = _PyVarObject_HEAD_INIT(&_PyHamt_BitmapNode_Type, 0), \

Include/internal/pycore_tuple.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,25 @@ _PyTuple_Recycle(PyObject *op)
4747
}
4848
}
4949

50+
/* Below are the official constants from the xxHash specification. Optimizing
51+
compilers should emit a single "rotate" instruction for the
52+
_PyTuple_HASH_XXROTATE() expansion. If that doesn't happen for some important
53+
platform, the macro could be changed to expand to a platform-specific rotate
54+
spelling instead.
55+
*/
56+
#if SIZEOF_PY_UHASH_T > 4
57+
#define _PyTuple_HASH_XXPRIME_1 ((Py_uhash_t)11400714785074694791ULL)
58+
#define _PyTuple_HASH_XXPRIME_2 ((Py_uhash_t)14029467366897019727ULL)
59+
#define _PyTuple_HASH_XXPRIME_5 ((Py_uhash_t)2870177450012600261ULL)
60+
#define _PyTuple_HASH_XXROTATE(x) ((x << 31) | (x >> 33)) /* Rotate left 31 bits */
61+
#else
62+
#define _PyTuple_HASH_XXPRIME_1 ((Py_uhash_t)2654435761UL)
63+
#define _PyTuple_HASH_XXPRIME_2 ((Py_uhash_t)2246822519UL)
64+
#define _PyTuple_HASH_XXPRIME_5 ((Py_uhash_t)374761393UL)
65+
#define _PyTuple_HASH_XXROTATE(x) ((x << 13) | (x >> 19)) /* Rotate left 13 bits */
66+
#endif
67+
#define _PyTuple_HASH_EMPTY (_PyTuple_HASH_XXPRIME_5 + (_PyTuple_HASH_XXPRIME_5 ^ 3527539UL))
68+
5069
#ifdef __cplusplus
5170
}
5271
#endif

Objects/tupleobject.c

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -301,23 +301,8 @@ tuple_repr(PyObject *self)
301301
For the xxHash specification, see
302302
https://github.com/Cyan4973/xxHash/blob/master/doc/xxhash_spec.md
303303
304-
Below are the official constants from the xxHash specification. Optimizing
305-
compilers should emit a single "rotate" instruction for the
306-
_PyHASH_XXROTATE() expansion. If that doesn't happen for some important
307-
platform, the macro could be changed to expand to a platform-specific rotate
308-
spelling instead.
304+
The constants for the hard function are defined in pycore_tuple.h.
309305
*/
310-
#if SIZEOF_PY_UHASH_T > 4
311-
#define _PyHASH_XXPRIME_1 ((Py_uhash_t)11400714785074694791ULL)
312-
#define _PyHASH_XXPRIME_2 ((Py_uhash_t)14029467366897019727ULL)
313-
#define _PyHASH_XXPRIME_5 ((Py_uhash_t)2870177450012600261ULL)
314-
#define _PyHASH_XXROTATE(x) ((x << 31) | (x >> 33)) /* Rotate left 31 bits */
315-
#else
316-
#define _PyHASH_XXPRIME_1 ((Py_uhash_t)2654435761UL)
317-
#define _PyHASH_XXPRIME_2 ((Py_uhash_t)2246822519UL)
318-
#define _PyHASH_XXPRIME_5 ((Py_uhash_t)374761393UL)
319-
#define _PyHASH_XXROTATE(x) ((x << 13) | (x >> 19)) /* Rotate left 13 bits */
320-
#endif
321306

322307
static Py_hash_t
323308
tuple_hash(PyObject *op)
@@ -330,19 +315,19 @@ tuple_hash(PyObject *op)
330315

331316
Py_ssize_t len = Py_SIZE(v);
332317
PyObject **item = v->ob_item;
333-
Py_uhash_t acc = _PyHASH_XXPRIME_5;
318+
Py_uhash_t acc = _PyTuple_HASH_XXPRIME_5;
334319
for (Py_ssize_t i = 0; i < len; i++) {
335320
Py_uhash_t lane = PyObject_Hash(item[i]);
336321
if (lane == (Py_uhash_t)-1) {
337322
return -1;
338323
}
339-
acc += lane * _PyHASH_XXPRIME_2;
340-
acc = _PyHASH_XXROTATE(acc);
341-
acc *= _PyHASH_XXPRIME_1;
324+
acc += lane * _PyTuple_HASH_XXPRIME_2;
325+
acc = _PyTuple_HASH_XXROTATE(acc);
326+
acc *= _PyTuple_HASH_XXPRIME_1;
342327
}
343328

344329
/* Add input length, mangled to keep the historical value of hash(()). */
345-
acc += len ^ (_PyHASH_XXPRIME_5 ^ 3527539UL);
330+
acc += len ^ (_PyTuple_HASH_XXPRIME_5 ^ 3527539UL);
346331

347332
if (acc == (Py_uhash_t)-1) {
348333
acc = 1546275796;

0 commit comments

Comments
 (0)