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.

140 lines
6.1 KiB

  1. //Author: Wang Yi <godspeed_china@yeah.net>
  2. #ifndef wyhash_final_version
  3. #define wyhash_final_version
  4. //defines that change behavior
  5. #ifndef WYHASH_CONDOM
  6. #define WYHASH_CONDOM 1 //0: read 8 bytes before and after boudaries, dangerous but fastest. 1: normal valid behavior 2: extra protection against entropy loss (probability=2^-63), aka. "blind multiplication"
  7. #endif
  8. #define WYHASH_32BIT_MUM 0 //faster on 32 bit system
  9. //includes
  10. #include <stdint.h>
  11. #include <string.h>
  12. #if defined(_MSC_VER) && defined(_M_X64)
  13. #include <intrin.h>
  14. #pragma intrinsic(_umul128)
  15. #endif
  16. #if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__)
  17. #define _likely_(x) __builtin_expect(x,1)
  18. #define _unlikely_(x) __builtin_expect(x,0)
  19. #else
  20. #define _likely_(x) (x)
  21. #define _unlikely_(x) (x)
  22. #endif
  23. //mum function
  24. static inline uint64_t _wyrot(uint64_t x) { return (x>>32)|(x<<32); }
  25. static inline void _wymum(uint64_t *A, uint64_t *B){
  26. #if(WYHASH_32BIT_MUM)
  27. uint64_t hh=(*A>>32)*(*B>>32), hl=(*A>>32)*(unsigned)*B, lh=(unsigned)*A*(*B>>32), ll=(uint64_t)(unsigned)*A*(unsigned)*B;
  28. #if(WYHASH_CONDOM>1)
  29. *A^=_wyrot(hl)^hh; *B^=_wyrot(lh)^ll;
  30. #else
  31. *A=_wyrot(hl)^hh; *B=_wyrot(lh)^ll;
  32. #endif
  33. #elif defined(__SIZEOF_INT128__)
  34. __uint128_t r=*A; r*=*B;
  35. #if(WYHASH_CONDOM>1)
  36. *A^=(uint64_t)r; *B^=(uint64_t)(r>>64);
  37. #else
  38. *A=(uint64_t)r; *B=(uint64_t)(r>>64);
  39. #endif
  40. #elif defined(_MSC_VER) && defined(_M_X64)
  41. #if(WYHASH_CONDOM>1)
  42. uint64_t a, b;
  43. a=_umul128(*A,*B,&b);
  44. *A^=a; *B^=b;
  45. #else
  46. *A=_umul128(*A,*B,B);
  47. #endif
  48. #else
  49. uint64_t ha=*A>>32, hb=*B>>32, la=(uint32_t)*A, lb=(uint32_t)*B, hi, lo;
  50. uint64_t rh=ha*hb, rm0=ha*lb, rm1=hb*la, rl=la*lb, t=rl+(rm0<<32), c=t<rl;
  51. lo=t+(rm1<<32); c+=lo<t; hi=rh+(rm0>>32)+(rm1>>32)+c;
  52. #if(WYHASH_CONDOM>1)
  53. *A^=lo; *B^=hi;
  54. #else
  55. *A=lo; *B=hi;
  56. #endif
  57. #endif
  58. }
  59. static inline uint64_t _wymix(uint64_t A, uint64_t B){ _wymum(&A,&B); return A^B; }
  60. //read functions
  61. #ifndef WYHASH_LITTLE_ENDIAN
  62. #if defined(_WIN32) || defined(__LITTLE_ENDIAN__) || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
  63. #define WYHASH_LITTLE_ENDIAN 1
  64. #elif defined(__BIG_ENDIAN__) || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
  65. #define WYHASH_LITTLE_ENDIAN 0
  66. #endif
  67. #endif
  68. #if (WYHASH_LITTLE_ENDIAN)
  69. static inline uint64_t _wyr8(const uint8_t *p) { uint64_t v; memcpy(&v, p, 8); return v;}
  70. static inline uint64_t _wyr4(const uint8_t *p) { unsigned v; memcpy(&v, p, 4); return v;}
  71. #elif defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__)
  72. static inline uint64_t _wyr8(const uint8_t *p) { uint64_t v; memcpy(&v, p, 8); return __builtin_bswap64(v);}
  73. static inline uint64_t _wyr4(const uint8_t *p) { unsigned v; memcpy(&v, p, 4); return __builtin_bswap32(v);}
  74. #elif defined(_MSC_VER)
  75. static inline uint64_t _wyr8(const uint8_t *p) { uint64_t v; memcpy(&v, p, 8); return _byteswap_uint64(v);}
  76. static inline uint64_t _wyr4(const uint8_t *p) { unsigned v; memcpy(&v, p, 4); return _byteswap_ulong(v);}
  77. #endif
  78. static inline uint64_t _wyr3(const uint8_t *p, unsigned k) { return (((uint64_t)p[0])<<16)|(((uint64_t)p[k>>1])<<8)|p[k-1];}
  79. //wyhash function
  80. static inline uint64_t _wyfinish16(const uint8_t *p, uint64_t len, uint64_t seed, const uint64_t *secret, uint64_t i){
  81. #if(WYHASH_CONDOM>0)
  82. uint64_t a, b;
  83. if(_likely_(i<=8)){
  84. if(_likely_(i>=4)){ a=_wyr4(p); b=_wyr4(p+i-4); }
  85. else if (_likely_(i)){ a=_wyr3(p,i); b=0; }
  86. else a=b=0;
  87. }
  88. else{ a=_wyr8(p); b=_wyr8(p+i-8); }
  89. return _wymix(secret[1]^len,_wymix(a^secret[1], b^seed));
  90. #else
  91. #define oneshot_shift ((i<8)*((8-i)<<3))
  92. return _wymix(secret[1]^len,_wymix((_wyr8(p)<<oneshot_shift)^secret[1],(_wyr8(p+i-8)>>oneshot_shift)^seed));
  93. #endif
  94. }
  95. static inline uint64_t _wyfinish(const uint8_t *p, uint64_t len, uint64_t seed, const uint64_t *secret, uint64_t i){
  96. if(_likely_(i<=16)) return _wyfinish16(p,len,seed,secret,i);
  97. return _wyfinish(p+16,len,_wymix(_wyr8(p)^secret[1],_wyr8(p+8)^seed),secret,i-16);
  98. }
  99. static inline uint64_t wyhash(const void *key, uint64_t len, uint64_t seed, const uint64_t *secret){
  100. const uint8_t *p=(const uint8_t *)key;
  101. uint64_t i=len; seed^=*secret;
  102. if(_unlikely_(i>64)){
  103. uint64_t see1=seed;
  104. do{
  105. seed=_wymix(_wyr8(p)^secret[1],_wyr8(p+8)^seed)^_wymix(_wyr8(p+16)^secret[2],_wyr8(p+24)^seed);
  106. see1=_wymix(_wyr8(p+32)^secret[3],_wyr8(p+40)^see1)^_wymix(_wyr8(p+48)^secret[4],_wyr8(p+56)^see1);
  107. p+=64; i-=64;
  108. }while(i>64);
  109. seed^=see1;
  110. }
  111. return _wyfinish(p,len,seed,secret,i);
  112. }
  113. //utility functions
  114. const uint64_t _wyp[5] = {0xa0761d6478bd642full, 0xe7037ed1a0b428dbull, 0x8ebc6af09c88c6e3ull, 0x589965cc75374cc3ull, 0x1d8e4e27c47d124full};
  115. static inline uint64_t wyhash64(uint64_t A, uint64_t B){ A^=_wyp[0]; B^=_wyp[1]; _wymum(&A,&B); return _wymix(A^_wyp[0],B^_wyp[1]);}
  116. static inline uint64_t wyrand(uint64_t *seed){ *seed+=_wyp[0]; return _wymix(*seed,*seed^_wyp[1]);}
  117. static inline double wy2u01(uint64_t r){ const double _wynorm=1.0/(1ull<<52); return (r>>12)*_wynorm;}
  118. 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;}
  119. static inline void make_secret(uint64_t seed, uint64_t *secret){
  120. 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 };
  121. for(size_t i=0;i<5;i++){
  122. uint8_t ok;
  123. do{
  124. ok=1; secret[i]=0;
  125. for(size_t j=0;j<64;j+=8) secret[i]|=((uint64_t)c[wyrand(&seed)%sizeof(c)])<<j;
  126. if(secret[i]%2==0){ ok=0; continue; }
  127. for(size_t j=0;j<i;j++)
  128. #if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__)
  129. if(__builtin_popcountll(secret[j]^secret[i])!=32){ ok=0; break; }
  130. #elif defined(_MSC_VER) && defined(_M_X64)
  131. if(_mm_popcnt_u64(secret[j]^secret[i])!=32){ ok=0; break; }
  132. #endif
  133. if(!ok)continue;
  134. for(uint64_t j=3;j<0x100000000ull;j+=2) if(secret[i]%j==0){ ok=0; break; }
  135. }while(!ok);
  136. }
  137. }
  138. #endif