This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Fixed-point multiplication

I want to multiply two fixed point numbers. After the multiplication I have to shift the result so that the binary point is correct.
Example:

int a;
int b;
int c;
c = (a * b) >> 10

The multiplication a*b produces a long int value in the MD register. After the shift operation the int result (the lower byte) should be stored in c.
My problem is how to use then 32-bit MD register. When I look at the assembler code only the lower byte of the MD register is used for the shift operation, but I first I want to shift the whole register and then take only the lower byte.
How can I implement that in C? Who knows an application note about fixed-point arithmetic with the C166?

Parents
  • As Andrew said, this is a C issue. Take your favourite book on C and read about types and expressions. There are a few pitfalls there.
    If both operands a and b are of type int, then the product (a * b) will be of type int, which is not what you are expecting. If you want the result to be of type long, try ((long)a * b): it will suffice to cast one of the operands to long.
    Make sure you take care of overflows and that you understand the differences between signed and unsigned arithmetics.

    - mike

Reply
  • As Andrew said, this is a C issue. Take your favourite book on C and read about types and expressions. There are a few pitfalls there.
    If both operands a and b are of type int, then the product (a * b) will be of type int, which is not what you are expecting. If you want the result to be of type long, try ((long)a * b): it will suffice to cast one of the operands to long.
    Make sure you take care of overflows and that you understand the differences between signed and unsigned arithmetics.

    - mike

Children