[Top][All Lists]

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

Re: [avr-gcc-list] GCC 3.0.2 unsigned int bug?

From: Chris Elmquist
Subject: Re: [avr-gcc-list] GCC 3.0.2 unsigned int bug?
Date: Mon, 17 Dec 2001 18:01:14 -0600
User-agent: Mutt/1.2.5i

On Monday (12/17/2001 at 01:29PM -0800), Bruce D. Lightner wrote:
> Apparently, this is a "feature", not a bug.  You no doubt have the "avr-gcc"
> optimizer turned on (e.g., "-Os) and thereby gave it free license to do
> anthing it wants to your loop code.  The newest releases of "avr-gcc" does
> just that.  I would expect the optimizer to remove the loop completely, but
> instead it decrements the loop count by a seemingly arbitrary large number
> on each iteration.
> You can probably fix the problem by either declaring "d" volatile, but you
> won't believe big the code gets that "avr-gcc" generates in that case! 
> Also, you can make "d" global by moving it outside the delay() routine,
> which gives you tight code, but costs a couple of bytes of RAM.
> I just "got bit" by this one myself when re-compiling some old AVR C-code
> under the new "improved" version of "avr-gcc".  This comes up so often in
> this discussion list that I'm tempted to hunt this one down and fix it!
> Here's what I ended up doing...
>    static void dummy(void) { }
>    void delay(void)
>    {
>        unsigned short count = 25000;
>        while(--count) dummy();
>    }
> This gives one a little better AVR code (and the correct behavior even with
> the "optimizer" turned on).  But I can't promise that sometime in the future
> that "gcc" might force one to move "dummy()" to a separately compiled
> module!

Hi Bruce...  thanks for the reply.  Sure enough, the -Os was in effect and
it was optimizing the thing down to uselessness :-)  Of course, I have
bigger plans for the lousy loop but had never seen GCC so that kind of
"random" thing to me before.

In any case, here's my "improved" loop, suitable for fooling the optimizer

void delay(void)
        unsigned int    d;

        for (d=0x1000; d; d--)
                __asm__ __volatile__ ( "nop" : : );

0000004a <delay>:
  4a:   80 e0           ldi     r24, 0x00       ; 0
  4c:   90 e1           ldi     r25, 0x10       ; 16
  4e:   00 00           nop
  50:   01 97           sbiw    r24, 0x01       ; 1
  52:   e9 f7           brne    .-6             ; 0x4e
  54:   08 95           ret

which is more like what I was expecting.

Thanks for the help.



Chris Elmquist      mailto:address@hidden     http://www.pobox.com/~chrise

reply via email to

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