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.

291 lines
7.9 KiB

  1. /*
  2. * hash_64 - 64 bit Fowler/Noll/Vo-0 FNV-1a hash code
  3. *
  4. * @(#) $Revision: 5.1 $
  5. * @(#) $Id: hash_64a.c,v 5.1 2009/06/30 09:01:38 chongo Exp $
  6. * @(#) $Source: /usr/local/src/cmd/fnv/RCS/hash_64a.c,v $
  7. *
  8. ***
  9. *
  10. * Fowler/Noll/Vo hash
  11. *
  12. * The basis of this hash algorithm was taken from an idea sent
  13. * as reviewer comments to the IEEE POSIX P1003.2 committee by:
  14. *
  15. * Phong Vo (http://www.research.att.com/info/kpv/)
  16. * Glenn Fowler (http://www.research.att.com/~gsf/)
  17. *
  18. * In a subsequent ballot round:
  19. *
  20. * Landon Curt Noll (http://www.isthe.com/chongo/)
  21. *
  22. * improved on their algorithm. Some people tried this hash
  23. * and found that it worked rather well. In an EMail message
  24. * to Landon, they named it the ``Fowler/Noll/Vo'' or FNV hash.
  25. *
  26. * FNV hashes are designed to be fast while maintaining a low
  27. * collision rate. The FNV speed allows one to quickly hash lots
  28. * of data while maintaining a reasonable collision rate. See:
  29. *
  30. * http://www.isthe.com/chongo/tech/comp/fnv/index.html
  31. *
  32. * for more details as well as other forms of the FNV hash.
  33. *
  34. ***
  35. *
  36. * To use the recommended 64 bit FNV-1a hash, pass FNV1A_64_INIT as the
  37. * Fnv64_t hashval argument to fnv_64a_buf() or fnv_64a_str().
  38. *
  39. ***
  40. *
  41. * Please do not copyright this code. This code is in the public domain.
  42. *
  43. * LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  44. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
  45. * EVENT SHALL LANDON CURT NOLL BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  46. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  47. * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  48. * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  49. * PERFORMANCE OF THIS SOFTWARE.
  50. *
  51. * By:
  52. * chongo <Landon Curt Noll> /\oo/\
  53. * http://www.isthe.com/chongo/
  54. *
  55. * Share and Enjoy! :-)
  56. */
  57. #include <stdlib.h>
  58. #include "fnv.h"
  59. /*
  60. * FNV-1a defines the initial basis to be non-zero
  61. */
  62. #if !defined(HAVE_64BIT_LONG_LONG)
  63. const Fnv64_t fnv1a_64_init = { 0x84222325, 0xcbf29ce4 };
  64. #endif /* ! HAVE_64BIT_LONG_LONG */
  65. /*
  66. * 64 bit magic FNV-1a prime
  67. */
  68. #if defined(HAVE_64BIT_LONG_LONG)
  69. #define FNV_64_PRIME ((Fnv64_t)0x100000001b3ULL)
  70. #else /* HAVE_64BIT_LONG_LONG */
  71. #define FNV_64_PRIME_LOW ((unsigned long)0x1b3) /* lower bits of FNV prime */
  72. #define FNV_64_PRIME_SHIFT (8) /* top FNV prime shift above 2^32 */
  73. #endif /* HAVE_64BIT_LONG_LONG */
  74. /*
  75. * fnv_64a_buf - perform a 64 bit Fowler/Noll/Vo FNV-1a hash on a buffer
  76. *
  77. * input:
  78. * buf - start of buffer to hash
  79. * len - length of buffer in octets
  80. * hval - previous hash value or 0 if first call
  81. *
  82. * returns:
  83. * 64 bit hash as a static hash type
  84. *
  85. * NOTE: To use the recommended 64 bit FNV-1a hash, use FNV1A_64_INIT as the
  86. * hval arg on the first call to either fnv_64a_buf() or fnv_64a_str().
  87. */
  88. Fnv64_t
  89. fnv_64a_buf(void *buf, size_t len, Fnv64_t hval)
  90. {
  91. unsigned char *bp = (unsigned char *)buf; /* start of buffer */
  92. unsigned char *be = bp + len; /* beyond end of buffer */
  93. #if defined(HAVE_64BIT_LONG_LONG)
  94. /*
  95. * FNV-1a hash each octet of the buffer
  96. */
  97. while (bp < be) {
  98. /* xor the bottom with the current octet */
  99. hval ^= (Fnv64_t)*bp++;
  100. /* multiply by the 64 bit FNV magic prime mod 2^64 */
  101. #if defined(NO_FNV_GCC_OPTIMIZATION)
  102. hval *= FNV_64_PRIME;
  103. #else /* NO_FNV_GCC_OPTIMIZATION */
  104. hval += (hval << 1) + (hval << 4) + (hval << 5) +
  105. (hval << 7) + (hval << 8) + (hval << 40);
  106. #endif /* NO_FNV_GCC_OPTIMIZATION */
  107. }
  108. #else /* HAVE_64BIT_LONG_LONG */
  109. unsigned long val[4]; /* hash value in base 2^16 */
  110. unsigned long tmp[4]; /* tmp 64 bit value */
  111. /*
  112. * Convert Fnv64_t hval into a base 2^16 array
  113. */
  114. val[0] = hval.w32[0];
  115. val[1] = (val[0] >> 16);
  116. val[0] &= 0xffff;
  117. val[2] = hval.w32[1];
  118. val[3] = (val[2] >> 16);
  119. val[2] &= 0xffff;
  120. /*
  121. * FNV-1a hash each octet of the buffer
  122. */
  123. while (bp < be) {
  124. /* xor the bottom with the current octet */
  125. val[0] ^= (unsigned long)*bp++;
  126. /*
  127. * multiply by the 64 bit FNV magic prime mod 2^64
  128. *
  129. * Using 0x100000001b3 we have the following digits base 2^16:
  130. *
  131. * 0x0 0x100 0x0 0x1b3
  132. *
  133. * which is the same as:
  134. *
  135. * 0x0 1<<FNV_64_PRIME_SHIFT 0x0 FNV_64_PRIME_LOW
  136. */
  137. /* multiply by the lowest order digit base 2^16 */
  138. tmp[0] = val[0] * FNV_64_PRIME_LOW;
  139. tmp[1] = val[1] * FNV_64_PRIME_LOW;
  140. tmp[2] = val[2] * FNV_64_PRIME_LOW;
  141. tmp[3] = val[3] * FNV_64_PRIME_LOW;
  142. /* multiply by the other non-zero digit */
  143. tmp[2] += val[0] << FNV_64_PRIME_SHIFT; /* tmp[2] += val[0] * 0x100 */
  144. tmp[3] += val[1] << FNV_64_PRIME_SHIFT; /* tmp[3] += val[1] * 0x100 */
  145. /* propagate carries */
  146. tmp[1] += (tmp[0] >> 16);
  147. val[0] = tmp[0] & 0xffff;
  148. tmp[2] += (tmp[1] >> 16);
  149. val[1] = tmp[1] & 0xffff;
  150. val[3] = tmp[3] + (tmp[2] >> 16);
  151. val[2] = tmp[2] & 0xffff;
  152. /*
  153. * Doing a val[3] &= 0xffff; is not really needed since it simply
  154. * removes multiples of 2^64. We can discard these excess bits
  155. * outside of the loop when we convert to Fnv64_t.
  156. */
  157. }
  158. /*
  159. * Convert base 2^16 array back into an Fnv64_t
  160. */
  161. hval.w32[1] = ((val[3]<<16) | val[2]);
  162. hval.w32[0] = ((val[1]<<16) | val[0]);
  163. #endif /* HAVE_64BIT_LONG_LONG */
  164. /* return our new hash value */
  165. return hval;
  166. }
  167. /*
  168. * fnv_64a_str - perform a 64 bit Fowler/Noll/Vo FNV-1a hash on a buffer
  169. *
  170. * input:
  171. * buf - start of buffer to hash
  172. * hval - previous hash value or 0 if first call
  173. *
  174. * returns:
  175. * 64 bit hash as a static hash type
  176. *
  177. * NOTE: To use the recommended 64 bit FNV-1a hash, use FNV1A_64_INIT as the
  178. * hval arg on the first call to either fnv_64a_buf() or fnv_64a_str().
  179. */
  180. Fnv64_t
  181. fnv_64a_str(char *str, Fnv64_t hval)
  182. {
  183. unsigned char *s = (unsigned char *)str; /* unsigned string */
  184. #if defined(HAVE_64BIT_LONG_LONG)
  185. /*
  186. * FNV-1a hash each octet of the string
  187. */
  188. while (*s) {
  189. /* xor the bottom with the current octet */
  190. hval ^= (Fnv64_t)*s++;
  191. /* multiply by the 64 bit FNV magic prime mod 2^64 */
  192. #if defined(NO_FNV_GCC_OPTIMIZATION)
  193. hval *= FNV_64_PRIME;
  194. #else /* NO_FNV_GCC_OPTIMIZATION */
  195. hval += (hval << 1) + (hval << 4) + (hval << 5) +
  196. (hval << 7) + (hval << 8) + (hval << 40);
  197. #endif /* NO_FNV_GCC_OPTIMIZATION */
  198. }
  199. #else /* !HAVE_64BIT_LONG_LONG */
  200. unsigned long val[4]; /* hash value in base 2^16 */
  201. unsigned long tmp[4]; /* tmp 64 bit value */
  202. /*
  203. * Convert Fnv64_t hval into a base 2^16 array
  204. */
  205. val[0] = hval.w32[0];
  206. val[1] = (val[0] >> 16);
  207. val[0] &= 0xffff;
  208. val[2] = hval.w32[1];
  209. val[3] = (val[2] >> 16);
  210. val[2] &= 0xffff;
  211. /*
  212. * FNV-1a hash each octet of the string
  213. */
  214. while (*s) {
  215. /* xor the bottom with the current octet */
  216. /*
  217. * multiply by the 64 bit FNV magic prime mod 2^64
  218. *
  219. * Using 1099511628211, we have the following digits base 2^16:
  220. *
  221. * 0x0 0x100 0x0 0x1b3
  222. *
  223. * which is the same as:
  224. *
  225. * 0x0 1<<FNV_64_PRIME_SHIFT 0x0 FNV_64_PRIME_LOW
  226. */
  227. /* multiply by the lowest order digit base 2^16 */
  228. tmp[0] = val[0] * FNV_64_PRIME_LOW;
  229. tmp[1] = val[1] * FNV_64_PRIME_LOW;
  230. tmp[2] = val[2] * FNV_64_PRIME_LOW;
  231. tmp[3] = val[3] * FNV_64_PRIME_LOW;
  232. /* multiply by the other non-zero digit */
  233. tmp[2] += val[0] << FNV_64_PRIME_SHIFT; /* tmp[2] += val[0] * 0x100 */
  234. tmp[3] += val[1] << FNV_64_PRIME_SHIFT; /* tmp[3] += val[1] * 0x100 */
  235. /* propagate carries */
  236. tmp[1] += (tmp[0] >> 16);
  237. val[0] = tmp[0] & 0xffff;
  238. tmp[2] += (tmp[1] >> 16);
  239. val[1] = tmp[1] & 0xffff;
  240. val[3] = tmp[3] + (tmp[2] >> 16);
  241. val[2] = tmp[2] & 0xffff;
  242. /*
  243. * Doing a val[3] &= 0xffff; is not really needed since it simply
  244. * removes multiples of 2^64. We can discard these excess bits
  245. * outside of the loop when we convert to Fnv64_t.
  246. */
  247. val[0] ^= (unsigned long)(*s++);
  248. }
  249. /*
  250. * Convert base 2^16 array back into an Fnv64_t
  251. */
  252. hval.w32[1] = ((val[3]<<16) | val[2]);
  253. hval.w32[0] = ((val[1]<<16) | val[0]);
  254. #endif /* !HAVE_64BIT_LONG_LONG */
  255. /* return our new hash value */
  256. return hval;
  257. }