2
0

murmurhash.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * `murmurhash.h' - murmurhash
  3. *
  4. * copyright (c) 2014 joseph werle <[email protected]>
  5. * Copyright (c) 2015-2016 The Khronos Group Inc.
  6. * Copyright (c) 2015-2016 Valve Corporation
  7. * Copyright (c) 2015-2016 LunarG, Inc.
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and/or associated documentation files (the "Materials"), to
  11. * deal in the Materials without restriction, including without limitation the
  12. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  13. * sell copies of the Materials, and to permit persons to whom the Materials are
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice(s) and this permission notice shall be included in
  17. * all copies or substantial portions of the Materials.
  18. *
  19. * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  22. *
  23. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  24. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  25. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
  26. * USE OR OTHER DEALINGS IN THE MATERIALS.
  27. */
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <stdint.h>
  31. #include "murmurhash.h"
  32. uint32_t murmurhash(const char *key, size_t len, uint32_t seed) {
  33. uint32_t c1 = 0xcc9e2d51;
  34. uint32_t c2 = 0x1b873593;
  35. uint32_t r1 = 15;
  36. uint32_t r2 = 13;
  37. uint32_t m = 5;
  38. uint32_t n = 0xe6546b64;
  39. uint32_t h = 0;
  40. uint32_t k = 0;
  41. uint8_t *d = (uint8_t *)key; // 32 bit extract from `key'
  42. const uint32_t *chunks = NULL;
  43. const uint8_t *tail = NULL; // tail - last 8 bytes
  44. int i = 0;
  45. int l = (int)len / 4; // chunk length
  46. h = seed;
  47. chunks = (const uint32_t *)(d + l * 4); // body
  48. tail = (const uint8_t *)(d + l * 4); // last 8 byte chunk of `key'
  49. // for each 4 byte chunk of `key'
  50. for (i = -l; i != 0; ++i) {
  51. // next 4 byte chunk of `key'
  52. k = chunks[i];
  53. // encode next 4 byte chunk of `key'
  54. k *= c1;
  55. k = (k << r1) | (k >> (32 - r1));
  56. k *= c2;
  57. // append to hash
  58. h ^= k;
  59. h = (h << r2) | (h >> (32 - r2));
  60. h = h * m + n;
  61. }
  62. k = 0;
  63. // remainder
  64. switch (len & 3) { // `len % 4'
  65. case 3:
  66. k ^= (tail[2] << 16);
  67. // fall through
  68. case 2:
  69. k ^= (tail[1] << 8);
  70. // fall through
  71. case 1:
  72. k ^= tail[0];
  73. k *= c1;
  74. k = (k << r1) | (k >> (32 - r1));
  75. k *= c2;
  76. h ^= k;
  77. }
  78. h ^= len;
  79. h ^= (h >> 16);
  80. h *= 0x85ebca6b;
  81. h ^= (h >> 13);
  82. h *= 0xc2b2ae35;
  83. h ^= (h >> 16);
  84. return h;
  85. }