hashFunction.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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. #include "core/util/hashFunction.h"
  30. namespace Torque
  31. {
  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(const U8 *k, U32 length, U32 initval)
  96. {
  97. U32 a,b,c,len;
  98. /* Set up the internal state */
  99. len = length;
  100. a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
  101. c = initval; /* the previous hash value */
  102. /*---------------------------------------- handle most of the key */
  103. while (len >= 12)
  104. {
  105. a += (k[0] +((U32)k[1]<<8) +((U32)k[2]<<16) +((U32)k[3]<<24));
  106. b += (k[4] +((U32)k[5]<<8) +((U32)k[6]<<16) +((U32)k[7]<<24));
  107. c += (k[8] +((U32)k[9]<<8) +((U32)k[10]<<16)+((U32)k[11]<<24));
  108. mix(a,b,c);
  109. k += 12; len -= 12;
  110. }
  111. /*------------------------------------- handle the last 11 bytes */
  112. c += length;
  113. switch(len) /* all the case statements fall through */
  114. {
  115. case 11: c+=((U32)k[10]<<24);
  116. case 10: c+=((U32)k[9]<<16);
  117. case 9 : c+=((U32)k[8]<<8);
  118. /* the first byte of c is reserved for the length */
  119. case 8 : b+=((U32)k[7]<<24);
  120. case 7 : b+=((U32)k[6]<<16);
  121. case 6 : b+=((U32)k[5]<<8);
  122. case 5 : b+=k[4];
  123. case 4 : a+=((U32)k[3]<<24);
  124. case 3 : a+=((U32)k[2]<<16);
  125. case 2 : a+=((U32)k[1]<<8);
  126. case 1 : a+=k[0];
  127. /* case 0: nothing left to add */
  128. }
  129. mix(a,b,c);
  130. /*-------------------------------------------- report the result */
  131. return c;
  132. }
  133. /*
  134. --------------------------------------------------------------------
  135. mix -- mix 3 64-bit values reversibly.
  136. mix() takes 48 machine instructions, but only 24 cycles on a superscalar
  137. machine (like Intel's new MMX architecture). It requires 4 64-bit
  138. registers for 4::2 parallelism.
  139. All 1-bit deltas, all 2-bit deltas, all deltas composed of top bits of
  140. (a,b,c), and all deltas of bottom bits were tested. All deltas were
  141. tested both on random keys and on keys that were nearly all zero.
  142. These deltas all cause every bit of c to change between 1/3 and 2/3
  143. of the time (well, only 113/400 to 287/400 of the time for some
  144. 2-bit delta). These deltas all cause at least 80 bits to change
  145. among (a,b,c) when the mix is run either forward or backward (yes it
  146. is reversible).
  147. This implies that a hash using mix64 has no funnels. There may be
  148. characteristics with 3-bit deltas or bigger, I didn't test for
  149. those.
  150. --------------------------------------------------------------------
  151. */
  152. #define mix64(a,b,c) \
  153. { \
  154. a -= b; a -= c; a ^= (c>>43); \
  155. b -= c; b -= a; b ^= (a<<9); \
  156. c -= a; c -= b; c ^= (b>>8); \
  157. a -= b; a -= c; a ^= (c>>38); \
  158. b -= c; b -= a; b ^= (a<<23); \
  159. c -= a; c -= b; c ^= (b>>5); \
  160. a -= b; a -= c; a ^= (c>>35); \
  161. b -= c; b -= a; b ^= (a<<49); \
  162. c -= a; c -= b; c ^= (b>>11); \
  163. a -= b; a -= c; a ^= (c>>12); \
  164. b -= c; b -= a; b ^= (a<<18); \
  165. c -= a; c -= b; c ^= (b>>22); \
  166. }
  167. /*
  168. --------------------------------------------------------------------
  169. hash64() -- hash a variable-length key into a 64-bit value
  170. k : the key (the unaligned variable-length array of bytes)
  171. len : the length of the key, counting by bytes
  172. level : can be any 8-byte value
  173. Returns a 64-bit value. Every bit of the key affects every bit of
  174. the return value. No funnels. Every 1-bit and 2-bit delta achieves
  175. avalanche. About 41+5len instructions.
  176. The best hash table sizes are powers of 2. There is no need to do
  177. mod a prime (mod is sooo slow!). If you need less than 64 bits,
  178. use a bitmask. For example, if you need only 10 bits, do
  179. h = (h & hashmask(10));
  180. In which case, the hash table should have hashsize(10) elements.
  181. If you are hashing n strings (ub1 **)k, do it like this:
  182. for (i=0, h=0; i<n; ++i) h = hash( k[i], len[i], h);
  183. By Bob Jenkins, Jan 4 1997. [email protected]. You may
  184. use this code any way you wish, private, educational, or commercial,
  185. but I would appreciate if you give me credit.
  186. See http://burtleburtle.net/bob/hash/evahash.html
  187. Use for hash table lookup, or anything where one collision in 2^^64
  188. is acceptable. Do NOT use for cryptographic purposes.
  189. --------------------------------------------------------------------
  190. */
  191. U64 hash64( const U8 *k, U32 length, U64 initval )
  192. {
  193. U64 a,b,c,len;
  194. /* Set up the internal state */
  195. len = length;
  196. a = b = initval; /* the previous hash value */
  197. c = 0x9e3779b97f4a7c13LL; /* the golden ratio; an arbitrary value */
  198. /*---------------------------------------- handle most of the key */
  199. while (len >= 24)
  200. {
  201. a += (k[0] +((U64)k[ 1]<< 8)+((U64)k[ 2]<<16)+((U64)k[ 3]<<24)
  202. +((U64)k[4 ]<<32)+((U64)k[ 5]<<40)+((U64)k[ 6]<<48)+((U64)k[ 7]<<56));
  203. b += (k[8] +((U64)k[ 9]<< 8)+((U64)k[10]<<16)+((U64)k[11]<<24)
  204. +((U64)k[12]<<32)+((U64)k[13]<<40)+((U64)k[14]<<48)+((U64)k[15]<<56));
  205. c += (k[16] +((U64)k[17]<< 8)+((U64)k[18]<<16)+((U64)k[19]<<24)
  206. +((U64)k[20]<<32)+((U64)k[21]<<40)+((U64)k[22]<<48)+((U64)k[23]<<56));
  207. mix64(a,b,c);
  208. k += 24; len -= 24;
  209. }
  210. /*------------------------------------- handle the last 23 bytes */
  211. c += length;
  212. switch(len) /* all the case statements fall through */
  213. {
  214. case 23: c+=((U64)k[22]<<56);
  215. case 22: c+=((U64)k[21]<<48);
  216. case 21: c+=((U64)k[20]<<40);
  217. case 20: c+=((U64)k[19]<<32);
  218. case 19: c+=((U64)k[18]<<24);
  219. case 18: c+=((U64)k[17]<<16);
  220. case 17: c+=((U64)k[16]<<8);
  221. /* the first byte of c is reserved for the length */
  222. case 16: b+=((U64)k[15]<<56);
  223. case 15: b+=((U64)k[14]<<48);
  224. case 14: b+=((U64)k[13]<<40);
  225. case 13: b+=((U64)k[12]<<32);
  226. case 12: b+=((U64)k[11]<<24);
  227. case 11: b+=((U64)k[10]<<16);
  228. case 10: b+=((U64)k[ 9]<<8);
  229. case 9: b+=((U64)k[ 8]);
  230. case 8: a+=((U64)k[ 7]<<56);
  231. case 7: a+=((U64)k[ 6]<<48);
  232. case 6: a+=((U64)k[ 5]<<40);
  233. case 5: a+=((U64)k[ 4]<<32);
  234. case 4: a+=((U64)k[ 3]<<24);
  235. case 3: a+=((U64)k[ 2]<<16);
  236. case 2: a+=((U64)k[ 1]<<8);
  237. case 1: a+=((U64)k[ 0]);
  238. /* case 0: nothing left to add */
  239. }
  240. mix64(a,b,c);
  241. /*-------------------------------------------- report the result */
  242. return c;
  243. }
  244. } // namespace