xref: /freebsd/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_hash.h (revision 7877fdebeeb35fad1cbbafce22598b1bdf97c786)
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 
17 namespace __sanitizer {
18 class 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 }  //namespace __sanitizer
42 
43 #endif  // SANITIZER_HASH_H
44