[llvm] r248665 - [BranchProbability] Manually round the floating point output.
Benjamin Kramer via llvm-commits
llvm-commits at lists.llvm.org
Sat Sep 26 03:09:38 PDT 2015
Author: d0k
Date: Sat Sep 26 05:09:36 2015
New Revision: 248665
URL: http://llvm.org/viewvc/llvm-project?rev=248665&view=rev
Log:
[BranchProbability] Manually round the floating point output.
llvm::format compiles down to snprintf which has no defined rounding for
floating point arguments, and MSVC has implemented it differently from
what the BSD libcs and glibc do. Try to emulate the glibc rounding
behavior to avoid changing tests.
While there simplify code a bit and move trivial methods inline.
Modified:
llvm/trunk/include/llvm/Support/BranchProbability.h
llvm/trunk/lib/Support/BranchProbability.cpp
Modified: llvm/trunk/include/llvm/Support/BranchProbability.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/BranchProbability.h?rev=248665&r1=248664&r2=248665&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/BranchProbability.h (original)
+++ llvm/trunk/include/llvm/Support/BranchProbability.h Sat Sep 26 05:09:36 2015
@@ -79,8 +79,20 @@ public:
/// \return \c Num divided by \c this.
uint64_t scaleByInverse(uint64_t Num) const;
- BranchProbability &operator+=(BranchProbability RHS);
- BranchProbability &operator-=(BranchProbability RHS);
+ BranchProbability &operator+=(BranchProbability RHS) {
+ assert(N <= D - RHS.N &&
+ "The sum of branch probabilities should not exceed one!");
+ N += RHS.N;
+ return *this;
+ }
+
+ BranchProbability &operator-=(BranchProbability RHS) {
+ assert(N >= RHS.N &&
+ "Can only subtract a smaller probability from a larger one!");
+ N -= RHS.N;
+ return *this;
+ }
+
BranchProbability &operator*=(BranchProbability RHS) {
N = (static_cast<uint64_t>(N) * RHS.N + D / 2) / D;
return *this;
Modified: llvm/trunk/lib/Support/BranchProbability.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/BranchProbability.cpp?rev=248665&r1=248664&r2=248665&view=diff
==============================================================================
--- llvm/trunk/lib/Support/BranchProbability.cpp (original)
+++ llvm/trunk/lib/Support/BranchProbability.cpp Sat Sep 26 05:09:36 2015
@@ -19,20 +19,13 @@
using namespace llvm;
+const uint32_t BranchProbability::D;
+
raw_ostream &BranchProbability::print(raw_ostream &OS) const {
- auto GetHexDigit = [](int Val) -> char {
- assert(Val < 16);
- if (Val < 10)
- return '0' + Val;
- return 'a' + Val - 10;
- };
- OS << "0x";
- for (int Digits = 0; Digits < 8; ++Digits)
- OS << GetHexDigit(N >> (28 - Digits * 4) & 0xf);
- OS << " / 0x";
- for (int Digits = 0; Digits < 8; ++Digits)
- OS << GetHexDigit(D >> (28 - Digits * 4) & 0xf);
- OS << " = " << format("%.2f%%", ((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;
}
@@ -50,25 +43,11 @@ BranchProbability::BranchProbability(uin
}
}
-BranchProbability &BranchProbability::operator+=(BranchProbability RHS) {
- assert(N <= D - RHS.N &&
- "The sum of branch probabilities should not exceed one!");
- N += RHS.N;
- return *this;
-}
-
-BranchProbability &BranchProbability::operator-=(BranchProbability RHS) {
- assert(N >= RHS.N &&
- "Can only subtract a smaller probability from a larger one!");
- N -= RHS.N;
- return *this;
-}
-
// 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>
-inline uint64_t scale(uint64_t Num, uint32_t N, uint32_t D) {
+static uint64_t scale(uint64_t Num, uint32_t N, uint32_t D) {
if (ConstD > 0)
D = ConstD;
More information about the llvm-commits
mailing list