]> git.ipfire.org Git - thirdparty/gcc.git/commit
i386: Implement .SAT_SUB for unsigned scalar integers [PR112600]
authorUros Bizjak <ubizjak@gmail.com>
Sun, 9 Jun 2024 10:09:13 +0000 (12:09 +0200)
committerUros Bizjak <ubizjak@gmail.com>
Sun, 9 Jun 2024 10:10:28 +0000 (12:10 +0200)
commit8bb6b2f4ae19c3aab7d7a5e5c8f5965f89d90e01
treeb22aa103dfdd6c37bbedbd139a74aad38d5da2b5
parentad2775b0e3d65b0b844bfd13e2f8b15240fb3b93
i386: Implement .SAT_SUB for unsigned scalar integers [PR112600]

The following testcase:

unsigned
sub_sat (unsigned x, unsigned y)
{
  unsigned res;
  res = x - y;
  res &= -(x >= y);
  return res;
}

currently compiles (-O2) to:

sub_sat:
        movl    %edi, %edx
        xorl    %eax, %eax
        subl    %esi, %edx
        cmpl    %esi, %edi
        setnb   %al
        negl    %eax
        andl    %edx, %eax
        ret

We can expand through ussub{m}3 optab to use carry flag from the subtraction
and generate code using SBB instruction implementing:

    unsigned res = x - y;
    res &= ~(-(x < y));

sub_sat:
        subl    %esi, %edi
        sbbl    %eax, %eax
        notl    %eax
        andl    %edi, %eax
        ret

PR target/112600

gcc/ChangeLog:

* config/i386/i386.md (ussub<mode>3): New expander.
(sub<mode>_3): Ditto.

gcc/testsuite/ChangeLog:

* gcc.target/i386/pr112600-b.c: New test.
gcc/config/i386/i386.md
gcc/testsuite/gcc.target/i386/pr112600-b.c [new file with mode: 0644]