https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128893

            Bug ID: 128893
           Summary: [m68k] soft-float long double helpers work in double
                    precision and truncate, so libc float formatting is
                    wrong
           Product: gcc
           Version: 15.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libgcc
          Assignee: unassigned at gcc dot gnu.org
          Reporter: graeme.harker at gmail dot com
  Target Milestone: ---
            Target: m68k-*-linux*

Created attachment 66235
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66235&action=edit
test case: XFmode precision of fpgnulib's helpers

With -msoft-float (or a CPU with no FPU), m68k long double is still the
96-bit extended format and <float.h> still says LDBL_MANT_DIG is 64, but the
XFmode helpers libgcc provides for it, from libgcc/config/m68k/fpgnulib.c
built with -DEXTFLOAT (t-floatlib), compute in double and do not round:

  /* It also includes simplistic support for extended floats (by working in
     double precision). */
  ...
  /* We assume all numbers are normalized, don't do any rounding, etc.  */

  long double __addxf3 (long double x1, long double x2)
  {
    return (double) x1 + (double) x2;
  }

and the same for __subxf3, __mulxf3 and __divxf3; __truncxfdf2 drops the low
mantissa bits instead of rounding. The file is the same in 15.2.0 and on
master.

The attached xf-precision.c prints (words of the 96-bit value):

                 got                           correctly rounded
  1/3            3ffd0000 aaaaaaaa aaaaa800    3ffd0000 aaaaaaaa aaaaaaab
  1 + 2^-60      3fff0000 80000000 00000000    3fff0000 80000000 00000010
  1 + 2^-60 == 1 (should be !=)
  (double)x      3ff00000 00000000             3ff00000 00000001
                 where x = 1 + 2^-53 + 2^-60 (3fff0000 80000000 00000408)

This is not only a matter of reduced precision. C libraries format and
parse floating point with long double arithmetic, relying on LDBL_MANT_DIG,
so ordinary double values come out wrong. With musl 1.2.6 built soft-float
for a 68030, from a busybox 1.38.0 shell (its printf parses the argument
with strtod and prints it with the C library's printf):

  printf '%.1f\n' 0.2                     -> 0.1
  printf '%.17g\n' 0.1                    -> 0.099999999999994315
  printf '%.17g\n' 1e22                   -> 1.0000000000001304e+22
  printf '%g\n' 1e-320                    -> 0
  printf '%g\n' 1.7976931348623157e308    -> invalid number, inf
  seq 0 0.1 0.5                           -> 0.0 0.0 0.1 0.2 0.3 0.4

With an FPU the helpers are never called, which is presumably why this has
gone unnoticed.

How this was tested: fpgnulib.c from the GCC 15.2.0 release, compiled plain
and with -DEXTFLOAT as t-floatlib does (-m68030 -msoft-float -O2), and
linked ahead of the distribution's libgcc (Ubuntu's m68k-linux-gnu-gcc
15.2.0, whose own libgcc is built for 68020+68881). Everything above was
run under qemu-system-m68k 10.2.1 (-M virt); the code involved is integer
only. I have not built a complete FPU-less GCC, so the libgcc tested is
this file and not a stock multilib.

Would correctly rounded XFmode helpers be acceptable here, for example from
soft-fp as other targets use?

Reply via email to