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.

141 lines
3.8 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. #ifndef GENERICHASH_H
  8. #define GENERICHASH_H
  9. #if defined(_WIN32)
  10. #pragma once
  11. #endif
  12. //-----------------------------------------------------------------------------
  13. unsigned FASTCALL HashString( const char *pszKey );
  14. unsigned FASTCALL HashStringCaseless( const char *pszKey );
  15. unsigned FASTCALL HashStringCaselessConventional( const char *pszKey );
  16. unsigned FASTCALL Hash4( const void *pKey );
  17. unsigned FASTCALL Hash8( const void *pKey );
  18. unsigned FASTCALL Hash12( const void *pKey );
  19. unsigned FASTCALL Hash16( const void *pKey );
  20. unsigned FASTCALL HashBlock( const void *pKey, unsigned size );
  21. unsigned FASTCALL HashInt( const int key );
  22. // hash a uint32 into a uint32
  23. FORCEINLINE uint32 HashIntAlternate( uint32 n)
  24. {
  25. n = ( n + 0x7ed55d16 ) + ( n << 12 );
  26. n = ( n ^ 0xc761c23c ) ^ ( n >> 19 );
  27. n = ( n + 0x165667b1 ) + ( n << 5 );
  28. n = ( n + 0xd3a2646c ) ^ ( n << 9 );
  29. n = ( n + 0xfd7046c5 ) + ( n << 3 );
  30. n = ( n ^ 0xb55a4f09 ) ^ ( n >> 16 );
  31. return n;
  32. }
  33. inline uint64 HashUint64( uint64 s )
  34. {
  35. // Thomas Wang hash, http://www.concentric.net/~ttwang/tech/inthash.htm
  36. s = ( ~s ) + ( s << 21 ); // s = (s << 21) - s - 1;
  37. s = s ^ ( s >> 24 );
  38. s = ( s + ( s << 3 ) ) + ( s << 8 ); // s * 265
  39. s = s ^ ( s >> 14 );
  40. s = ( s + ( s << 2 ) ) + ( s << 4 ); // s * 21
  41. s = s ^ ( s >> 28 );
  42. s = s + ( s << 31 );
  43. return s;
  44. }
  45. inline intp HashIntp( intp s )
  46. {
  47. #ifdef PLATFORM_64BITS
  48. COMPILE_TIME_ASSERT( sizeof( s ) == sizeof( uint64 ) );
  49. return ( intp )HashUint64( ( uint64 )s );
  50. #else
  51. return HashInt( s );
  52. #endif
  53. }
  54. inline unsigned HashIntConventional( const int n ) // faster but less effective
  55. {
  56. // first byte
  57. unsigned hash = 0xAAAAAAAA + (n & 0xFF);
  58. // second byte
  59. hash = ( hash << 5 ) + hash + ( (n >> 8) & 0xFF );
  60. // third byte
  61. hash = ( hash << 5 ) + hash + ( (n >> 16) & 0xFF );
  62. // fourth byte
  63. hash = ( hash << 5 ) + hash + ( (n >> 24) & 0xFF );
  64. return hash;
  65. /* this is the old version, which would cause a load-hit-store on every
  66. line on a PowerPC, and therefore took hundreds of clocks to execute!
  67. byte *p = (byte *)&n;
  68. unsigned hash = 0xAAAAAAAA + *p++;
  69. hash = ( ( hash << 5 ) + hash ) + *p++;
  70. hash = ( ( hash << 5 ) + hash ) + *p++;
  71. return ( ( hash << 5 ) + hash ) + *p;
  72. */
  73. }
  74. //-----------------------------------------------------------------------------
  75. template <typename T>
  76. inline unsigned HashItem( const T &item )
  77. {
  78. // TODO: Confirm comiler optimizes out unused paths
  79. if ( sizeof(item) == 4 )
  80. return Hash4( &item );
  81. else if ( sizeof(item) == 8 )
  82. return Hash8( &item );
  83. else if ( sizeof(item) == 12 )
  84. return Hash12( &item );
  85. else if ( sizeof(item) == 16 )
  86. return Hash16( &item );
  87. else
  88. return HashBlock( &item, sizeof(item) );
  89. }
  90. template <> inline unsigned HashItem<int>(const int &key )
  91. {
  92. return HashInt( key );
  93. }
  94. template <> inline unsigned HashItem<unsigned>(const unsigned &key )
  95. {
  96. return HashInt( (int)key );
  97. }
  98. template<> inline unsigned HashItem<const char *>(const char * const &pszKey )
  99. {
  100. return HashString( pszKey );
  101. }
  102. template<> inline unsigned HashItem<char *>(char * const &pszKey )
  103. {
  104. return HashString( pszKey );
  105. }
  106. //-----------------------------------------------------------------------------
  107. //-----------------------------------------------------------------------------
  108. // Murmur hash
  109. //-----------------------------------------------------------------------------
  110. uint32 MurmurHash2( const void * key, int len, uint32 seed );
  111. // return murmurhash2 of a downcased string
  112. uint32 MurmurHash2LowerCase( char const *pString, uint32 nSeed );
  113. uint64 MurmurHash64( const void * key, int len, uint32 seed );
  114. #endif /* !GENERICHASH_H */