blob: e890e1e903e1f1f44d304d76f3ffe5cf1293ed32 [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.
Kostya Serebryany5a5d5542016-08-17 23:09:57 +000028 inline void AddValue(uintptr_t Value) {
Kostya Serebryany30443902016-08-16 21:28:05 +000029 uintptr_t Idx = Value < kMapSizeInBits ? Value : Value % kMapSizeInBits;
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000030 uintptr_t WordIdx = Idx / kBitsInWord;
31 uintptr_t BitIdx = Idx % kBitsInWord;
32 Map[WordIdx] |= 1UL << BitIdx;
33 }
34
Kostya Serebryanyd46a59f2016-08-16 19:33:51 +000035 // Merges 'Other' into 'this', clears 'Other',
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000036 // returns the number of set bits in 'this'.
Kostya Serebryany5a5d5542016-08-17 23:09:57 +000037 __attribute__((target("popcnt")))
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000038 size_t MergeFrom(ValueBitMap &Other) {
39 uintptr_t Res = 0;
40 for (size_t i = 0; i < kMapSizeInWords; i++) {
41 auto O = Other.Map[i];
42 auto M = Map[i];
43 if (O) {
44 Map[i] = (M |= O);
45 Other.Map[i] = 0;
46 }
Kostya Serebryanyd46a59f2016-08-16 19:33:51 +000047 if (M)
48 Res += __builtin_popcountl(M);
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000049 }
50 return Res;
51 }
52
53 private:
54 uintptr_t Map[kMapSizeInWords] __attribute__((aligned(512)));
55};
56
57} // namespace fuzzer
58
59#endif // LLVM_FUZZER_VALUE_BIT_MAP_H