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

            Bug ID: 129085
           Summary: RISC-V: signed _BitInt(1) division result not extended
                    to 1 bit (gcc.dg/torture/bitint-97.c fails)
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: frederic.mahe at cirad dot fr
  Target Milestone: ---
            Target: riscv64-unknown-linux-gnu

gcc.dg/torture/bitint-97.c fails at every optimization level on
riscv64. It fails in all the riscv64 results I checked: native on a
SpacemiT K1, SUSE riscv64 (rv64gc, 2026-11-13), and the RISE CI
(rv64gc, rv64gcv). It passes on x86_64 and aarch64. The failing element
is `a[2] = b[2] / c[2]`, which computes -1 / -1 in signed _BitInt(1).

Reduced:

    [[gnu::noipa]] signed _BitInt(1)
    d (signed _BitInt(1) b, signed _BitInt(1) c)
    {
      return b / c;
    }
    int main ()
    {
      signed _BitInt(1) r = d (-1, -1);
      __builtin_printf ("%d\n", (int) r);
      return 0;
    }

    $ gcc -O2 -std=c2y -fwrapv -march=rv64gc t.c && ./a.out
    1          (expected -1)

The same output appears at -O0.

    d:
            divw    a0,a0,a1
            slliw   t0,a0,24
            sraiw   a0,t0,24
            ret

The result of `divw` (1) is sign-extended from bit 7 instead of bit 0,
so the returned _BitInt(1) has the wrong upper bits. If the RISC-V
psABI requires _BitInt values to be extended, this breaks it. The
caller then reads the value as 1 when it converts it to int.

Without -fwrapv, -1 / -1 overflows signed _BitInt(1) and
-fsanitize=undefined reports it; bitint-97.c uses -fwrapv, so the
result must wrap to -1.

Compiler (native riscv64, gcc -v):

    Target: riscv64-unknown-linux-gnu
    Configured with: /srv/gcc/src/configure --enable-languages=c,c++
      --disable-multilib
--with-arch=rv64gcv_zba_zbb_zbc_zbs_zicond_zfh_zvfh_zvl256b
      --with-abi=lp64d
    gcc version 17.0.0 20261120 (experimental) (GCC)   [728d0bebb85]

Found with help from Claude (Anthropic); checked by hand on hardware.

Reply via email to