avr-gcc-list
[Top][All Lists]
Advanced

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

Re: [avr-gcc-list] Serial Communication


From: Neil Johnson
Subject: Re: [avr-gcc-list] Serial Communication
Date: Thu, 29 Jan 2004 16:43:04 +0000 (GMT)

Hi,

> A couple nits to pick...
> The second case should actually be
>
>       index = (index + 1) % (USART_TX_BUFFER_MASK+1);

Ooops, a bit too keen with the 'ole cut-n-paste.  Yes, this *should* be:

>       index = (index + 1) % USART_TX_BUFFER_SIZE;

Also,

> There is another option as well, that some might find clearer:
>
>       if (++index >= USART_TX_BUFFER_SIZE)
>          index = 0;
>
> which should be more efficient than "%" if USART_TX_BUFFER_SIZE isn't a
> power of 2.

Good point.  Won't apply if using the RHS as an expression though, e.g.

        output = buffer[index++ % USART_TX_BUFFER_SIZE];

Just tried a quick experiment with GCC 3.2.2 for Redhat Linux to see if
there is a possibility of GCC spotting this.

Code:

------------------------------------------------------------------

#define USART_TX_BUFFER_SIZE    ( 64 )
#define USART_TX_BUFFER_MASK    ( USART_TX_BUFFER_SIZE - 1 )

int withmodop(int buffer[], unsigned int i)
{
        int x = buffer[i % USART_TX_BUFFER_SIZE];

        return x;
}

int withandop(int buffer[], unsigned int i)
{
        int x = buffer[i & USART_TX_BUFFER_MASK];

        return x;
}

------------------------------------------------------------------

GCC generates the exact same code for both functions.  But only if "i" is
unsigned int.  If it is declared as int it generates code to noodle around
with i before using it to index into buffer.

It would be worth repeating this for the AVR target (I'm at work where I
don't have AVRGCC installed).

Cheers,
Neil

--
Neil Johnson :: Computer Laboratory :: University of Cambridge ::
http://www.njohnson.co.uk          http://www.cl.cam.ac.uk/~nej22
----  IEE Cambridge Branch: http://www.iee-cambridge.org.uk  ----


reply via email to

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