diff options
| author | Kostya Serebryany <kcc@google.com> | 2016-08-16 17:37:13 +0000 |
|---|---|---|
| committer | Kostya Serebryany <kcc@google.com> | 2016-08-16 17:37:13 +0000 |
| commit | c98ef718eab85e44701a23f63c1e69eb6e43cc51 (patch) | |
| tree | ca93d1c5f2de4cde517294ca6eb579797c4ed002 /llvm/lib/Fuzzer/FuzzerValueBitMap.h | |
| parent | 4893aff94ed2acbef2ff91592a423195baee47dc (diff) | |
| download | bcm5719-llvm-c98ef718eab85e44701a23f63c1e69eb6e43cc51.tar.gz bcm5719-llvm-c98ef718eab85e44701a23f63c1e69eb6e43cc51.zip | |
[libFuzzer] refactoring around PCMap, NFC
llvm-svn: 278825
Diffstat (limited to 'llvm/lib/Fuzzer/FuzzerValueBitMap.h')
| -rw-r--r-- | llvm/lib/Fuzzer/FuzzerValueBitMap.h | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/llvm/lib/Fuzzer/FuzzerValueBitMap.h b/llvm/lib/Fuzzer/FuzzerValueBitMap.h new file mode 100644 index 00000000000..41169d66bc0 --- /dev/null +++ b/llvm/lib/Fuzzer/FuzzerValueBitMap.h @@ -0,0 +1,57 @@ +//===- FuzzerValueBitMap.h - INTERNAL - Bit map -----------------*- C++ -* ===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// ValueBitMap. +//===----------------------------------------------------------------------===// + +#ifndef LLVM_FUZZER_VALUE_BIT_MAP_H +#define LLVM_FUZZER_VALUE_BIT_MAP_H + +namespace fuzzer { + +// A bit map containing kMapSizeInWords bits. +struct ValueBitMap { + static const size_t kMapSizeInBits = 65371; // Prime. + static const size_t kMapSizeInBitsAligned = 65536; // 2^16 + static const size_t kBitsInWord = (sizeof(uintptr_t) * 8); + static const size_t kMapSizeInWords = kMapSizeInBitsAligned / kBitsInWord; + public: + // Clears all bits. + void Reset() { memset(Map, 0, sizeof(Map)); } + + // Computed a hash function of Value and sets the corresponding bit. + void AddValue(uintptr_t Value) { + uintptr_t Idx = Value % kMapSizeInBits; + uintptr_t WordIdx = Idx / kBitsInWord; + uintptr_t BitIdx = Idx % kBitsInWord; + Map[WordIdx] |= 1UL << BitIdx; + } + + // Merges 'Other' into 'this', clear Other, + // returns the number of set bits in 'this'. + size_t MergeFrom(ValueBitMap &Other) { + uintptr_t Res = 0; + for (size_t i = 0; i < kMapSizeInWords; i++) { + auto O = Other.Map[i]; + auto M = Map[i]; + if (O) { + Map[i] = (M |= O); + Other.Map[i] = 0; + } + Res += __builtin_popcountl(M); + } + return Res; + } + + private: + uintptr_t Map[kMapSizeInWords] __attribute__((aligned(512))); +}; + +} // namespace fuzzer + +#endif // LLVM_FUZZER_VALUE_BIT_MAP_H |

