xxhash.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * xxHash - Fast Hash algorithm
  3. * Copyright (C) 2012-2016, Yann Collet
  4. *
  5. * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following disclaimer
  15. * in the documentation and/or other materials provided with the
  16. * distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. * You can contact the author at :
  31. * - xxHash homepage: http://www.xxhash.com
  32. * - xxHash source repository : https://github.com/Cyan4973/xxHash
  33. */
  34. /* based on revision d2df04efcbef7d7f6886d345861e5dfda4edacc1 Removed
  35. * everything but a simple interface for computing XXh64. */
  36. #include "llvm/Support/xxhash.h"
  37. #include "llvm/Support/Endian.h"
  38. #include <stdlib.h>
  39. #include <string.h>
  40. using namespace llvm;
  41. using namespace support;
  42. static uint64_t rotl64(uint64_t X, size_t R) {
  43. return (X << R) | (X >> (64 - R));
  44. }
  45. static const uint64_t PRIME64_1 = 11400714785074694791ULL;
  46. static const uint64_t PRIME64_2 = 14029467366897019727ULL;
  47. static const uint64_t PRIME64_3 = 1609587929392839161ULL;
  48. static const uint64_t PRIME64_4 = 9650029242287828579ULL;
  49. static const uint64_t PRIME64_5 = 2870177450012600261ULL;
  50. static uint64_t round(uint64_t Acc, uint64_t Input) {
  51. Acc += Input * PRIME64_2;
  52. Acc = rotl64(Acc, 31);
  53. Acc *= PRIME64_1;
  54. return Acc;
  55. }
  56. static uint64_t mergeRound(uint64_t Acc, uint64_t Val) {
  57. Val = round(0, Val);
  58. Acc ^= Val;
  59. Acc = Acc * PRIME64_1 + PRIME64_4;
  60. return Acc;
  61. }
  62. uint64_t llvm::xxHash64(StringRef Data) {
  63. size_t Len = Data.size();
  64. uint64_t Seed = 0;
  65. const unsigned char *P = Data.bytes_begin();
  66. const unsigned char *const BEnd = Data.bytes_end();
  67. uint64_t H64;
  68. if (Len >= 32) {
  69. const unsigned char *const Limit = BEnd - 32;
  70. uint64_t V1 = Seed + PRIME64_1 + PRIME64_2;
  71. uint64_t V2 = Seed + PRIME64_2;
  72. uint64_t V3 = Seed + 0;
  73. uint64_t V4 = Seed - PRIME64_1;
  74. do {
  75. V1 = round(V1, endian::read64le(P));
  76. P += 8;
  77. V2 = round(V2, endian::read64le(P));
  78. P += 8;
  79. V3 = round(V3, endian::read64le(P));
  80. P += 8;
  81. V4 = round(V4, endian::read64le(P));
  82. P += 8;
  83. } while (P <= Limit);
  84. H64 = rotl64(V1, 1) + rotl64(V2, 7) + rotl64(V3, 12) + rotl64(V4, 18);
  85. H64 = mergeRound(H64, V1);
  86. H64 = mergeRound(H64, V2);
  87. H64 = mergeRound(H64, V3);
  88. H64 = mergeRound(H64, V4);
  89. } else {
  90. H64 = Seed + PRIME64_5;
  91. }
  92. H64 += (uint64_t)Len;
  93. while (P + 8 <= BEnd) {
  94. uint64_t const K1 = round(0, endian::read64le(P));
  95. H64 ^= K1;
  96. H64 = rotl64(H64, 27) * PRIME64_1 + PRIME64_4;
  97. P += 8;
  98. }
  99. if (P + 4 <= BEnd) {
  100. H64 ^= (uint64_t)(endian::read32le(P)) * PRIME64_1;
  101. H64 = rotl64(H64, 23) * PRIME64_2 + PRIME64_3;
  102. P += 4;
  103. }
  104. while (P < BEnd) {
  105. H64 ^= (*P) * PRIME64_5;
  106. H64 = rotl64(H64, 11) * PRIME64_1;
  107. P++;
  108. }
  109. H64 ^= H64 >> 33;
  110. H64 *= PRIME64_2;
  111. H64 ^= H64 >> 29;
  112. H64 *= PRIME64_3;
  113. H64 ^= H64 >> 32;
  114. return H64;
  115. }
  116. uint64_t llvm::xxHash64(ArrayRef<uint8_t> Data) {
  117. return xxHash64({(const char *)Data.data(), Data.size()});
  118. }