> At 11:39 10-05-05 +0200, you wrote:
>
> >void Swap_TAB_UART(unsigned char ix, unsigned char len)
> >{
> >unsigned char i, tmp, l;
> >
> >l = len/2;
> >i = 0;
> >do
> >  {
> >  tmp = TAB_UART[ix+i];
> >  TAB_UART[ix+i] = TAB_UART[ix+len-i-1];
> >  TAB_UART[ix+len-i-1] = tmp;
> >  i++;
> >  } while (i<l);
> >}
>
> Using 'complex' array indexes like these is asking for trouble (yes, I
know
> it should work...) I've seen commercial compilers choking on these sort of
> constructions as well.
> It is better to use a variable and pre-calculate the index like this:
>

There have been a few poor-quality commercial compilers for very
C-unfriendly processors that failed to generate correct code (or even *any*
code) for complex array indexes - Microchip's Pic16 compiler springs to
mind.  But I doubt if there are any such compilers still available.

Regarding gcc, handling TAB_UART[ix+len-i-1] is front-end stuff - if it did
not work correctly on the msp430, then it would not work correctly on x86
gcc, and I think someone would have noticed if that were the case.

Generating *good* code from such constructs is a different matter - there
are plenty of commercial compilers for small micros that will generate poor
code for such constructs, and thus run-time size and speed can be improved
by "hand-optomising" the C code.  One of the joys of gcc is that you don't
need to do that sort of thing to nearly the same extent - you write the C
code the way it makes best sense to write it, and let the compiler generate
target code that is close to as good as you can get from "hand-optomised" C
code.  I suspect that by the time gcc 4 is at 4.1, you will be hard pushed
to improve on the original code.

Of course, if you want to improve on the original code by pre-calculating
and manually optomising the C code, then you should do so at the pointer
level, and move the calculations outside the loop (although I suspect gcc
will do that automatically for your code):

void Swap_TAB_UART(unsigned char ix, unsigned char len)
{
 unsigned char n = len/2;
 unsigned char* p = &TAB_UART[ix];
 unsigned char* q = p + len - 1;

 do {
  unsigned char tmp = *p;
  *p++ = *q;
  *q-- = tmp;
 } while (n--);
}




> void Swap_TAB_UART(unsigned char ix, unsigned char len)
> {
> unsigned char i, tmp, l;
>
> short a,b;
>
> l = len/2;
> i = 0;
> do
>   {
>   a=ix+i;
>   b=ix+len-i-1;
>   tmp = TAB_UART[a];
>   TAB_UART[a] = TAB_UART[b];
>   TAB_UART[b] = tmp;
>   i++;
>   } while (i<l);
> }
>
>
> Nico Coesel
>
>



Reply via email to