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

A signed value clamped to the bounds of a narrower signed type is signed
saturating truncation.  Recognize both nesting orders of the MIN and MAX
operations.

Require exact bounds at the input precision, signed input and output types,
a narrowing conversion, and one use of the outer clamp.

With AArch64 -O2 -march=armv8-a -fopenmp-simd:

before:

clamp_i16_4:
        ldr     q0, [x1]
        movi    v30.4s, 0x7f, msl 8
        mvni    v31.4s, 0x7f, msl 8
        smin    v30.4s, v0.4s, v30.4s
        smax    v30.4s, v30.4s, v31.4s
        xtn     v30.4h, v30.4s
        str     d30, [x0]
        ret

after:

clamp_i16_4:
        ldr     q31, [x1]
        sqxtn   v31.4h, v31.4s
        str     d31, [x0]
        ret

Runtime tests cover both clamp orders, two widths, and LTO.  Target tests
cover valid forms and inexact bounds.

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

Ok for trunk?

Changes since v3:
- Check the outer clamp use at this consumer.

gcc/ChangeLog:

        * match-sat-alu.pd (signed_integer_sat_trunc): Add the constant-clamp
        form.

gcc/testsuite/ChangeLog:

        * gcc.dg/vect/vect-sat-trunc-clamp-2.c: New test.
        * gcc.target/aarch64/vect-sat-trunc-clamp-s-1.c: Likewise.
        * gcc.target/aarch64/vect-sat-trunc-clamp-s-negative-1.c: Likewise.

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

Reply via email to