Parcourir la source

Update namedict.c

blueloveTH il y a 6 mois
Parent
commit
7135e1ce59
1 fichiers modifiés avec 14 ajouts et 1 suppressions
  1. 14 1
      src/objects/namedict.c

+ 14 - 1
src/objects/namedict.c

@@ -5,7 +5,20 @@
 #include <string.h>
 #include <assert.h>
 
-#define HASH_KEY(__k) ((uintptr_t)(__k) >> 3U)
+// https://jfdube.wordpress.com/2011/10/12/hashing-strings-and-pointers-avoiding-common-pitfalls/
+uintptr_t ThomasWangInt32Hash(void* Ptr) {
+    // Here we think only the lower 32 bits are useful
+    uint32_t Value = (uint32_t)(uintptr_t)Ptr;
+    Value = ~Value + (Value << 15);
+    Value = Value ^ (Value >> 12);
+    Value = Value + (Value << 2);
+    Value = Value ^ (Value >> 4);
+    Value = Value * 2057;
+    Value = Value ^ (Value >> 16);
+    return Value;
+}
+
+#define HASH_KEY(__k) ThomasWangInt32Hash(__k)
 
 #define HASH_PROBE_0(__k, ok, i)                                                                   \
     ok = false;                                                                                    \