Take number 2. I moved the adjustment to the exponent into __pformat_xldouble.
>From b5a399f21cb15f5d3a38b312c7889adede225ff6 Mon Sep 17 00:00:00 2001
From: Patrick Northon <[email protected]>
Date: Tue, 7 Sep 2021 12:11:20 -0400
Subject: [PATCH] Fix exponent when formatting long double in hexadecimal.

Some denormalized values were emited incorrectly due to incorrect conditional adjustement of the exponent.
---
 mingw-w64-crt/stdio/mingw_pformat.c | 20 +++-----------------
 1 file changed, 3 insertions(+), 17 deletions(-)

diff --git a/mingw-w64-crt/stdio/mingw_pformat.c b/mingw-w64-crt/stdio/mingw_pformat.c
index b2e1af6ec..c38847442 100644
--- a/mingw-w64-crt/stdio/mingw_pformat.c
+++ b/mingw-w64-crt/stdio/mingw_pformat.c
@@ -2045,7 +2045,7 @@ void __pformat_emit_xfloat( __pformat_fpreg_t value, __pformat_t *stream )
        * of this is equivalent to an increment of the exponent. We will
        * discard a whole digit to match glibc's behavior.
        */
-      value.__pformat_fpreg_exponent++;
+      value.__pformat_fpreg_exponent += 4;
       value.__pformat_fpreg_mantissa >>= 3;
     }
 
@@ -2086,20 +2086,6 @@ void __pformat_emit_xfloat( __pformat_fpreg_t value, __pformat_t *stream )
            */
           *p++ = '.';
         }
-
-        /* If the most significant hexadecimal digit of the encoded
-         * output value is greater than one, then the indicated value
-         * will appear too large, by an additional binary exponent
-         * corresponding to the number of higher order bit positions
-         * which it occupies...
-         */
-        while( value.__pformat_fpreg_mantissa > 1 )
-        {
-          /* so reduce the exponent value to compensate...
-           */
-          value.__pformat_fpreg_exponent--;
-          value.__pformat_fpreg_mantissa >>= 1;
-        }
       }
 
       else if( stream->precision > 0 )
@@ -2305,14 +2291,14 @@ void __pformat_xldouble( long double x, __pformat_t *stream )
         {
           /* ...this mantissa represents a subnormal value.
            */
-          z.__pformat_fpreg_exponent = -0x3FFF - 2;
+          z.__pformat_fpreg_exponent = 1 - 0x3FFF - 3;
         }
       }
       else
         /* This argument represents a non-zero normal number;
          * eliminate the bias from the exponent...
          */
-        z.__pformat_fpreg_exponent -= 0x3FFF;
+        z.__pformat_fpreg_exponent -= 0x3FFF + 3;
 
       /* Finally, hand the adjusted representation off to the
        * generalised hexadecimal floating point format handler...
-- 
2.33.0

_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to