Everyone,

> I'm using msp430-gcc 3.2.2, which upto now worked fine, but 
> is giving me strange behaviour now. The following code (which when compiled
> identically for the PC works perfectly), produces incorrect results when
> compiled by msp430-gcc -O2:
> 
> #define GETBIT(d,n) (((d)>>(n))&1)
> // calculates 4 parity bits for (8,12) hamming
> unsigned char hamming_parity(unsigned char d)
> {
>  unsigned char HP1,HP2,HP3,HP4;
> 
>  // calculate parity bits
>  HP1 = GETBIT(d,0) ^ GETBIT(d,1) ^ GETBIT(d,3) ^ GETBIT(d,4) ^ GETBIT(d,6);
>  HP2 = GETBIT(d,0) ^ GETBIT(d,2) ^ GETBIT(d,3) ^ GETBIT(d,5) ^ GETBIT(d,6);
>  HP3 = GETBIT(d,1) ^ GETBIT(d,2) ^ GETBIT(d,3) ^ GETBIT(d,7);
>  HP4 = GETBIT(d,4) ^ GETBIT(d,5) ^ GETBIT(d,6) ^ GETBIT(d,7);
> 
>  // combine into parity nibble
>  return (HP4<<3)|(HP3<<2)|(HP2<<1)|HP1;
> }

For those that experience the same bug, I thought I'd post the way I was able 
to get round this: simply encapsulate the macro in a function, like this:

#define BIT(d,n) getbit(d,n)
static unsigned char inline getbit(unsigned char d, unsigned char n) {return 
(((d)>>(n))&1);}
// calculates 4 parity bits for (8,12) hamming
unsigned char hamming_parity(unsigned char d)
{
        unsigned char P1,P2,P3,P4;

        // calculate parity bits
        P1 = BIT(d,0) ^ BIT(d,1) ^ BIT(d,3) ^ BIT(d,4) ^ BIT(d,6);
        P2 = BIT(d,0) ^ BIT(d,2) ^ BIT(d,3) ^ BIT(d,5) ^ BIT(d,6);
        P3 = BIT(d,1) ^ BIT(d,2) ^ BIT(d,3) ^ BIT(d,7);
        P4 = BIT(d,4) ^ BIT(d,5) ^ BIT(d,6) ^ BIT(d,7);

        // combine into parity nibble
        return (P4<<3)|(P3<<2)|(P2<<1)|P1;
}

Maybe someone with the needed time and expertise can have a look at what causes 
the compiler to emit the bad code (see original post) in the first place? Or is 
this bug maybe already fixed in a more recent version of the compiler?

greetings,
Tom

Reply via email to