blob: 97f4c5e51b9992268250acaf422d2501ba971aa6 [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
Kostya Serebryany86586182016-09-21 21:17:23 +000015#include "FuzzerDefs.h"
16
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000017namespace fuzzer {
18
19// A bit map containing kMapSizeInWords bits.
20struct ValueBitMap {
21 static const size_t kMapSizeInBits = 65371; // Prime.
22 static const size_t kMapSizeInBitsAligned = 65536; // 2^16
23 static const size_t kBitsInWord = (sizeof(uintptr_t) * 8);
24 static const size_t kMapSizeInWords = kMapSizeInBitsAligned / kBitsInWord;
25 public:
Kostya Serebryany0800b812016-09-23 23:51:58 +000026 static const size_t kNumberOfItems = kMapSizeInBits;
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000027 // Clears all bits.
28 void Reset() { memset(Map, 0, sizeof(Map)); }
29
Kostya Serebryany53501782016-09-15 04:36:45 +000030 // Computes a hash function of Value and sets the corresponding bit.
31 // Returns true if the bit was changed from 0 to 1.
32 inline bool AddValue(uintptr_t Value) {
Kostya Serebryany30443902016-08-16 21:28:05 +000033 uintptr_t Idx = Value < kMapSizeInBits ? Value : Value % kMapSizeInBits;
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000034 uintptr_t WordIdx = Idx / kBitsInWord;
35 uintptr_t BitIdx = Idx % kBitsInWord;
Kostya Serebryany53501782016-09-15 04:36:45 +000036 uintptr_t Old = Map[WordIdx];
37 uintptr_t New = Old | (1UL << BitIdx);
38 Map[WordIdx] = New;
39 return New != Old;
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000040 }
41
Kostya Serebryany0800b812016-09-23 23:51:58 +000042 inline bool Get(uintptr_t Idx) {
43 assert(Idx < kMapSizeInBits);
44 uintptr_t WordIdx = Idx / kBitsInWord;
45 uintptr_t BitIdx = Idx % kBitsInWord;
46 return Map[WordIdx] & (1UL << BitIdx);
47 }
48
Kostya Serebryanyd28099d2016-09-23 00:22:46 +000049 size_t GetNumBitsSinceLastMerge() const { return NumBits; }
50
51 // Merges 'Other' into 'this', clears 'Other', updates NumBits,
52 // returns true if new bits were added.
Kostya Serebryanybceadcf2016-08-24 01:38:42 +000053 ATTRIBUTE_TARGET_POPCNT
Kostya Serebryanyd28099d2016-09-23 00:22:46 +000054 bool MergeFrom(ValueBitMap &Other) {
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000055 uintptr_t Res = 0;
Kostya Serebryanyd28099d2016-09-23 00:22:46 +000056 size_t OldNumBits = NumBits;
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000057 for (size_t i = 0; i < kMapSizeInWords; i++) {
58 auto O = Other.Map[i];
59 auto M = Map[i];
60 if (O) {
61 Map[i] = (M |= O);
62 Other.Map[i] = 0;
63 }
Kostya Serebryanyd46a59f2016-08-16 19:33:51 +000064 if (M)
65 Res += __builtin_popcountl(M);
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000066 }
Kostya Serebryanyd28099d2016-09-23 00:22:46 +000067 NumBits = Res;
68 return OldNumBits < NumBits;
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000069 }
70
71 private:
Kostya Serebryanya9a135b2016-09-29 15:51:28 +000072 size_t NumBits = 0;
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000073 uintptr_t Map[kMapSizeInWords] __attribute__((aligned(512)));
74};
75
76} // namespace fuzzer
77
78#endif // LLVM_FUZZER_VALUE_BIT_MAP_H