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

            Bug ID: 129030
           Summary: [arm] __aeabi_dsub/__adddf3 misrounds some
                    subtractions when the exponents differ by 33
           Product: gcc
           Version: 14.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libgcc
          Assignee: unassigned at gcc dot gnu.org
          Reporter: igorrain95 at gmail dot com
  Target Milestone: ---

The ARM soft-float double add/subtract in libgcc/config/arm/ieee754-df.S
(__aeabi_dadd, __aeabi_dsub, __adddf3, __subdf3) returns a result one ulp
away from the correctly rounded result for some effective subtractions where
the operands' exponents differ by exactly 33.

GCC: arm-none-eabi-gcc 14.2.1 20241119 (Arm GNU Toolchain 14.2.rel1, Debian
package 15:14.2.rel1-1). This is a vendor build, not an FSF release, but the
routine is upstream's, unchanged since 2018 (see the end). "gcc -v":

Using built-in specs.
COLLECT_GCC=arm-none-eabi-gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/arm-none-eabi/14.2.1/lto-wrapper
Target: arm-none-eabi
Configured with: ../configure --build=x86_64-linux-gnu --prefix=/usr
--includedir='/usr/lib/include' --mandir='/usr/lib/share/man'
--infodir='/usr/lib/share/info' --sysconfdir=/etc --localstatedir=/var
--disable-option-checking --disable-silent-rules
--libdir='/usr/lib/lib/x86_64-linux-gnu'
--libexecdir='/usr/lib/lib/x86_64-linux-gnu' --disable-maintainer-mode
--disable-dependency-tracking --mandir=/usr/share/man
--enable-languages=c,c++,lto --enable-multilib --disable-decimal-float
--disable-libffi --disable-libgomp --disable-libmudflap --disable-libquadmath
--disable-libssp --disable-libstdcxx-pch --disable-nls --disable-shared
--disable-threads --enable-tls --build=x86_64-linux-gnu --target=arm-none-eabi
--with-system-zlib --with-gnu-as --with-gnu-ld --with-pkgversion=15:14.2.rel1-1
--without-included-gettext --prefix=/usr/lib
--infodir=/usr/share/doc/gcc-arm-none-eabi/info
--htmldir=/usr/share/doc/gcc-arm-none-eabi/html
--pdfdir=/usr/share/doc/gcc-arm-none-eabi/pdf --bindir=/usr/bin
--libexecdir=/usr/lib --libdir=/usr/lib --disable-libstdcxx
--host=x86_64-linux-gnu --with-headers=no --without-newlib
--with-multilib-list=rmprofile,aprofile ASFLAGS= ASFLAGS_FOR_BUILD= CFLAGS='-g
-O2 -Werror=implicit-function-declaration
-ffile-prefix-map=/build/reproducible-path/gcc-arm-none-eabi-14.2.rel1=.
-fstack-protector-strong -fstack-clash-protection -fcf-protection'
'CFLAGS_FOR_BUILD=-g -O2 -Werror=implicit-function-declaration
-ffile-prefix-map=/build/reproducible-path/gcc-arm-none-eabi-14.2.rel1=.
-fstack-protector-strong -fstack-clash-protection -fcf-protection'
CPPFLAGS='-Wdate-time -D_FORTIFY_SOURCE=2' 'CPPFLAGS_FOR_BUILD=-Wdate-time
-D_FORTIFY_SOURCE=2' CXXFLAGS='-g -O2
-ffile-prefix-map=/build/reproducible-path/gcc-arm-none-eabi-14.2.rel1=.
-fstack-protector-strong -fstack-clash-protection -fcf-protection'
'CXXFLAGS_FOR_BUILD=-g -O2
-ffile-prefix-map=/build/reproducible-path/gcc-arm-none-eabi-14.2.rel1=.
-fstack-protector-strong -fstack-clash-protection -fcf-protection'
DFLAGS=-frelease DFLAGS_FOR_BUILD=-frelease FCFLAGS='-g -O2
-ffile-prefix-map=/build/reproducible-path/gcc-arm-none-eabi-14.2.rel1=.
-fstack-protector-strong -fstack-clash-protection -fcf-protection'
'FCFLAGS_FOR_BUILD=-g -O2
-ffile-prefix-map=/build/reproducible-path/gcc-arm-none-eabi-14.2.rel1=.
-fstack-protector-strong -fstack-clash-protection -fcf-protection' FFLAGS='-g
-O2 -ffile-prefix-map=/build/reproducible-path/gcc-arm-none-eabi-14.2.rel1=.
-fstack-protector-strong -fstack-clash-protection -fcf-protection'
'FFLAGS_FOR_BUILD=-g -O2
-ffile-prefix-map=/build/reproducible-path/gcc-arm-none-eabi-14.2.rel1=.
-fstack-protector-strong -fstack-clash-protection -fcf-protection'
LDFLAGS=-Wl,-z,relro LDFLAGS_FOR_BUILD=-Wl,-z,relro OBJCFLAGS='-g -O2
-ffile-prefix-map=/build/reproducible-path/gcc-arm-none-eabi-14.2.rel1=.
-fstack-protector-strong -fstack-clash-protection -fcf-protection'
'OBJCFLAGS_FOR_BUILD=-g -O2
-ffile-prefix-map=/build/reproducible-path/gcc-arm-none-eabi-14.2.rel1=.
-fstack-protector-strong -fstack-clash-protection -fcf-protection'
OBJCXXFLAGS='-g -O2
-ffile-prefix-map=/build/reproducible-path/gcc-arm-none-eabi-14.2.rel1=.
-fstack-protector-strong -fstack-clash-protection -fcf-protection'
'OBJCXXFLAGS_FOR_BUILD=-g -O2
-ffile-prefix-map=/build/reproducible-path/gcc-arm-none-eabi-14.2.rel1=.
-fstack-protector-strong -fstack-clash-protection -fcf-protection'
INHIBIT_LIBC_CFLAGS=-DUSE_TM_CLONE_REGISTRY=0 AR_FOR_TARGET=arm-none-eabi-ar
AS_FOR_TARGET=arm-none-eabi-as LD_FOR_TARGET=arm-none-eabi-ld
NM_FOR_TARGET=arm-none-eabi-nm OBJDUMP_FOR_TARGET=arm-none-eabi-objdump
RANLIB_FOR_TARGET=arm-none-eabi-ranlib READELF_FOR_TARGET=arm-none-eabi-readelf
STRIP_FOR_TARGET=arm-none-eabi-strip SED=/bin/sed SHELL=/bin/sh BASH=/bin/bash
CONFIG_SHELL=/bin/bash
Thread model: single
Supported LTO compression algorithms: zlib
gcc version 14.2.1 20241119 (15:14.2.rel1-1) 

