-void BranchProbability::print(raw_ostream &OS) const {
- OS << N << " / " << D << " = " << format("%g%%", ((double)N / D) * 100.0);
+const uint32_t BranchProbability::D;
+
+raw_ostream &BranchProbability::print(raw_ostream &OS) const {
+ if (isUnknown())
+ return OS << "?%";
+
+ // 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;
+ return OS << format("0x%08" PRIx32 " / 0x%08" PRIx32 " = %.2f%%", N, D,
+ Percent);
+}
+
+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);
+ }