metrohash64crc.cpp 5.7 KB

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