[Top][All Lists]

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

Re: [avr-gcc-list] Shorter code?

From: Paulo Marques
Subject: Re: [avr-gcc-list] Shorter code?
Date: Thu, 12 Jun 2008 15:20:18 +0100
User-agent: Thunderbird (X11/20071210)

Ruud Vlaming wrote:

Normally gcc generates well optimized code, but
sometimes i wunder how gcc can do simple things
so complicated.

Here is an example, [...]

Is there a way to write the methode defined above in C to make the generate this assembly? Some special combine function maybe?

This missed optimization happens because the compiler promotes all operands to int before doing the operations and then it actually optimizes the "<< 8" using just mov's :P

One trick I use to help the compiler is to define something like:

typedef struct { uint8_t l, h; } BYTES;

typedef union {
  uint16_t w;
  BYTES b;

This way you can define:

WORD uxTickCount;

and then access the low byte using uxTickCount.b.l and the high byte using uxTickCount.b.h.

Then the function above would become:

uint16_t genGetTickCount(void)
  return uxTickCount.w;

At which point, it is probably not very useful anymore ;)

This trick could probably be even better by using anonymous structs, but I never bothered to try it.

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

"As far as we know, our computer has never had an undetected error."

reply via email to

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