#120591 - sammyjojo - Sun Mar 04, 2007 10:35 pm
Okay so from the way I understand it, if you have a two fixed-point numbers in N.M format (we'll just assume that the N and M are the same for both numbers) and you mutiply them together, you'll get a fixed-point number in 2N.2M format. For example if I have a 16.16 number and I multiply it with another 16.16 number I'll get a 32.32 number.
I figured out a way to multiple two N.M numbers without needed a 32.32 inbetween number, but it takes too many operations...
or I can cast the variable to a 8-byte one for the multiplication and then shift down and cast it back to 4-bytes...
Are these my only alternatives or is a there a better way that I'm just thinking of to where I don't need an intermediate 8-byte value?
This is for some fixed-point stuff that I working on the for the DS, so I was also wondering casting to a long long for intermediate step even work on that system? Being a 32-bit system would that just mean it could access a 64-bit value by taking 2 accesses to get 2 32-bit values that it would magically combine and figure out a way to work with it as a 64-bit value internally? So roughly working with 8-byte values instead of 4-byte ones would take twice as long or am I just completely thinking about this in the wrong way?
I figured out a way to multiple two N.M numbers without needed a 32.32 inbetween number, but it takes too many operations...
Code: |
int fractionalMask = ~(~0 << M); int magnitude = value0 >> M; int rightMagnitude = value1 >> M; int fractional = value0 & fractionalMask; int rightFractional = value1 & fractionalMask; int result = ((magnitude * rightMagnitude) << M) + (magnitude * rightFractional) + (fractional * rightMagnitude) + ((fractional * rightFractional) >> M); |
or I can cast the variable to a 8-byte one for the multiplication and then shift down and cast it back to 4-bytes...
Code: |
int result = static_cast<int>((static_cast<long long>(value0) * static_cast<long long>(value1)) >> M); |
Are these my only alternatives or is a there a better way that I'm just thinking of to where I don't need an intermediate 8-byte value?
This is for some fixed-point stuff that I working on the for the DS, so I was also wondering casting to a long long for intermediate step even work on that system? Being a 32-bit system would that just mean it could access a 64-bit value by taking 2 accesses to get 2 32-bit values that it would magically combine and figure out a way to work with it as a 64-bit value internally? So roughly working with 8-byte values instead of 4-byte ones would take twice as long or am I just completely thinking about this in the wrong way?