Kris Heidenstrom wrote:
AFAIK and as has been pointed out, operand sizes for
multiplication in C determine the width of the result:

16 x 16 gives a 16-bit result
32 x 32 gives a 32-bit result.

The MSP's hardware multiplier does 16 x 16 with a
32-bit result, but there's no general way to ask
for that in C. I just wrote a function to do it,
using assembly language to access the hardware
multiplier directly.

unsigned long umul_161632(unsigned int, unsigned int);

The best way to write this function is:

unsigned long umul_161632(unsigned int a, unsigned int b) {
    return (unsigned long) a * b;
}


The compiler can *do* this! This little discussion here is not the first time anyone has thought of 16x16->32 or 8x8->16 multiplication, and this is not the first gcc port to consider it.

The rules of C mean that operations are independent of the result type or return type, so you must cast one of the operands to a larger type to get the correct result (the other operand will automatically be coerced into the larger type). But the compiler is smart enough to figure out that it can use a smaller and faster multiply operation.




Reply via email to