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

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

Re: [avr-gcc-list] Missing compiler optimizations?


From: Paulo Marques
Subject: Re: [avr-gcc-list] Missing compiler optimizations?
Date: Mon, 23 Jan 2006 09:40:50 +0000
User-agent: Mozilla Thunderbird 1.0.6 (X11/20050716)

Erik Walthinsen wrote:
[...]
In other cases I've seen all kinds of relatively common idioms get translated very poorly. A classic one is constructing a word from two bytes, which in my case happens as TWI interrupts feed them to me one at a time:

  w = b | (c<<8);
  68:   99 27           eor     r25, r25
  6a:   33 27           eor     r19, r19
  6c:   32 2f           mov     r19, r18
  6e:   22 27           eor     r18, r18
  70:   82 2b           or      r24, r18
  72:   93 2b           or      r25, r19

w is 16 bits, b and c are both 8. This should be doable with just two mov's, but instead it takes 6 instructions. I understand that it's promoting the variables, but it's still inefficient.

I can not help you with the other stuff, but for this case I usually define a structure like this:

typedef struct { uint8_t l, h} BYTES;

typedef union {
        uint16_t w;
        BYTES b;
} WORD;

Then, you can just do:

        var.b.l = b;
        var.b.h = c;

and access the 16 bit value with "var.w";

Not only gcc gets this right, but it even makes the code more readble :)

I hope this helps,

--
Paulo Marques
Software Development Department - Grupo PIE, S.A.
Phone: +351 252 290600, Fax: +351 252 290601
Web: www.grupopie.com

Pointy-Haired Boss: I don't see anything that could stand in our way.
           Dilbert: Sanity? Reality? The laws of physics?




reply via email to

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