From 72d5e4d098d9d90ace6cedcecaa22e923beed9f8 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sat, 23 Nov 2024 15:52:56 +0800 Subject: [PATCH] Revert "add `cpy310_tuplehash`" This reverts commit b03feb0294c984002992b5b226df02d0d53103f5. --- include/pocketpy/common/algorithm.h | 3 --- src/common/algorithm.c | 39 ----------------------------- src/modules/linalg.c | 9 ++++--- src/public/py_tuple.c | 17 +++++-------- 4 files changed, 11 insertions(+), 57 deletions(-) diff --git a/include/pocketpy/common/algorithm.h b/include/pocketpy/common/algorithm.h index f927f392..8f4f5b88 100644 --- a/include/pocketpy/common/algorithm.h +++ b/include/pocketpy/common/algorithm.h @@ -1,7 +1,6 @@ #pragma once #include -#include #define c11__less(a, b) ((a) < (b)) @@ -39,5 +38,3 @@ bool c11__stable_sort(void* ptr, int elem_size, int (*f_lt)(const void* a, const void* b, void* extra), void* extra); - -uint64_t cpy310_tuplehash(uint64_t* p, int len); \ No newline at end of file diff --git a/src/common/algorithm.c b/src/common/algorithm.c index c6918ee8..e908984e 100644 --- a/src/common/algorithm.c +++ b/src/common/algorithm.c @@ -54,42 +54,3 @@ bool c11__stable_sort(void* ptr_, free(tmp); return true; } - -/* Hash Functions */ - -// https://github.com/python/cpython/blob/v3.10.15/Objects/tupleobject.c#L406 -#define SIZEOF_PY_UHASH_T 8 -typedef uint64_t Py_uhash_t; - -#if SIZEOF_PY_UHASH_T > 4 -#define _PyHASH_XXPRIME_1 ((Py_uhash_t)11400714785074694791ULL) -#define _PyHASH_XXPRIME_2 ((Py_uhash_t)14029467366897019727ULL) -#define _PyHASH_XXPRIME_5 ((Py_uhash_t)2870177450012600261ULL) -#define _PyHASH_XXROTATE(x) ((x << 31) | (x >> 33)) /* Rotate left 31 bits */ -#else -#define _PyHASH_XXPRIME_1 ((Py_uhash_t)2654435761UL) -#define _PyHASH_XXPRIME_2 ((Py_uhash_t)2246822519UL) -#define _PyHASH_XXPRIME_5 ((Py_uhash_t)374761393UL) -#define _PyHASH_XXROTATE(x) ((x << 13) | (x >> 19)) /* Rotate left 13 bits */ -#endif - -uint64_t cpy310_tuplehash(uint64_t* p, int len){ - Py_uhash_t acc = _PyHASH_XXPRIME_5; - for (int i = 0; i < len; i++) { - Py_uhash_t lane = p[i]; - if (lane == (Py_uhash_t)-1) { - return -1; - } - acc += lane * _PyHASH_XXPRIME_2; - acc = _PyHASH_XXROTATE(acc); - acc *= _PyHASH_XXPRIME_1; - } - - /* Add input length, mangled to keep the historical value of hash(()). */ - acc += len ^ (_PyHASH_XXPRIME_5 ^ 3527539UL); - - if (acc == (Py_uhash_t)-1) { - return 1546275796; - } - return acc; -} diff --git a/src/modules/linalg.c b/src/modules/linalg.c index 3ee7dfe2..5a88dccb 100644 --- a/src/modules/linalg.c +++ b/src/modules/linalg.c @@ -306,8 +306,7 @@ DEF_VECTOR_INT_OPS(3) static bool vec2i__hash__(int argc, py_Ref argv) { PY_CHECK_ARGC(1); c11_vec2i v = py_tovec2i(argv); - uint64_t values[2] = {v.x, v.y}; - uint64_t hash = cpy310_tuplehash(values, 2); + uint64_t hash = ((uint64_t)v.x << 32) | (uint64_t)v.y; py_newint(py_retval(), (py_i64)hash); return true; } @@ -315,8 +314,10 @@ static bool vec2i__hash__(int argc, py_Ref argv) { static bool vec3i__hash__(int argc, py_Ref argv) { PY_CHECK_ARGC(1); c11_vec3i v = py_tovec3i(argv); - uint64_t values[3] = {v.x, v.y, v.z}; - uint64_t hash = cpy310_tuplehash(values, 3); + uint64_t x_part = (uint64_t)(v.x & 0xFFFFFF); + uint64_t y_part = (uint64_t)(v.y & 0xFFFFFF); + uint64_t z_part = (uint64_t)(v.z & 0xFFFF); + uint64_t hash = (x_part << 40) | (y_part << 16) | z_part; py_newint(py_retval(), (py_i64)hash); return true; } diff --git a/src/public/py_tuple.c b/src/public/py_tuple.c index e6d73cc7..8a9a3d55 100644 --- a/src/public/py_tuple.c +++ b/src/public/py_tuple.c @@ -155,19 +155,14 @@ static bool tuple__hash__(int argc, py_Ref argv) { PY_CHECK_ARGC(1); int length = py_tuple_len(argv); py_TValue* data = py_tuple_data(argv); - uint64_t __buffer[8]; - uint64_t* p = length <= 8 ? __buffer : malloc(sizeof(uint64_t) * length); + uint64_t x = 1000003; for(int i = 0; i < length; i++) { - py_i64 x; - if(!py_hash(&data[i], &x)) { - if(p != __buffer) free(p); - return false; - } - p[i] = x; + py_i64 y; + if(!py_hash(&data[i], &y)) return false; + // recommended by Github Copilot + x = x ^ (y + 0x9e3779b9 + (x << 6) + (x >> 2)); } - uint64_t hash = cpy310_tuplehash(p, length); - if(p != __buffer) free(p); - py_newint(py_retval(), (py_i64)hash); + py_newint(py_retval(), x); return true; }