N. Coesel wrote:

> With optimisation on, anything may happen (math rules will be
> obeyed). In this case (multiply by 2) the compiler will probably use a
> shift instead of the multiplier.

Scrap the snippet.  Here's what is relevant to the problem at hand:

--->8---

#include <stdint.h>

int32_t a;
uint16_t b;

// assign value to b ...

a = -(b * 17u);

--->8---

The thing here is that if that piece of code is translated by
a standard conforming compiler, it will _always_ make the result
of the multiplication a uint16_t leading to the wrong result as
multiplication of two uint16_t quantities is not closed over
the set of values of uint16_t.  If, however, the line is
rewritten as

a = -(((int32_t )b) * 17u);

or even

a = -(((uint32_t )b) * 17u);

the correct result follows with the drawback of requesting a 32 bit
unsigned int multiplication (which I know is not available) yielding
a 32 bit result instead of a 16 bit unsigned int multiplication
yielding a 32 bit result that _is_ actually available in hardware
on the MSP.  What I am interested in is the "canonical" way of
having the compiler emit either of the MPY or MPYS assembler
insns _at_all_times_.  Can this be propagated via C alone ?


Cheers,
Christian


Reply via email to