Debian Codegraph @

Include/internal/pycore_pyhash.h

raw
1 #ifndef Py_INTERNAL_PYHASH_H
2 #define Py_INTERNAL_PYHASH_H
3
4 #ifndef Py_BUILD_CORE
5 # error "this header requires Py_BUILD_CORE define"
6 #endif
7
8 // Similar to Py_HashPointer(), but don't replace -1 with -2.
9 static inline Py_hash_t
10 _Py_HashPointerRaw(const void *ptr)
11 {
12 uintptr_t x = (uintptr_t)ptr;
13 Py_BUILD_ASSERT(sizeof(x) == sizeof(ptr));
14
15 // Bottom 3 or 4 bits are likely to be 0; rotate x by 4 to the right
16 // to avoid excessive hash collisions for dicts and sets.
17 x = (x >> 4) | (x << (8 * sizeof(uintptr_t) - 4));
18
19 Py_BUILD_ASSERT(sizeof(x) == sizeof(Py_hash_t));
20 return (Py_hash_t)x;
21 }
22
23 /* Hash secret
24 *
25 * memory layout on 64 bit systems
26 * cccccccc cccccccc cccccccc uc -- unsigned char[24]
27 * pppppppp ssssssss ........ fnv -- two Py_hash_t
28 * k0k0k0k0 k1k1k1k1 ........ siphash -- two uint64_t
29 * ........ ........ ssssssss djbx33a -- 16 bytes padding + one Py_hash_t
30 * ........ ........ eeeeeeee pyexpat XML hash salt
31 *
32 * memory layout on 32 bit systems
33 * cccccccc cccccccc cccccccc uc
34 * ppppssss ........ ........ fnv -- two Py_hash_t
35 * k0k0k0k0 k1k1k1k1 ........ siphash -- two uint64_t (*)
36 * ........ ........ ssss.... djbx33a -- 16 bytes padding + one Py_hash_t
37 * ........ ........ eeee.... pyexpat XML hash salt
38 *
39 * (*) The siphash member may not be available on 32 bit platforms without
40 * an unsigned int64 data type.
41 */
42 typedef union {
43 /* ensure 24 bytes */
44 unsigned char uc[24];
45 /* two Py_hash_t for FNV */
46 struct {
47 Py_hash_t prefix;
48 Py_hash_t suffix;
49 } fnv;
50 /* two uint64 for SipHash24 */
51 struct {
52 uint64_t k0;
53 uint64_t k1;
54 } siphash;
55 /* a different (!) Py_hash_t for small string optimization */
56 struct {
57 unsigned char padding[16];
58 Py_hash_t suffix;
59 } djbx33a;
60 struct {
61 unsigned char padding[16];
62 Py_hash_t hashsalt;
63 } expat;
64 } _Py_HashSecret_t;
65
66 // Export for '_elementtree' shared extension
67 PyAPI_DATA(_Py_HashSecret_t) _Py_HashSecret;
68
69 #ifdef Py_DEBUG
70 extern int _Py_HashSecret_Initialized;
71 #endif
72
73
74 #ifndef MS_WINDOWS
75 # define _py_urandom_cache_INIT \
76 { \
77 .fd = -1, \
78 }
79 #else
80 # define _py_urandom_cache_INIT {0}
81 #endif
82
83 #define pyhash_state_INIT \
84 { \
85 .urandom_cache = _py_urandom_cache_INIT, \
86 }
87
88
89 extern uint64_t _Py_KeyedHash(uint64_t key, const void *src, Py_ssize_t src_sz);
90
91 #endif // !Py_INTERNAL_PYHASH_H