On 10/26/26 4:16 PM, Igor Shevlyakov wrote:
(x + y) >> 1 is IFN_AVG_FLOOR (x, y) and ((x + y) + 1) >> 1 is
IFN_AVG_CEIL (x, y) whenever the sum does not wrap. The existing
IFN_AVG_FLOOR fold for vectors proves that from leading zero bits of x
and y. For a signed type whose overflow is undefined the sum cannot
wrap anyway, so code such as the column pass of a 4x4 DCT butterfly,
for (int i = 0; i < n; i++)
r[i] = (a[i] + b[i] + c[i] + d[i] + 1) >> 1;
is vectorized as two adds and an arithmetic shift per vector where one
add and a rounding halving add (srhadd on AArch64) would do.
vect_recog_average_pattern only handles the narrowing form, where the
sum is computed in a wider type than x and y.
Fold (x + y) >> 1 without looking at the bits of x and y when overflow
is undefined, and add the same fold of ((x + y) + 1) >> 1 to
IFN_AVG_CEIL, with the same conditions.
Assisted-by: Claude Opus 5.5
gcc/ChangeLog:
* match.pd ((x + y) >> 1 -> IFN_AVG_FLOOR (x, y)): Also fold
when overflow is undefined.
(((x + y) + 1) >> 1 -> IFN_AVG_CEIL (x, y)): New fold.
gcc/testsuite/ChangeLog:
* gcc.dg/vect/vect-avg-17.c: New test.
* gcc.target/aarch64/vect_hadd_same_width_1.c: New test.
* gcc.target/aarch64/vect_hadd_same_width_2.c: New test.
Signed-off-by: Igor Shevlyakov <[email protected]>
LGTM. No concerns from Sashiko either.
Thanks!
jeff