Forums

Mixed fixed point Q format multiply?

Started by Unknown February 3, 2008
Hello:
I have to multiply 2 signed mixed format fixed point values on
Blackfin.
One is in "normal" Q1.31 but the other is in Q10.22.  Help?
Thanks.
flinnlouis@aol.com wrote:


> I have to multiply 2 signed mixed format fixed point values on > Blackfin. > One is in "normal" Q1.31 but the other is in Q10.22. Help?
When you multiply two numbers, the number of digits after the binary point is the sum of the digits after the binary point in the numbers being multiplied. Normally, some of those bits will be discarded. If doing it in software, you will want a processor that can generate a 64 bit product from two 32 bit numbers. (Sometimes only the high 32 bits are enough.) In hardware, you just have to get out the appropriate bits. If Q1.31 means a sign bit and 31 bits after the binary point, and Q10.22 means a sign bit, 9 bits before, and 22 bits after the binary point, and you multiply them together the product will have up to 9 bits before and 53 bits after the binary point. If you take the high 32 bits of the 64 bit (signed) product the result will be Q11.21 For a Q10.22 product you need the leftmost 33 bits of the product, with the high bit ignored. -- glen
On Feb 4, 2:15 am, glen herrmannsfeldt <g...@ugcs.caltech.edu> wrote:
> flinnlo...@aol.com wrote: > > I have to multiply 2 signed mixed format fixed point values on > > Blackfin. > > One is in "normal" Q1.31 but the other is in Q10.22. Help? > > When you multiply two numbers, the number of digits after the > binary point is the sum of the digits after the binary > point in the numbers being multiplied. Normally, some of > those bits will be discarded. > > If doing it in software, you will want a processor that can > generate a 64 bit product from two 32 bit numbers. > (Sometimes only the high 32 bits are enough.) > In hardware, you just have to get out the appropriate bits. > > If Q1.31 means a sign bit and 31 bits after the binary point, > and Q10.22 means a sign bit, 9 bits before, and 22 bits after > the binary point, and you multiply them together the product > will have up to 9 bits before and 53 bits after the binary point. > > If you take the high 32 bits of the 64 bit (signed) product > the result will be Q11.21 For a Q10.22 product you need the > leftmost 33 bits of the product, with the high bit ignored.
this guy posted to the music-dsp mailing list and i suggested that he post it here. i also said this simple thing about the subject: first, even though i never used a Blackfin, i didn't think that the Blackfin did 32x32 bit MPY or MAC as a native instruction. but i i'm pretty sure that they have a short snippet of code that does a 32x32 bit signed fractional (that is Q1.31) multiply and, checking it out superficially on the web, i think i found a place that said it takes 6 instructions. nonetheless if this operation results in: Q1.31 x Q1.31 = Q1.31 the fact is, if you can count on *just one* of the multiplicands being Q1.31, then it doesn't matter where the binary point is on the other multiplicand, as long as you accept that the product is the same species of animal. that is: Q1.31 x Q10.22 = Q10.22 will happen naturally, and you need not shift anything. but you have to know, to start with, that the multiplication naturally does Q1.31 x Q1.31 = Q1.31 . that is, that the 32-bit signed integers (Q32.0) having the identical bits of the 32-bit fractionals are multiplied, the 64-bit product (which nearly always has the 2 MSBs identical) is shifted to the left by one bit (losing one of those redundant MSBs), and your 32-bit result is taken out of the most significant word of the result (perhaps the one bad case, -1.0 x -1.0, has to be tested for and the result would be 0.999999999). now if that is what your multiply does, then all you need to assume is that *one* of the multiplicands need be Q1.31. the other multiplicand can be anything and wherever the binary point is for that multiplicand is where the binary point will be in the 32-bit product. r b-j
robert bristow-johnson wrote:

(snip)

> this guy posted to the music-dsp mailing list and i suggested that he > post it here.
> i also said this simple thing about the subject: first, even though i > never used a Blackfin, i didn't think that the Blackfin did 32x32 bit > MPY or MAC as a native instruction. but i i'm pretty sure that they > have a short snippet of code that does a 32x32 bit signed fractional > (that is Q1.31) multiply and, checking it out superficially on the > web, i think i found a place that said it takes 6 instructions.
> nonetheless if this operation results in:
> Q1.31 x Q1.31 = Q1.31
> the fact is, if you can count on *just one* of the multiplicands being > Q1.31, then it doesn't matter where the binary point is on the other > multiplicand, as long as you accept that the product is the same > species of animal. that is:
> Q1.31 x Q10.22 = Q10.22
Yes.
> will happen naturally, and you need not shift anything. but you have > to know, to start with, that the multiplication naturally does Q1.31 > x Q1.31 = Q1.31 . that is, that the 32-bit signed integers (Q32.0) > having the identical bits of the 32-bit fractionals are multiplied, > the 64-bit product (which nearly always has the 2 MSBs identical) is > shifted to the left by one bit (losing one of those redundant MSBs), > and your 32-bit result is taken out of the most significant word of > the result (perhaps the one bad case, -1.0 x -1.0, has to be tested > for and the result would be 0.999999999).
Presumably when designing filters you know not to use -1.0 for a coefficient so that won't happen.
> now if that is what your multiply does, then all you need to assume is > that *one* of the multiplicands need be Q1.31. the other multiplicand > can be anything and wherever the binary point is for that multiplicand > is where the binary point will be in the 32-bit product.
Computer hardware tradition from before DSP has been to supply the 2N bit product. That an a double length shift instruction lets you do any scaled binary multiply. I had hoped that the OP would notice that the rules are the same as for decimal multiply if you replace digits after the decimal point with bits after the binary point. -- glen