hash.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * This code was taken from http://ccodearchive.net/info/hash.html
  3. * The original file was modified to remove unwanted code
  4. * and some changes to fit the current build environment
  5. */
  6. /*
  7. -------------------------------------------------------------------------------
  8. lookup3.c, by Bob Jenkins, May 2006, Public Domain.
  9. These are functions for producing 32-bit hashes for hash table lookup.
  10. hash_word(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
  11. are externally useful functions. Routines to test the hash are included
  12. if SELF_TEST is defined. You can use this free for any purpose. It's in
  13. the public domain. It has no warranty.
  14. You probably want to use hashlittle(). hashlittle() and hashbig()
  15. hash byte arrays. hashlittle() is is faster than hashbig() on
  16. little-endian machines. Intel and AMD are little-endian machines.
  17. On second thought, you probably want hashlittle2(), which is identical to
  18. hashlittle() except it returns two 32-bit hashes for the price of one.
  19. You could implement hashbig2() if you wanted but I haven't bothered here.
  20. If you want to find a hash of, say, exactly 7 integers, do
  21. a = i1; b = i2; c = i3;
  22. mix(a,b,c);
  23. a += i4; b += i5; c += i6;
  24. mix(a,b,c);
  25. a += i7;
  26. final(a,b,c);
  27. then use c as the hash value. If you have a variable length array of
  28. 4-byte integers to hash, use hash_word(). If you have a byte array (like
  29. a character string), use hashlittle(). If you have several byte arrays, or
  30. a mix of things, see the comments above hashlittle().
  31. Why is this so big? I read 12 bytes at a time into 3 4-byte integers,
  32. then mix those integers. This is fast (you can do a lot more thorough
  33. mixing with 12*3 instructions on 3 integers than you can with 3 instructions
  34. on 1 byte), but shoehorning those bytes into integers efficiently is messy.
  35. -------------------------------------------------------------------------------
  36. */
  37. #include <netlink/hash.h>
  38. #if HAVE_LITTLE_ENDIAN
  39. #define HASH_LITTLE_ENDIAN 1
  40. #define HASH_BIG_ENDIAN 0
  41. #elif HAVE_BIG_ENDIAN
  42. #define HASH_LITTLE_ENDIAN 0
  43. #define HASH_BIG_ENDIAN 1
  44. #else
  45. #error Unknown endian
  46. #endif
  47. #define hashsize(n) ((uint32_t)1<<(n))
  48. #define hashmask(n) (hashsize(n)-1)
  49. #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
  50. /*
  51. -------------------------------------------------------------------------------
  52. mix -- mix 3 32-bit values reversibly.
  53. This is reversible, so any information in (a,b,c) before mix() is
  54. still in (a,b,c) after mix().
  55. If four pairs of (a,b,c) inputs are run through mix(), or through
  56. mix() in reverse, there are at least 32 bits of the output that
  57. are sometimes the same for one pair and different for another pair.
  58. This was tested for:
  59. * pairs that differed by one bit, by two bits, in any combination
  60. of top bits of (a,b,c), or in any combination of bottom bits of
  61. (a,b,c).
  62. * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
  63. the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
  64. is commonly produced by subtraction) look like a single 1-bit
  65. difference.
  66. * the base values were pseudorandom, all zero but one bit set, or
  67. all zero plus a counter that starts at zero.
  68. Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
  69. satisfy this are
  70. 4 6 8 16 19 4
  71. 9 15 3 18 27 15
  72. 14 9 3 7 17 3
  73. Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
  74. for "differ" defined as + with a one-bit base and a two-bit delta. I
  75. used http://burtleburtle.net/bob/hash/avalanche.html to choose
  76. the operations, constants, and arrangements of the variables.
  77. This does not achieve avalanche. There are input bits of (a,b,c)
  78. that fail to affect some output bits of (a,b,c), especially of a. The
  79. most thoroughly mixed value is c, but it doesn't really even achieve
  80. avalanche in c.
  81. This allows some parallelism. Read-after-writes are good at doubling
  82. the number of bits affected, so the goal of mixing pulls in the opposite
  83. direction as the goal of parallelism. I did what I could. Rotates
  84. seem to cost as much as shifts on every machine I could lay my hands
  85. on, and rotates are much kinder to the top and bottom bits, so I used
  86. rotates.
  87. -------------------------------------------------------------------------------
  88. */
  89. #define mix(a,b,c) \
  90. { \
  91. a -= c; a ^= rot(c, 4); c += b; \
  92. b -= a; b ^= rot(a, 6); a += c; \
  93. c -= b; c ^= rot(b, 8); b += a; \
  94. a -= c; a ^= rot(c,16); c += b; \
  95. b -= a; b ^= rot(a,19); a += c; \
  96. c -= b; c ^= rot(b, 4); b += a; \
  97. }
  98. /*
  99. -------------------------------------------------------------------------------
  100. final -- final mixing of 3 32-bit values (a,b,c) into c
  101. Pairs of (a,b,c) values differing in only a few bits will usually
  102. produce values of c that look totally different. This was tested for
  103. * pairs that differed by one bit, by two bits, in any combination
  104. of top bits of (a,b,c), or in any combination of bottom bits of
  105. (a,b,c).
  106. * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
  107. the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
  108. is commonly produced by subtraction) look like a single 1-bit
  109. difference.
  110. * the base values were pseudorandom, all zero but one bit set, or
  111. all zero plus a counter that starts at zero.
  112. These constants passed:
  113. 14 11 25 16 4 14 24
  114. 12 14 25 16 4 14 24
  115. and these came close:
  116. 4 8 15 26 3 22 24
  117. 10 8 15 26 3 22 24
  118. 11 8 15 26 3 22 24
  119. -------------------------------------------------------------------------------
  120. */
  121. #define final(a,b,c) \
  122. { \
  123. c ^= b; c -= rot(b,14); \
  124. a ^= c; a -= rot(c,11); \
  125. b ^= a; b -= rot(a,25); \
  126. c ^= b; c -= rot(b,16); \
  127. a ^= c; a -= rot(c,4); \
  128. b ^= a; b -= rot(a,14); \
  129. c ^= b; c -= rot(b,24); \
  130. }
  131. /*
  132. -------------------------------------------------------------------------------
  133. hashlittle() -- hash a variable-length key into a 32-bit value
  134. k : the key (the unaligned variable-length array of bytes)
  135. length : the length of the key, counting by bytes
  136. val2 : IN: can be any 4-byte value OUT: second 32 bit hash.
  137. Returns a 32-bit value. Every bit of the key affects every bit of
  138. the return value. Two keys differing by one or two bits will have
  139. totally different hash values. Note that the return value is better
  140. mixed than val2, so use that first.
  141. The best hash table sizes are powers of 2. There is no need to do
  142. mod a prime (mod is sooo slow!). If you need less than 32 bits,
  143. use a bitmask. For example, if you need only 10 bits, do
  144. h = (h & hashmask(10));
  145. In which case, the hash table should have hashsize(10) elements.
  146. If you are hashing n strings (uint8_t **)k, do it like this:
  147. for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h);
  148. By Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this
  149. code any way you wish, private, educational, or commercial. It's free.
  150. Use for hash table lookup, or anything where one collision in 2^^32 is
  151. acceptable. Do NOT use for cryptographic purposes.
  152. -------------------------------------------------------------------------------
  153. */
  154. static uint32_t hashlittle( const void *key, size_t length, uint32_t *val2 )
  155. {
  156. uint32_t a,b,c; /* internal state */
  157. union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
  158. /* Set up the internal state */
  159. a = b = c = 0xdeadbeef + ((uint32_t)length) + *val2;
  160. u.ptr = key;
  161. if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
  162. const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
  163. const uint8_t *k8;
  164. /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
  165. while (length > 12)
  166. {
  167. a += k[0];
  168. b += k[1];
  169. c += k[2];
  170. mix(a,b,c);
  171. length -= 12;
  172. k += 3;
  173. }
  174. /*----------------------------- handle the last (probably partial) block */
  175. /*
  176. * "k[2]&0xffffff" actually reads beyond the end of the string, but
  177. * then masks off the part it's not allowed to read. Because the
  178. * string is aligned, the masked-off tail is in the same word as the
  179. * rest of the string. Every machine with memory protection I've seen
  180. * does it on word boundaries, so is OK with this. But VALGRIND will
  181. * still catch it and complain. The masking trick does make the hash
  182. * noticably faster for short strings (like English words).
  183. *
  184. * Not on my testing with gcc 4.5 on an intel i5 CPU, at least --RR.
  185. */
  186. #if 0
  187. switch(length)
  188. {
  189. case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
  190. case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
  191. case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
  192. case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
  193. case 8 : b+=k[1]; a+=k[0]; break;
  194. case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
  195. case 6 : b+=k[1]&0xffff; a+=k[0]; break;
  196. case 5 : b+=k[1]&0xff; a+=k[0]; break;
  197. case 4 : a+=k[0]; break;
  198. case 3 : a+=k[0]&0xffffff; break;
  199. case 2 : a+=k[0]&0xffff; break;
  200. case 1 : a+=k[0]&0xff; break;
  201. case 0 : return c; /* zero length strings require no mixing */
  202. }
  203. #else /* make valgrind happy */
  204. k8 = (const uint8_t *)k;
  205. switch(length)
  206. {
  207. case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
  208. case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
  209. case 10: c+=((uint32_t)k8[9])<<8; /* fall through */
  210. case 9 : c+=k8[8]; /* fall through */
  211. case 8 : b+=k[1]; a+=k[0]; break;
  212. case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
  213. case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */
  214. case 5 : b+=k8[4]; /* fall through */
  215. case 4 : a+=k[0]; break;
  216. case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
  217. case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */
  218. case 1 : a+=k8[0]; break;
  219. case 0 : return c;
  220. }
  221. #endif /* !valgrind */
  222. } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
  223. const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
  224. const uint8_t *k8;
  225. /*--------------- all but last block: aligned reads and different mixing */
  226. while (length > 12)
  227. {
  228. a += k[0] + (((uint32_t)k[1])<<16);
  229. b += k[2] + (((uint32_t)k[3])<<16);
  230. c += k[4] + (((uint32_t)k[5])<<16);
  231. mix(a,b,c);
  232. length -= 12;
  233. k += 6;
  234. }
  235. /*----------------------------- handle the last (probably partial) block */
  236. k8 = (const uint8_t *)k;
  237. switch(length)
  238. {
  239. case 12: c+=k[4]+(((uint32_t)k[5])<<16);
  240. b+=k[2]+(((uint32_t)k[3])<<16);
  241. a+=k[0]+(((uint32_t)k[1])<<16);
  242. break;
  243. case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
  244. case 10: c+=k[4];
  245. b+=k[2]+(((uint32_t)k[3])<<16);
  246. a+=k[0]+(((uint32_t)k[1])<<16);
  247. break;
  248. case 9 : c+=k8[8]; /* fall through */
  249. case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
  250. a+=k[0]+(((uint32_t)k[1])<<16);
  251. break;
  252. case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
  253. case 6 : b+=k[2];
  254. a+=k[0]+(((uint32_t)k[1])<<16);
  255. break;
  256. case 5 : b+=k8[4]; /* fall through */
  257. case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
  258. break;
  259. case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
  260. case 2 : a+=k[0];
  261. break;
  262. case 1 : a+=k8[0];
  263. break;
  264. case 0 : return c; /* zero length requires no mixing */
  265. }
  266. } else { /* need to read the key one byte at a time */
  267. const uint8_t *k = (const uint8_t *)key;
  268. /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
  269. while (length > 12)
  270. {
  271. a += k[0];
  272. a += ((uint32_t)k[1])<<8;
  273. a += ((uint32_t)k[2])<<16;
  274. a += ((uint32_t)k[3])<<24;
  275. b += k[4];
  276. b += ((uint32_t)k[5])<<8;
  277. b += ((uint32_t)k[6])<<16;
  278. b += ((uint32_t)k[7])<<24;
  279. c += k[8];
  280. c += ((uint32_t)k[9])<<8;
  281. c += ((uint32_t)k[10])<<16;
  282. c += ((uint32_t)k[11])<<24;
  283. mix(a,b,c);
  284. length -= 12;
  285. k += 12;
  286. }
  287. /*-------------------------------- last block: affect all 32 bits of (c) */
  288. switch(length) /* all the case statements fall through */
  289. {
  290. case 12: c+=((uint32_t)k[11])<<24;
  291. case 11: c+=((uint32_t)k[10])<<16;
  292. case 10: c+=((uint32_t)k[9])<<8;
  293. case 9 : c+=k[8];
  294. case 8 : b+=((uint32_t)k[7])<<24;
  295. case 7 : b+=((uint32_t)k[6])<<16;
  296. case 6 : b+=((uint32_t)k[5])<<8;
  297. case 5 : b+=k[4];
  298. case 4 : a+=((uint32_t)k[3])<<24;
  299. case 3 : a+=((uint32_t)k[2])<<16;
  300. case 2 : a+=((uint32_t)k[1])<<8;
  301. case 1 : a+=k[0];
  302. break;
  303. case 0 : return c;
  304. }
  305. }
  306. final(a,b,c);
  307. *val2 = b;
  308. return c;
  309. }
  310. /*
  311. * hashbig():
  312. * This is the same as hash_word() on big-endian machines. It is different
  313. * from hashlittle() on all machines. hashbig() takes advantage of
  314. * big-endian byte ordering.
  315. */
  316. static uint32_t hashbig( const void *key, size_t length, uint32_t *val2)
  317. {
  318. uint32_t a,b,c;
  319. union { const void *ptr; size_t i; } u; /* to cast key to (size_t) happily */
  320. /* Set up the internal state */
  321. a = b = c = 0xdeadbeef + ((uint32_t)length) + *val2;
  322. u.ptr = key;
  323. if (HASH_BIG_ENDIAN && ((u.i & 0x3) == 0)) {
  324. const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
  325. const uint8_t *k8;
  326. /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
  327. while (length > 12)
  328. {
  329. a += k[0];
  330. b += k[1];
  331. c += k[2];
  332. mix(a,b,c);
  333. length -= 12;
  334. k += 3;
  335. }
  336. /*----------------------------- handle the last (probably partial) block */
  337. /*
  338. * "k[2]<<8" actually reads beyond the end of the string, but
  339. * then shifts out the part it's not allowed to read. Because the
  340. * string is aligned, the illegal read is in the same word as the
  341. * rest of the string. Every machine with memory protection I've seen
  342. * does it on word boundaries, so is OK with this. But VALGRIND will
  343. * still catch it and complain. The masking trick does make the hash
  344. * noticably faster for short strings (like English words).
  345. *
  346. * Not on my testing with gcc 4.5 on an intel i5 CPU, at least --RR.
  347. */
  348. #if 0
  349. switch(length)
  350. {
  351. case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
  352. case 11: c+=k[2]&0xffffff00; b+=k[1]; a+=k[0]; break;
  353. case 10: c+=k[2]&0xffff0000; b+=k[1]; a+=k[0]; break;
  354. case 9 : c+=k[2]&0xff000000; b+=k[1]; a+=k[0]; break;
  355. case 8 : b+=k[1]; a+=k[0]; break;
  356. case 7 : b+=k[1]&0xffffff00; a+=k[0]; break;
  357. case 6 : b+=k[1]&0xffff0000; a+=k[0]; break;
  358. case 5 : b+=k[1]&0xff000000; a+=k[0]; break;
  359. case 4 : a+=k[0]; break;
  360. case 3 : a+=k[0]&0xffffff00; break;
  361. case 2 : a+=k[0]&0xffff0000; break;
  362. case 1 : a+=k[0]&0xff000000; break;
  363. case 0 : return c; /* zero length strings require no mixing */
  364. }
  365. #else /* make valgrind happy */
  366. k8 = (const uint8_t *)k;
  367. switch(length) /* all the case statements fall through */
  368. {
  369. case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
  370. case 11: c+=((uint32_t)k8[10])<<8; /* fall through */
  371. case 10: c+=((uint32_t)k8[9])<<16; /* fall through */
  372. case 9 : c+=((uint32_t)k8[8])<<24; /* fall through */
  373. case 8 : b+=k[1]; a+=k[0]; break;
  374. case 7 : b+=((uint32_t)k8[6])<<8; /* fall through */
  375. case 6 : b+=((uint32_t)k8[5])<<16; /* fall through */
  376. case 5 : b+=((uint32_t)k8[4])<<24; /* fall through */
  377. case 4 : a+=k[0]; break;
  378. case 3 : a+=((uint32_t)k8[2])<<8; /* fall through */
  379. case 2 : a+=((uint32_t)k8[1])<<16; /* fall through */
  380. case 1 : a+=((uint32_t)k8[0])<<24; break;
  381. case 0 : return c;
  382. }
  383. #endif /* !VALGRIND */
  384. } else { /* need to read the key one byte at a time */
  385. const uint8_t *k = (const uint8_t *)key;
  386. /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
  387. while (length > 12)
  388. {
  389. a += ((uint32_t)k[0])<<24;
  390. a += ((uint32_t)k[1])<<16;
  391. a += ((uint32_t)k[2])<<8;
  392. a += ((uint32_t)k[3]);
  393. b += ((uint32_t)k[4])<<24;
  394. b += ((uint32_t)k[5])<<16;
  395. b += ((uint32_t)k[6])<<8;
  396. b += ((uint32_t)k[7]);
  397. c += ((uint32_t)k[8])<<24;
  398. c += ((uint32_t)k[9])<<16;
  399. c += ((uint32_t)k[10])<<8;
  400. c += ((uint32_t)k[11]);
  401. mix(a,b,c);
  402. length -= 12;
  403. k += 12;
  404. }
  405. /*-------------------------------- last block: affect all 32 bits of (c) */
  406. switch(length) /* all the case statements fall through */
  407. {
  408. case 12: c+=k[11];
  409. case 11: c+=((uint32_t)k[10])<<8;
  410. case 10: c+=((uint32_t)k[9])<<16;
  411. case 9 : c+=((uint32_t)k[8])<<24;
  412. case 8 : b+=k[7];
  413. case 7 : b+=((uint32_t)k[6])<<8;
  414. case 6 : b+=((uint32_t)k[5])<<16;
  415. case 5 : b+=((uint32_t)k[4])<<24;
  416. case 4 : a+=k[3];
  417. case 3 : a+=((uint32_t)k[2])<<8;
  418. case 2 : a+=((uint32_t)k[1])<<16;
  419. case 1 : a+=((uint32_t)k[0])<<24;
  420. break;
  421. case 0 : return c;
  422. }
  423. }
  424. final(a,b,c);
  425. *val2 = b;
  426. return c;
  427. }
  428. uint32_t nl_hash_any(const void *key, size_t length, uint32_t base)
  429. {
  430. if (HASH_BIG_ENDIAN)
  431. return hashbig(key, length, &base);
  432. else
  433. return hashlittle(key, length, &base);
  434. }