blob: 41169d66bc015f6a642b972b93903e0b843c1185 [file] [log] [blame]
Kostya Serebryanyc98ef712016-08-16 17:37:13 +00001//===- FuzzerValueBitMap.h - INTERNAL - Bit map -----------------*- C++ -* ===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9// ValueBitMap.
10//===----------------------------------------------------------------------===//
11
12#ifndef LLVM_FUZZER_VALUE_BIT_MAP_H
13#define LLVM_FUZZER_VALUE_BIT_MAP_H
14
15namespace fuzzer {
16
17// A bit map containing kMapSizeInWords bits.
18struct ValueBitMap {
19 static const size_t kMapSizeInBits = 65371; // Prime.
20 static const size_t kMapSizeInBitsAligned = 65536; // 2^16
21 static const size_t kBitsInWord = (sizeof(uintptr_t) * 8);
22 static const size_t kMapSizeInWords = kMapSizeInBitsAligned / kBitsInWord;
23 public:
24 // Clears all bits.
25 void Reset() { memset(Map, 0, sizeof(Map)); }
26
27 // Computed a hash function of Value and sets the corresponding bit.
28 void AddValue(uintptr_t Value) {
29 uintptr_t Idx = Value % kMapSizeInBits;
30 uintptr_t WordIdx = Idx / kBitsInWord;
31 uintptr_t BitIdx = Idx % kBitsInWord;
32 Map[WordIdx] |= 1UL << BitIdx;
33 }
34
35 // Merges 'Other' into 'this', clear Other,
36 // returns the number of set bits in 'this'.
37 size_t MergeFrom(ValueBitMap &Other) {
38 uintptr_t Res = 0;
39 for (size_t i = 0; i < kMapSizeInWords; i++) {
40 auto O = Other.Map[i];
41 auto M = Map[i];
42 if (O) {
43 Map[i] = (M |= O);
44 Other.Map[i] = 0;
45 }
46 Res += __builtin_popcountl(M);
47 }
48 return Res;
49 }
50
51 private:
52 uintptr_t Map[kMapSizeInWords] __attribute__((aligned(512)));
53};
54
55} // namespace fuzzer
56
57#endif // LLVM_FUZZER_VALUE_BIT_MAP_H