You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

382 lines
13KB

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