[Top][All Lists]

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

RE: [avr-gcc-list] optimization and interrupt

From: Ben Mann
Subject: RE: [avr-gcc-list] optimization and interrupt
Date: Thu, 10 Mar 2005 07:29:41 +0800


The optimised code is correct.

It is actually doing both comparisons, because it:

subtracts 113 from diffcapt (values below 113 become greater than 0xFF8F)
compares diffcapt to 24 (137 - 113)
branches if same or higher.

It is faster and more efficient than two comparisons

Ben Mann

-----Original Message-----
From: address@hidden
[mailto:address@hidden On Behalf Of Jörg
Sent: Wednesday, 9 March 2005 11:44 PM
To: address@hidden
Subject: [avr-gcc-list] optimization and interrupt

avr-gcc behaves strange when optimizing interrupt code.
In my interrupt routine I compare a variable against two constants. I added
the code at the end of this posting. After compiling I found only one
comparison (113) in the assembler code:

movw r24,r20          
sts (lastcapt)+1,r19  
sts lastcapt,r18      
subi r24,lo8(-(-113)) 
sbci r25,hi8(-(-113)) 
sbiw r24,24           
brsh .L1              

After changing optimization level to 0 i got:

std Y+3,r24
std Y+4,r25
ldd r24,Y+1
ldd r25,Y+2           
sts (lastcapt)+1,r25  
sts lastcapt,r24      
ldd r24,Y+3           
ldd r25,Y+4           
cpi r24,113           
cpc r25,__zero_reg__  
brlo .L1              
ldd r24,Y+3           
ldd r25,Y+4           
cpi r24,137           

So here a two comparisons (113, 137) as expected.
It helps to declare diffcapt to volatile but that doesn't
make sense to me as it is only used in the interrupt
I hope someone has a logical answer, let me believe
in gcc again.

Regards, Joerg

volatile uint16_t       lastcapt;

        uint16_t actcapt;
        uint16_t diffcapt;

        actcapt = ICR1;

        diffcapt = actcapt - lastcapt;
        lastcapt = actcapt;

        if( (diffcapt > 112)  &&  (diffcapt < 137) )

AVR-GCC-list mailing list

reply via email to

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