4 #ifndef DEBUG_STADTX_HASH
5 #define DEBUG_STADTX_HASH 0
10 #define _ROTL_SIZED(x,r,s) ( ((x) << (r)) | ((x) >> ((s) - (r))) )
11 #define _ROTR_SIZED(x,r,s) ( ((x) << ((s) - (r))) | ((x) >> (r)) )
12 #define ROTL64(x,r) _ROTL_SIZED(x,r,64)
13 #define ROTR64(x,r) _ROTR_SIZED(x,r,64)
16 #ifndef PERL_SEEN_HV_FUNC_H
28 #define U8 unsigned char
40 #ifndef STADTX_STATIC_INLINE
41 #ifdef PERL_STATIC_INLINE
42 #define STADTX_STATIC_INLINE PERL_STATIC_INLINE
44 #define STADTX_STATIC_INLINE static inline
50 #define STMT_END while(0)
53 #ifndef STADTX_UNALIGNED_AND_LITTLE_ENDIAN
54 #define STADTX_UNALIGNED_AND_LITTLE_ENDIAN 1
57 #if STADTX_ALLOW_UNALIGNED_AND_LITTLE_ENDIAN
59 #define U8TO64_LE(ptr) (*((const U64 *)(ptr)))
62 #define U8TO32_LE(ptr) (*((const U32 *)(ptr)))
65 #define U8TO16_LE(ptr) (*((const U16 *)(ptr)))
69 #define U8TO64_LE(ptr) (\
70 (U64)(ptr)[7] << 56 | \
71 (U64)(ptr)[6] << 48 | \
72 (U64)(ptr)[5] << 40 | \
73 (U64)(ptr)[4] << 32 | \
74 (U64)(ptr)[3] << 24 | \
75 (U64)(ptr)[2] << 16 | \
76 (U64)(ptr)[1] << 8 | \
81 #define U8TO32_LE(ptr) (\
82 (U32)(ptr)[3] << 24 | \
83 (U32)(ptr)[2] << 16 | \
84 (U32)(ptr)[1] << 8 | \
89 #define U8TO16_LE(ptr) (\
90 (U16)(ptr)[1] << 8 | \
96 /* do a marsaglia xor-shift permutation followed by a
97 * multiply by a prime (presumably large) and another
98 * marsaglia xor-shift permutation.
99 * One of these thoroughly changes the bits of the input.
100 * Two of these with different primes passes the Strict Avalanche Criteria
101 * in all the tests I did.
103 * Note that v cannot end up zero after a scramble64 unless it
104 * was zero in the first place.
106 #define STADTX_SCRAMBLE64(v,prime) STMT_START { \
117 STADTX_STATIC_INLINE void stadtx_seed_state (
121 U64 *seed= (U64 *)seed_ch;
122 U64 *state= (U64 *)state_ch;
123 /* first we apply two masks to each word of the seed, this means that
124 * a) at least one of state[0] and state[2] is nonzero,
125 * b) at least one of state[1] and state[3] is nonzero
126 * c) that state[0] and state[2] are different
127 * d) that state[1] and state[3] are different
128 * e) that the replacement value for any zero's is a totally different from the seed value.
129 * (iow, if seed[0] is 0x43f6a8885a308d31UL then state[0] becomes 0, which is the replaced
130 * with 1, which is totally different.). */
131 /* hex expansion of pi, skipping first two digits. pi= 3.2[43f6...]*/
132 /* pi value in hex from here:
133 * http://turner.faculty.swau.edu/mathematics/materialslibrary/pi/pibases.html*/
134 state[0]= seed[0] ^ 0x43f6a8885a308d31UL;
135 state[1]= seed[1] ^ 0x3198a2e03707344aUL;
136 state[2]= seed[0] ^ 0x4093822299f31d00UL;
137 state[3]= seed[1] ^ 0x82efa98ec4e6c894UL;
138 if (!state[0]) state[0]=1;
139 if (!state[1]) state[1]=2;
140 if (!state[2]) state[2]=4;
141 if (!state[3]) state[3]=8;
142 /* and now for good measure we double scramble all four -
143 * a double scramble guarantees a complete avalanche of all the
144 * bits in the seed - IOW, by the time we are hashing the
145 * four state vectors should be completely different and utterly
146 * uncognizable from the input seed bits */
147 STADTX_SCRAMBLE64(state[0],0x801178846e899d17UL);
148 STADTX_SCRAMBLE64(state[0],0xdd51e5d1c9a5a151UL);
149 STADTX_SCRAMBLE64(state[1],0x93a7d6c8c62e4835UL);
150 STADTX_SCRAMBLE64(state[1],0x803340f36895c2b5UL);
151 STADTX_SCRAMBLE64(state[2],0xbea9344eb7565eebUL);
152 STADTX_SCRAMBLE64(state[2],0xcd95d1e509b995cdUL);
153 STADTX_SCRAMBLE64(state[3],0x9999791977e30c13UL);
154 STADTX_SCRAMBLE64(state[3],0xaab8b6b05abfc6cdUL);
157 #define STADTX_K0_U64 0xb89b0f8e1655514fUL
158 #define STADTX_K1_U64 0x8c6f736011bd5127UL
159 #define STADTX_K2_U64 0x8f29bd94edce7b39UL
160 #define STADTX_K3_U64 0x9c1b8e1e9628323fUL
162 #define STADTX_K2_U32 0x802910e3
163 #define STADTX_K3_U32 0x819b13af
164 #define STADTX_K4_U32 0x91cb27e5
165 #define STADTX_K5_U32 0xc1a269c1
167 STADTX_STATIC_INLINE U64 stadtx_hash_with_state(
172 U64 *state= (U64 *)state_ch;
174 U64 v0= state[0] ^ ((key_len+1) * STADTX_K0_U64);
175 U64 v1= state[1] ^ ((key_len+2) * STADTX_K1_U64);
179 v0 += U8TO64_LE(key) * STADTX_K3_U64;
180 v0= ROTR64(v0, 17) ^ v1;
181 v1= ROTR64(v1, 53) + v0;
184 v0 += U8TO64_LE(key) * STADTX_K3_U64;
185 v0= ROTR64(v0, 17) ^ v1;
186 v1= ROTR64(v1, 53) + v0;
189 v0 += U8TO64_LE(key) * STADTX_K3_U64;
190 v0= ROTR64(v0, 17) ^ v1;
191 v1= ROTR64(v1, 53) + v0;
196 switch ( len & 0x7 ) {
197 case 7: v0 += (U64)key[6] << 32;
198 case 6: v1 += (U64)key[5] << 48;
199 case 5: v0 += (U64)key[4] << 16;
200 case 4: v1 += (U64)U8TO32_LE(key);
202 case 3: v0 += (U64)key[2] << 48;
203 case 2: v1 += (U64)U8TO16_LE(key);
205 case 1: v0 += (U64)key[0];
206 case 0: v1 = ROTL64(v1, 32) ^ 0xFF;
210 v0 = ROTR64(v0,33) + v1;
211 v1 = ROTL64(v1,17) ^ v0;
212 v0 = ROTL64(v0,43) + v1;
213 v1 = ROTL64(v1,31) - v0;
214 v0 = ROTL64(v0,13) ^ v1;
216 v0 = ROTL64(v0,41) + v1;
217 v1 = ROTL64(v1,37) ^ v0;
218 v0 = ROTR64(v0,39) + v1;
219 v1 = ROTR64(v1,15) + v0;
220 v0 = ROTL64(v0,15) ^ v1;
224 U64 v2= state[2] ^ ((key_len+3) * STADTX_K2_U64);
225 U64 v3= state[3] ^ ((key_len+4) * STADTX_K3_U64);
228 v0 += (U64)U8TO64_LE(key+ 0) * STADTX_K2_U32; v0= ROTL64(v0,57) ^ v3;
229 v1 += (U64)U8TO64_LE(key+ 8) * STADTX_K3_U32; v1= ROTL64(v1,63) ^ v2;
230 v2 += (U64)U8TO64_LE(key+16) * STADTX_K4_U32; v2= ROTR64(v2,47) + v0;
231 v3 += (U64)U8TO64_LE(key+24) * STADTX_K5_U32; v3= ROTR64(v3,11) - v1;
234 } while ( len >= 32 );
236 switch ( len >> 3 ) {
237 case 3: v0 += ((U64)U8TO64_LE(key) * STADTX_K2_U32); key += 8; v0= ROTL64(v0,57) ^ v3;
238 case 2: v1 += ((U64)U8TO64_LE(key) * STADTX_K3_U32); key += 8; v1= ROTL64(v1,63) ^ v2;
239 case 1: v2 += ((U64)U8TO64_LE(key) * STADTX_K4_U32); key += 8; v2= ROTR64(v2,47) + v0;
240 case 0: v3 = ROTR64(v3,11) - v1;
242 v0 ^= (len+1) * STADTX_K3_U64;
243 switch ( len & 0x7 ) {
244 case 7: v1 += (U64)key[6];
245 case 6: v2 += (U64)U8TO16_LE(key+4);
246 v3 += (U64)U8TO32_LE(key);
248 case 5: v1 += (U64)key[4];
249 case 4: v2 += (U64)U8TO32_LE(key);
251 case 3: v3 += (U64)key[2];
252 case 2: v1 += (U64)U8TO16_LE(key);
254 case 1: v2 += (U64)key[0];
255 case 0: v3 = ROTL64(v3, 32) ^ 0xFF;
269 v2 = ROTR64(v2,43) - v3;
270 v2 = ROTL64(v2,55) ^ v0;
272 v3 = ROTR64(v3, 7) - v2;
278 v3 = ROTR64(v3,17) ^ v2;
285 v0 = ROTR64(v0, 1) - v1;
287 return v0 ^ v1 ^ v2 ^ v3;
291 STADTX_STATIC_INLINE U64 stadtx_hash(
297 stadtx_seed_state(seed_ch,(U8*)state);
298 return stadtx_hash_with_state((U8*)state,key,key_len);