GCC Newspaper
OCTOBER 6, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
profile-count

profile-count: Handle signed values in profile_probability::apply

Profile probability calculations now correctly handle signed inputs, preventing overflow and ensuring accurate rounding.

This commit fixes an issue in profile_probability::apply where signed input values were not handled correctly, leading to incorrect rounding and potential overflow. The function uses division, and the rounding behavior for negative numbers was problematic. The fix scales the magnitude of the signed value using safe_scale_64bit and then restores the sign, ensuring correct results. It also preserves the sign for unit probabilities and handles uninitialized probabilities as before. New selftests verify the corrected behavior.

In Details

The profile_probability::apply function in profile-count.h previously handled signed gcov_type inputs poorly due to the RDIV instruction's rounding behavior on negative numbers. This commit modifies the function to scale the unsigned magnitude of the input using safe_scale_64bit, then reapply the original sign. This ensures correct rounding for negative inputs and prevents overflow during multiplication with the fixed-point probability. Unit probabilities now return the input directly to maintain representability of minimum gcov_type values. Selftests are added to cover signed rou…

For Context
profile-count
A subsystem within GCC responsible for handling and processing profile data, typically gathered during program execution for optimization purposes (e.g., PGO - Profile Guided Optimization).
profile_probability::apply
A method within the profile-count system that applies a calculated probability (often derived from profile data) to an estimated cost or value.
gcov_type
An integer type used by GCC's coverage tool (gcov) to store counts and other profile-related data. It is typically a 64-bit integer.
RDIV
A division instruction, often referring to a specific machine instruction or an internal compiler representation for division that includes rounding logic.
PGO
Profile-Guided Optimization. A compiler optimization technique where the compiler uses runtime execution profiles collected from previous runs of the program to make more informed optimization decisions.
Filed Under: profile-countPGObugfixsigned values