On 9/16/26 9:16 AM, [email protected] wrote:
From: Kyrylo Tkachov <[email protected]>

A widened saturating addition can clamp the sum with either of these forms:

   SUM <= MAX ? SUM : MAX
   SUM < MAX ? SUM : MAX

The strict comparison is canonicalized to SUM <= MAX - 1.  It is equivalent
because equality makes both arms produce MAX.  Accept MAX and MAX - 1 as the
comparison limit.

AArch64 -O2:

before:

        uxtw    x2, w1
        add     x2, x2, w0, uxtw
        add     w0, w0, w1
        mov     w1, 4294967294
        cmp     x2, x1
        csinv   w0, w0, wzr, ls

after:

        adds    w0, w0, w1
        csinv   w0, w0, wzr, cc

Bootstrapped and tested on aarch64-none-linux-gnu.

Ok for trunk?

Changes since v3:
- Rebase on current trunk.

gcc/ChangeLog:

        * match-sat-alu.pd (unsigned_integer_sat_add): Accept a compare
        against MAX - 1 in the widened form.

gcc/testsuite/ChangeLog:

        * gcc.target/aarch64/sat_u_add_widen_lt-1.c: New test.

Signed-off-by: Kyrylo Tkachov <[email protected]>
OK
jeff

Reply via email to