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

            Bug ID: 129192
           Summary: [17 Regression] wrong code at -O2 and -O3: vectorized
                    average `((unsigned) short + (unsigned) unsigned short
                    + 1) >> 1` uses the signed fallback (psraw) since
                    r17-3888
           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: haoxintu at gmail dot com
  Target Milestone: ---

Hi,

$cat repro.c

short a[16], c[16];
unsigned short b[16];
__attribute__((noipa)) void
f (void)
{
  for (int i = 0; i < 16; i++)
    c[i] = ((unsigned) a[i] + (unsigned) b[i] + 1u) >> 1;
}
int main (void)
{
  b[0] = 0xffff;
  f ();
  if (c[0] != -32768)
    __builtin_abort ();
  return 0;
}

$ gcc -O0 repro.c && ./a.out                          # ok
$ gcc -O2 repro.c && ./a.out                          # Aborted
$ gcc -O2 -fno-tree-loop-vectorize repro.c && ./a.out # ok

For `i == 0`: `(0 + 0xffff + 1) >> 1 == 0x8000`, so `c[0]` must be `(short)
0x8000 == -32768`. The operands are converted to
`unsigned` before the addition, so the sum cannot overflow and the result is
exact.

The vectorizer recognizes an average of 16-bit values, but one operand is
`short` and the other `unsigned short`. Trunk picks the signed type and emits
the generic average fallback with an arithmetic shift (`psraw $1`), i.e. it
computes
`(short) 0 + (short) 0xffff + 1 = -1 + 1 = 0`, `0 >> 1 = 0`. That is wrong for
this mixed-sign input: no 16-bit type, signed or unsigned, can represent both
operand ranges.

## Cause

**Bisected to r17-3888** (765ef64246d, "vect: Allow signed types in average
fallback [PR89007]", liuhongt, 2026-09-03):
https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=765ef64246da4b65ca394b29095cbf891006977d

C-only builds of the two commits, same testcase:

| Commit | repro.c -O3 |
|---|---|
| a937d8f7b55 (parent of r17-3888) | ok |
| **765ef64246d (r17-3888) "vect: Allow signed types in average fallback
[PR89007]"** | **aborts** |

r17-3888 removed the unsigned restriction of the fallback in
`vect_recog_average_pattern`. Before it, the pattern was detected but the
fallback was refused for the signed type, so the loop was vectorized with the
plain widened arithmetic (correct). The fallback itself is fine for signed
types whose operands are both signed; the problem is choosing a 16-bit type at
all when the two operands have different signedness (one `short`, one `unsigned
short`), so the narrow computation loses the range of one of them.

## Checked versions

- gcc trunk c10b819941b (2026-11-30): -O2 and -O3 abort; -O0, -O1, -Os ok.
- gcc 16, 15, 13, 12, 11: ok at every level.
- clang trunk: ok.
- UB: clean under ASan+UBSan and MSan (clang 21, -O0); gcc -O0 and clang agree.

Godbolt (gcc trunk, -O2 aborts, -O0 runs): https://godbolt.org/z/aKv4GvMdq

Thanks.

Reply via email to