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

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

Re: [avr-gcc-list] Urgent Queries about AVR-GCC


From: David Gay
Subject: Re: [avr-gcc-list] Urgent Queries about AVR-GCC
Date: Tue, 03 Jun 2003 09:26:19 -0700
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.3) Gecko/20030314

Joerg Wunsch wrote:
"Suresh B Joshi" <address@hidden> wrote:
(2) When using the switch statement, even if the control
   expression is an unsigned char variable, it is promoted
   to 16 bit unsigned int, by eor R25,R25 instruction.


IMHO, this is required by the C standard.  Also, you quote enums as an
example, they are (unfortunately) always equivalent to type "int".

IMNSHO that's a bogus argument. Sure, the C standard says that the standard integer promotions are applied to the switch expression. But a compiler is only ever required to behave "as-if" the standard is followed, so in the case of a switch on an unsigned 8-bit integer, it can discard all cases < 0 and > 255, then do byte comparisons.
Of course, changing gcc to do this is probably not trivial...

An example where gcc does do this kind of optimisation is:

  char a, b;
  ...
  if (a == b) { ... }

The C rules say that a and b are promoted to int before the comparison, but gcc (of course) chooses to compare them with a byte comparison.

An example that generates large code which would be hard to remove is:

  signed char x = ...;
  unsigned char y = ...;

  if (x == y) { ...
  }

The comparison has to promote both x and y to int, then compare them (the compiler could also choose to test the high bit of x or y and fail the comparison if it fails, but that shouldn't be much shorter).

David Gay
address@hidden



reply via email to

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