Team Fortress 2 Source Code as on 22/4/2020
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.

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