42#include <netlink/hash.h>
45#define HASH_LITTLE_ENDIAN 1
46#define HASH_BIG_ENDIAN 0
48#define HASH_LITTLE_ENDIAN 0
49#define HASH_BIG_ENDIAN 1
54#define hashsize(n) ((uint32_t)1<<(n))
55#define hashmask(n) (hashsize(n)-1)
56#define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
104 a -= c; a ^= rot(c, 4); c += b; \
105 b -= a; b ^= rot(a, 6); a += c; \
106 c -= b; c ^= rot(b, 8); b += a; \
107 a -= c; a ^= rot(c,16); c += b; \
108 b -= a; b ^= rot(a,19); a += c; \
109 c -= b; c ^= rot(b, 4); b += a; \
137#define final(a,b,c) \
139 c ^= b; c -= rot(b,14); \
140 a ^= c; a -= rot(c,11); \
141 b ^= a; b -= rot(a,25); \
142 c ^= b; c -= rot(b,16); \
143 a ^= c; a -= rot(c,4); \
144 b ^= a; b -= rot(a,14); \
145 c ^= b; c -= rot(b,24); \
176static uint32_t hashlittle(
const void *key,
size_t length, uint32_t *val2 )
179 union {
const void *ptr;
size_t i; } u;
182 a = b = c = 0xdeadbeef + ((uint32_t)length) + *val2;
185 if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
186 const uint32_t *k = (
const uint32_t *)key;
215 case 12: c+=k[2]; b+=k[1]; a+=k[0];
break;
216 case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0];
break;
217 case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0];
break;
218 case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0];
break;
219 case 8 : b+=k[1]; a+=k[0];
break;
220 case 7 : b+=k[1]&0xffffff; a+=k[0];
break;
221 case 6 : b+=k[1]&0xffff; a+=k[0];
break;
222 case 5 : b+=k[1]&0xff; a+=k[0];
break;
223 case 4 : a+=k[0];
break;
224 case 3 : a+=k[0]&0xffffff;
break;
225 case 2 : a+=k[0]&0xffff;
break;
226 case 1 : a+=k[0]&0xff;
break;
232 k8 = (
const uint8_t *)k;
235 case 12: c+=k[2]; b+=k[1]; a+=k[0];
break;
236 case 11: c+=((uint32_t)k8[10])<<16;
237 case 10: c+=((uint32_t)k8[9])<<8;
239 case 8 : b+=k[1]; a+=k[0];
break;
240 case 7 : b+=((uint32_t)k8[6])<<16;
241 case 6 : b+=((uint32_t)k8[5])<<8;
243 case 4 : a+=k[0];
break;
244 case 3 : a+=((uint32_t)k8[2])<<16;
245 case 2 : a+=((uint32_t)k8[1])<<8;
246 case 1 : a+=k8[0];
break;
252 }
else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
253 const uint16_t *k = (
const uint16_t *)key;
259 a += k[0] + (((uint32_t)k[1])<<16);
260 b += k[2] + (((uint32_t)k[3])<<16);
261 c += k[4] + (((uint32_t)k[5])<<16);
268 k8 = (
const uint8_t *)k;
271 case 12: c+=k[4]+(((uint32_t)k[5])<<16);
272 b+=k[2]+(((uint32_t)k[3])<<16);
273 a+=k[0]+(((uint32_t)k[1])<<16);
275 case 11: c+=((uint32_t)k8[10])<<16;
277 b+=k[2]+(((uint32_t)k[3])<<16);
278 a+=k[0]+(((uint32_t)k[1])<<16);
281 case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
282 a+=k[0]+(((uint32_t)k[1])<<16);
284 case 7 : b+=((uint32_t)k8[6])<<16;
286 a+=k[0]+(((uint32_t)k[1])<<16);
289 case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
291 case 3 : a+=((uint32_t)k8[2])<<16;
300 const uint8_t *k = (
const uint8_t *)key;
306 a += ((uint32_t)k[1])<<8;
307 a += ((uint32_t)k[2])<<16;
308 a += ((uint32_t)k[3])<<24;
310 b += ((uint32_t)k[5])<<8;
311 b += ((uint32_t)k[6])<<16;
312 b += ((uint32_t)k[7])<<24;
314 c += ((uint32_t)k[9])<<8;
315 c += ((uint32_t)k[10])<<16;
316 c += ((uint32_t)k[11])<<24;
325 case 12: c+=((uint32_t)k[11])<<24;
326 case 11: c+=((uint32_t)k[10])<<16;
327 case 10: c+=((uint32_t)k[9])<<8;
329 case 8 : b+=((uint32_t)k[7])<<24;
330 case 7 : b+=((uint32_t)k[6])<<16;
331 case 6 : b+=((uint32_t)k[5])<<8;
333 case 4 : a+=((uint32_t)k[3])<<24;
334 case 3 : a+=((uint32_t)k[2])<<16;
335 case 2 : a+=((uint32_t)k[1])<<8;
353static uint32_t hashbig(
const void *key,
size_t length, uint32_t *val2)
356 union {
const void *ptr;
size_t i; } u;
359 a = b = c = 0xdeadbeef + ((uint32_t)length) + *val2;
362 if (HASH_BIG_ENDIAN && ((u.i & 0x3) == 0)) {
363 const uint32_t *k = (
const uint32_t *)key;
392 case 12: c+=k[2]; b+=k[1]; a+=k[0];
break;
393 case 11: c+=k[2]&0xffffff00; b+=k[1]; a+=k[0];
break;
394 case 10: c+=k[2]&0xffff0000; b+=k[1]; a+=k[0];
break;
395 case 9 : c+=k[2]&0xff000000; b+=k[1]; a+=k[0];
break;
396 case 8 : b+=k[1]; a+=k[0];
break;
397 case 7 : b+=k[1]&0xffffff00; a+=k[0];
break;
398 case 6 : b+=k[1]&0xffff0000; a+=k[0];
break;
399 case 5 : b+=k[1]&0xff000000; a+=k[0];
break;
400 case 4 : a+=k[0];
break;
401 case 3 : a+=k[0]&0xffffff00;
break;
402 case 2 : a+=k[0]&0xffff0000;
break;
403 case 1 : a+=k[0]&0xff000000;
break;
409 k8 = (
const uint8_t *)k;
412 case 12: c+=k[2]; b+=k[1]; a+=k[0];
break;
413 case 11: c+=((uint32_t)k8[10])<<8;
414 case 10: c+=((uint32_t)k8[9])<<16;
415 case 9 : c+=((uint32_t)k8[8])<<24;
416 case 8 : b+=k[1]; a+=k[0];
break;
417 case 7 : b+=((uint32_t)k8[6])<<8;
418 case 6 : b+=((uint32_t)k8[5])<<16;
419 case 5 : b+=((uint32_t)k8[4])<<24;
420 case 4 : a+=k[0];
break;
421 case 3 : a+=((uint32_t)k8[2])<<8;
422 case 2 : a+=((uint32_t)k8[1])<<16;
423 case 1 : a+=((uint32_t)k8[0])<<24;
break;
430 const uint8_t *k = (
const uint8_t *)key;
435 a += ((uint32_t)k[0])<<24;
436 a += ((uint32_t)k[1])<<16;
437 a += ((uint32_t)k[2])<<8;
438 a += ((uint32_t)k[3]);
439 b += ((uint32_t)k[4])<<24;
440 b += ((uint32_t)k[5])<<16;
441 b += ((uint32_t)k[6])<<8;
442 b += ((uint32_t)k[7]);
443 c += ((uint32_t)k[8])<<24;
444 c += ((uint32_t)k[9])<<16;
445 c += ((uint32_t)k[10])<<8;
446 c += ((uint32_t)k[11]);
456 case 11: c+=((uint32_t)k[10])<<8;
457 case 10: c+=((uint32_t)k[9])<<16;
458 case 9 : c+=((uint32_t)k[8])<<24;
460 case 7 : b+=((uint32_t)k[6])<<8;
461 case 6 : b+=((uint32_t)k[5])<<16;
462 case 5 : b+=((uint32_t)k[4])<<24;
464 case 3 : a+=((uint32_t)k[2])<<8;
465 case 2 : a+=((uint32_t)k[1])<<16;
466 case 1 : a+=((uint32_t)k[0])<<24;
477uint32_t nl_hash_any(
const void *key,
size_t length, uint32_t base)
480 return hashbig(key, length, &base);
482 return hashlittle(key, length, &base);