Kostya Serebryany | c98ef71 | 2016-08-16 17:37:13 +0000 | [diff] [blame] | 1 | //===- 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 | |
| 15 | namespace fuzzer { |
| 16 | |
| 17 | // A bit map containing kMapSizeInWords bits. |
| 18 | struct 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 Serebryany | 5a5d554 | 2016-08-17 23:09:57 +0000 | [diff] [blame] | 28 | inline void AddValue(uintptr_t Value) { |
Kostya Serebryany | 3044390 | 2016-08-16 21:28:05 +0000 | [diff] [blame] | 29 | uintptr_t Idx = Value < kMapSizeInBits ? Value : Value % kMapSizeInBits; |
Kostya Serebryany | c98ef71 | 2016-08-16 17:37:13 +0000 | [diff] [blame] | 30 | uintptr_t WordIdx = Idx / kBitsInWord; |
| 31 | uintptr_t BitIdx = Idx % kBitsInWord; |
| 32 | Map[WordIdx] |= 1UL << BitIdx; |
| 33 | } |
| 34 | |
Kostya Serebryany | d46a59f | 2016-08-16 19:33:51 +0000 | [diff] [blame] | 35 | // Merges 'Other' into 'this', clears 'Other', |
Kostya Serebryany | c98ef71 | 2016-08-16 17:37:13 +0000 | [diff] [blame] | 36 | // returns the number of set bits in 'this'. |
Kostya Serebryany | 5a5d554 | 2016-08-17 23:09:57 +0000 | [diff] [blame] | 37 | __attribute__((target("popcnt"))) |
Kostya Serebryany | c98ef71 | 2016-08-16 17:37:13 +0000 | [diff] [blame] | 38 | 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 Serebryany | d46a59f | 2016-08-16 19:33:51 +0000 | [diff] [blame] | 47 | if (M) |
| 48 | Res += __builtin_popcountl(M); |
Kostya Serebryany | c98ef71 | 2016-08-16 17:37:13 +0000 | [diff] [blame] | 49 | } |
| 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 |