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

            Bug ID: 128133
           Summary: wrong code at -O2 and -O3 converting an unsigned
                    _BitInt(17) array to unsigned int
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kyuwoncho18 at gmail dot com
  Target Milestone: ---
            Target: x86_64-pc-linux-gnu

#include <stdio.h>

__attribute__((noinline))
void convert(unsigned *dst, unsigned _BitInt(17) *src)
{
  for (int i = 0; i < 4; ++i)
    dst[i] = src[i];
}

int main(void)
{
  unsigned _BitInt(17) src[4];
  unsigned dst[4];
  __builtin_memset(src, 0xff, sizeof src);
  convert(dst, src);
  printf("%u\n", dst[0]);
}

godbolt: https://godbolt.org/z/hsh9bbb5c

On x86-64, memset sets all 17 value bits of each src element to 1, so
conversion to unsigned int must produce 131071 regardless of the padding bits.
With -O2 or -O3 this prints 4294967295; with -O0, -O1, -Os or -O2
-fno-tree-loop-vectorize it prints the correct 131071.

Reply via email to