While I don't have access to the GCC source code and no idea of its inner workings, I can offer one of my 'wild guesses' (TM) which turn out to be at least partially correct to a surprisingly high percentage ;)
The second approach does an 8x8 bit multiplication, resulting in a 16 bit intermediate result. This delivers the correct value to the following division. But in the first approach, the compiler could optimize the whole thing to a ((A<<8)-A) which is faster and smaller than a multiplication with 255. Unfortunately this might be done on 8 bit range, dropping the upper 8 bit, or subtracting the shifted value instead of the original one, resulting in always zero. It is even possible that the whole calculation is precalculated at compile time, as a and b contain known values. JMGross ----- Ursprüngliche Nachricht ----- Von: Hardy Griech An: GCC for MSP430 - http://mspgcc.sf.net Gesendet am: 28 Apr 2009 21:32:12 Betreff: [Mspgcc-users] Fwd: DEFINEd value messes up 8-bit multiplication. Why?] > I have a problem with multiplication when I compile for the sky platform. > I have the following C code: >#define PRR_SCALE 255 ... >uint8_t a = 3; >uint8_t b = 4; >uint8_t prr; >prr = (PRR_SCALE * a) / b; >printf("prr: %u\n", prr); >If I compile this the result is 0 while I expected 191. > >If I change it to: >uint8_t a = 3; >uint8_t b = 4; >uint8_t c = 255; >uint8_t prr; >prr = (c * a) / b; >printf("prr: %u\n", prr); >it works out correctly and prints 191. _______________________________________________ Mspgcc-users mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/mspgcc-users
