Reading this interesting discussion while ago regarding the different implementations of signed numbers modulus calculation in gcc and clang raised a question for me (which is not discussed in the discussion).
Why on earth, the implementation of this:
if(num % 2 == 1)
starts with this (similar at both clang and gcc):
movl    %edi, %eax
shrl    $31, %eax
addl    %edi, %eax
Why are we starting with ((num >> 31) + num) ? why taking the MSB and add it to the number? where is this coming from?
 
    