[llvm] r290034 - [libFuzzer] speed up __sanitizer_cov_trace_switch a bit more (remove DIV)

Kostya Serebryany via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 16 18:23:35 PST 2016


Author: kcc
Date: Fri Dec 16 20:23:35 2016
New Revision: 290034

URL: http://llvm.org/viewvc/llvm-project?rev=290034&view=rev
Log:
[libFuzzer] speed up __sanitizer_cov_trace_switch a bit more (remove DIV)

Modified:
    llvm/trunk/lib/Fuzzer/FuzzerTracePC.cpp

Modified: llvm/trunk/lib/Fuzzer/FuzzerTracePC.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Fuzzer/FuzzerTracePC.cpp?rev=290034&r1=290033&r2=290034&view=diff
==============================================================================
--- llvm/trunk/lib/Fuzzer/FuzzerTracePC.cpp (original)
+++ llvm/trunk/lib/Fuzzer/FuzzerTracePC.cpp Fri Dec 16 20:23:35 2016
@@ -299,7 +299,17 @@ void __sanitizer_cov_trace_switch(uint64
   uint64_t N = Cases[0];
   uint64_t *Vals = Cases + 2;
   char *PC = (char*)__builtin_return_address(0);
-  size_t Idx = Counter % N;
+  // We need a random number < N using Counter as a seed. But w/o DIV.
+  // * find a power of two >= N
+  // * mask Counter with this power of two.
+  // * maybe subtract N.
+  size_t Nlog = sizeof(long) * 8 - __builtin_clzl((long)N);
+  size_t PowerOfTwoGeN = 1U << Nlog;
+  assert(PowerOfTwoGeN >= N);
+  size_t Idx = Counter & (PowerOfTwoGeN - 1);
+  if (Idx >= N)
+    Idx -= N;
+  assert(Idx < N);
   uint64_t TwoIn32 = 1ULL << 32;
   if ((Val | Vals[Idx]) < TwoIn32)
     fuzzer::TPC.HandleCmp(PC + Idx, static_cast<uint32_t>(Val),




More information about the llvm-commits mailing list