diff options
author | orivej <orivej@yandex-team.ru> | 2022-02-10 16:44:49 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:44:49 +0300 |
commit | 718c552901d703c502ccbefdfc3c9028d608b947 (patch) | |
tree | 46534a98bbefcd7b1f3faa5b52c138ab27db75b7 /contrib/libs/llvm12/lib/Support/BranchProbability.cpp | |
parent | e9656aae26e0358d5378e5b63dcac5c8dbe0e4d0 (diff) | |
download | ydb-718c552901d703c502ccbefdfc3c9028d608b947.tar.gz |
Restoring authorship annotation for <orivej@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'contrib/libs/llvm12/lib/Support/BranchProbability.cpp')
-rw-r--r-- | contrib/libs/llvm12/lib/Support/BranchProbability.cpp | 224 |
1 files changed, 112 insertions, 112 deletions
diff --git a/contrib/libs/llvm12/lib/Support/BranchProbability.cpp b/contrib/libs/llvm12/lib/Support/BranchProbability.cpp index 60d5478a90..9bba1b8f70 100644 --- a/contrib/libs/llvm12/lib/Support/BranchProbability.cpp +++ b/contrib/libs/llvm12/lib/Support/BranchProbability.cpp @@ -1,112 +1,112 @@ -//===-------------- lib/Support/BranchProbability.cpp -----------*- C++ -*-===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// -// -// This file implements Branch Probability class. -// -//===----------------------------------------------------------------------===// - -#include "llvm/Support/BranchProbability.h" -#include "llvm/Config/llvm-config.h" -#include "llvm/Support/Debug.h" -#include "llvm/Support/Format.h" -#include "llvm/Support/raw_ostream.h" -#include <cassert> - -using namespace llvm; - -constexpr 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); -} - -#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) -LLVM_DUMP_METHOD void BranchProbability::dump() const { print(dbgs()) << '\n'; } -#endif - -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); - } -} - -BranchProbability -BranchProbability::getBranchProbability(uint64_t Numerator, - uint64_t Denominator) { - assert(Numerator <= Denominator && "Probability cannot be bigger than 1!"); - // Scale down Denominator to fit in a 32-bit integer. - int Scale = 0; - while (Denominator > UINT32_MAX) { - Denominator >>= 1; - Scale++; - } - return BranchProbability(Numerator >> Scale, Denominator); -} - -// 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. - if (!Num || D == N) - return Num; - - // Split Num into upper and lower parts to multiply, then recombine. - uint64_t ProductHigh = (Num >> 32) * N; - uint64_t ProductLow = (Num & UINT32_MAX) * N; - - // Split into 32-bit digits. - uint32_t Upper32 = ProductHigh >> 32; - uint32_t Lower32 = ProductLow & UINT32_MAX; - uint32_t Mid32Partial = ProductHigh & UINT32_MAX; - uint32_t Mid32 = Mid32Partial + (ProductLow >> 32); - - // Carry. - Upper32 += Mid32 < Mid32Partial; - - uint64_t Rem = (uint64_t(Upper32) << 32) | Mid32; - uint64_t UpperQ = Rem / D; - - // Check for overflow. - if (UpperQ > UINT32_MAX) - return UINT64_MAX; - - Rem = ((Rem % D) << 32) | Lower32; - uint64_t LowerQ = Rem / D; - uint64_t Q = (UpperQ << 32) + LowerQ; - - // Check for overflow. - return Q < LowerQ ? UINT64_MAX : Q; -} - -uint64_t BranchProbability::scale(uint64_t Num) const { - return ::scale<D>(Num, N, D); -} - -uint64_t BranchProbability::scaleByInverse(uint64_t Num) const { - return ::scale<0>(Num, D, N); -} +//===-------------- lib/Support/BranchProbability.cpp -----------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file implements Branch Probability class. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Support/BranchProbability.h" +#include "llvm/Config/llvm-config.h" +#include "llvm/Support/Debug.h" +#include "llvm/Support/Format.h" +#include "llvm/Support/raw_ostream.h" +#include <cassert> + +using namespace llvm; + +constexpr 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); +} + +#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) +LLVM_DUMP_METHOD void BranchProbability::dump() const { print(dbgs()) << '\n'; } +#endif + +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); + } +} + +BranchProbability +BranchProbability::getBranchProbability(uint64_t Numerator, + uint64_t Denominator) { + assert(Numerator <= Denominator && "Probability cannot be bigger than 1!"); + // Scale down Denominator to fit in a 32-bit integer. + int Scale = 0; + while (Denominator > UINT32_MAX) { + Denominator >>= 1; + Scale++; + } + return BranchProbability(Numerator >> Scale, Denominator); +} + +// 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. + if (!Num || D == N) + return Num; + + // Split Num into upper and lower parts to multiply, then recombine. + uint64_t ProductHigh = (Num >> 32) * N; + uint64_t ProductLow = (Num & UINT32_MAX) * N; + + // Split into 32-bit digits. + uint32_t Upper32 = ProductHigh >> 32; + uint32_t Lower32 = ProductLow & UINT32_MAX; + uint32_t Mid32Partial = ProductHigh & UINT32_MAX; + uint32_t Mid32 = Mid32Partial + (ProductLow >> 32); + + // Carry. + Upper32 += Mid32 < Mid32Partial; + + uint64_t Rem = (uint64_t(Upper32) << 32) | Mid32; + uint64_t UpperQ = Rem / D; + + // Check for overflow. + if (UpperQ > UINT32_MAX) + return UINT64_MAX; + + Rem = ((Rem % D) << 32) | Lower32; + uint64_t LowerQ = Rem / D; + uint64_t Q = (UpperQ << 32) + LowerQ; + + // Check for overflow. + return Q < LowerQ ? UINT64_MAX : Q; +} + +uint64_t BranchProbability::scale(uint64_t Num) const { + return ::scale<D>(Num, N, D); +} + +uint64_t BranchProbability::scaleByInverse(uint64_t Num) const { + return ::scale<0>(Num, D, N); +} |