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
12#ifndef SANITIZER_HASH_H
13#define SANITIZER_HASH_H
14
15#include "sanitizer_internal_defs.h"
16
17namespace __sanitizer {
18class MurMur2HashBuilder {
19  static const u32 m = 0x5bd1e995;
20  static const u32 seed = 0x9747b28c;
21  static const u32 r = 24;
22  u32 h;
23
24 public:
25  explicit MurMur2HashBuilder(u32 init = 0) { h = seed ^ init; }
26  void add(u32 k) {
27    k *= m;
28    k ^= k >> r;
29    k *= m;
30    h *= m;
31    h ^= k;
32  }
33  u32 get() {
34    u32 x = h;
35    x ^= x >> 13;
36    x *= m;
37    x ^= x >> 15;
38    return x;
39  }
40};
41
42class MurMur2Hash64Builder {
43  static const u64 m = 0xc6a4a7935bd1e995ull;
44  static const u64 seed = 0x9747b28c9747b28cull;
45  static const u64 r = 47;
46  u64 h;
47
48 public:
49  explicit MurMur2Hash64Builder(u64 init = 0) { h = seed ^ (init * m); }
50  void add(u64 k) {
51    k *= m;
52    k ^= k >> r;
53    k *= m;
54    h ^= k;
55    h *= m;
56  }
57  u64 get() {
58    u64 x = h;
59    x ^= x >> r;
60    x *= m;
61    x ^= x >> r;
62    return x;
63  }
64};
65}  //namespace __sanitizer
66
67#endif  // SANITIZER_HASH_H
68