I am trying to multiply the A * B in a 16-bit fixed point, While keeping as much accuracy as possible while keeping. In the integer category signed in A 16-bit, B is divided by 1000 and always 0.001 and 9.999 Between. This is happening sometime when I have been dealing with such problems, so:
- I know that after going to 32-bit variable, simply enter
A * B / 1000 < / Code> I can, - I want to do all the work without moving it to 32-bit (because I 'only have 16-bit multiplication)
Is there any easy way to do this?
Edit: Edit: Comes from A <0,0> 0 and 4000, so all possible results are also in the 16-bit range. B user, Set digit-to-digit number in X.XXX mask, this is the reason that the operation / 1000 .
No, you have to go up to 32 bits. In general, the products of two 16-bit numbers will always give you 32 bit detailed results.
You should check the CPU instructions set which you are working on, because most of the multiplying instructions on 16 bit machines have the option of returning results directly to 32 bit integers.
> This will help you because:
Brief test work (Small A, Small B) {int A32 = a; Int B32 = b; Return A32 * B32 / 1000} For compile the compiler to multiply 32bit * 32bit on your machine it can be done very slowly or in many stages, 16bit polygon by using.
A small inline assembly or better than a compiler can greatly enhance the internal things
Here is an example for Texas Instruments C64X + DSP, Which are such intrinsicities:
Short examination (Small A, Small B) {int product = _mpy (a, b)); // Calculates the product, return 32 bit integer product / 1000; } Another thought was: You are dividing by 1000. Is this your choice constant? To use the power of two as the basis of your fixed point numbers, it will be very fast. 1024 is close to why you do not do this:
Change (a * b) / 1024 < / Code> Change? The compiler can adapt it by using 10 bits using a shift. It should be faster than mutual multiplication.
Comments
Post a Comment