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 | |
Kostya Serebryany | 8658618 | 2016-09-21 21:17:23 +0000 | [diff] [blame] | 15 | #include "FuzzerDefs.h" |
| 16 | |
Kostya Serebryany | c98ef71 | 2016-08-16 17:37:13 +0000 | [diff] [blame] | 17 | namespace fuzzer { |
| 18 | |
| 19 | // A bit map containing kMapSizeInWords bits. |
| 20 | struct 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 Serebryany | 5350178 | 2016-09-15 04:36:45 +0000 | [diff] [blame] | 29 | // 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 Serebryany | 3044390 | 2016-08-16 21:28:05 +0000 | [diff] [blame] | 32 | uintptr_t Idx = Value < kMapSizeInBits ? Value : Value % kMapSizeInBits; |
Kostya Serebryany | c98ef71 | 2016-08-16 17:37:13 +0000 | [diff] [blame] | 33 | uintptr_t WordIdx = Idx / kBitsInWord; |
| 34 | uintptr_t BitIdx = Idx % kBitsInWord; |
Kostya Serebryany | 5350178 | 2016-09-15 04:36:45 +0000 | [diff] [blame] | 35 | uintptr_t Old = Map[WordIdx]; |
| 36 | uintptr_t New = Old | (1UL << BitIdx); |
| 37 | Map[WordIdx] = New; |
| 38 | return New != Old; |
Kostya Serebryany | c98ef71 | 2016-08-16 17:37:13 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Kostya Serebryany | d46a59f | 2016-08-16 19:33:51 +0000 | [diff] [blame] | 41 | // Merges 'Other' into 'this', clears 'Other', |
Kostya Serebryany | c98ef71 | 2016-08-16 17:37:13 +0000 | [diff] [blame] | 42 | // returns the number of set bits in 'this'. |
Kostya Serebryany | bceadcf | 2016-08-24 01:38:42 +0000 | [diff] [blame] | 43 | ATTRIBUTE_TARGET_POPCNT |
Kostya Serebryany | c98ef71 | 2016-08-16 17:37:13 +0000 | [diff] [blame] | 44 | size_t MergeFrom(ValueBitMap &Other) { |
| 45 | uintptr_t Res = 0; |
| 46 | for (size_t i = 0; i < kMapSizeInWords; i++) { |
| 47 | auto O = Other.Map[i]; |
| 48 | auto M = Map[i]; |
| 49 | if (O) { |
| 50 | Map[i] = (M |= O); |
| 51 | Other.Map[i] = 0; |
| 52 | } |
Kostya Serebryany | d46a59f | 2016-08-16 19:33:51 +0000 | [diff] [blame] | 53 | if (M) |
| 54 | Res += __builtin_popcountl(M); |
Kostya Serebryany | c98ef71 | 2016-08-16 17:37:13 +0000 | [diff] [blame] | 55 | } |
| 56 | return Res; |
| 57 | } |
| 58 | |
| 59 | private: |
| 60 | uintptr_t Map[kMapSizeInWords] __attribute__((aligned(512))); |
| 61 | }; |
| 62 | |
| 63 | } // namespace fuzzer |
| 64 | |
| 65 | #endif // LLVM_FUZZER_VALUE_BIT_MAP_H |