]> git.ipfire.org Git - thirdparty/gcc.git/commit
Fortran: [PR93727] Add EX format rounding for truncated hex mantissa
authorJerry DeLisle <jvdelisle@gcc.gnu.org>
Sun, 24 May 2026 03:28:43 +0000 (20:28 -0700)
committerJerry DeLisle <jvdelisle@gcc.gnu.org>
Thu, 28 May 2026 00:37:58 +0000 (17:37 -0700)
commitaacccc01d2bb3dd76ee37281b9f2f04aa9c13f5c
treefdf06836deaddc768e0e12f6561795a433cb6e10
parent9769e8a450ff3d5998d8023818e22c656d3a7b14
Fortran: [PR93727] Add EX format rounding for truncated hex mantissa

Implement proper rounding of the hex mantissa in write_ex when the
user specifies a d smaller than full precision.  All Fortran ROUND=
modes are supported: ROUND_NEAREST (ties-to-even), ROUND_COMPATIBLE
(ties away from zero), ROUND_UP, ROUND_DOWN, and ROUND_ZERO.
ROUND_PROCDEFINED and ROUND_UNSPECIFIED default to ROUND_NEAREST on
IEEE 754 systems, consistent with the decimal format behaviour.

Carry propagation handles the case where incrementing a string of
trailing F hex digits reaches the integer digit; if that overflows
(F → 16) the output is normalized by setting the integer digit to 8
and incrementing the binary exponent by one.

Assisted by: Claude Sonnet 4.6

PR fortran/93727

libgfortran/ChangeLog:

* io/write.c (write_ex): Replace simple truncation with
rounding-aware logic respecting dtp round_status.  Add carry
propagation and integer-digit normalization.
* io/write_float.def: Change use of GFC_UINTEGER_8 to
long long unsigned.

gcc/testsuite/ChangeLog:

* gfortran.dg/EXformat_3.F90: New test covering rounding for
KIND=4, 8, 10, and 16: clear round-up, ties-to-even (truncate
and round-up cases), carry propagation, and normalization.
* gfortran.dg/EXrounding.F90: New test checking the various
rounding modes for all kinds.
gcc/testsuite/gfortran.dg/EXformat_3.F90 [new file with mode: 0644]
gcc/testsuite/gfortran.dg/EXrounding.F90 [new file with mode: 0644]
libgfortran/io/write.c
libgfortran/io/write_float.def