sanitizer_hash.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //===-- sanitizer_common.h --------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements a simple hash function.
  10. //===----------------------------------------------------------------------===//
  11. #ifndef SANITIZER_HASH_H
  12. #define SANITIZER_HASH_H
  13. #include "sanitizer_internal_defs.h"
  14. namespace __sanitizer {
  15. class MurMur2HashBuilder {
  16. static const u32 m = 0x5bd1e995;
  17. static const u32 seed = 0x9747b28c;
  18. static const u32 r = 24;
  19. u32 h;
  20. public:
  21. explicit MurMur2HashBuilder(u32 init = 0) { h = seed ^ init; }
  22. void add(u32 k) {
  23. k *= m;
  24. k ^= k >> r;
  25. k *= m;
  26. h *= m;
  27. h ^= k;
  28. }
  29. u32 get() {
  30. u32 x = h;
  31. x ^= x >> 13;
  32. x *= m;
  33. x ^= x >> 15;
  34. return x;
  35. }
  36. };
  37. class MurMur2Hash64Builder {
  38. static const u64 m = 0xc6a4a7935bd1e995ull;
  39. static const u64 seed = 0x9747b28c9747b28cull;
  40. static const u64 r = 47;
  41. u64 h;
  42. public:
  43. explicit MurMur2Hash64Builder(u64 init = 0) { h = seed ^ (init * m); }
  44. void add(u64 k) {
  45. k *= m;
  46. k ^= k >> r;
  47. k *= m;
  48. h ^= k;
  49. h *= m;
  50. }
  51. u64 get() {
  52. u64 x = h;
  53. x ^= x >> r;
  54. x *= m;
  55. x ^= x >> r;
  56. return x;
  57. }
  58. };
  59. } //namespace __sanitizer
  60. #endif // SANITIZER_HASH_H