https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93727
--- Comment #33 from Jerry DeLisle <jvdelisle at gcc dot gnu.org> --- Created attachment 64540 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=64540&action=edit Proposed patch to handle rounding of hexadecimal formats >From a31c9ee1433e2f5813985ca9ba2d660ffcdfe995 Mon Sep 17 00:00:00 2001 From: Jerry DeLisle <[email protected]> Date: Sat, 23 May 2026 20:28:43 -0700 Subject: [PATCH] Fortran: [PR93727] Add EX format rounding for truncated hex mantissa MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. 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. 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. Assisted by: Claude Sonnet 4.6
