https://gcc.gnu.org/g:ee3f1197b6f2889276edd2dca956eaeec3449ee6

commit r16-7877-gee3f1197b6f2889276edd2dca956eaeec3449ee6
Author: Richard Biener <[email protected]>
Date:   Mon Feb 2 15:29:44 2026 +0100

    middle-end/45273 - avoid host double in profiling
    
    The following replaces the last host double computation by using
    int64_t instead to avoid overflow of 32bit (but capped to
    REG_BR_PROB_BASE) values.
    
            PR middle-end/45273
            * predict.cc (combine_predictions_for_insn): Use int64_t
            math instead of double.

Diff:
---
 gcc/predict.cc | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/gcc/predict.cc b/gcc/predict.cc
index de8f9c2211f5..2859f64964b7 100644
--- a/gcc/predict.cc
+++ b/gcc/predict.cc
@@ -1046,13 +1046,14 @@ combine_predictions_for_insn (rtx_insn *insn, 
basic_block bb)
             + (REG_BR_PROB_BASE - combined_probability)
             * (REG_BR_PROB_BASE - probability));
 
-       /* Use FP math to avoid overflows of 32bit integers.  */
+       /* Use int64_t math to avoid overflows of 32bit integers.  */
        if (d == 0)
          /* If one probability is 0% and one 100%, avoid division by zero.  */
          combined_probability = REG_BR_PROB_BASE / 2;
        else
-         combined_probability = (((double) combined_probability) * probability
-                                 * REG_BR_PROB_BASE / d + 0.5);
+         combined_probability = ((((int64_t) combined_probability)
+                                  * probability
+                                  * REG_BR_PROB_BASE + (d / 2)) / d);
       }
 
   /* Decide which heuristic to use.  In case we didn't match anything,
@@ -1399,14 +1400,14 @@ combine_predictions_for_bb (basic_block bb, bool 
dry_run)
               + (REG_BR_PROB_BASE - combined_probability)
               * (REG_BR_PROB_BASE - probability));
 
-         /* Use FP math to avoid overflows of 32bit integers.  */
+         /* Use int64_t math to avoid overflows of 32bit integers.  */
          if (d == 0)
            /* If one probability is 0% and one 100%, avoid division by zero.  
*/
            combined_probability = REG_BR_PROB_BASE / 2;
          else
-           combined_probability = (((double) combined_probability)
-                                   * probability
-                                   * REG_BR_PROB_BASE / d + 0.5);
+           combined_probability = ((((int64_t) combined_probability)
+                                    * probability
+                                    * REG_BR_PROB_BASE + (d / 2)) / d);
        }
     }

Reply via email to