metrohash128.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // metrohash128.cpp
  2. //
  3. // The MIT License (MIT)
  4. //
  5. // Copyright (c) 2015 J. Andrew Rogers
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in all
  15. // copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. // SOFTWARE.
  24. //
  25. #include "metrohash.h"
  26. void metrohash128_1(const uint8_t * key, uint64_t len, uint32_t seed, uint8_t * out)
  27. {
  28. static const uint64_t k0 = 0xC83A91E1;
  29. static const uint64_t k1 = 0x8648DBDB;
  30. static const uint64_t k2 = 0x7BDEC03B;
  31. static const uint64_t k3 = 0x2F5870A5;
  32. const uint8_t * ptr = reinterpret_cast<const uint8_t*>(key);
  33. const uint8_t * const end = ptr + len;
  34. uint64_t v[4];
  35. v[0] = ((static_cast<uint64_t>(seed) - k0) * k3) + len;
  36. v[1] = ((static_cast<uint64_t>(seed) + k1) * k2) + len;
  37. if (len >= 32)
  38. {
  39. v[2] = ((static_cast<uint64_t>(seed) + k0) * k2) + len;
  40. v[3] = ((static_cast<uint64_t>(seed) - k1) * k3) + len;
  41. do
  42. {
  43. v[0] += read_u64(ptr) * k0; ptr += 8; v[0] = rotate_right(v[0],29) + v[2];
  44. v[1] += read_u64(ptr) * k1; ptr += 8; v[1] = rotate_right(v[1],29) + v[3];
  45. v[2] += read_u64(ptr) * k2; ptr += 8; v[2] = rotate_right(v[2],29) + v[0];
  46. v[3] += read_u64(ptr) * k3; ptr += 8; v[3] = rotate_right(v[3],29) + v[1];
  47. }
  48. while (ptr <= (end - 32));
  49. v[2] ^= rotate_right(((v[0] + v[3]) * k0) + v[1], 26) * k1;
  50. v[3] ^= rotate_right(((v[1] + v[2]) * k1) + v[0], 26) * k0;
  51. v[0] ^= rotate_right(((v[0] + v[2]) * k0) + v[3], 26) * k1;
  52. v[1] ^= rotate_right(((v[1] + v[3]) * k1) + v[2], 30) * k0;
  53. }
  54. if ((end - ptr) >= 16)
  55. {
  56. v[0] += read_u64(ptr) * k2; ptr += 8; v[0] = rotate_right(v[0],33) * k3;
  57. v[1] += read_u64(ptr) * k2; ptr += 8; v[1] = rotate_right(v[1],33) * k3;
  58. v[0] ^= rotate_right((v[0] * k2) + v[1], 17) * k1;
  59. v[1] ^= rotate_right((v[1] * k3) + v[0], 17) * k0;
  60. }
  61. if ((end - ptr) >= 8)
  62. {
  63. v[0] += read_u64(ptr) * k2; ptr += 8; v[0] = rotate_right(v[0],33) * k3;
  64. v[0] ^= rotate_right((v[0] * k2) + v[1], 20) * k1;
  65. }
  66. if ((end - ptr) >= 4)
  67. {
  68. v[1] += read_u32(ptr) * k2; ptr += 4; v[1] = rotate_right(v[1],33) * k3;
  69. v[1] ^= rotate_right((v[1] * k3) + v[0], 18) * k0;
  70. }
  71. if ((end - ptr) >= 2)
  72. {
  73. v[0] += read_u16(ptr) * k2; ptr += 2; v[0] = rotate_right(v[0],33) * k3;
  74. v[0] ^= rotate_right((v[0] * k2) + v[1], 24) * k1;
  75. }
  76. if ((end - ptr) >= 1)
  77. {
  78. v[1] += read_u8 (ptr) * k2; v[1] = rotate_right(v[1],33) * k3;
  79. v[1] ^= rotate_right((v[1] * k3) + v[0], 24) * k0;
  80. }
  81. v[0] += rotate_right((v[0] * k0) + v[1], 13);
  82. v[1] += rotate_right((v[1] * k1) + v[0], 37);
  83. v[0] += rotate_right((v[0] * k2) + v[1], 13);
  84. v[1] += rotate_right((v[1] * k3) + v[0], 37);
  85. memcpy(out, v, 16);
  86. }
  87. void metrohash128_2(const uint8_t * key, uint64_t len, uint32_t seed, uint8_t * out)
  88. {
  89. static const uint64_t k0 = 0xD6D018F5;
  90. static const uint64_t k1 = 0xA2AA033B;
  91. static const uint64_t k2 = 0x62992FC1;
  92. static const uint64_t k3 = 0x30BC5B29;
  93. const uint8_t * ptr = reinterpret_cast<const uint8_t*>(key);
  94. const uint8_t * const end = ptr + len;
  95. uint64_t v[4];
  96. v[0] = ((static_cast<uint64_t>(seed) - k0) * k3) + len;
  97. v[1] = ((static_cast<uint64_t>(seed) + k1) * k2) + len;
  98. if (len >= 32)
  99. {
  100. v[2] = ((static_cast<uint64_t>(seed) + k0) * k2) + len;
  101. v[3] = ((static_cast<uint64_t>(seed) - k1) * k3) + len;
  102. do
  103. {
  104. v[0] += read_u64(ptr) * k0; ptr += 8; v[0] = rotate_right(v[0],29) + v[2];
  105. v[1] += read_u64(ptr) * k1; ptr += 8; v[1] = rotate_right(v[1],29) + v[3];
  106. v[2] += read_u64(ptr) * k2; ptr += 8; v[2] = rotate_right(v[2],29) + v[0];
  107. v[3] += read_u64(ptr) * k3; ptr += 8; v[3] = rotate_right(v[3],29) + v[1];
  108. }
  109. while (ptr <= (end - 32));
  110. v[2] ^= rotate_right(((v[0] + v[3]) * k0) + v[1], 33) * k1;
  111. v[3] ^= rotate_right(((v[1] + v[2]) * k1) + v[0], 33) * k0;
  112. v[0] ^= rotate_right(((v[0] + v[2]) * k0) + v[3], 33) * k1;
  113. v[1] ^= rotate_right(((v[1] + v[3]) * k1) + v[2], 33) * k0;
  114. }
  115. if ((end - ptr) >= 16)
  116. {
  117. v[0] += read_u64(ptr) * k2; ptr += 8; v[0] = rotate_right(v[0],29) * k3;
  118. v[1] += read_u64(ptr) * k2; ptr += 8; v[1] = rotate_right(v[1],29) * k3;
  119. v[0] ^= rotate_right((v[0] * k2) + v[1], 29) * k1;
  120. v[1] ^= rotate_right((v[1] * k3) + v[0], 29) * k0;
  121. }
  122. if ((end - ptr) >= 8)
  123. {
  124. v[0] += read_u64(ptr) * k2; ptr += 8; v[0] = rotate_right(v[0],29) * k3;
  125. v[0] ^= rotate_right((v[0] * k2) + v[1], 29) * k1;
  126. }
  127. if ((end - ptr) >= 4)
  128. {
  129. v[1] += read_u32(ptr) * k2; ptr += 4; v[1] = rotate_right(v[1],29) * k3;
  130. v[1] ^= rotate_right((v[1] * k3) + v[0], 25) * k0;
  131. }
  132. if ((end - ptr) >= 2)
  133. {
  134. v[0] += read_u16(ptr) * k2; ptr += 2; v[0] = rotate_right(v[0],29) * k3;
  135. v[0] ^= rotate_right((v[0] * k2) + v[1], 30) * k1;
  136. }
  137. if ((end - ptr) >= 1)
  138. {
  139. v[1] += read_u8 (ptr) * k2; v[1] = rotate_right(v[1],29) * k3;
  140. v[1] ^= rotate_right((v[1] * k3) + v[0], 18) * k0;
  141. }
  142. v[0] += rotate_right((v[0] * k0) + v[1], 33);
  143. v[1] += rotate_right((v[1] * k1) + v[0], 33);
  144. v[0] += rotate_right((v[0] * k2) + v[1], 33);
  145. v[1] += rotate_right((v[1] * k3) + v[0], 33);
  146. memcpy(out, v, 16);
  147. }