4 #ifndef DEBUG_STADTX_HASH
5 #define DEBUG_STADTX_HASH 0
8 #ifndef PERL_SEEN_HV_FUNC_H_
20 #define U8 unsigned char
33 #ifndef STADTX_STATIC_INLINE
34 #ifdef PERL_STATIC_INLINE
35 #define STADTX_STATIC_INLINE PERL_STATIC_INLINE
37 #define STADTX_STATIC_INLINE static inline
43 #define STMT_END while(0)
46 /* Find best way to ROTL32/ROTL64 */
48 #include <stdlib.h> /* Microsoft put _rotl declaration in here */
49 #define ROTL32(x,r) _rotl(x,r)
50 #define ROTR32(x,r) _rotr(x,r)
51 #define ROTL64(x,r) _rotl64(x,r)
52 #define ROTR64(x,r) _rotr64(x,r)
54 /* gcc recognises this code and generates a rotate instruction for CPUs with one */
55 #define ROTL32(x,r) (((U32)(x) << (r)) | ((U32)(x) >> (32 - (r))))
56 #define ROTR32(x,r) (((U32)(x) << (32 - (r))) | ((U32)(x) >> (r)))
57 #define ROTL64(x,r) ( ( (U64)(x) << (r) ) | ( (U64)(x) >> ( 64 - (r) ) ) )
58 #define ROTR64(x,r) ( ( (U64)(x) << ( 64 - (r) ) ) | ( (U64)(x) >> (r) ) )
62 /* do a marsaglia xor-shift permutation followed by a
63 * multiply by a prime (presumably large) and another
64 * marsaglia xor-shift permutation.
65 * One of these thoroughly changes the bits of the input.
66 * Two of these with different primes passes the Strict Avalanche Criteria
67 * in all the tests I did.
69 * Note that v cannot end up zero after a scramble64 unless it
70 * was zero in the first place.
72 #define STADTX_SCRAMBLE64(v,prime) STMT_START { \
83 STADTX_STATIC_INLINE void stadtx_seed_state (
87 const U64 *seed= (const U64 *)seed_ch;
88 U64 *state= (U64 *)state_ch;
89 /* first we apply two masks to each word of the seed, this means that
90 * a) at least one of state[0] and state[2] is nonzero,
91 * b) at least one of state[1] and state[3] is nonzero
92 * c) that state[0] and state[2] are different
93 * d) that state[1] and state[3] are different
94 * e) that the replacement value for any zero's is a totally different from the seed value.
95 * (iow, if seed[0] is 0x43f6a8885a308d31UL then state[0] becomes 0, which is the replaced
96 * with 1, which is totally different.). */
97 /* hex expansion of pi, skipping first two digits. pi= 3.2[43f6...]*/
98 /* pi value in hex from here:
99 * http://turner.faculty.swau.edu/mathematics/materialslibrary/pi/pibases.html*/
100 state[0]= seed[0] ^ UINT64_C(0x43f6a8885a308d31);
101 state[1]= seed[1] ^ UINT64_C(0x3198a2e03707344a);
102 state[2]= seed[0] ^ UINT64_C(0x4093822299f31d00);
103 state[3]= seed[1] ^ UINT64_C(0x82efa98ec4e6c894);
104 if (!state[0]) state[0]=1;
105 if (!state[1]) state[1]=2;
106 if (!state[2]) state[2]=4;
107 if (!state[3]) state[3]=8;
108 /* and now for good measure we double scramble all four -
109 * a double scramble guarantees a complete avalanche of all the
110 * bits in the seed - IOW, by the time we are hashing the
111 * four state vectors should be completely different and utterly
112 * uncognizable from the input seed bits */
113 STADTX_SCRAMBLE64(state[0],UINT64_C(0x801178846e899d17));
114 STADTX_SCRAMBLE64(state[0],UINT64_C(0xdd51e5d1c9a5a151));
115 STADTX_SCRAMBLE64(state[1],UINT64_C(0x93a7d6c8c62e4835));
116 STADTX_SCRAMBLE64(state[1],UINT64_C(0x803340f36895c2b5));
117 STADTX_SCRAMBLE64(state[2],UINT64_C(0xbea9344eb7565eeb));
118 STADTX_SCRAMBLE64(state[2],UINT64_C(0xcd95d1e509b995cd));
119 STADTX_SCRAMBLE64(state[3],UINT64_C(0x9999791977e30c13));
120 STADTX_SCRAMBLE64(state[3],UINT64_C(0xaab8b6b05abfc6cd));
123 #define STADTX_K0_U64 UINT64_C(0xb89b0f8e1655514f)
124 #define STADTX_K1_U64 UINT64_C(0x8c6f736011bd5127)
125 #define STADTX_K2_U64 UINT64_C(0x8f29bd94edce7b39)
126 #define STADTX_K3_U64 UINT64_C(0x9c1b8e1e9628323f)
128 #define STADTX_K2_U32 0x802910e3
129 #define STADTX_K3_U32 0x819b13af
130 #define STADTX_K4_U32 0x91cb27e5
131 #define STADTX_K5_U32 0xc1a269c1
133 STADTX_STATIC_INLINE U64 stadtx_hash_with_state(
138 U64 *state= (U64 *)state_ch;
139 STRLEN len = key_len;
140 U64 v0= state[0] ^ ((key_len+1) * STADTX_K0_U64);
141 U64 v1= state[1] ^ ((key_len+2) * STADTX_K1_U64);
145 v0 += U8TO64_LE(key) * STADTX_K3_U64;
146 v0= ROTR64(v0, 17) ^ v1;
147 v1= ROTR64(v1, 53) + v0;
151 v0 += U8TO64_LE(key) * STADTX_K3_U64;
152 v0= ROTR64(v0, 17) ^ v1;
153 v1= ROTR64(v1, 53) + v0;
157 v0 += U8TO64_LE(key) * STADTX_K3_U64;
158 v0= ROTR64(v0, 17) ^ v1;
159 v1= ROTR64(v1, 53) + v0;
165 switch ( len & 0x7 ) {
166 case 7: v0 += (U64)key[6] << 32;
168 case 6: v1 += (U64)key[5] << 48;
170 case 5: v0 += (U64)key[4] << 16;
172 case 4: v1 += (U64)U8TO32_LE(key);
174 case 3: v0 += (U64)key[2] << 48;
176 case 2: v1 += (U64)U8TO16_LE(key);
178 case 1: v0 += (U64)key[0];
180 case 0: v1 = ROTL64(v1, 32) ^ 0xFF;
184 v0 = ROTR64(v0,33) + v1;
185 v1 = ROTL64(v1,17) ^ v0;
186 v0 = ROTL64(v0,43) + v1;
187 v1 = ROTL64(v1,31) - v0;
188 v0 = ROTL64(v0,13) ^ v1;
190 v0 = ROTL64(v0,41) + v1;
191 v1 = ROTL64(v1,37) ^ v0;
192 v0 = ROTR64(v0,39) + v1;
193 v1 = ROTR64(v1,15) + v0;
194 v0 = ROTL64(v0,15) ^ v1;
198 U64 v2= state[2] ^ ((key_len+3) * STADTX_K2_U64);
199 U64 v3= state[3] ^ ((key_len+4) * STADTX_K3_U64);
202 v0 += (U64)U8TO64_LE(key+ 0) * STADTX_K2_U32; v0= ROTL64(v0,57) ^ v3;
203 v1 += (U64)U8TO64_LE(key+ 8) * STADTX_K3_U32; v1= ROTL64(v1,63) ^ v2;
204 v2 += (U64)U8TO64_LE(key+16) * STADTX_K4_U32; v2= ROTR64(v2,47) + v0;
205 v3 += (U64)U8TO64_LE(key+24) * STADTX_K5_U32; v3= ROTR64(v3,11) - v1;
208 } while ( len >= 32 );
210 switch ( len >> 3 ) {
211 case 3: v0 += ((U64)U8TO64_LE(key) * STADTX_K2_U32); key += 8; v0= ROTL64(v0,57) ^ v3;
213 case 2: v1 += ((U64)U8TO64_LE(key) * STADTX_K3_U32); key += 8; v1= ROTL64(v1,63) ^ v2;
215 case 1: v2 += ((U64)U8TO64_LE(key) * STADTX_K4_U32); key += 8; v2= ROTR64(v2,47) + v0;
217 case 0: v3 = ROTR64(v3,11) - v1;
220 v0 ^= (len+1) * STADTX_K3_U64;
221 switch ( len & 0x7 ) {
222 case 7: v1 += (U64)key[6];
224 case 6: v2 += (U64)U8TO16_LE(key+4);
225 v3 += (U64)U8TO32_LE(key);
227 case 5: v1 += (U64)key[4];
229 case 4: v2 += (U64)U8TO32_LE(key);
231 case 3: v3 += (U64)key[2];
233 case 2: v1 += (U64)U8TO16_LE(key);
235 case 1: v2 += (U64)key[0];
237 case 0: v3 = ROTL64(v3, 32) ^ 0xFF;
251 v2 = ROTR64(v2,43) - v3;
252 v2 = ROTL64(v2,55) ^ v0;
254 v3 = ROTR64(v3, 7) - v2;
260 v3 = ROTR64(v3,17) ^ v2;
267 v0 = ROTR64(v0, 1) - v1;
269 return v0 ^ v1 ^ v2 ^ v3;
273 STADTX_STATIC_INLINE U64 stadtx_hash(
279 stadtx_seed_state(seed_ch,(U8*)state);
280 return stadtx_hash_with_state((U8*)state,key,key_len);