Mercurial > hg > Members > tobaru > cbc > CbC_llvm
diff lib/Support/BranchProbability.cpp @ 95:afa8332a0e37
LLVM 3.8
author | Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 13 Oct 2015 17:48:58 +0900 |
parents | 54457678186b |
children | 7d135dc70f03 |
line wrap: on
line diff
--- a/lib/Support/BranchProbability.cpp Wed Feb 18 14:56:07 2015 +0900 +++ b/lib/Support/BranchProbability.cpp Tue Oct 13 17:48:58 2015 +0900 @@ -15,17 +15,42 @@ #include "llvm/Support/Debug.h" #include "llvm/Support/Format.h" #include "llvm/Support/raw_ostream.h" +#include <cassert> using namespace llvm; +const uint32_t BranchProbability::D; + raw_ostream &BranchProbability::print(raw_ostream &OS) const { - return OS << N << " / " << D << " = " - << format("%g%%", ((double)N / D) * 100.0); + // Get a percentage rounded to two decimal digits. This avoids + // implementation-defined rounding inside printf. + double Percent = rint(((double)N / D) * 100.0 * 100.0) / 100.0; + OS << format("0x%08" PRIx32 " / 0x%08" PRIx32 " = %.2f%%", N, D, Percent); + return OS; } void BranchProbability::dump() const { print(dbgs()) << '\n'; } +BranchProbability::BranchProbability(uint32_t Numerator, uint32_t Denominator) { + assert(Denominator > 0 && "Denominator cannot be 0!"); + assert(Numerator <= Denominator && "Probability cannot be bigger than 1!"); + if (Denominator == D) + N = Numerator; + else { + uint64_t Prob64 = + (Numerator * static_cast<uint64_t>(D) + Denominator / 2) / Denominator; + N = static_cast<uint32_t>(Prob64); + } +} + +// If ConstD is not zero, then replace D by ConstD so that division and modulo +// operations by D can be optimized, in case this function is not inlined by the +// compiler. +template <uint32_t ConstD> static uint64_t scale(uint64_t Num, uint32_t N, uint32_t D) { + if (ConstD > 0) + D = ConstD; + assert(D && "divide by 0"); // Fast path for multiplying by 1.0. @@ -65,9 +90,9 @@ } uint64_t BranchProbability::scale(uint64_t Num) const { - return ::scale(Num, N, D); + return ::scale<D>(Num, N, D); } uint64_t BranchProbability::scaleByInverse(uint64_t Num) const { - return ::scale(Num, D, N); + return ::scale<0>(Num, D, N); }