From fe335593152fd96bbfd970541a89add763c887a4 Mon Sep 17 00:00:00 2001 From: Gilles Grospellier Date: Sat, 1 Oct 2022 18:32:12 +0200 Subject: [PATCH] [JIT] Fix potential division by zero in fgopt.cpp (#76424) Division by zero in C++ is undefined behavior on some architecture and should be avoided. Co-authored-by: Andy Ayers --- src/coreclr/jit/fgopt.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/coreclr/jit/fgopt.cpp b/src/coreclr/jit/fgopt.cpp index bbd294684de05..ab50d166a7a5d 100644 --- a/src/coreclr/jit/fgopt.cpp +++ b/src/coreclr/jit/fgopt.cpp @@ -4988,10 +4988,10 @@ bool Compiler::fgReorderBlocks(bool useProfile) double notTakenCount = ((double)edgeToBlock->edgeWeightMin() + (double)edgeToBlock->edgeWeightMax()) / 2.0; double totalCount = takenCount + notTakenCount; - double takenRatio = takenCount / totalCount; - // If the takenRatio is greater or equal to 51% then we will reverse the branch - if (takenRatio < 0.51) + // If the takenRatio (takenCount / totalCount) is greater or equal to 51% then we will reverse + // the branch + if (takenCount < (0.51 * totalCount)) { reorderBlock = false; }