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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|target                      |middle-end
           Severity|normal                      |enhancement

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This is better:
        xorl    %eax, %eax
        testq   %rdi, %rdi
        cmovle  %rdi, %rax
        negq    %rax

Or:
        movq    %rdi, %rax
        sarq    $63, %rax
        andq    %rdi, %rax
        negq    %rax

That is -MIN<x,0> but MAX<-x,0> gives the best:
        negq    %rdi
        movl    $0, %eax
        cmovns  %rdi, %rax

That is all three of these functions should give the same code gen:
#include <stdint.h>

uint64_t clamp1(int64_t x) {
    return -((x < 0) ? x : 0);
}

uint64_t clamp3(int64_t x) {
    x = -x;
    return ((x > 0) ? x : 0);
}

uint64_t clamp2(int64_t x) {
    return ((x < 0) ? -x : 0);
}

Reply via email to