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.

74 lines
1.6 KiB

  1. //======= Copyright � Valve Corporation, All rights reserved. =================
  2. //
  3. // Public domain MurmurHash3 by Austin Appleby is a very solid general-purpose
  4. // hash with a 32-bit output. References:
  5. // http://code.google.com/p/smhasher/ (home of MurmurHash3)
  6. // https://sites.google.com/site/murmurhash/avalanche
  7. // http://www.strchr.com/hash_functions
  8. //
  9. //=============================================================================
  10. #include <stdafx.h>
  11. #include "murmurhash3.h"
  12. //-----------------------------------------------------------------------------
  13. uint32 MurmurHash3_32( const void * key, size_t len, uint32 seed, bool bCaselessStringVariant )
  14. {
  15. const uint8 * data = (const uint8*)key;
  16. const ptrdiff_t nblocks = len / 4;
  17. uint32 uSourceBitwiseAndMask = 0xDFDFDFDF | ((uint32)bCaselessStringVariant - 1);
  18. uint32 h1 = seed;
  19. //----------
  20. // body
  21. const uint32 * blocks = (const uint32 *)(data + nblocks*4);
  22. for(ptrdiff_t i = -nblocks; i; i++)
  23. {
  24. uint32 k1 = LittleDWord(blocks[i]);
  25. k1 &= uSourceBitwiseAndMask;
  26. k1 *= 0xcc9e2d51;
  27. k1 = (k1 << 15) | (k1 >> 17);
  28. k1 *= 0x1b873593;
  29. h1 ^= k1;
  30. h1 = (h1 << 13) | (h1 >> 19);
  31. h1 = h1*5+0xe6546b64;
  32. }
  33. //----------
  34. // tail
  35. const uint8 * tail = (const uint8*)(data + nblocks*4);
  36. uint32 k1 = 0;
  37. switch(len & 3)
  38. {
  39. case 3: k1 ^= tail[2] << 16;
  40. case 2: k1 ^= tail[1] << 8;
  41. case 1: k1 ^= tail[0];
  42. k1 &= uSourceBitwiseAndMask;
  43. k1 *= 0xcc9e2d51;
  44. k1 = (k1 << 15) | (k1 >> 17);
  45. k1 *= 0x1b873593;
  46. h1 ^= k1;
  47. };
  48. //----------
  49. // finalization
  50. h1 ^= len;
  51. h1 ^= h1 >> 16;
  52. h1 *= 0x85ebca6b;
  53. h1 ^= h1 >> 13;
  54. h1 *= 0xc2b2ae35;
  55. h1 ^= h1 >> 16;
  56. return h1;
  57. }