PreviousNext
Circular buffer index increment
Help > Appendix > Code Optimization > Circular buffer index increment

FLASH-                        RAM                Speed+

 

When for instance a circular buffer index is incremented and the buffer length is a power of two the buffer index can be incremented a better way.

 

index = (index + 1) % BUFFER_LENGTH;

#if 0 != (BUFFER_LENGTH & (BUFFER_LENGTH - 1))

#error BUFFER_LENGTH is not power of 2

#endif

 

index++;

index &= ~BUFFER_LENGTH;