Commit 8f4b37664a for ffmpeg

commit 8f4b37664a7b57f3466220365a6c045e66d39b7d
Author: Baptiste Coudurier <baptiste.coudurier@gmail.com>
Date:   Sun Nov 22 22:34:48 2026 -0800

    avutil/avsscanf: round decimal and hexadecimal input once

    On x87, %f double-rounded 0.4999999850988388 to 0x3f000000
    instead of 0x3effffff.

    Classify the discarded digits with integers, then round with one
    float conversion in its rounding mode. Preserve signed zero and
    set ERANGE for inexact subnormal or zero results and for overflow,
    including overflow from a rounding carry.

    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
    Signed-off-by: Baptiste Coudurier <baptiste.coudurier@gmail.com>
    Assisted-by: Fairy

diff --git a/libavutil/avsscanf.c b/libavutil/avsscanf.c
index 0a49295e76..b3c7085e23 100644
--- a/libavutil/avsscanf.c
+++ b/libavutil/avsscanf.c
@@ -241,6 +241,64 @@ static long long scanexp(FFFILE *f, int pok)
 #define KMAX 128
 #define MASK (KMAX-1)

+static double signed_zero(int sign)
+{
+    /* Keep copysign from folding away the sign with -fno-signed-zeros. */
+    volatile double zero = 0.0;
+    return copysign(zero, sign);
+}
+
+/* Round sign * x * 2^e2 to bits of precision, no finer than 2^emin.
+ * x must be nonzero. If sticky is set, x includes the halfway bit and
+ * at least one lower bit beyond x was nonzero. For inexact results, a
+ * proxy with the same retained parity and relation to halfway rounds
+ * once in the float conversion's rounding mode. */
+static double round_scale(uint64_t x, int sticky, long long e2,
+                          int bits, int emin, int sign)
+{
+    int emax = -emin-bits+3, min_normal = emin+bits, tail;
+    long long e;
+    uint64_t rem;
+
+    /* Normalize x, so that the value is in [2^(e2-1), 2^e2) */
+    for (e2 += 64; !(x >> 63); e2--)
+        x <<= 1;
+
+    /* volatile keeps the product at run time, in the current rounding mode */
+    if (e2 > emax) {
+        volatile double huge = DBL_MAX;
+        errno = ERANGE;
+        return sign * DBL_MAX * huge;
+    }
+    /* Below half the least subnormal, only a sticky bit remains */
+    if (e2 < emin) {
+        x = 0;
+        sticky = 1;
+        e2 = emin;
+    }
+
+    /* Limit precision for denormal results */
+    if (bits > e2-emin)
+        bits = e2-emin;
+    rem = x << bits;
+    x = x >> (63-bits) >> 1;
+
+    /* 0 zero, 1 below half, 2 half, 3 above half */
+    tail = rem > 1ULL<<63 ? 3 : rem == 1ULL<<63 ? 2+sticky : rem || sticky;
+    if (tail) {
+        double proxy = (1 << (FLT_MANT_DIG-1)) + (x & 1);
+        volatile float r = sign * (proxy + 0.25*tail);
+        x += r != sign * proxy;
+    }
+
+    /* A rounding carry advances the exponent */
+    e = e2 + (x >> bits);
+    if (e > emax || (e < min_normal && tail))
+        errno = ERANGE;
+
+    return scalbn(sign * (double)(int64_t)x, e2-bits);
+}
+
 static double decfloat(FFFILE *f, int c, int bits, int emin, int sign, int pok)
 {
     uint32_t x[KMAX];
@@ -252,11 +310,9 @@ static double decfloat(FFFILE *f, int c, int bits, int emin, int sign, int pok)
     int gotdig = 0, gotrad = 0;
     int rp;
     int e2;
-    int emax = -emin-bits+3;
-    int denormal = 0;
-    double y;
-    double frac=0;
-    double bias=0;
+    int sticky = 0;
+    uint32_t t = 0;
+    uint64_t m;
     static const int p10s[] = { 10, 100, 1000, 10000,
         100000, 1000000, 10000000, 100000000 };

@@ -318,19 +374,16 @@ static double decfloat(FFFILE *f, int c, int bits, int emin, int sign, int pok)
     }

     /* Handle zero specially to avoid nasty special cases later */
-    if (!x[0]) return sign * 0.0;
+    if (!x[0]) return signed_zero(sign);

     /* Optimize small integers (w/no exponent) and over/under-flow */
     if (lrp==dc && dc<10 && (bits>30 || x[0]>>bits==0))
         return sign * (double)x[0];
