Complex SX math (is this possible)
Direct Digital Labs
Posts: 27
I need to calculate a ratio and then figure out from that ratio what the value of the data is based upon the known quotient data that the ratio was derived from.
·
I am curious as to what the best way to approach this in ASM is. Even if someone can suggest a way in SX/B so I can get the idea I should be able to get the theory working and port it over to ASM.
·
For an example, if the ratio is based on lets say a quotient of 64.000. I need to calculate what 1 : 0.75 of 64.000 is or 75% which would be 48.000. The quotient that the data will be derived from will always be a whole number.
·
The data that is collected is two bytes in a 5.11 format. Where the first 5 bits represent the integer portion of the ratio and the following 11 bits represents the fraction portion.
·
Ratio = 1 : IIIII.FFFFFFFFFFF······················ ;I = Integer, F = Fraction
·
Example 1:0.75 = %0000011000000000
·
Resolution of each value
F = 0 to 31
I = 2048/0 to 2047· or· ·································· ;approximately 0.0005 to 0.9995
·
I can separate the Integer and Fraction by a simple masking and rotation of the bits into 3 bytes. The First byte holds the integer. Second byte holds the upper bits of the fraction and the Third byte holds the lower 8 bits of the fraction.
·
I do not need the full 11 bit resolution of the fraction and would be happy with an 8 bit fraction as this would obviously make the calculations much easier. Would simple truncation of the 3 LSB’s work?
·
Where I = 255/0 to 254·································· ;approximately 0.0039 to 0.9961
·
Also the largest output value in my application of the Integer would be 215 so there is no worry of overflowing an 8 bit value on the Integer portion of the output.
·
I do not need the Ratio just the final number (such as 48.000 in my example). Number of clock cycles is not a huge factor as this is a non time critical calculation and I should have a whole page or two available for code and a bank or two of RAM if need be in an SX48.
·
Is this even possible with the limited math functions of the SX?
·
I’m not asking for code just curious on theories on how this could be done.
·
-Dan-
·
I am curious as to what the best way to approach this in ASM is. Even if someone can suggest a way in SX/B so I can get the idea I should be able to get the theory working and port it over to ASM.
·
For an example, if the ratio is based on lets say a quotient of 64.000. I need to calculate what 1 : 0.75 of 64.000 is or 75% which would be 48.000. The quotient that the data will be derived from will always be a whole number.
·
The data that is collected is two bytes in a 5.11 format. Where the first 5 bits represent the integer portion of the ratio and the following 11 bits represents the fraction portion.
·
Ratio = 1 : IIIII.FFFFFFFFFFF······················ ;I = Integer, F = Fraction
·
Example 1:0.75 = %0000011000000000
·
Resolution of each value
F = 0 to 31
I = 2048/0 to 2047· or· ·································· ;approximately 0.0005 to 0.9995
·
I can separate the Integer and Fraction by a simple masking and rotation of the bits into 3 bytes. The First byte holds the integer. Second byte holds the upper bits of the fraction and the Third byte holds the lower 8 bits of the fraction.
·
I do not need the full 11 bit resolution of the fraction and would be happy with an 8 bit fraction as this would obviously make the calculations much easier. Would simple truncation of the 3 LSB’s work?
·
Where I = 255/0 to 254·································· ;approximately 0.0039 to 0.9961
·
Also the largest output value in my application of the Integer would be 215 so there is no worry of overflowing an 8 bit value on the Integer portion of the output.
·
I do not need the Ratio just the final number (such as 48.000 in my example). Number of clock cycles is not a huge factor as this is a non time critical calculation and I should have a whole page or two available for code and a bank or two of RAM if need be in an SX48.
·
Is this even possible with the limited math functions of the SX?
·
I’m not asking for code just curious on theories on how this could be done.
·
-Dan-
Comments
And therein lies the dilemma - time.
If you want the SX to be a dedicated math co-processor it is only 8 bit and there is a perfectly good 32 bit co-processor sold by Parallax.
If you want to NOT use a coprocessor, it depends entirely on the timing of the task.
Most of the problem is time consumed in making an 8 bit device handle·32 bit mathematics. You need to move 4 bytes about to get each denominator and numerator into an appropriated position.
Then because all multiply and divide is binary [noparse][[/noparse]done by shift left and shift right], these 'shift functions' must carry through 4 registers. A lot of housekeeping.
Of course, there is your pesky little decimal point. That decimal point is Base 10, not Binary. So, you must convert all your decimal numbers to binary before you do the math rather than just try to convienently 'pop in a decimal point'. That requires another set of registers and additional math to keep track to the Decimal point.
Seems that you are trying to mix Decimal and Binary operations.
Is that enough theory?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"When all think alike, no one is thinking very much.' - Walter Lippmann (1889-1974)
······································································ Warm regards,····· G. Herzog [noparse][[/noparse]·黃鶴 ]·in Taiwan
Post Edited (Kramer) : 2/2/2006 11:32:21 AM GMT
Your example says 64.000.
So does that mean that the quotient has a fractional part also ?
Is it also 5.11 format ?
Assuming that the quotient is 5.11 also, you want to do a 16x16 multiply and the 32-bit result will be 10.22 format.
SXList has two programs for 16x16 mult: http://www.sxlist.com/techref/ubicom/lib/math/mul.htm
[noparse][[/noparse]edit] Oops, just realized that 64.000 will not fit a 5.11 format.·So what format is it ?
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"SX-Video·Module" Now available from Parallax for only $28.95
http://www.parallax.com/detail.asp?product_id=30012
"SX-Video OSD module" Now available from Parallax for only·$49.95
http://www.parallax.com/detail.asp?product_id=30015
Product web site: www.sxvm.com
"Ability may get you to the top, but it takes character to keep you there."
Post Edited (Bean (Hitt Consulting)) : 2/2/2006 1:26:12 PM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·1+1=10
For example X = Y + Zi
Such math is used for calculating Impedance rather than Resistance in filters and tuned circuits.
I don't think you really what to do that on an SX chip.
It really comes down to the 'mission' of the microcontroller is not to be a numerical calculator. It has all those 'autonomous' I/O pins to control things or distribute data serially in several directions.
It can do it. It can teach you how to do such programing. But starting with your 'formating idea' is really a hard way to learn. You should do some 8 bit math, then some 16 bit math, and so on. Walk before you run. In this way you will appreciate the limitations and ability to use data tables for repetative tasks rather than brute force calculations. The coding is much simpler too.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"When all think alike, no one is thinking very much.' - Walter Lippmann (1889-1974)
······································································ Warm regards,····· G. Herzog [noparse][[/noparse]·黃鶴 ]·in Taiwan
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·1+1=10
·
The simplest form of the calculations is as follows
·
f/2048 + i * q = n
·
Where f = fractional, I = integer, q = quotient and n being the desired result.
I’d be happy with a result that went out only to the hundredths.
·
I’ve been using stamps for over 5 years and just got on board with the SX about a year ago and am familiar with most math functions on the SX but the simplest way to handle this to me is unclear.
Seeing now the math is not that complex however dealing with the large fractional and decimal point is.
Thanks for all the feedback!
-Dan-
Post Edited (Direct Digital Labs) : 2/2/2006 5:53:34 PM GMT
I assume you meant "(f/2048 + i) * q = n" ?
What are the 6 static quotents ? What is the range of i ?
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"SX-Video·Module" Now available from Parallax for only $28.95
http://www.parallax.com/detail.asp?product_id=30012
"SX-Video OSD module" Now available from Parallax for only·$49.95
http://www.parallax.com/detail.asp?product_id=30015
Product web site: www.sxvm.com
"Ability may get you to the top, but it takes character to keep you there."
·
"(f/2048 + i) * q = n"
the 6 statics "q"·are· 32, 44.1, 48, 64, 88.2, 96
The range of "i" is 0 to 6
-Dan-
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·1+1=10
-Dan-
http://www.piclist.com/techref/piclist/codegen/constdivmul.htm·will generate code for you to do the equation in the shortest amount of time (be sure to select the SX for the processor). Depending on how the quotients are used, you can reduce the amount of code by recognising that many of the quotients are multiples of 2 of other quotients, meaning you can use code that does a (f/2048 + i)* 44 and shift the result to the left once to perform the same calculation of the quotient 88.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·1+1=10
(f + 2048*i) * q = 2048*n
n = ((f + (i<<11))*q)>>11
Simple integer add, multiply and shifts!
Like Paul said, q·being integer only gives little error
regards peter
Thank you very much for your time.
-Dan-
n = ((f*q)>>11) + (i*q)
n = (((f>>3)*q)>>8) + (i*q)
max value: (((2048>>3)*96)>>8) + (6*96) = (24576>>8) + (576) = 672
16bit math should suffice
regards peter
·
Thanks again!
·
-Dan-
Thanks, BBAL
Here is the link to it: www.parallax.com/detail.asp?product_id=604-00030a
I'm not sure about Tan, but I know you can do SIN, COS, and ATAN with CORDIC functions.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"SX-Video·Module" Now available from Parallax for only $28.95
http://www.parallax.com/detail.asp?product_id=30012
"SX-Video OSD module" Now available from Parallax for only·$49.95
http://www.parallax.com/detail.asp?product_id=30015
Product web site: www.sxvm.com
"Ability may get you to the top, but it takes character to keep you there."
Post Edited (Bean (Hitt Consulting)) : 2/3/2006 1:56:07 PM GMT
Danke!
BBAL