One optimization I've always found impressive:
#include <stdint.h>
uint32_t testFunction(uint32_t x)
{
return ((x & 0xff) << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | ((x & 0xff000000) >> 24);
}
compiles into: testFunction(unsigned int):
mov eax, edi
bswap eax
ret
Another fun one, that only works with clang: #include <stdint.h>
int testFunction(uint64_t x)
{
int count;
for (count = 0; x; count++)
x &= x - 1;
return count;
}
compiles into: testFunction(unsigned long):
popcnt rax, rdi
ret