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

            Bug ID: 120664
           Summary: uint8_t triggers signed integer overflow for
                    std::experimental::simd multiplcation
           Product: gcc
           Version: 15.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: stuart.a.hayhurst at gmail dot com
  Target Milestone: ---

Created attachment 61644
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=61644&action=edit
Reproducer

Running GCC 15.1 on Debian Sid + Experimental, vectors of unsigned 8-bit
integers are causing signed integer overflows for small values that wouldn't
cause an overflow, even with type promotion.

It seems that they're being treated as one signed integer instead of a vector,
and the higher bits that get discarded are causing signed integer overflows,
detected by UBSan.

I'm using an AMD Ryzen 7 7700X, so znver4.

Compiling the supplied reproducer with `g++ -std=c++23 -march=native -O3 -Wall
-Wextra -Wpedantic -o bug bug.cpp -fsanitize=address,undefined -g` gives no
warnings or errors.

Running it with `./bug` gives the following:
```
/usr/include/c++/15/experimental/bits/simd_x86.h:1359:12: runtime error: signed
integer overflow: 168037128 * 151587081 cannot be represented in type 'int'
```

In the header near the line it's complaining about, changing
`reinterpret_cast<int>` to `reinterpret_cast<unsigned int>` fixes it, and so
does adding `& 0xff` to a few of the shifts to discard the extra bits. I'd
propose a patch with one of these, but I'm nowhere near familiar enough with it
to avoid breaking something else.

Reply via email to