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.

116 lines
3.3 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 unsigned HashIntConventional( const int n ) // faster but less effective
  34. {
  35. // first byte
  36. unsigned hash = 0xAAAAAAAA + (n & 0xFF);
  37. // second byte
  38. hash = ( hash << 5 ) + hash + ( (n >> 8) & 0xFF );
  39. // third byte
  40. hash = ( hash << 5 ) + hash + ( (n >> 16) & 0xFF );
  41. // fourth byte
  42. hash = ( hash << 5 ) + hash + ( (n >> 24) & 0xFF );
  43. return hash;
  44. /* this is the old version, which would cause a load-hit-store on every
  45. line on a PowerPC, and therefore took hundreds of clocks to execute!
  46. byte *p = (byte *)&n;
  47. unsigned hash = 0xAAAAAAAA + *p++;
  48. hash = ( ( hash << 5 ) + hash ) + *p++;
  49. hash = ( ( hash << 5 ) + hash ) + *p++;
  50. return ( ( hash << 5 ) + hash ) + *p;
  51. */
  52. }
  53. //-----------------------------------------------------------------------------
  54. template <typename T>
  55. inline unsigned HashItem( const T &item )
  56. {
  57. // TODO: Confirm comiler optimizes out unused paths
  58. if ( sizeof(item) == 4 )
  59. return Hash4( &item );
  60. else if ( sizeof(item) == 8 )
  61. return Hash8( &item );
  62. else if ( sizeof(item) == 12 )
  63. return Hash12( &item );
  64. else if ( sizeof(item) == 16 )
  65. return Hash16( &item );
  66. else
  67. return HashBlock( &item, sizeof(item) );
  68. }
  69. template <> inline unsigned HashItem<int>(const int &key )
  70. {
  71. return HashInt( key );
  72. }
  73. template <> inline unsigned HashItem<unsigned>(const unsigned &key )
  74. {
  75. return HashInt( (int)key );
  76. }
  77. template<> inline unsigned HashItem<const char *>(const char * const &pszKey )
  78. {
  79. return HashString( pszKey );
  80. }
  81. template<> inline unsigned HashItem<char *>(char * const &pszKey )
  82. {
  83. return HashString( pszKey );
  84. }
  85. //-----------------------------------------------------------------------------
  86. //-----------------------------------------------------------------------------
  87. // Murmur hash
  88. //-----------------------------------------------------------------------------
  89. uint32 MurmurHash2( const void * key, int len, uint32 seed );
  90. // return murmurhash2 of a downcased string
  91. uint32 MurmurHash2LowerCase( char const *pString, uint32 nSeed );
  92. uint64 MurmurHash64( const void * key, int len, uint32 seed );
  93. #endif /* !GENERICHASH_H */