[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] crc16.h - why inlined

From: Marek Michalkiewicz
Subject: Re: [avr-gcc-list] crc16.h - why inlined
Date: Thu, 9 Jan 2003 17:05:32 +0100 (CET)

> I used these days the _crc16_update function which works very well. 

It's a popular CRC-16 function, used in the MODBUS RTU protocol
(http://www.modicon.com/techpubs/toc7.html) - hand-optimized to make
it fast (no slow one-bit-at-a-time loops) and small (no big tables)
compared to the usual CRC implementations.  I needed it, and thought
it could be useful for someone else, so I added it to avr-libc.

> But why it is defined as an inlined function? As I can see there is 
> no additional prologue/epilogue code if compiled without inline.

It's a tradeoff - inlined code is just 23 single-cycle instructions,
and only needs one temporary register (in addition to r0).  On the
other hand, a real function call has its overhead (call/ret) and
GCC must assume that all call-used registers are clobbered, even
if only one really is.  So, unless you need this in many places of
the program, and code size is an issue - the inlined code is a win.

Some applications will use _crc16_update on a single character
sent or received by the UART, in an interrupt handler where speed
is critical.  Other application may need a function to calculate
CRC of a memory area - then it makes sense to repeat the inlined
code in a loop, but put the call overhead outside of the loop.

In general, if it's a relatively small piece of code, and uses
only a few registers, and is only used no more than a few times
in the whole program, making it an inline function makes better
code (always faster than a function call, and often smaller too).

Hope this helps,

avr-gcc-list at http://avr1.org

reply via email to

[Prev in Thread] Current Thread [Next in Thread]