blob: fdc92189b4776da078536e30d3b76cd30af38b1c [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:
26 // Clears all bits.
27 void Reset() { memset(Map, 0, sizeof(Map)); }
28
Kostya Serebryany53501782016-09-15 04:36:45 +000029 // Computes a hash function of Value and sets the corresponding bit.
30 // Returns true if the bit was changed from 0 to 1.
31 inline bool AddValue(uintptr_t Value) {
Kostya Serebryany30443902016-08-16 21:28:05 +000032 uintptr_t Idx = Value < kMapSizeInBits ? Value : Value % kMapSizeInBits;
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000033 uintptr_t WordIdx = Idx / kBitsInWord;
34 uintptr_t BitIdx = Idx % kBitsInWord;
Kostya Serebryany53501782016-09-15 04:36:45 +000035 uintptr_t Old = Map[WordIdx];
36 uintptr_t New = Old | (1UL << BitIdx);
37 Map[WordIdx] = New;
38 return New != Old;
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000039 }
40
Kostya Serebryanyd28099d2016-09-23 00:22:46 +000041 size_t GetNumBitsSinceLastMerge() const { return NumBits; }
42
43 // Merges 'Other' into 'this', clears 'Other', updates NumBits,
44 // returns true if new bits were added.
Kostya Serebryanybceadcf2016-08-24 01:38:42 +000045 ATTRIBUTE_TARGET_POPCNT
Kostya Serebryanyd28099d2016-09-23 00:22:46 +000046 bool MergeFrom(ValueBitMap &Other) {
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000047 uintptr_t Res = 0;
Kostya Serebryanyd28099d2016-09-23 00:22:46 +000048 size_t OldNumBits = NumBits;
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000049 for (size_t i = 0; i < kMapSizeInWords; i++) {
50 auto O = Other.Map[i];
51 auto M = Map[i];
52 if (O) {
53 Map[i] = (M |= O);
54 Other.Map[i] = 0;
55 }
Kostya Serebryanyd46a59f2016-08-16 19:33:51 +000056 if (M)
57 Res += __builtin_popcountl(M);
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000058 }
Kostya Serebryanyd28099d2016-09-23 00:22:46 +000059 NumBits = Res;
60 return OldNumBits < NumBits;
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000061 }
62
63 private:
Kostya Serebryanyd28099d2016-09-23 00:22:46 +000064 size_t NumBits;
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000065 uintptr_t Map[kMapSizeInWords] __attribute__((aligned(512)));
66};
67
68} // namespace fuzzer
69
70#endif // LLVM_FUZZER_VALUE_BIT_MAP_H