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: Dave Hansen
Subject: Re: [avr-gcc-list] Serial Communication
Date: Thu, 29 Jan 2004 12:15:36 -0500

From: Neil Johnson <address@hidden>
[...]
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).

OK, I tested it with avr-gcc, and the resulting functions were identical.

Just to play around a little, I changed the type of the second parameter (i) in each function to unsigned char. The code using the mod (%) operator was actually two bytes shorter (42 vs. 44 bytes): the code using & converted i to a two byte value before performing the AND. But the AND was performed in all cases.

Regards,
  -=Dave

_________________________________________________________________
Let the new MSN Premium Internet Software make the most of your high-speed experience. http://join.msn.com/?pgmarket=en-us&page=byoa/prem&ST=1



reply via email to

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