hash_32.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * hash_32 - 32 bit Fowler/Noll/Vo hash code
  3. *
  4. * @(#) $Revision: 5.1 $
  5. * @(#) $Id: hash_32.c,v 5.1 2009/06/30 09:13:32 chongo Exp $
  6. * @(#) $Source: /usr/local/src/cmd/fnv/RCS/hash_32.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. * NOTE: The FNV-0 historic hash is not recommended. One should use
  36. * the FNV-1 hash instead.
  37. *
  38. * To use the 32 bit FNV-0 historic hash, pass FNV0_32_INIT as the
  39. * Fnv32_t hashval argument to fnv_32_buf() or fnv_32_str().
  40. *
  41. * To use the recommended 32 bit FNV-1 hash, pass FNV1_32_INIT as the
  42. * Fnv32_t hashval argument to fnv_32_buf() or fnv_32_str().
  43. *
  44. ***
  45. *
  46. * Please do not copyright this code. This code is in the public domain.
  47. *
  48. * LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  49. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
  50. * EVENT SHALL LANDON CURT NOLL BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  51. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  52. * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  53. * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  54. * PERFORMANCE OF THIS SOFTWARE.
  55. *
  56. * By:
  57. * chongo <Landon Curt Noll> /\oo/\
  58. * http://www.isthe.com/chongo/
  59. *
  60. * Share and Enjoy! :-)
  61. */
  62. #include <stdlib.h>
  63. #include "fnv.h"
  64. /*
  65. * 32 bit magic FNV-0 and FNV-1 prime
  66. */
  67. #define FNV_32_PRIME ((Fnv32_t)0x01000193)
  68. /*
  69. * fnv_32_buf - perform a 32 bit Fowler/Noll/Vo hash on a buffer
  70. *
  71. * input:
  72. * buf - start of buffer to hash
  73. * len - length of buffer in octets
  74. * hval - previous hash value or 0 if first call
  75. *
  76. * returns:
  77. * 32 bit hash as a static hash type
  78. *
  79. * NOTE: To use the 32 bit FNV-0 historic hash, use FNV0_32_INIT as the hval
  80. * argument on the first call to either fnv_32_buf() or fnv_32_str().
  81. *
  82. * NOTE: To use the recommended 32 bit FNV-1 hash, use FNV1_32_INIT as the hval
  83. * argument on the first call to either fnv_32_buf() or fnv_32_str().
  84. */
  85. Fnv32_t
  86. fnv_32_buf(void *buf, size_t len, Fnv32_t hval)
  87. {
  88. unsigned char *bp = (unsigned char *)buf; /* start of buffer */
  89. unsigned char *be = bp + len; /* beyond end of buffer */
  90. /*
  91. * FNV-1 hash each octet in the buffer
  92. */
  93. while (bp < be) {
  94. /* multiply by the 32 bit FNV magic prime mod 2^32 */
  95. #if defined(NO_FNV_GCC_OPTIMIZATION)
  96. hval *= FNV_32_PRIME;
  97. #else
  98. hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
  99. #endif
  100. /* xor the bottom with the current octet */
  101. hval ^= (Fnv32_t)*bp++;
  102. }
  103. /* return our new hash value */
  104. return hval;
  105. }
  106. /*
  107. * fnv_32_str - perform a 32 bit Fowler/Noll/Vo hash on a string
  108. *
  109. * input:
  110. * str - string to hash
  111. * hval - previous hash value or 0 if first call
  112. *
  113. * returns:
  114. * 32 bit hash as a static hash type
  115. *
  116. * NOTE: To use the 32 bit FNV-0 historic hash, use FNV0_32_INIT as the hval
  117. * argument on the first call to either fnv_32_buf() or fnv_32_str().
  118. *
  119. * NOTE: To use the recommended 32 bit FNV-1 hash, use FNV1_32_INIT as the hval
  120. * argument on the first call to either fnv_32_buf() or fnv_32_str().
  121. */
  122. Fnv32_t
  123. fnv_32_str(char *str, Fnv32_t hval)
  124. {
  125. unsigned char *s = (unsigned char *)str; /* unsigned string */
  126. /*
  127. * FNV-1 hash each octet in the buffer
  128. */
  129. while (*s) {
  130. /* multiply by the 32 bit FNV magic prime mod 2^32 */
  131. #if defined(NO_FNV_GCC_OPTIMIZATION)
  132. hval *= FNV_32_PRIME;
  133. #else
  134. hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
  135. #endif
  136. /* xor the bottom with the current octet */
  137. hval ^= (Fnv32_t)*s++;
  138. }
  139. /* return our new hash value */
  140. return hval;
  141. }