If I declare the function behind the hyperlink in your question static inline
, then gcc -O3
compiles the call to the function on known arguments to a single instruction:
~ $ cat t.c
#include <stdint.h>
static inline uint8_t
_crc_ibutton_update(uint8_t crc, uint8_t data)
{
uint8_t i;
crc = crc ^ data;
for (i = 0; i < 8; i++)
{
if (crc & 0x01)
crc = (crc >> 1) ^ 0x8C;
else
crc >>= 1;
}
return crc;
}
int x;
int main()
{
x = _crc_ibutton_update(17, 42);
}
~ $ gcc -O2 -S t.c
~ $ cat t.s
...
movq _x@GOTPCREL(%rip), %rax
movl $158, (%rax)
popq %rbp
ret
...
This is with “gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)”, and it also works with “gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5.1)” (then requiring -O3
).