Counter Strike : Global Offensive Source Code
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.

431 lines
12 KiB

  1. //======= Copyright � 2005, , Valve Corporation, All rights reserved. =========
  2. //
  3. // Purpose: Variant Pearson Hash general purpose hashing algorithm described
  4. // by Cargill in C++ Report 1994. Generates a 16-bit result.
  5. //
  6. //=============================================================================
  7. #include <stdlib.h>
  8. #include "tier0/basetypes.h"
  9. #include "tier0/platform.h"
  10. #include "generichash.h"
  11. #include <ctype.h>
  12. #include "tier0/dbg.h"
  13. #include "tier1/strtools.h"
  14. // NOTE: This has to be the last file included!
  15. #include "tier0/memdbgon.h"
  16. //-----------------------------------------------------------------------------
  17. //
  18. // Table of randomly shuffled values from 0-255 generated by:
  19. //
  20. //-----------------------------------------------------------------------------
  21. /*
  22. void MakeRandomValues()
  23. {
  24. int i, j, r;
  25. unsigned t;
  26. srand( 0xdeadbeef );
  27. for ( i = 0; i < 256; i++ )
  28. {
  29. g_nRandomValues[i] = (unsigned )i;
  30. }
  31. for (j = 0; j < 8; j++)
  32. {
  33. for (i = 0; i < 256; i++)
  34. {
  35. r = rand() & 0xff;
  36. t = g_nRandomValues[i];
  37. g_nRandomValues[i] = g_nRandomValues[r];
  38. g_nRandomValues[r] = t;
  39. }
  40. }
  41. printf("static unsigned g_nRandomValues[256] =\n{\n");
  42. for (i = 0; i < 256; i += 16)
  43. {
  44. printf("\t");
  45. for (j = 0; j < 16; j++)
  46. printf(" %3d,", g_nRandomValues[i+j]);
  47. printf("\n");
  48. }
  49. printf("};\n");
  50. }
  51. */
  52. static unsigned g_nRandomValues[256] =
  53. {
  54. 238, 164, 191, 168, 115, 16, 142, 11, 213, 214, 57, 151, 248, 252, 26, 198,
  55. 13, 105, 102, 25, 43, 42, 227, 107, 210, 251, 86, 66, 83, 193, 126, 108,
  56. 131, 3, 64, 186, 192, 81, 37, 158, 39, 244, 14, 254, 75, 30, 2, 88,
  57. 172, 176, 255, 69, 0, 45, 116, 139, 23, 65, 183, 148, 33, 46, 203, 20,
  58. 143, 205, 60, 197, 118, 9, 171, 51, 233, 135, 220, 49, 71, 184, 82, 109,
  59. 36, 161, 169, 150, 63, 96, 173, 125, 113, 67, 224, 78, 232, 215, 35, 219,
  60. 79, 181, 41, 229, 149, 153, 111, 217, 21, 72, 120, 163, 133, 40, 122, 140,
  61. 208, 231, 211, 200, 160, 182, 104, 110, 178, 237, 15, 101, 27, 50, 24, 189,
  62. 177, 130, 187, 92, 253, 136, 100, 212, 19, 174, 70, 22, 170, 206, 162, 74,
  63. 247, 5, 47, 32, 179, 117, 132, 195, 124, 123, 245, 128, 236, 223, 12, 84,
  64. 54, 218, 146, 228, 157, 94, 106, 31, 17, 29, 194, 34, 56, 134, 239, 246,
  65. 241, 216, 127, 98, 7, 204, 154, 152, 209, 188, 48, 61, 87, 97, 225, 85,
  66. 90, 167, 155, 112, 145, 114, 141, 93, 250, 4, 201, 156, 38, 89, 226, 196,
  67. 1, 235, 44, 180, 159, 121, 119, 166, 190, 144, 10, 91, 76, 230, 221, 80,
  68. 207, 55, 58, 53, 175, 8, 6, 52, 68, 242, 18, 222, 103, 249, 147, 129,
  69. 138, 243, 28, 185, 62, 59, 240, 202, 234, 99, 77, 73, 199, 137, 95, 165,
  70. };
  71. //-----------------------------------------------------------------------------
  72. // String
  73. //-----------------------------------------------------------------------------
  74. unsigned FASTCALL HashString( const char *pszKey )
  75. {
  76. const uint8 *k = (const uint8 *)pszKey;
  77. unsigned even = 0,
  78. odd = 0,
  79. n;
  80. while ((n = *k++) != 0)
  81. {
  82. even = g_nRandomValues[odd ^ n];
  83. if ((n = *k++) != 0)
  84. odd = g_nRandomValues[even ^ n];
  85. else
  86. break;
  87. }
  88. return (even << 8) | odd ;
  89. }
  90. //-----------------------------------------------------------------------------
  91. // Case-insensitive string
  92. //-----------------------------------------------------------------------------
  93. unsigned FASTCALL HashStringCaseless( const char *pszKey )
  94. {
  95. const uint8 *k = (const uint8 *) pszKey;
  96. unsigned even = 0,
  97. odd = 0,
  98. n;
  99. while ((n = toupper(*k++)) != 0)
  100. {
  101. even = g_nRandomValues[odd ^ n];
  102. if ((n = toupper(*k++)) != 0)
  103. odd = g_nRandomValues[even ^ n];
  104. else
  105. break;
  106. }
  107. return (even << 8) | odd;
  108. }
  109. //-----------------------------------------------------------------------------
  110. // 32 bit conventional case-insensitive string
  111. //-----------------------------------------------------------------------------
  112. uint32 FASTCALL HashStringCaselessConventional( const char *pszKey )
  113. {
  114. uint32 hash = 0xAAAAAAAA; // Alternating 1's and 0's to maximize the effect of the later multiply and add
  115. hash += ( 2 * V_strlen( pszKey ) ); // Add the string length * 2 to the hash to give it more variety
  116. for( ; *pszKey ; pszKey++ )
  117. {
  118. hash = ( ( hash << 5 ) + hash ) + (uint8)tolower(*pszKey);
  119. }
  120. return hash;
  121. }
  122. //-----------------------------------------------------------------------------
  123. // int hash
  124. //-----------------------------------------------------------------------------
  125. unsigned FASTCALL HashInt( const int n )
  126. {
  127. register unsigned even, odd;
  128. odd = g_nRandomValues[(((unsigned)n >> 8) & 0xff)];
  129. even = g_nRandomValues[odd ^ ((unsigned)n >> 24)];
  130. odd = g_nRandomValues[even ^ ((unsigned)n >> 16) & 0xff];
  131. even = g_nRandomValues[odd ^ ((unsigned)n >> 8) & 0xff];
  132. odd = g_nRandomValues[even ^ ((unsigned)n & 0xff)];
  133. return (even << 8) | odd;
  134. }
  135. //-----------------------------------------------------------------------------
  136. // 4-byte hash
  137. //-----------------------------------------------------------------------------
  138. unsigned FASTCALL Hash4( const void *pKey )
  139. {
  140. register const uint32 * p = (const uint32 *) pKey;
  141. register unsigned even,
  142. odd,
  143. n;
  144. n = *p;
  145. odd = g_nRandomValues[((n >> 8) & 0xff)];
  146. even = g_nRandomValues[odd ^ (n >> 24)];
  147. odd = g_nRandomValues[even ^ (n >> 16) & 0xff];
  148. even = g_nRandomValues[odd ^ (n >> 8) & 0xff];
  149. odd = g_nRandomValues[even ^ (n & 0xff)];
  150. return (even << 8) | odd;
  151. }
  152. //-----------------------------------------------------------------------------
  153. // 8-byte hash
  154. //-----------------------------------------------------------------------------
  155. unsigned FASTCALL Hash8( const void *pKey )
  156. {
  157. register const uint32 * p = (const uint32 *) pKey;
  158. register unsigned even,
  159. odd,
  160. n;
  161. n = *p;
  162. odd = g_nRandomValues[((n >> 8) & 0xff)];
  163. even = g_nRandomValues[odd ^ (n >> 24)];
  164. odd = g_nRandomValues[even ^ (n >> 16) & 0xff];
  165. even = g_nRandomValues[odd ^ (n >> 8) & 0xff];
  166. odd = g_nRandomValues[even ^ (n & 0xff)];
  167. n = *(p+1);
  168. even = g_nRandomValues[odd ^ (n >> 24)];
  169. odd = g_nRandomValues[even ^ (n >> 16) & 0xff];
  170. even = g_nRandomValues[odd ^ (n >> 8) & 0xff];
  171. odd = g_nRandomValues[even ^ (n & 0xff)];
  172. return (even << 8) | odd;
  173. }
  174. //-----------------------------------------------------------------------------
  175. // 12-byte hash
  176. //-----------------------------------------------------------------------------
  177. unsigned FASTCALL Hash12( const void *pKey )
  178. {
  179. register const uint32 * p = (const uint32 *) pKey;
  180. register unsigned even,
  181. odd,
  182. n;
  183. n = *p;
  184. odd = g_nRandomValues[((n >> 8) & 0xff)];
  185. even = g_nRandomValues[odd ^ (n >> 24)];
  186. odd = g_nRandomValues[even ^ (n >> 16) & 0xff];
  187. even = g_nRandomValues[odd ^ (n >> 8) & 0xff];
  188. odd = g_nRandomValues[even ^ (n & 0xff)];
  189. n = *(p+1);
  190. even = g_nRandomValues[odd ^ (n >> 24)];
  191. odd = g_nRandomValues[even ^ (n >> 16) & 0xff];
  192. even = g_nRandomValues[odd ^ (n >> 8) & 0xff];
  193. odd = g_nRandomValues[even ^ (n & 0xff)];
  194. n = *(p+2);
  195. even = g_nRandomValues[odd ^ (n >> 24)];
  196. odd = g_nRandomValues[even ^ (n >> 16) & 0xff];
  197. even = g_nRandomValues[odd ^ (n >> 8) & 0xff];
  198. odd = g_nRandomValues[even ^ (n & 0xff)];
  199. return (even << 8) | odd;
  200. }
  201. //-----------------------------------------------------------------------------
  202. // 16-byte hash
  203. //-----------------------------------------------------------------------------
  204. unsigned FASTCALL Hash16( const void *pKey )
  205. {
  206. register const uint32 * p = (const uint32 *) pKey;
  207. register unsigned even,
  208. odd,
  209. n;
  210. n = *p;
  211. odd = g_nRandomValues[((n >> 8) & 0xff)];
  212. even = g_nRandomValues[odd ^ (n >> 24)];
  213. odd = g_nRandomValues[even ^ (n >> 16) & 0xff];
  214. even = g_nRandomValues[odd ^ (n >> 8) & 0xff];
  215. odd = g_nRandomValues[even ^ (n & 0xff)];
  216. n = *(p+1);
  217. even = g_nRandomValues[odd ^ (n >> 24)];
  218. odd = g_nRandomValues[even ^ (n >> 16) & 0xff];
  219. even = g_nRandomValues[odd ^ (n >> 8) & 0xff];
  220. odd = g_nRandomValues[even ^ (n & 0xff)];
  221. n = *(p+2);
  222. even = g_nRandomValues[odd ^ (n >> 24)];
  223. odd = g_nRandomValues[even ^ (n >> 16) & 0xff];
  224. even = g_nRandomValues[odd ^ (n >> 8) & 0xff];
  225. odd = g_nRandomValues[even ^ (n & 0xff)];
  226. n = *(p+3);
  227. even = g_nRandomValues[odd ^ (n >> 24)];
  228. odd = g_nRandomValues[even ^ (n >> 16) & 0xff];
  229. even = g_nRandomValues[odd ^ (n >> 8) & 0xff];
  230. odd = g_nRandomValues[even ^ (n & 0xff)];
  231. return (even << 8) | odd;
  232. }
  233. //-----------------------------------------------------------------------------
  234. // Arbitrary fixed length hash
  235. //-----------------------------------------------------------------------------
  236. unsigned FASTCALL HashBlock( const void *pKey, unsigned size )
  237. {
  238. const uint8 * k = (const uint8 *) pKey;
  239. unsigned even = 0,
  240. odd = 0,
  241. n;
  242. while (size)
  243. {
  244. --size;
  245. n = *k++;
  246. even = g_nRandomValues[odd ^ n];
  247. if (size)
  248. {
  249. --size;
  250. n = *k++;
  251. odd = g_nRandomValues[even ^ n];
  252. }
  253. else
  254. break;
  255. }
  256. return (even << 8) | odd;
  257. }
  258. //-----------------------------------------------------------------------------
  259. // Murmur hash
  260. //-----------------------------------------------------------------------------
  261. uint32 MurmurHash2( const void * key, int len, uint32 seed )
  262. {
  263. // 'm' and 'r' are mixing constants generated offline.
  264. // They're not really 'magic', they just happen to work well.
  265. const uint32 m = 0x5bd1e995;
  266. const int r = 24;
  267. // Initialize the hash to a 'random' value
  268. uint32 h = seed ^ len;
  269. // Mix 4 bytes at a time into the hash
  270. const unsigned char * data = (const unsigned char *)key;
  271. while(len >= 4)
  272. {
  273. uint32 k = LittleDWord( *(uint32 *)data );
  274. k *= m;
  275. k ^= k >> r;
  276. k *= m;
  277. h *= m;
  278. h ^= k;
  279. data += 4;
  280. len -= 4;
  281. }
  282. // Handle the last few bytes of the input array
  283. switch(len)
  284. {
  285. case 3: h ^= data[2] << 16;
  286. case 2: h ^= data[1] << 8;
  287. case 1: h ^= data[0];
  288. h *= m;
  289. };
  290. // Do a few final mixes of the hash to ensure the last few
  291. // bytes are well-incorporated.
  292. h ^= h >> 13;
  293. h *= m;
  294. h ^= h >> 15;
  295. return h;
  296. }
  297. #define TOLOWERU( c ) ( ( uint32 ) ( ( ( c >= 'A' ) && ( c <= 'Z' ) )? c + 32 : c ) )
  298. uint32 MurmurHash2LowerCase( char const *pString, uint32 nSeed )
  299. {
  300. int nLen = V_strlen( pString );
  301. char *p = ( char * ) stackalloc( nLen + 1 );
  302. for( int i = 0; i < nLen ; i++ )
  303. {
  304. p[i] = TOLOWERU( pString[i] );
  305. }
  306. return MurmurHash2( p, nLen, nSeed );
  307. }
  308. //-----------------------------------------------------------------------------
  309. // Murmur hash, 64 bit- endian neutral
  310. //-----------------------------------------------------------------------------
  311. uint64 MurmurHash64( const void * key, int len, uint32 seed )
  312. {
  313. // 'm' and 'r' are mixing constants generated offline.
  314. // They're not really 'magic', they just happen to work well.
  315. const uint32 m = 0x5bd1e995;
  316. const int r = 24;
  317. // Initialize the hash to a 'random' value
  318. uint32 h1 = seed ^ len;
  319. uint32 h2 = 0;
  320. // Mix 4 bytes at a time into the hash
  321. const uint32 * data = (const uint32 *)key;
  322. while ( len >= 8 )
  323. {
  324. uint32 k1 = LittleDWord( *data++ );
  325. k1 *= m; k1 ^= k1 >> r; k1 *= m;
  326. h1 *= m; h1 ^= k1;
  327. len -= 4;
  328. uint32 k2 = LittleDWord( *data++ );
  329. k2 *= m; k2 ^= k2 >> r; k2 *= m;
  330. h2 *= m; h2 ^= k2;
  331. len -= 4;
  332. }
  333. if(len >= 4)
  334. {
  335. uint32 k1 = LittleDWord( *data++ );
  336. k1 *= m; k1 ^= k1 >> r; k1 *= m;
  337. h1 *= m; h1 ^= k1;
  338. len -= 4;
  339. }
  340. // Handle the last few bytes of the input array
  341. switch(len)
  342. {
  343. case 3: h2 ^= ((uint8*)data)[2] << 16;
  344. case 2: h2 ^= ((uint8*)data)[1] << 8;
  345. case 1: h2 ^= ((uint8*)data)[0];
  346. h2 *= m;
  347. };
  348. h1 ^= h2 >> 18; h1 *= m;
  349. h2 ^= h1 >> 22; h2 *= m;
  350. h1 ^= h2 >> 17; h1 *= m;
  351. h2 ^= h1 >> 19; h2 *= m;
  352. uint64 h = h1;
  353. h = (h << 32) | h2;
  354. return h;
  355. }