metrohash128crc.cpp 6.2 KB

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