metrohash128crc.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // metrohash128crc.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. #include <nmmintrin.h>
  27. void metrohash128crc_1(const uint8_t * key, uint64_t len, uint32_t seed, uint8_t * out)
  28. {
  29. static const uint64_t k0 = 0xC83A91E1;
  30. static const uint64_t k1 = 0x8648DBDB;
  31. static const uint64_t k2 = 0x7BDEC03B;
  32. static const uint64_t k3 = 0x2F5870A5;
  33. const uint8_t * ptr = reinterpret_cast<const uint8_t*>(key);
  34. const uint8_t * const end = ptr + len;
  35. uint64_t v[4];
  36. v[0] = ((static_cast<uint64_t>(seed) - k0) * k3) + len;
  37. v[1] = ((static_cast<uint64_t>(seed) + k1) * k2) + len;
  38. if (len >= 32)
  39. {
  40. v[2] = ((static_cast<uint64_t>(seed) + k0) * k2) + len;
  41. v[3] = ((static_cast<uint64_t>(seed) - k1) * k3) + len;
  42. do
  43. {
  44. v[0] ^= _mm_crc32_u64(v[0], read_u64(ptr)); ptr += 8;
  45. v[1] ^= _mm_crc32_u64(v[1], read_u64(ptr)); ptr += 8;
  46. v[2] ^= _mm_crc32_u64(v[2], read_u64(ptr)); ptr += 8;
  47. v[3] ^= _mm_crc32_u64(v[3], read_u64(ptr)); ptr += 8;
  48. }
  49. while (ptr <= (end - 32));
  50. v[2] ^= rotate_right(((v[0] + v[3]) * k0) + v[1], 34) * k1;
  51. v[3] ^= rotate_right(((v[1] + v[2]) * k1) + v[0], 37) * k0;
  52. v[0] ^= rotate_right(((v[0] + v[2]) * k0) + v[3], 34) * k1;
  53. v[1] ^= rotate_right(((v[1] + v[3]) * k1) + v[2], 37) * k0;
  54. }
  55. if ((end - ptr) >= 16)
  56. {
  57. v[0] += read_u64(ptr) * k2; ptr += 8; v[0] = rotate_right(v[0],34) * k3;
  58. v[1] += read_u64(ptr) * k2; ptr += 8; v[1] = rotate_right(v[1],34) * k3;
  59. v[0] ^= rotate_right((v[0] * k2) + v[1], 30) * k1;
  60. v[1] ^= rotate_right((v[1] * k3) + v[0], 30) * k0;
  61. }
  62. if ((end - ptr) >= 8)
  63. {
  64. v[0] += read_u64(ptr) * k2; ptr += 8; v[0] = rotate_right(v[0],36) * k3;
  65. v[0] ^= rotate_right((v[0] * k2) + v[1], 23) * k1;
  66. }
  67. if ((end - ptr) >= 4)
  68. {
  69. v[1] ^= _mm_crc32_u64(v[0], read_u32(ptr)); ptr += 4;
  70. v[1] ^= rotate_right((v[1] * k3) + v[0], 19) * k0;
  71. }
  72. if ((end - ptr) >= 2)
  73. {
  74. v[0] ^= _mm_crc32_u64(v[1], read_u16(ptr)); ptr += 2;
  75. v[0] ^= rotate_right((v[0] * k2) + v[1], 13) * k1;
  76. }
  77. if ((end - ptr) >= 1)
  78. {
  79. v[1] ^= _mm_crc32_u64(v[0], read_u8 (ptr));
  80. v[1] ^= rotate_right((v[1] * k3) + v[0], 17) * k0;
  81. }
  82. v[0] += rotate_right((v[0] * k0) + v[1], 11);
  83. v[1] += rotate_right((v[1] * k1) + v[0], 26);
  84. v[0] += rotate_right((v[0] * k0) + v[1], 11);
  85. v[1] += rotate_right((v[1] * k1) + v[0], 26);
  86. memcpy(out, v, 16);
  87. }
  88. void metrohash128crc_2(const uint8_t * key, uint64_t len, uint32_t seed, uint8_t * out)
  89. {
  90. static const uint64_t k0 = 0xEE783E2F;
  91. static const uint64_t k1 = 0xAD07C493;
  92. static const uint64_t k2 = 0x797A90BB;
  93. static const uint64_t k3 = 0x2E4B2E1B;
  94. const uint8_t * ptr = reinterpret_cast<const uint8_t*>(key);
  95. const uint8_t * const end = ptr + len;
  96. uint64_t v[4];
  97. v[0] = ((static_cast<uint64_t>(seed) - k0) * k3) + len;
  98. v[1] = ((static_cast<uint64_t>(seed) + k1) * k2) + len;
  99. if (len >= 32)
  100. {
  101. v[2] = ((static_cast<uint64_t>(seed) + k0) * k2) + len;
  102. v[3] = ((static_cast<uint64_t>(seed) - k1) * k3) + len;
  103. do
  104. {
  105. v[0] ^= _mm_crc32_u64(v[0], read_u64(ptr)); ptr += 8;
  106. v[1] ^= _mm_crc32_u64(v[1], read_u64(ptr)); ptr += 8;
  107. v[2] ^= _mm_crc32_u64(v[2], read_u64(ptr)); ptr += 8;
  108. v[3] ^= _mm_crc32_u64(v[3], read_u64(ptr)); ptr += 8;
  109. }
  110. while (ptr <= (end - 32));
  111. v[2] ^= rotate_right(((v[0] + v[3]) * k0) + v[1], 12) * k1;
  112. v[3] ^= rotate_right(((v[1] + v[2]) * k1) + v[0], 19) * k0;
  113. v[0] ^= rotate_right(((v[0] + v[2]) * k0) + v[3], 12) * k1;
  114. v[1] ^= rotate_right(((v[1] + v[3]) * k1) + v[2], 19) * k0;
  115. }
  116. if ((end - ptr) >= 16)
  117. {
  118. v[0] += read_u64(ptr) * k2; ptr += 8; v[0] = rotate_right(v[0],41) * k3;
  119. v[1] += read_u64(ptr) * k2; ptr += 8; v[1] = rotate_right(v[1],41) * k3;
  120. v[0] ^= rotate_right((v[0] * k2) + v[1], 10) * k1;
  121. v[1] ^= rotate_right((v[1] * k3) + v[0], 10) * k0;
  122. }
  123. if ((end - ptr) >= 8)
  124. {
  125. v[0] += read_u64(ptr) * k2; ptr += 8; v[0] = rotate_right(v[0],34) * k3;
  126. v[0] ^= rotate_right((v[0] * k2) + v[1], 22) * k1;
  127. }
  128. if ((end - ptr) >= 4)
  129. {
  130. v[1] ^= _mm_crc32_u64(v[0], read_u32(ptr)); ptr += 4;
  131. v[1] ^= rotate_right((v[1] * k3) + v[0], 14) * k0;
  132. }
  133. if ((end - ptr) >= 2)
  134. {
  135. v[0] ^= _mm_crc32_u64(v[1], read_u16(ptr)); ptr += 2;
  136. v[0] ^= rotate_right((v[0] * k2) + v[1], 15) * k1;
  137. }
  138. if ((end - ptr) >= 1)
  139. {
  140. v[1] ^= _mm_crc32_u64(v[0], read_u8 (ptr));
  141. v[1] ^= rotate_right((v[1] * k3) + v[0], 18) * k0;
  142. }
  143. v[0] += rotate_right((v[0] * k0) + v[1], 15);
  144. v[1] += rotate_right((v[1] * k1) + v[0], 27);
  145. v[0] += rotate_right((v[0] * k0) + v[1], 15);
  146. v[1] += rotate_right((v[1] * k1) + v[0], 27);
  147. memcpy(out, v, 16);
  148. }