Commit 83620773 authored by Benjamin Peterson's avatar Benjamin Peterson Committed by GitHub

fix my byte-swapping implementation (#4772)

parent 4e3e1563
...@@ -2197,21 +2197,21 @@ static PyObject * ...@@ -2197,21 +2197,21 @@ static PyObject *
_imp_source_hash_impl(PyObject *module, long key, Py_buffer *source) _imp_source_hash_impl(PyObject *module, long key, Py_buffer *source)
/*[clinic end generated code: output=edb292448cf399ea input=9aaad1e590089789]*/ /*[clinic end generated code: output=edb292448cf399ea input=9aaad1e590089789]*/
{ {
uint64_t hash = _Py_KeyedHash((uint64_t)key, source->buf, source->len); union {
uint64_t x;
char data[sizeof(uint64_t)];
} hash;
hash.x = _Py_KeyedHash((uint64_t)key, source->buf, source->len);
#if !PY_LITTLE_ENDIAN #if !PY_LITTLE_ENDIAN
// Force to little-endian. There really ought to be a succinct standard way // Force to little-endian. There really ought to be a succinct standard way
// to do this. // to do this.
union { for (size_t i = 0; i < sizeof(hash.data)/2; i++) {
uint64_t x; char tmp = hash.data[i];
unsigned char data[sizeof(uint64_t)]; hash.data[i] = hash.data[sizeof(hash.data) - i - 1];
} pun; hash.data[sizeof(hash.data) - i - 1] = tmp;
pun.x = hash;
for (size_t i = 0; i < sizeof(pun.data); i++) {
pun.data[sizeof(pun.data) - i - 1] = pun.data[i];
} }
hash = pun.x;
#endif #endif
return PyBytes_FromStringAndSize((const char *)&hash, sizeof(hash)); return PyBytes_FromStringAndSize(hash.data, sizeof(hash.data));
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment