PR #26285 opened by Baptiste Coudurier (bcoudurier)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26285
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26285.patch

On x87, `%f` double rounded near float midpoints: 0.4999999850988388 gave 
0x3f000000 instead of 0x3effffff.
decfloat() now rounds in integer arithmetic. Doubles are unchanged with SSE and 
-std=c17; the new fate-avsscanf test passes on a 32-bit x87 build.

🤖 Generated with [Claude Code](https://claude.com/claude-code)



From f0dc9c65329bbbe6765e855aa48d7ce02a5215cf Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Sun, 22 Nov 2026 22:34:48 -0800
Subject: [PATCH] avutil/avsscanf: round decimal input in integer arithmetic

Excess x87 precision double rounded %f near float midpoints.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavutil/Makefile         |  1 +
 libavutil/avsscanf.c       | 56 +++++++++++++++----------------
 libavutil/tests/.gitignore |  1 +
 libavutil/tests/avsscanf.c | 67 ++++++++++++++++++++++++++++++++++++++
 tests/fate/libavutil.mak   |  4 +++
 tests/ref/fate/avsscanf    | 24 ++++++++++++++
 6 files changed, 124 insertions(+), 29 deletions(-)
 create mode 100644 libavutil/tests/avsscanf.c
 create mode 100644 tests/ref/fate/avsscanf

diff --git a/libavutil/Makefile b/libavutil/Makefile
index 1f91fd5d170..8b650eb53cc 100644
--- a/libavutil/Makefile
+++ b/libavutil/Makefile
@@ -264,6 +264,7 @@ TESTPROGS = adler32                                         
            \
             ambient_viewing_environment                                 \
             atomic                                                      \
             audio_fifo                                                  \
+            avsscanf                                                    \
             avstring                                                    \
             base64                                                      \
             blowfish                                                    \
diff --git a/libavutil/avsscanf.c b/libavutil/avsscanf.c
index 0a49295e765..10d5223dc83 100644
--- a/libavutil/avsscanf.c
+++ b/libavutil/avsscanf.c
@@ -254,9 +254,9 @@ static double decfloat(FFFILE *f, int c, int bits, int 
emin, int sign, int pok)
     int e2;
     int emax = -emin-bits+3;
     int denormal = 0;
+    int tail = 0, inexact, shift;
+    uint64_t m;
     double y;
-    double frac=0;
-    double bias=0;
     static const int p10s[] = { 10, 100, 1000, 10000,
         100000, 1000000, 10000000, 100000000 };
 
@@ -436,14 +436,12 @@ static double decfloat(FFFILE *f, int c, int bits, int 
emin, int sign, int pok)
         }
     }
 
