[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [avr-gcc-list] Cicrular Buffer routines
From: |
Dave Hylands |
Subject: |
Re: [avr-gcc-list] Cicrular Buffer routines |
Date: |
Tue, 16 Aug 2005 17:11:48 -0700 |
Hi Bob,
> You can test to make sure that you do have a power of two buffer size with the
> following:
>
> #define myQ_SIZE 64
>
> #define myQ_MASK ( myQ__SIZE - 1 )
> #if ( myQ__SIZE & myQ__MASK )
> #error myQ_SIZE buffer size is not a power of 2
> #endif
>
> The effect is to check if only one, and only one, bit is set in myQ_SIZE.
Good point. I think I would refine things slightly like this:
#define CBUF_Size( cbuf ) ( sizeof( cbuf.m_entry ) / sizeof(
cbuf.m_entry[ 0 ] ))
and replace all occurences of cbuf##_SIZE with CBUF_Size( cbuf )
You now no longer need the myQ_SIZE macro.
Then add:
#define CBUF_SizeIsPowerOf2( cbuf ) (( CBUF_Size( cbuf ) & (
CBUF_Size( cbuf ) - 1 )) == 0 )
Then for each circular buffer you create, you can throw in a variant of:
#if ( !CBUF_SizeIsPowerOf2( myQ ))
# error size of myQ not a power of 2
#endif
--
Dave Hylands
Vancouver, BC, Canada
http://www.DaveHylands.com/