hashFunction.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. // Borrowed from: http://burtleburtle.net/bob/hash/doobs.html
  23. //
  24. // Original code by:
  25. //
  26. // By Bob Jenkins, 1996. [email protected]. You may use this
  27. // code any way you wish, private, educational, or commercial. It's free.
  28. #include "platform/platform.h"
  29. // Debug Profiling.
  30. #include "debug/profiler.h"
  31. #include "algorithm/hashFunction.h"
  32. #define hashsize(n) ((U32)1 << (n))
  33. #define hashmask(n) (hashsize(n) - 1)
  34. /*
  35. --------------------------------------------------------------------
  36. mix -- mix 3 32-bit values reversibly.
  37. For every delta with one or two bits set, and the deltas of all three
  38. high bits or all three low bits, whether the original value of a,b,c
  39. is almost all zero or is uniformly distributed,
  40. * If mix() is run forward or backward, at least 32 bits in a,b,c
  41. have at least 1/4 probability of changing.
  42. * If mix() is run forward, every bit of c will change between 1/3 and
  43. 2/3 of the time. (Well, 22/100 and 78/100 for some 2-bit deltas.)
  44. mix() was built out of 36 single-cycle latency instructions in a
  45. structure that could supported 2x parallelism, like so:
  46. a -= b;
  47. a -= c; x = (c>>13);
  48. b -= c; a ^= x;
  49. b -= a; x = (a<<8);
  50. c -= a; b ^= x;
  51. c -= b; x = (b>>13);
  52. ...
  53. Unfortunately, superscalar Pentiums and Sparcs can't take advantage
  54. of that parallelism. They've also turned some of those single-cycle
  55. latency instructions into multi-cycle latency instructions. Still,
  56. this is the fastest good hash I could find. There were about 2^^68
  57. to choose from. I only looked at a billion or so.
  58. --------------------------------------------------------------------
  59. */
  60. #define mix(a,b,c) \
  61. { \
  62. a -= b; a -= c; a ^= (c >> 13); \
  63. b -= c; b -= a; b ^= (a << 8); \
  64. c -= a; c -= b; c ^= (b >> 13); \
  65. a -= b; a -= c; a ^= (c >> 12); \
  66. b -= c; b -= a; b ^= (a << 16); \
  67. c -= a; c -= b; c ^= (b >> 5); \
  68. a -= b; a -= c; a ^= (c >> 3); \
  69. b -= c; b -= a; b ^= (a << 10); \
  70. c -= a; c -= b; c ^= (b >> 15); \
  71. }
  72. /*
  73. --------------------------------------------------------------------
  74. hash() -- hash a variable-length key into a 32-bit value
  75. k : the key (the unaligned variable-length array of bytes)
  76. len : the length of the key, counting by bytes
  77. initval : can be any 4-byte value
  78. Returns a 32-bit value. Every bit of the key affects every bit of
  79. the return value. Every 1-bit and 2-bit delta achieves avalanche.
  80. About 6*len+35 instructions.
  81. The best hash table sizes are powers of 2. There is no need to do
  82. mod a prime (mod is sooo slow!). If you need less than 32 bits,
  83. use a bitmask. For example, if you need only 10 bits, do
  84. h = (h & hashmask(10));
  85. In which case, the hash table should have hashsize(10) elements.
  86. If you are hashing n strings (U8 **)k, do it like this:
  87. for (i=0, h=0; i<n; ++i) h = hash( k[i], len[i], h);
  88. By Bob Jenkins, 1996. [email protected]. You may use this
  89. code any way you wish, private, educational, or commercial. It's free.
  90. See http://burtleburtle.net/bob/hash/evahash.html
  91. Use for hash table lookup, or anything where one collision in 2^^32 is
  92. acceptable. Do NOT use for cryptographic purposes.
  93. --------------------------------------------------------------------
  94. */
  95. U32 hash(register U8 *k, register U32 length, register U32 initval)
  96. {
  97. // Debug Profiling.
  98. PROFILE_SCOPE(HashFunction);
  99. U32 a, b, c, len;
  100. /* Set up the internal state */
  101. len = length;
  102. a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
  103. c = initval; /* the previous hash value */
  104. /*---------------------------------------- handle most of the key */
  105. while (len >= 12)
  106. {
  107. a += (k[0] + ((U32)k[1] << 8) + ((U32)k[ 2] << 16) + ((U32)k[ 3] << 24));
  108. b += (k[4] + ((U32)k[5] << 8) + ((U32)k[ 6] << 16) + ((U32)k[ 7] << 24));
  109. c += (k[8] + ((U32)k[9] << 8) + ((U32)k[10] << 16) + ((U32)k[11] << 24));
  110. mix(a, b, c);
  111. k += 12;
  112. len -= 12;
  113. }
  114. /*------------------------------------- handle the last 11 bytes */
  115. c += length;
  116. switch (len) /* all the case statements fall through */
  117. {
  118. case 11: c += ((U32)k[10] << 24);
  119. case 10: c += ((U32)k[9] << 16);
  120. case 9 : c += ((U32)k[8] << 8);
  121. /* the first byte of c is reserved for the length */
  122. case 8 : b += ((U32)k[7] << 24);
  123. case 7 : b += ((U32)k[6] << 16);
  124. case 6 : b += ((U32)k[5] << 8);
  125. case 5 : b += k[4];
  126. case 4 : a += ((U32)k[3] << 24);
  127. case 3 : a += ((U32)k[2] << 16);
  128. case 2 : a += ((U32)k[1] << 8);
  129. case 1 : a += k[0];
  130. /* case 0: nothing left to add */
  131. }
  132. mix(a, b, c);
  133. /*-------------------------------------------- report the result */
  134. return c;
  135. }