jenkins_hash.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * @file jenkins_hash.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Jenkins hash functions for use in hash tables.
  25. * The resulting hashes depend on the endianness of the machine.
  26. */
  27. #ifndef BADVPN_MISC_JENKINS_HASH_H
  28. #define BADVPN_MISC_JENKINS_HASH_H
  29. #include <stdint.h>
  30. static uint32_t jenkins_one_at_a_time_hash (uint8_t *key, int key_len);
  31. static uint32_t jenkins_lookup2_hash (uint8_t *k, uint32_t length, uint32_t initval);
  32. uint32_t jenkins_one_at_a_time_hash (uint8_t *key, int key_len)
  33. {
  34. uint32_t hash = 0;
  35. int i;
  36. for (i = 0; i < key_len; i++) {
  37. hash += key[i];
  38. hash += (hash << 10);
  39. hash ^= (hash >> 6);
  40. }
  41. hash += (hash << 3);
  42. hash ^= (hash >> 11);
  43. hash += (hash << 15);
  44. return hash;
  45. }
  46. #define lookup2_hashsize(n) ((uint32_t)1<<(n))
  47. #define lookup2_hashmask(n) (hashsize(n)-1)
  48. #define lookup2_mix(a,b,c) \
  49. { \
  50. a -= b; a -= c; a ^= (c>>13); \
  51. b -= c; b -= a; b ^= (a<<8); \
  52. c -= a; c -= b; c ^= (b>>13); \
  53. a -= b; a -= c; a ^= (c>>12); \
  54. b -= c; b -= a; b ^= (a<<16); \
  55. c -= a; c -= b; c ^= (b>>5); \
  56. a -= b; a -= c; a ^= (c>>3); \
  57. b -= c; b -= a; b ^= (a<<10); \
  58. c -= a; c -= b; c ^= (b>>15); \
  59. }
  60. uint32_t jenkins_lookup2_hash (uint8_t *k, uint32_t length, uint32_t initval)
  61. {
  62. uint32_t a,b,c,len;
  63. /* Set up the internal state */
  64. len = length;
  65. a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
  66. c = initval; /* the previous hash value */
  67. /*---------------------------------------- handle most of the key */
  68. while (len >= 12)
  69. {
  70. a += (k[0] +((uint32_t)k[1]<<8) +((uint32_t)k[2]<<16) +((uint32_t)k[3]<<24));
  71. b += (k[4] +((uint32_t)k[5]<<8) +((uint32_t)k[6]<<16) +((uint32_t)k[7]<<24));
  72. c += (k[8] +((uint32_t)k[9]<<8) +((uint32_t)k[10]<<16)+((uint32_t)k[11]<<24));
  73. lookup2_mix(a,b,c);
  74. k += 12; len -= 12;
  75. }
  76. /*------------------------------------- handle the last 11 bytes */
  77. c += length;
  78. switch(len) /* all the case statements fall through */
  79. {
  80. case 11: c+=((uint32_t)k[10]<<24);
  81. case 10: c+=((uint32_t)k[9]<<16);
  82. case 9 : c+=((uint32_t)k[8]<<8);
  83. /* the first byte of c is reserved for the length */
  84. case 8 : b+=((uint32_t)k[7]<<24);
  85. case 7 : b+=((uint32_t)k[6]<<16);
  86. case 6 : b+=((uint32_t)k[5]<<8);
  87. case 5 : b+=k[4];
  88. case 4 : a+=((uint32_t)k[3]<<24);
  89. case 3 : a+=((uint32_t)k[2]<<16);
  90. case 2 : a+=((uint32_t)k[1]<<8);
  91. case 1 : a+=k[0];
  92. /* case 0: nothing left to add */
  93. }
  94. lookup2_mix(a,b,c);
  95. /*-------------------------------------------- report the result */
  96. return c;
  97. }
  98. #endif