-    if (lrp > -emin/2) {
-        errno = ERANGE;
-        return sign * DBL_MAX * DBL_MAX;
-    }
-    if (lrp < emin-2*DBL_MANT_DIG) {
-        errno = ERANGE;
-        return sign * DBL_MIN * DBL_MIN;
-    }
+    /* Round stand-ins far outside the range instead */
+    if (lrp > -emin/2)
+        return round_scale(1, 0, -emin, bits, emin, sign);
+    if (lrp < emin-2*DBL_MANT_DIG)
+        return round_scale(1, 0, 2*emin, bits, emin, sign);

     /* Align incomplete final B1B digit */
     if (j) {
@@ -436,70 +489,26 @@ 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];
-    }
-
-    y *= sign;
-
-    /* Limit precision for denormal results */
-    if (bits > DBL_MANT_DIG+e2-emin) {
-        bits = DBL_MANT_DIG+e2-emin;
-        if (bits<0) bits=0;
-        denormal = 1;
+        m = 1000000000 * m + x[a+i & MASK];
     }

-    /* 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 */
+    /* Keep a half bit and a sticky bit of the remaining digits */
     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;
-        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++;
-    }
-
-    y += frac;
-    y -= bias;
-
-    if ((e2+DBL_MANT_DIG & INT_MAX) > emax-5) {
-        if (fabs(y) >= pow(2, DBL_MANT_DIG)) {
-            if (denormal && bits==DBL_MANT_DIG+e2-emin)
-                denormal = 0;
-            y *= 0.5;
-            e2++;
-        }
-        if (e2+DBL_MANT_DIG>emax || (denormal && frac))
-            errno = ERANGE;
+        t = x[a+i & MASK];
+        sticky = t % 500000000 || (a+i+1 & MASK) != z;
     }

-    return scalbn(y, e2);
+    return round_scale(2*m + t/500000000, sticky, e2-1, bits, emin, sign);
 }

 static double hexfloat(FFFILE *f, int bits, int emin, int sign, int pok)
 {
-    uint32_t x = 0;
-    double y = 0;
-    double scale = 1;
-    double bias = 0;
-    int gottail = 0, gotrad = 0, gotdig = 0;
+    uint64_t x = 0;
+    int sticky = 0;
+    int gotrad = 0, gotdig = 0;
     long long rp = 0;
     long long dc = 0;
     long long e2 = 0;
@@ -528,14 +537,10 @@ static double hexfloat(FFFILE *f, int bits, int emin, int sign, int pok)
             gotdig = 1;
             if (c > '9') d = (c|32)+10-'a';
             else d = c-'0';
-            if (dc<8) {
+            if (dc < 16)
                 x = x*16 + d;
-            } else if (dc < DBL_MANT_DIG/4+1) {
-                y += d*(scale/=16);
-            } else if (d && !gottail) {
-                y += 0.5*scale;
-                gottail = 1;
-            }
+            else if (d)
+                sticky = 1;
             dc++;
         }
     }
@@ -550,7 +555,7 @@ static double hexfloat(FFFILE *f, int bits, int emin, int sign, int pok)
         return sign * 0.0;
     }
     if (!gotrad) rp = dc;
-    while (dc<8) x *= 16, dc++;
+    while (dc<16) x *= 16, dc++;
     if ((c|32)=='p') {
         e2 = scanexp(f, pok);
         if (e2 == LLONG_MIN) {
@@ -565,45 +570,11 @@ static double hexfloat(FFFILE *f, int bits, int emin, int sign, int pok)
     } else {
         shunget(f);
     }
-    e2 += 4*rp - 32;
-
-    if (!x) return sign * 0.0;
-    if (e2 > -emin) {
-        errno = ERANGE;
-        return sign * DBL_MAX * DBL_MAX;
-    }
-    if (e2 < emin-2*DBL_MANT_DIG) {
-        errno = ERANGE;
-        return sign * DBL_MIN * DBL_MIN;
-    }
-
-    while (x < 0x80000000) {
-        if (y>=0.5) {
-            x += x + 1;
-            y += y - 1;
-        } else {
-            x += x;
-            y += y;
-        }
-        e2--;
-    }
-
-    if (bits > 32+e2-emin) {
-        bits = 32+e2-emin;
-        if (bits<0) bits=0;
-    }
-
-    if (bits < DBL_MANT_DIG)
-        bias = copysign(scalbn(1, 32+DBL_MANT_DIG-bits-1), sign);
-
-    if (bits<32 && y && !(x&1)) x++, y=0;
-
-    y = bias + sign*(double)x + sign*y;
-    y -= bias;
+    e2 += 4*rp - 64;

-    if (!y) errno = ERANGE;
+    if (!x) return signed_zero(sign);

-    return scalbn(y, e2);
+    return round_scale(x, sticky, e2, bits, emin, sign);
 }

 static double fffloatscan(FFFILE *f, int prec, int pok)