-    /* Assemble desired bits into floating point variable */
-    for (y=i=0; i<LD_B1B_DIG; i++) {
+    /* Assemble desired bits into an integer */
+    for (m=i=0; i<LD_B1B_DIG; i++) {
         if ((a+i & MASK)==z) x[(z=(z+1 & MASK))-1 & MASK] = 0;
-        y = 1000000000.0L * y + x[a+i & MASK];
+        m = 1000000000 * m + x[a+i & MASK];
     }
 
-    y *= sign;
-
     /* Limit precision for denormal results */
     if (bits > DBL_MANT_DIG+e2-emin) {
         bits = DBL_MANT_DIG+e2-emin;
@@ -451,33 +449,33 @@ static double decfloat(FFFILE *f, int c, int bits, int 
emin, int sign, int pok)
         denormal = 1;
     }
 
-    /* Calculate bias term to force rounding, move out lower bits */
-    if (bits < DBL_MANT_DIG) {
-        bias = copysign(scalbn(1, 2*DBL_MANT_DIG-bits-1), y);
-        frac = fmod(y, scalbn(1, DBL_MANT_DIG-bits));
-        y -= frac;
-        y += bias;
-    }
-
-    /* Process tail of decimal input so it can affect rounding */
+    /* Classify the tail of decimal input: 0 zero, 1 below half, 2 half, 3 
above half */
     if ((a+i & MASK) != z) {
         uint32_t t = x[a+i & MASK];
         if (t < 500000000 && (t || (a+i+1 & MASK) != z))
-            frac += 0.25*sign;
+            tail = 1;
         else if (t > 500000000)
-            frac += 0.75*sign;
-        else if (t == 500000000) {
-            if ((a+i+1 & MASK) == z)
-                frac += 0.5*sign;
-            else
-                frac += 0.75*sign;
-        }
-        if (DBL_MANT_DIG-bits >= 2 && !fmod(frac, 1))
-            frac++;
+            tail = 3;
+        else if (t == 500000000)
+            tail = (a+i+1 & MASK) == z ? 2 : 3;
     }
 
-    y += frac;
-    y -= bias;
+    /* Round to nearest even in integer arithmetic, so that excess
+     * floating point precision cannot cause double rounding */
+    shift = DBL_MANT_DIG - bits;
+    if (shift) {
+        uint64_t rem  = m & ((1ULL << shift) - 1);
+        uint64_t half = 1ULL << (shift - 1);
+        inexact = rem || tail;
+        m >>= shift;
+        if (rem > half || (rem == half && (tail || (m & 1))))
+            m++;
+    } else {
+        inexact = tail;
+        if (tail == 3 || (tail == 2 && (m & 1)))
+            m++;
+    }
+    y = sign * (double)(m << shift);
 
     if ((e2+DBL_MANT_DIG & INT_MAX) > emax-5) {
         if (fabs(y) >= pow(2, DBL_MANT_DIG)) {
@@ -486,7 +484,7 @@ static double decfloat(FFFILE *f, int c, int bits, int 
emin, int sign, int pok)
             y *= 0.5;
             e2++;
         }
-        if (e2+DBL_MANT_DIG>emax || (denormal && frac))
+        if (e2+DBL_MANT_DIG>emax || (denormal && inexact))
             errno = ERANGE;
     }
 
diff --git a/libavutil/tests/.gitignore b/libavutil/tests/.gitignore
index ac300592490..172ca9a8147 100644
--- a/libavutil/tests/.gitignore
+++ b/libavutil/tests/.gitignore
@@ -4,6 +4,7 @@
 /ambient_viewing_environment
 /atomic
 /audio_fifo
+/avsscanf
 /avstring
 /base64
 /blowfish
diff --git a/libavutil/tests/avsscanf.c b/libavutil/tests/avsscanf.c
new file mode 100644
index 00000000000..71226f6b8ee
--- /dev/null
+++ b/libavutil/tests/avsscanf.c
@@ -0,0 +1,67 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <inttypes.h>
+#include <stdio.h>
+
+#include "libavutil/avstring.h"
+#include "libavutil/intfloat.h"
+#include "libavutil/macros.h"
+
+static const char *const tests[] = {
+    /* float midpoints: exact, just above, just below */
+    "0.4999999850988388",
+    "0.49999998509883880615234375",
+    "0.499999985098838806152343750000001",
+    "0.499999985098838806152343749999999",
+    "0.9999999701976776123046875",
+    "0.99999997019767761230468749999999",
+    "1.000000059604644775390625",
+    "1.0000000596046447753906250000001",
+    "1.0000000596046447753906249999999",
+    "1.000000178813934326171875",
+    "1.0000001788139343261718749999999",
+    "16777215.5",
+    "16777215.49999999",
+    "16777216.99999999",
+    /* float denormal midpoints */
+    
"2.101947696487225606385594374934874196920392912814773657635602425834686624028790902229957282543182373046875e-45",
+    
"2.1019476964872256063855943749348741969203929128147736576356024258346866240287909022299572825431823730468749999999e-45",
+    
"1.1754942807573642917278829910357665133228589927589904276829631184250030649651730385585324256680905818939208984375e-38",
+    
"1.17549428075736429172788299103576651332285899275899042768296311842500306496517303855853242566809058189392089843749999999e-38",
+    /* doubles */
+    "0.1",
+    "9007199254740993",
+    "9007199254740993.0000000001",
+    "2.2250738585072011e-308",
+    "4.9e-324",
+    "1.7976931348623157e308",
+};
+
+int main(void)
+{
+    for (int i = 0; i < FF_ARRAY_ELEMS(tests); i++) {
+        float f = -1;
+        double d = -1;
+        int ret = av_sscanf(tests[i], "%f", &f);
+        ret += av_sscanf(tests[i], "%lf", &d);
+        printf("%d %08"PRIx32" %016"PRIx64" %s\n", ret,
+               av_float2int(f), av_double2int(d), tests[i]);
+    }
+    return 0;
+}
diff --git a/tests/fate/libavutil.mak b/tests/fate/libavutil.mak
index fa55ebdbe36..bed70d8c598 100644
--- a/tests/fate/libavutil.mak
+++ b/tests/fate/libavutil.mak
@@ -39,6 +39,10 @@ FATE_LIBAVUTIL += fate-audio_fifo
 fate-audio_fifo: libavutil/tests/audio_fifo$(EXESUF)
 fate-audio_fifo: CMD = run libavutil/tests/audio_fifo$(EXESUF)
 
+FATE_LIBAVUTIL += fate-avsscanf
+fate-avsscanf: libavutil/tests/avsscanf$(EXESUF)
+fate-avsscanf: CMD = run libavutil/tests/avsscanf$(EXESUF)
+
 FATE_LIBAVUTIL += fate-avstring
 fate-avstring: libavutil/tests/avstring$(EXESUF)
 fate-avstring: CMD = run libavutil/tests/avstring$(EXESUF)
diff --git a/tests/ref/fate/avsscanf b/tests/ref/fate/avsscanf
new file mode 100644
index 00000000000..50ce4e4e227
--- /dev/null
+++ b/tests/ref/fate/avsscanf
@@ -0,0 +1,24 @@
+2 3effffff 3fdffffff0000000 0.4999999850988388
+2 3f000000 3fdffffff0000000 0.49999998509883880615234375
+2 3f000000 3fdffffff0000000 0.499999985098838806152343750000001
+2 3effffff 3fdffffff0000000 0.499999985098838806152343749999999
+2 3f800000 3feffffff0000000 0.9999999701976776123046875
+2 3f7fffff 3feffffff0000000 0.99999997019767761230468749999999
+2 3f800000 3ff0000010000000 1.000000059604644775390625
+2 3f800001 3ff0000010000000 1.0000000596046447753906250000001
+2 3f800000 3ff0000010000000 1.0000000596046447753906249999999
+2 3f800002 3ff0000030000000 1.000000178813934326171875
+2 3f800001 3ff0000030000000 1.0000001788139343261718749999999
+2 4b800000 416ffffff0000000 16777215.5
+2 4b7fffff 416fffffeffffffb 16777215.49999999
+2 4b800000 417000000ffffffd 16777216.99999999
+2 00000002 36a8000000000000 
2.101947696487225606385594374934874196920392912814773657635602425834686624028790902229957282543182373046875e-45
+2 00000001 36a8000000000000 
2.1019476964872256063855943749348741969203929128147736576356024258346866240287909022299572825431823730468749999999e-45
+2 00800000 380fffffe0000000 
1.1754942807573642917278829910357665133228589927589904276829631184250030649651730385585324256680905818939208984375e-38
+2 007fffff 380fffffe0000000 
1.17549428075736429172788299103576651332285899275899042768296311842500306496517303855853242566809058189392089843749999999e-38
+2 3dcccccd 3fb999999999999a 0.1
+2 5a000000 4340000000000000 9007199254740993
+2 5a000000 4340000000000001 9007199254740993.0000000001
+2 00000000 000fffffffffffff 2.2250738585072011e-308
+2 00000000 0000000000000001 4.9e-324
+2 7f800000 7fefffffffffffff 1.7976931348623157e308
-- 
2.54.1

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to