Testcase, pr.c:

int printf(const char *, ...);
int main(void)
{
  volatile double x = 2147483648.0;          /* 0x41e0000000000000 */
  volatile double y = 0x1.5555555555555p-2;  /* 0x3fd5555555555555 */
  double r = x - y;
  unsigned long long u;
  __builtin_memcpy(&u, &r, sizeof u);
  printf("%016llx (expected 41dfffffffeaaaab)\n", u);
  return u != 0x41dfffffffeaaaabULL;
}

vec.c, a two-entry vector table that QEMU's M-profile machine needs (not
part of the bug):

extern void _start(void);
__attribute__((naked)) void reset(void)
{
#ifdef __ARM_FP
  __asm__ volatile("ldr r0,=0xE000ED88\n ldr r1,[r0]\n orr r1,r1,#(0xF<<20)\n
str r1,[r0]\n dsb\n isb");
#endif
  __asm__ volatile("b _start");
}
__attribute__((section(".vectors"), used)) const void *vec[2] = { (void
*)0x20010000, (void *)reset };

To reproduce:

  arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb -O2 --specs=rdimon.specs \
      -Wl,--section-start=.vectors=0 pr.c vec.c -o pr.elf
  qemu-system-arm -M mps2-an386 -cpu cortex-m4 -semihosting -nographic \
      -monitor none -serial none -kernel pr.elf

Actual output:   41dfffffffeaaaaa (expected 41dfffffffeaaaab)
Expected output: 41dfffffffeaaaab (expected 41dfffffffeaaaab)

No diagnostics with -Wall -Wextra. Same result at -O0, at -O2, with
-fno-strict-aliasing -fwrapv -fno-aggressive-loop-optimizations, and with
-mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16. On x86-64 with
-fsanitize=address,undefined, pr.c runs clean and prints the expected value.
QEMU version: 10.0.13.

The exact difference is 2147483647.66666666666666668517...;
0x41dfffffffeaaaab is 7.9e-8 away from it and 0x41dfffffffeaaaaa 1.6e-7.
x86-64 SSE2 gives 0x41dfffffffeaaaab. Not tried on arm-linux-gnueabi; a
soft-float build there that links the same routine should behave the same.

Scope: __aeabi_dsub and __aeabi_dadd (with y negated) were called directly,
with integer-typed arguments so that the base calling convention holds on
every multilib, on 3,360 operand pairs with exponent differences 30 to 36 and
all sign combinations, and compared with the host's correctly rounded result.
The libgcc.a of 27 of the toolchain's 39 multilibs was run under QEMU
(mps2-an385/an386/an500/an505, mps3-an547, versatilepb, virt), covering ARM
and Thumb state, v4t, v5te, v7, v7-a, v7ve, v7-r, v8-a, v7-m, v7e-m,
v8-m.main and v8.1-m.main (MVE and PACBTI), soft, softfp and hard.

- Every multilib that uses _arm_addsubdf3.o (ieee754-df.S) gives the same
  59 wrong results out of 3,360. All are at a difference of 33, all are
  effective subtractions whose result is one binade below the larger
  operand, and each is off by one ulp.
- thumb/v6-m/nofp and thumb/v8-m.base/nofp use the generic soft-fp
  subdf3.o instead and give 0 wrong results on the same pairs.

Likely cause, from the disassembly of _arm_addsubdf3.o: when the exponent
difference exceeds 32, the smaller operand's low word is reduced to a sticky
bit (cmp r2, #1; lsl ip, r3, lr; orrcs ip, ip, #2), leaving only
64 - difference real bits at the top of ip. When the result then needs a
one-bit left shift before rounding (movs ip, ip, lsl #1), ip's second-highest
bit becomes the round bit. At a difference of 33 that bit does not hold the
low word's top bit, so the rounding decision is wrong. At 34 and above two
real bits remain; at 32 and below no bits are dropped.

Not a duplicate of PR 86512, which was about subnormal results; here both
operands and the result are normal. The last logic change to this path was
that fix (89fff9cc2bb4, 2018-08-02). Since 72e3a529238a (2018-12-19,
fmul/dmul) the file has had only copyright-year updates, the last being
254a858ae7ae, so trunk should behave the same. Trunk itself has not been
built.

Reply via email to