wyhash.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // This is free and unencumbered software released into the public domain under The Unlicense (http://unlicense.org/)
  2. // main repo: https://github.com/wangyi-fudan/wyhash
  3. // author: 王一 Wang Yi <godspeed_china@yeah.net>
  4. // contributors: Reini Urban, Dietrich Epp, Joshua Haberman, Tommy Ettinger, Daniel Lemire, Otmar Ertl, cocowalla, leo-yuriev, Diego Barrios Romero, paulie-g, dumblob, Yann Collet, ivte-ms, hyb, James Z.M. Gao, easyaspi314 (Devin), TheOneric
  5. /* quick example:
  6. string s="fjsakfdsjkf";
  7. uint64_t hash=wyhash(s.c_str(), s.size(), 0, _wyp);
  8. */
  9. #ifndef wyhash_final_version_4
  10. #define wyhash_final_version_4
  11. #ifndef WYHASH_CONDOM
  12. //protections that produce different results:
  13. //1: normal valid behavior
  14. //2: extra protection against entropy loss (probability=2^-63), aka. "blind multiplication"
  15. #define WYHASH_CONDOM 1
  16. #endif
  17. #ifndef WYHASH_32BIT_MUM
  18. //0: normal version, slow on 32 bit systems
  19. //1: faster on 32 bit systems but produces different results, incompatible with wy2u0k function
  20. #define WYHASH_32BIT_MUM 0
  21. #endif
  22. //includes
  23. #include <stdint.h>
  24. #include <string.h>
  25. #if defined(_MSC_VER) && defined(_M_X64)
  26. #include <intrin.h>
  27. #pragma intrinsic(_umul128)
  28. #endif
  29. //likely and unlikely macros
  30. #if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__)
  31. #define _likely_(x) __builtin_expect(x,1)
  32. #define _unlikely_(x) __builtin_expect(x,0)
  33. #else
  34. #define _likely_(x) (x)
  35. #define _unlikely_(x) (x)
  36. #endif
  37. //128bit multiply function
  38. static inline uint64_t _wyrot(uint64_t x) { return (x>>32)|(x<<32); }
  39. static inline void _wymum(uint64_t *A, uint64_t *B){
  40. #if(WYHASH_32BIT_MUM)
  41. uint64_t hh=(*A>>32)*(*B>>32), hl=(*A>>32)*(uint32_t)*B, lh=(uint32_t)*A*(*B>>32), ll=(uint64_t)(uint32_t)*A*(uint32_t)*B;
  42. #if(WYHASH_CONDOM>1)
  43. *A^=_wyrot(hl)^hh; *B^=_wyrot(lh)^ll;
  44. #else
  45. *A=_wyrot(hl)^hh; *B=_wyrot(lh)^ll;
  46. #endif
  47. #elif defined(__SIZEOF_INT128__)
  48. __uint128_t r=*A; r*=*B;
  49. #if(WYHASH_CONDOM>1)
  50. *A^=(uint64_t)r; *B^=(uint64_t)(r>>64);
  51. #else
  52. *A=(uint64_t)r; *B=(uint64_t)(r>>64);
  53. #endif
  54. #elif defined(_MSC_VER) && defined(_M_X64)
  55. #if(WYHASH_CONDOM>1)
  56. uint64_t a, b;
  57. a=_umul128(*A,*B,&b);
  58. *A^=a; *B^=b;
  59. #else
  60. *A=_umul128(*A,*B,B);
  61. #endif
  62. #else
  63. uint64_t ha=*A>>32, hb=*B>>32, la=(uint32_t)*A, lb=(uint32_t)*B, hi, lo;
  64. uint64_t rh=ha*hb, rm0=ha*lb, rm1=hb*la, rl=la*lb, t=rl+(rm0<<32), c=t<rl;
  65. lo=t+(rm1<<32); c+=lo<t; hi=rh+(rm0>>32)+(rm1>>32)+c;
  66. #if(WYHASH_CONDOM>1)
  67. *A^=lo; *B^=hi;
  68. #else
  69. *A=lo; *B=hi;
  70. #endif
  71. #endif
  72. }
  73. //multiply and xor mix function, aka MUM
  74. static inline uint64_t _wymix(uint64_t A, uint64_t B){ _wymum(&A,&B); return A^B; }
  75. //endian macros
  76. #ifndef WYHASH_LITTLE_ENDIAN
  77. #if defined(_WIN32) || defined(__LITTLE_ENDIAN__) || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
  78. #define WYHASH_LITTLE_ENDIAN 1
  79. #elif defined(__BIG_ENDIAN__) || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
  80. #define WYHASH_LITTLE_ENDIAN 0
  81. #else
  82. #warning could not determine endianness! Falling back to little endian.
  83. #define WYHASH_LITTLE_ENDIAN 1
  84. #endif
  85. #endif
  86. //read functions
  87. #if (WYHASH_LITTLE_ENDIAN)
  88. static inline uint64_t _wyr8(const uint8_t *p) { uint64_t v; memcpy(&v, p, 8); return v;}
  89. static inline uint64_t _wyr4(const uint8_t *p) { uint32_t v; memcpy(&v, p, 4); return v;}
  90. #elif defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__)
  91. static inline uint64_t _wyr8(const uint8_t *p) { uint64_t v; memcpy(&v, p, 8); return __builtin_bswap64(v);}
  92. static inline uint64_t _wyr4(const uint8_t *p) { uint32_t v; memcpy(&v, p, 4); return __builtin_bswap32(v);}
  93. #elif defined(_MSC_VER)
  94. static inline uint64_t _wyr8(const uint8_t *p) { uint64_t v; memcpy(&v, p, 8); return _byteswap_uint64(v);}
  95. static inline uint64_t _wyr4(const uint8_t *p) { uint32_t v; memcpy(&v, p, 4); return _byteswap_ulong(v);}
  96. #else
  97. static inline uint64_t _wyr8(const uint8_t *p) {
  98. uint64_t v; memcpy(&v, p, 8);
  99. return (((v >> 56) & 0xff)| ((v >> 40) & 0xff00)| ((v >> 24) & 0xff0000)| ((v >> 8) & 0xff000000)| ((v << 8) & 0xff00000000)| ((v << 24) & 0xff0000000000)| ((v << 40) & 0xff000000000000)| ((v << 56) & 0xff00000000000000));
  100. }
  101. static inline uint64_t _wyr4(const uint8_t *p) {
  102. uint32_t v; memcpy(&v, p, 4);
  103. return (((v >> 24) & 0xff)| ((v >> 8) & 0xff00)| ((v << 8) & 0xff0000)| ((v << 24) & 0xff000000));
  104. }
  105. #endif
  106. static inline uint64_t _wyr3(const uint8_t *p, size_t k) { return (((uint64_t)p[0])<<16)|(((uint64_t)p[k>>1])<<8)|p[k-1];}
  107. //wyhash main function
  108. static inline uint64_t wyhash(const void *key, size_t len, uint64_t seed, const uint64_t *secret){
  109. const uint8_t *p=(const uint8_t *)key; seed^=_wymix(seed^secret[0],secret[1]); uint64_t a, b;
  110. if(_likely_(len<=16)){
  111. if(_likely_(len>=4)){ a=(_wyr4(p)<<32)|_wyr4(p+((len>>3)<<2)); b=(_wyr4(p+len-4)<<32)|_wyr4(p+len-4-((len>>3)<<2)); }
  112. else if(_likely_(len>0)){ a=_wyr3(p,len); b=0;}
  113. else a=b=0;
  114. }
  115. else{
  116. size_t i=len;
  117. if(_unlikely_(i>48)){
  118. uint64_t see1=seed, see2=seed;
  119. do{
  120. seed=_wymix(_wyr8(p)^secret[1],_wyr8(p+8)^seed);
  121. see1=_wymix(_wyr8(p+16)^secret[2],_wyr8(p+24)^see1);
  122. see2=_wymix(_wyr8(p+32)^secret[3],_wyr8(p+40)^see2);
  123. p+=48; i-=48;
  124. }while(_likely_(i>48));
  125. seed^=see1^see2;
  126. }
  127. while(_unlikely_(i>16)){ seed=_wymix(_wyr8(p)^secret[1],_wyr8(p+8)^seed); i-=16; p+=16; }
  128. a=_wyr8(p+i-16); b=_wyr8(p+i-8);
  129. }
  130. a^=secret[1]; b^=seed; _wymum(&a,&b);
  131. return _wymix(a^secret[0]^len,b^secret[1]);
  132. }
  133. //the default secret parameters
  134. static const uint64_t _wyp[4] = {0xa0761d6478bd642full, 0xe7037ed1a0b428dbull, 0x8ebc6af09c88c6e3ull, 0x589965cc75374cc3ull};
  135. //a useful 64bit-64bit mix function to produce deterministic pseudo random numbers that can pass BigCrush and PractRand
  136. static inline uint64_t wyhash64(uint64_t A, uint64_t B){ A^=0xa0761d6478bd642full; B^=0xe7037ed1a0b428dbull; _wymum(&A,&B); return _wymix(A^0xa0761d6478bd642full,B^0xe7037ed1a0b428dbull);}
  137. //The wyrand PRNG that pass BigCrush and PractRand
  138. static inline uint64_t wyrand(uint64_t *seed){ *seed+=0xa0761d6478bd642full; return _wymix(*seed,*seed^0xe7037ed1a0b428dbull);}
  139. //convert any 64 bit pseudo random numbers to uniform distribution [0,1). It can be combined with wyrand, wyhash64 or wyhash.
  140. static inline double wy2u01(uint64_t r){ const double _wynorm=1.0/(1ull<<52); return (r>>12)*_wynorm;}
  141. //convert any 64 bit pseudo random numbers to APPROXIMATE Gaussian distribution. It can be combined with wyrand, wyhash64 or wyhash.
  142. static inline double wy2gau(uint64_t r){ const double _wynorm=1.0/(1ull<<20); return ((r&0x1fffff)+((r>>21)&0x1fffff)+((r>>42)&0x1fffff))*_wynorm-3.0;}
  143. #ifdef WYTRNG
  144. #include <sys/time.h>
  145. //The wytrand true random number generator, passed BigCrush.
  146. static inline uint64_t wytrand(uint64_t *seed){
  147. struct timeval t; gettimeofday(&t,0);
  148. uint64_t teed=(((uint64_t)t.tv_sec)<<32)|t.tv_usec;
  149. teed=_wymix(teed^_wyp[0],*seed^_wyp[1]);
  150. *seed=_wymix(teed^_wyp[0],_wyp[2]);
  151. return _wymix(*seed,*seed^_wyp[3]);
  152. }
  153. #endif
  154. #if(!WYHASH_32BIT_MUM)
  155. //fast range integer random number generation on [0,k) credit to Daniel Lemire. May not work when WYHASH_32BIT_MUM=1. It can be combined with wyrand, wyhash64 or wyhash.
  156. static inline uint64_t wy2u0k(uint64_t r, uint64_t k){ _wymum(&r,&k); return k; }
  157. #endif
  158. //make your own secret
  159. static inline void make_secret(uint64_t seed, uint64_t *secret){
  160. uint8_t c[] = {15, 23, 27, 29, 30, 39, 43, 45, 46, 51, 53, 54, 57, 58, 60, 71, 75, 77, 78, 83, 85, 86, 89, 90, 92, 99, 101, 102, 105, 106, 108, 113, 114, 116, 120, 135, 139, 141, 142, 147, 149, 150, 153, 154, 156, 163, 165, 166, 169, 170, 172, 177, 178, 180, 184, 195, 197, 198, 201, 202, 204, 209, 210, 212, 216, 225, 226, 228, 232, 240 };
  161. for(size_t i=0;i<4;i++){
  162. uint8_t ok;
  163. do{
  164. ok=1; secret[i]=0;
  165. for(size_t j=0;j<64;j+=8) secret[i]|=((uint64_t)c[wyrand(&seed)%sizeof(c)])<<j;
  166. if(secret[i]%2==0){ ok=0; continue; }
  167. for(size_t j=0;j<i;j++) {
  168. #if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__)
  169. if(__builtin_popcountll(secret[j]^secret[i])!=32){ ok=0; break; }
  170. #elif defined(_MSC_VER) && defined(_M_X64)
  171. if(_mm_popcnt_u64(secret[j]^secret[i])!=32){ ok=0; break; }
  172. #else
  173. //manual popcount
  174. uint64_t x = secret[j]^secret[i];
  175. x -= (x >> 1) & 0x5555555555555555;
  176. x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333);
  177. x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f;
  178. x = (x * 0x0101010101010101) >> 56;
  179. if(x!=32){ ok=0; break; }
  180. #endif
  181. }
  182. }while(!ok);
  183. }
  184. }
  185. #endif
  186. /* The Unlicense
  187. This is free and unencumbered software released into the public domain.
  188. Anyone is free to copy, modify, publish, use, compile, sell, or
  189. distribute this software, either in source code form or as a compiled
  190. binary, for any purpose, commercial or non-commercial, and by any
  191. means.
  192. In jurisdictions that recognize copyright laws, the author or authors
  193. of this software dedicate any and all copyright interest in the
  194. software to the public domain. We make this dedication for the benefit
  195. of the public at large and to the detriment of our heirs and
  196. successors. We intend this dedication to be an overt act of
  197. relinquishment in perpetuity of all present and future rights to this
  198. software under copyright law.
  199. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  200. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  201. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  202. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  203. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  204. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  205. OTHER DEALINGS IN THE SOFTWARE.
  206. For more information, please refer to <http://unlicense.org/>
  207. */