diff --git a/libavutil/tests/avstring.c b/libavutil/tests/avstring.c
index bc0bde358d..1113c639bd 100644
--- a/libavutil/tests/avstring.c
+++ b/libavutil/tests/avstring.c
@@ -16,12 +16,52 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */

+#include <errno.h>
+#include <inttypes.h>
 #include <stdio.h>

 #include "libavutil/common.h"
+#include "libavutil/intfloat.h"
 #include "libavutil/mem.h"
 #include "libavutil/avstring.h"

+static void test_av_sscanf(void)
+{
+    static const char *const tests[] = {
+        "0.4999999850988388",             /* x87 double rounding at a float midpoint */
+        "-7.006492321624086e-46",          /* float subnormal just above a midpoint */
+        "-2.4703282292062328e-324",       /* double subnormal just above a midpoint */
+        "2.2250738585072013e-308",        /* carry into the smallest normal double */
+        "0x1.fffffeffffffffffffffffffp-2", /* x87 double rounding, hex */
+        "0x1.3adfa3f0000070000cp12",       /* digits beyond the 32nd bit */
+        "0x1.00000000000008001p0",         /* sticky bit breaks the tie */
+        "0x1p-1074",                      /* exact double subnormal */
+        "-0x1p-1075",                     /* inexact double underflow to zero */
+        "0x1.8p-1074",                    /* inexact double subnormal */
+        "0x1.fffffep-127",                /* carry into the smallest normal float */
+        "0x1.ffffffp127",                 /* overflow from rounding carry */
+        "0x1p128",                        /* overflow before rounding */
+        "-0",
+        "-0x0p0",
+    };
+
+    printf("Testing av_sscanf()\n");
+    for (int i = 0; i < FF_ARRAY_ELEMS(tests); i++) {
+        float f = -1.0f;
+        double d = -1.0;
+        int float_erange, double_erange;
+
+        errno = 0;
+        av_sscanf(tests[i], "%f", &f);
+        float_erange = errno == ERANGE;
+        errno = 0;
+        av_sscanf(tests[i], "%lf", &d);
+        double_erange = errno == ERANGE;
+        printf("%08"PRIx32" %d %016"PRIx64" %d %s\n",
+               av_float2int(f), float_erange, av_double2int(d), double_erange, tests[i]);
+    }
+}
+
 int main(void)
 {
     int i;
@@ -109,5 +149,7 @@ int main(void)
     TEST_STRIREPLACE(haystack, needle [2], "Education consists mainly in what we have instead.");
     TEST_STRIREPLACE(haystack, needle [1], "Education consists mainly in what we have instead");

+    test_av_sscanf();
+
     return 0;
 }
diff --git a/tests/ref/fate/avstring b/tests/ref/fate/avstring
index 1ca9be52ed..0857fbefb4 100644
--- a/tests/ref/fate/avstring
+++ b/tests/ref/fate/avstring
@@ -34,3 +34,19 @@ path/comp = path/comp
 path/comp = path/comp
 path/comp = path/comp
 path/path2/comp/comp2 = path/path2/comp/comp2
+Testing av_sscanf()
+3effffff 0 3fdffffff0000000 0 0.4999999850988388
+80000001 1 b690000000000000 0 -7.006492321624086e-46
+80000000 1 8000000000000001 1 -2.4703282292062328e-324
+00000000 1 0010000000000000 0 2.2250738585072013e-308
+3effffff 0 3fdffffff0000000 0 0x1.fffffeffffffffffffffffffp-2
+459d6fd2 0 40b3adfa3f000007 0 0x1.3adfa3f0000070000cp12
+3f800000 0 3ff0000000000001 0 0x1.00000000000008001p0
+00000000 1 0000000000000001 0 0x1p-1074
+80000000 1 8000000000000000 1 -0x1p-1075
+00000000 1 0000000000000002 1 0x1.8p-1074
+00800000 0 380fffffe0000000 0 0x1.fffffep-127
+7f800000 1 47effffff0000000 0 0x1.ffffffp127
+7f800000 1 47f0000000000000 0 0x1p128
+80000000 0 8000000000000000 0 -0
+80000000 0 8000000000000000 0 -0x0p0