[Top][All Lists]

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

Re: [avr-gcc-list] Inconsisten behaviour of switch statement

From: Joerg Wunsch
Subject: Re: [avr-gcc-list] Inconsisten behaviour of switch statement
Date: Tue, 27 Mar 2007 09:50:55 +0200
User-agent: Mutt/1.5.11

As Schwichtenberg, Knut wrote:

> Is compiled to:
> .LM2:
>               lds r24,minute
>               clr r25
>               cpi r24,15
>               cpc r25,__zero_reg__
>               breq .L6
> .LM3:
>               cpi r24,16
>               cpc r25,__zero_reg__
>               brge .L10
>               or r24,r25
>               breq .L6
>               rjmp .L2
> .L10:
>               cpi r24,30
>               cpc r25,__zero_reg__
>               breq .L6
>               sbiw r24,45
>               brne .L2
> .L6:

Yes, but this is technically OK.  The volatile variable is read
exactly once, and then the decision is based on the value cached in
register rr24/25.  This is different from the behaviour you've been
describing in the initial posting (which I'd really consider erroneous
but could not reproduce so far).

> BTW: casting of the constant, the variable or both does not convince
> gcc to use a byte compare.

This is the "missed optimization" Eric was talking about.  Anyway,
GCC 4.1.1 gets you:

.global decide
        .type   decide, @function
/* prologue: frame size=0 */
/* prologue end (size=0) */
        lds r24,minute
        cpi r24,lo8(15)
        breq .L3
        cpi r24,lo8(16)
        brsh .L4
        tst r24
        breq .L3
        cpi r24,lo8(30)
        breq .L3
        cpi r24,lo8(45)
        brne .L5
        rcall dosomething

So it properly detects the high byte doesn't need to be taken care of.

cheers, J"org               .-.-.   --... ...--   -.. .  DL8DTL

http://www.sax.de/~joerg/                        NIC: JW11-RIPE
Never trust an operating system you don't have sources for. ;-)

reply via email to

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