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

## Re: [avr-gcc-list] Optimiser bloats code

 From: Wouter van Gulik Subject: Re: [avr-gcc-list] Optimiser bloats code Date: Wed, 1 Aug 2007 11:33:02 +0200 (CEST) User-agent: SquirrelMail/1.4.8

> Return values are promoted to an int.
>

Why? Is this a bug or a feature? Am I doing something wrong or is an u08
return always promoted to an int?

> You probably already know this, but you could also do:
>
> return PINB >> 5;
>
> which returns the same answer using the following:
>
>         in r24,54-0x20
>         swap r24
>         lsr r24
>         andi r24,0x7
>         clr r25
>         ret
>

Yes I know, (it is written above my example) I wanted to point out how bad
the results is when compiler start to "optimise" this.

Just curious, is there any faster way to bit invert as in my foo3 example
(see below). It now takes 9 instructions which is good but less is always
better.
A loop requires more instructions and is much slower. Anyone an idea on
smaller bit inversion for just 3 bits? Because if this is the smallest
way, you cant tell the compiler to do so :(

HTH

Wouter

//Force the compiler and voila! Optimal!
//Not bit inverted or bit inverted, the result is the same
uint8_t foo3(void) { //good
e0:   88 27           eor     r24, r24

uint8_t temp = 0;
asm volatile("clr %0" : "=r" (temp) :);
if(PINB & (1<<5)) temp |= (1<<2);
e2:   1d 99           sbic    0x03, 5 ; 3
e4:   84 60           ori     r24, 0x04       ; 4
if(PINB & (1<<6)) temp |= (1<<1);
e6:   1e 99           sbic    0x03, 6 ; 3
e8:   82 60           ori     r24, 0x02       ; 2
if(PINB & (1<<7)) temp |= (1<<0);
ea:   1f 99           sbic    0x03, 7 ; 3
ec:   81 60           ori     r24, 0x01       ; 1

return temp;
}
ee:   99 27           eor     r25, r25
f0:   08 95           ret