At 14:58 28-04-08 +0000, you wrote: >On 2008-04-28, Ralf Hildebrandt <[email protected]> wrote: > >> First of all: msp430-gcc and IAR have the same behavior >> >> my_long = (long)my_int_a * my_int_b; -- 32bit = 16bit * 16bit; >> >> But is this a violation of the standard? > >No. > >> I don't think so. If there is a cast of one operand to type >> long the compiler shall promote the other operand too, because >> arithmetics shall always use the "bigger" type. Up to here I >> agree with you. But after this the compiler is free to >> optimize everything away that is not needed to get a >> mathematically correct result. For the multiplication this >> means, that the upper 16 Bits of the two operands are not >> needed for a 32 bit wide result. Therefore the compiler is >> free to reduce the multiplication to a 16bit*16bit one. > >Indeed. It can be proven mathematically that when the upper 16 >bits of both operands are 0, the 32 bit result is the same for >a 16*16 multiply as it is for a 32*32 multiply. Therefore, the >compiler is free to do a 16*16 multiply, and many compilers >quite correctly do so.
I don't think this is true for signed multiplications. Can't reproduce the exact why and how right now, but I had some problems with signed multiplication in FPGA designs in which I had to extend the sign bit to all unused bits. Nico Coesel
