|
|
|
|
|
by usefulcat
3286 days ago
|
|
In some cases I suspect it depends a lot on the hardware. I did a bit of research into the "Integer minimum" algorithm. Given the following straightforward implementation: int min(int x, int y) { return y < x ? y : x; }
..most compilers will produce something very much like the following for x86-64 (from Compiler Explorer https://gcc.godbolt.org using -Ofast -march=haswell): min(int, int):
cmp esi, edi
mov eax, edi
cmovle eax, esi
ret
Compare that to the generated instructions for the algorithm listed in the "Integer minimum or maximum" section (x+(((y-x)>>(WORDBITS-1))&(y-x))): min(int, int):
sub esi, edi
mov eax, esi
sar eax, 31
and eax, esi
add eax, edi
ret
It's not immediately obvious to me which one is likely to be faster (I'm not an expert on x86-64 assembly). |
|