Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.0 KiB

  1. //===- BranchProbability.h - Branch Probability Wrapper ---------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // Definition of BranchProbability shared by IR and Machine Instructions.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_SUPPORT_BRANCHPROBABILITY_H
  14. #define LLVM_SUPPORT_BRANCHPROBABILITY_H
  15. #include "llvm/Support/DataTypes.h"
  16. #include <cassert>
  17. namespace llvm {
  18. class raw_ostream;
  19. // This class represents Branch Probability as a non-negative fraction.
  20. class BranchProbability {
  21. // Numerator
  22. uint32_t N;
  23. // Denominator
  24. uint32_t D;
  25. public:
  26. BranchProbability(uint32_t n, uint32_t d) : N(n), D(d) {
  27. assert(d > 0 && "Denomiator cannot be 0!");
  28. assert(n <= d && "Probability cannot be bigger than 1!");
  29. }
  30. static BranchProbability getZero() { return BranchProbability(0, 1); }
  31. static BranchProbability getOne() { return BranchProbability(1, 1); }
  32. uint32_t getNumerator() const { return N; }
  33. uint32_t getDenominator() const { return D; }
  34. // Return (1 - Probability).
  35. BranchProbability getCompl() const {
  36. return BranchProbability(D - N, D);
  37. }
  38. void print(raw_ostream &OS) const;
  39. void dump() const;
  40. bool operator==(BranchProbability RHS) const {
  41. return (uint64_t)N * RHS.D == (uint64_t)D * RHS.N;
  42. }
  43. bool operator!=(BranchProbability RHS) const {
  44. return !(*this == RHS);
  45. }
  46. bool operator<(BranchProbability RHS) const {
  47. return (uint64_t)N * RHS.D < (uint64_t)D * RHS.N;
  48. }
  49. bool operator>(BranchProbability RHS) const {
  50. return RHS < *this;
  51. }
  52. bool operator<=(BranchProbability RHS) const {
  53. return (uint64_t)N * RHS.D <= (uint64_t)D * RHS.N;
  54. }
  55. bool operator>=(BranchProbability RHS) const {
  56. return RHS <= *this;
  57. }
  58. };
  59. raw_ostream &operator<<(raw_ostream &OS, const BranchProbability &Prob);
  60. }
  61. #endif