GCC Newspaper
JUNE 15, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
fortran

Fortran: Add EX Format Rounding for Truncated Hex Mantissa

Implements proper rounding of the hex mantissa in Fortran's EX format, supporting all rounding modes and normalization.

This commit enhances Fortran’s EX format to correctly round the hexadecimal mantissa when the user specifies a precision smaller than the full precision. It implements all Fortran rounding modes, including ties-to-even, ties away from zero, round up, round down, and round to zero. The implementation also handles carry propagation and normalizes the output by adjusting the exponent when incrementing a mantissa causes it to overflow.

In Details

This commit addresses PR93727 by implementing proper rounding for truncated hex mantissas in Fortran's write_ex function. The changes involve modifying io/write.c to incorporate rounding-aware logic based on the dtp round_status. Carry propagation and integer-digit normalization are included. The commit introduces new tests gfortran.dg/EXformat_3.F90 and gfortran.dg/EXrounding.F90 to verify the functionality.

For Context

When Fortran writes a floating-point number in hexadecimal (EX) format, it needs to handle cases where the user asks for fewer digits than the full precision of the number. This commit ensures that the output is correctly rounded according to the specified rounding mode (e.g., rounding to the nearest even number). It also manages the complexities of carry propagation when rounding increases the value of the mantissa, potentially requiring normalization (adjusting the exponent).

Filed Under: fortranroundingioformatting