public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libgcc/114397] New: wrong code with _BitInt() division at -O0
@ 2024-03-19 17:39 zsojka at seznam dot cz
  2024-03-20 17:53 ` [Bug libgcc/114397] " jakub at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: zsojka at seznam dot cz @ 2024-03-19 17:39 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 114397
           Summary: wrong code with _BitInt() division at -O0
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: libgcc
          Assignee: unassigned at gcc dot gnu.org
          Reporter: zsojka at seznam dot cz
                CC: jakub at gcc dot gnu.org
  Target Milestone: ---
              Host: x86_64-pc-linux-gnu
            Target: x86_64-pc-linux-gnu

Created attachment 57740
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=57740&action=edit
reduced testcase

Output:
$ x86_64-pc-linux-gnu-gcc testcase.c
$ ./a.out 
Aborted

The result is 0 instead of -1.

$ x86_64-pc-linux-gnu-gcc -v
Using built-in specs.
COLLECT_GCC=/repo/gcc-trunk/binary-latest-amd64/bin/x86_64-pc-linux-gnu-gcc
COLLECT_LTO_WRAPPER=/repo/gcc-trunk/binary-trunk-r14-9532-20240318213256-g9eeca775367-checking-yes-rtl-df-extra-nobootstrap-amd64/bin/../libexec/gcc/x86_64-pc-linux-gnu/14.0.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /repo/gcc-trunk//configure --enable-languages=c,c++
--enable-valgrind-annotations --disable-nls --enable-checking=yes,rtl,df,extra
--disable-bootstrap --with-cloog --with-ppl --with-isl
--build=x86_64-pc-linux-gnu --host=x86_64-pc-linux-gnu
--target=x86_64-pc-linux-gnu --with-ld=/usr/bin/x86_64-pc-linux-gnu-ld
--with-as=/usr/bin/x86_64-pc-linux-gnu-as --enable-libsanitizer
--disable-libstdcxx-pch
--prefix=/repo/gcc-trunk//binary-trunk-r14-9532-20240318213256-g9eeca775367-checking-yes-rtl-df-extra-nobootstrap-amd64
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 14.0.1 20240319 (experimental) (GCC)

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug libgcc/114397] wrong code with _BitInt() division at -O0
  2024-03-19 17:39 [Bug libgcc/114397] New: wrong code with _BitInt() division at -O0 zsojka at seznam dot cz
@ 2024-03-20 17:53 ` jakub at gcc dot gnu.org
  2024-03-21 12:08 ` cvs-commit at gcc dot gnu.org
  2024-03-21 12:13 ` jakub at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: jakub at gcc dot gnu.org @ 2024-03-20 17:53 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |ASSIGNED
   Last reconfirmed|                            |2024-03-20
           Assignee|unassigned at gcc dot gnu.org      |jakub at gcc dot gnu.org

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Created attachment 57749
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=57749&action=edit
gcc14-pr114397.patch

Untested fix.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug libgcc/114397] wrong code with _BitInt() division at -O0
  2024-03-19 17:39 [Bug libgcc/114397] New: wrong code with _BitInt() division at -O0 zsojka at seznam dot cz
  2024-03-20 17:53 ` [Bug libgcc/114397] " jakub at gcc dot gnu.org
@ 2024-03-21 12:08 ` cvs-commit at gcc dot gnu.org
  2024-03-21 12:13 ` jakub at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-03-21 12:08 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:59b6cece54f33ac4994834d01e18269856576556

commit r14-9592-g59b6cece54f33ac4994834d01e18269856576556
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Thu Mar 21 13:07:50 2024 +0100

    libgcc: Fix up bitint division [PR114397]

    The Knuth's division algorithm relies on the number of dividend limbs
    to be greater ore equal to number of divisor limbs, which is why
    I've added a special case for un < vn at the start of __divmodbitint4.
    Unfortunately, my assumption that it then implies abs(v) > abs(u) and
    so quotient must be 0 and remainder same as dividend is incorrect.
    This is because this check is done before negation of the operands.
    While bitint_reduce_prec reduces precision from clearly useless limbs,
    the problematic case is when the dividend is unsigned or non-negative
    and divisor is negative.  We can have limbs (from MS to LS):
    dividend:       0       M       ?...
    divisor:        -1      -N      ?...
    where M has most significant bit set and M >= N (if M == N then it
    also the following limbs matter) and the most significant limbs can
    be even partial.  In this case, the quotient should be -1 rather than
    0.  bitint_reduce_prec will reduce the precision of the dividend so
    that M is the most significant limb, but can't reduce precision of the
    divisor to more than having the -1 as most significant limb, because
    -N doesn't have the most significant bit set.

    The following patch fixes it by detecting this problematic case in the
    un < vn handling, and instead of assuming q is 0 and r is u will
    decrease vn by 1 because it knows the later code will negate the divisor
    and it can be then expressed after negation in one fewer limbs.

    2024-03-21  Jakub Jelinek  <jakub@redhat.com>

            PR libgcc/114397
            * libgcc2.c (__divmodbitint4): Don't assume un < vn always means
            abs(v) > abs(u), check for a special case of un + 1 == vn where
            u is non-negative and v negative and after v's negation vn could
            be reduced by 1.

            * gcc.dg/torture/bitint-65.c: New test.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug libgcc/114397] wrong code with _BitInt() division at -O0
  2024-03-19 17:39 [Bug libgcc/114397] New: wrong code with _BitInt() division at -O0 zsojka at seznam dot cz
  2024-03-20 17:53 ` [Bug libgcc/114397] " jakub at gcc dot gnu.org
  2024-03-21 12:08 ` cvs-commit at gcc dot gnu.org
@ 2024-03-21 12:13 ` jakub at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: jakub at gcc dot gnu.org @ 2024-03-21 12:13 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Fixed.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-03-21 12:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-19 17:39 [Bug libgcc/114397] New: wrong code with _BitInt() division at -O0 zsojka at seznam dot cz
2024-03-20 17:53 ` [Bug libgcc/114397] " jakub at gcc dot gnu.org
2024-03-21 12:08 ` cvs-commit at gcc dot gnu.org
2024-03-21 12:13 ` jakub at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).