phash.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* Modified for use with yasm by Peter Johnson. */
  2. #include "util.h"
  3. /*
  4. --------------------------------------------------------------------
  5. lookupa.c, by Bob Jenkins, December 1996. Same as lookup2.c
  6. Use this code however you wish. Public Domain. No warranty.
  7. Source is http://burtleburtle.net/bob/c/lookupa.c
  8. --------------------------------------------------------------------
  9. */
  10. #include "phash.h"
  11. #define ub4 unsigned long
  12. #define hashsize(n) ((ub4)1<<(n))
  13. #define hashmask(n) (hashsize(n)-1)
  14. /*
  15. --------------------------------------------------------------------
  16. mix -- mix 3 32-bit values reversibly.
  17. For every delta with one or two bit set, and the deltas of all three
  18. high bits or all three low bits, whether the original value of a,b,c
  19. is almost all zero or is uniformly distributed,
  20. * If mix() is run forward or backward, at least 32 bits in a,b,c
  21. have at least 1/4 probability of changing.
  22. * If mix() is run forward, every bit of c will change between 1/3 and
  23. 2/3 of the time. (Well, 22/100 and 78/100 for some 2-bit deltas.)
  24. mix() was built out of 36 single-cycle latency instructions in a
  25. structure that could supported 2x parallelism, like so:
  26. a -= b;
  27. a -= c; x = (c>>13);
  28. b -= c; a ^= x;
  29. b -= a; x = (a<<8);
  30. c -= a; b ^= x;
  31. c -= b; x = (b>>13);
  32. ...
  33. Unfortunately, superscalar Pentiums and Sparcs can't take advantage
  34. of that parallelism. They've also turned some of those single-cycle
  35. latency instructions into multi-cycle latency instructions. Still,
  36. this is the fastest good hash I could find. There were about 2^^68
  37. to choose from. I only looked at a billion or so.
  38. --------------------------------------------------------------------
  39. */
  40. #define mix(a,b,c) \
  41. { \
  42. a -= b; a -= c; a ^= (c>>13); \
  43. a &= 0xffffffff; \
  44. b -= c; b -= a; b ^= (a<<8); \
  45. b &= 0xffffffff; \
  46. c -= a; c -= b; c ^= (b>>13); \
  47. c &= 0xffffffff; \
  48. a -= b; a -= c; a ^= (c>>12); \
  49. a &= 0xffffffff; \
  50. b -= c; b -= a; b ^= (a<<16); \
  51. b &= 0xffffffff; \
  52. c -= a; c -= b; c ^= (b>>5); \
  53. c &= 0xffffffff; \
  54. a -= b; a -= c; a ^= (c>>3); \
  55. a &= 0xffffffff; \
  56. b -= c; b -= a; b ^= (a<<10); \
  57. b &= 0xffffffff; \
  58. c -= a; c -= b; c ^= (b>>15); \
  59. c &= 0xffffffff; \
  60. }
  61. /*
  62. --------------------------------------------------------------------
  63. lookup() -- hash a variable-length key into a 32-bit value
  64. k : the key (the unaligned variable-length array of bytes)
  65. len : the length of the key, counting by bytes
  66. level : can be any 4-byte value
  67. Returns a 32-bit value. Every bit of the key affects every bit of
  68. the return value. Every 1-bit and 2-bit delta achieves avalanche.
  69. About 6len+35 instructions.
  70. The best hash table sizes are powers of 2. There is no need to do
  71. mod a prime (mod is sooo slow!). If you need less than 32 bits,
  72. use a bitmask. For example, if you need only 10 bits, do
  73. h = (h & hashmask(10));
  74. In which case, the hash table should have hashsize(10) elements.
  75. If you are hashing n strings (ub1 **)k, do it like this:
  76. for (i=0, h=0; i<n; ++i) h = lookup( k[i], len[i], h);
  77. By Bob Jenkins, 1996. bob_jenkins@burtleburtle.net. You may use this
  78. code any way you wish, private, educational, or commercial.
  79. See http://burtleburtle.net/bob/hash/evahash.html
  80. Use for hash table lookup, or anything where one collision in 2^32 is
  81. acceptable. Do NOT use for cryptographic purposes.
  82. --------------------------------------------------------------------
  83. */
  84. unsigned long
  85. phash_lookup(
  86. register const char *sk, /* the key */
  87. register size_t length, /* the length of the key */
  88. register unsigned long level) /* the previous hash, or an arbitrary value */
  89. {
  90. unsigned long a,b,c;
  91. size_t len;
  92. const unsigned char *k = (const unsigned char *)sk;
  93. /* Set up the internal state */
  94. len = length;
  95. a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
  96. c = level; /* the previous hash value */
  97. /*---------------------------------------- handle most of the key */
  98. while (len >= 12)
  99. {
  100. a += (k[0] +((ub4)k[1]<<8) +((ub4)k[2]<<16) +((ub4)k[3]<<24));
  101. a &= 0xffffffff;
  102. b += (k[4] +((ub4)k[5]<<8) +((ub4)k[6]<<16) +((ub4)k[7]<<24));
  103. b &= 0xffffffff;
  104. c += (k[8] +((ub4)k[9]<<8) +((ub4)k[10]<<16)+((ub4)k[11]<<24));
  105. c &= 0xffffffff;
  106. mix(a,b,c);
  107. k += 12; len -= 12;
  108. }
  109. /*------------------------------------- handle the last 11 bytes */
  110. c += (ub4)length;
  111. switch(len) /* all the case statements fall through */
  112. {
  113. case 11: c+=((ub4)k[10]<<24);
  114. case 10: c+=((ub4)k[9]<<16);
  115. case 9 : c+=((ub4)k[8]<<8);
  116. c &= 0xffffffff;
  117. /* the first byte of c is reserved for the length */
  118. case 8 : b+=((ub4)k[7]<<24);
  119. case 7 : b+=((ub4)k[6]<<16);
  120. case 6 : b+=((ub4)k[5]<<8);
  121. case 5 : b+=k[4];
  122. b &= 0xffffffff;
  123. case 4 : a+=((ub4)k[3]<<24);
  124. case 3 : a+=((ub4)k[2]<<16);
  125. case 2 : a+=((ub4)k[1]<<8);
  126. case 1 : a+=k[0];
  127. a &= 0xffffffff;
  128. /* case 0: nothing left to add */
  129. }
  130. mix(a,b,c);
  131. /*-------------------------------------------- report the result */
  132. return c;
  133. }
  134. /*
  135. --------------------------------------------------------------------
  136. mixc -- mixc 8 4-bit values as quickly and thoroughly as possible.
  137. Repeating mix() three times achieves avalanche.
  138. Repeating mix() four times eliminates all funnels and all
  139. characteristics stronger than 2^{-11}.
  140. --------------------------------------------------------------------
  141. */
  142. #define mixc(a,b,c,d,e,f,g,h) \
  143. { \
  144. a^=b<<11; d+=a; b+=c; \
  145. b^=c>>2; e+=b; c+=d; \
  146. c^=d<<8; f+=c; d+=e; \
  147. d^=e>>16; g+=d; e+=f; \
  148. e^=f<<10; h+=e; f+=g; \
  149. f^=g>>4; a+=f; g+=h; \
  150. g^=h<<8; b+=g; h+=a; \
  151. h^=a>>9; c+=h; a+=b; \
  152. }
  153. /*
  154. --------------------------------------------------------------------
  155. checksum() -- hash a variable-length key into a 256-bit value
  156. k : the key (the unaligned variable-length array of bytes)
  157. len : the length of the key, counting by bytes
  158. state : an array of CHECKSTATE 4-byte values (256 bits)
  159. The state is the checksum. Every bit of the key affects every bit of
  160. the state. There are no funnels. About 112+6.875len instructions.
  161. If you are hashing n strings (ub1 **)k, do it like this:
  162. for (i=0; i<8; ++i) state[i] = 0x9e3779b9;
  163. for (i=0, h=0; i<n; ++i) checksum( k[i], len[i], state);
  164. (c) Bob Jenkins, 1996. bob_jenkins@burtleburtle.net. You may use this
  165. code any way you wish, private, educational, or commercial, as long
  166. as this whole comment accompanies it.
  167. See http://burtleburtle.net/bob/hash/evahash.html
  168. Use to detect changes between revisions of documents, assuming nobody
  169. is trying to cause collisions. Do NOT use for cryptography.
  170. --------------------------------------------------------------------
  171. */
  172. void
  173. phash_checksum(
  174. register const char *sk,
  175. register size_t len,
  176. register unsigned long *state)
  177. {
  178. unsigned long a,b,c,d,e,f,g,h;
  179. size_t length;
  180. const unsigned char *k = (const unsigned char *)sk;
  181. /* Use the length and level; add in the golden ratio. */
  182. length = len;
  183. a=state[0]; b=state[1]; c=state[2]; d=state[3];
  184. e=state[4]; f=state[5]; g=state[6]; h=state[7];
  185. /*---------------------------------------- handle most of the key */
  186. while (len >= 32)
  187. {
  188. a += (k[0] +(k[1]<<8) +(k[2]<<16) +(k[3]<<24));
  189. b += (k[4] +(k[5]<<8) +(k[6]<<16) +(k[7]<<24));
  190. c += (k[8] +(k[9]<<8) +(k[10]<<16)+(k[11]<<24));
  191. d += (k[12]+(k[13]<<8)+(k[14]<<16)+(k[15]<<24));
  192. e += (k[16]+(k[17]<<8)+(k[18]<<16)+(k[19]<<24));
  193. f += (k[20]+(k[21]<<8)+(k[22]<<16)+(k[23]<<24));
  194. g += (k[24]+(k[25]<<8)+(k[26]<<16)+(k[27]<<24));
  195. h += (k[28]+(k[29]<<8)+(k[30]<<16)+(k[31]<<24));
  196. mixc(a,b,c,d,e,f,g,h);
  197. mixc(a,b,c,d,e,f,g,h);
  198. mixc(a,b,c,d,e,f,g,h);
  199. mixc(a,b,c,d,e,f,g,h);
  200. k += 32; len -= 32;
  201. }
  202. /*------------------------------------- handle the last 31 bytes */
  203. h += (ub4)length;
  204. switch(len)
  205. {
  206. case 31: h+=(k[30]<<24);
  207. case 30: h+=(k[29]<<16);
  208. case 29: h+=(k[28]<<8);
  209. case 28: g+=(k[27]<<24);
  210. case 27: g+=(k[26]<<16);
  211. case 26: g+=(k[25]<<8);
  212. case 25: g+=k[24];
  213. case 24: f+=(k[23]<<24);
  214. case 23: f+=(k[22]<<16);
  215. case 22: f+=(k[21]<<8);
  216. case 21: f+=k[20];
  217. case 20: e+=(k[19]<<24);
  218. case 19: e+=(k[18]<<16);
  219. case 18: e+=(k[17]<<8);
  220. case 17: e+=k[16];
  221. case 16: d+=(k[15]<<24);
  222. case 15: d+=(k[14]<<16);
  223. case 14: d+=(k[13]<<8);
  224. case 13: d+=k[12];
  225. case 12: c+=(k[11]<<24);
  226. case 11: c+=(k[10]<<16);
  227. case 10: c+=(k[9]<<8);
  228. case 9 : c+=k[8];
  229. case 8 : b+=(k[7]<<24);
  230. case 7 : b+=(k[6]<<16);
  231. case 6 : b+=(k[5]<<8);
  232. case 5 : b+=k[4];
  233. case 4 : a+=(k[3]<<24);
  234. case 3 : a+=(k[2]<<16);
  235. case 2 : a+=(k[1]<<8);
  236. case 1 : a+=k[0];
  237. }
  238. mixc(a,b,c,d,e,f,g,h);
  239. mixc(a,b,c,d,e,f,g,h);
  240. mixc(a,b,c,d,e,f,g,h);
  241. mixc(a,b,c,d,e,f,g,h);
  242. /*-------------------------------------------- report the result */
  243. state[0]=a; state[1]=b; state[2]=c; state[3]=d;
  244. state[4]=e; state[5]=f; state[6]=g; state[7]=h;
  245. }