Spin Float Vs Simple IDE Float
greybeard
Posts: 65
in Propeller 1
This is a post for informational purposes only. Please no Flames.
I recently had the occasion to port some specific code from SPIN to C using the Simple IDE. The target was an old Demo board using 5MHz crystal. The eventually target implementation depends more on the speed of the calculation and less on the precision. The basic timing concerned calculating the quantity
v = 1.65 *(1 + sin(x)) where x varied from 0 to PI/2.
I used the SPIN Float32Full library for the SPIN program and the C Math library for the C code program.
Basically I captured the clock counts before a and after the calculation. Incrementing the value of x after capturing the second count.
Further I ran the test in increments of 0.1794533 radians (5 degrees) -- an arbitrary choice
The results: The SPIN were consistent at 19504 counts for each calculations
The C Float results varied from 27152 at 0 radians (0 degrees) to max of 305488 counts at 0.785398 radians (45 degrees).
At 45 Degrees the results were 2.81672 from the SPIN float library and 2.816726 from the C float library.
It takes more lines of code to implement the desired calculations using the SPIN Float32Full library than with the C math library (3 lines vs 1 line) but the operations are the same i.e. function call to sin(x), addition, multiplication and final storage in the variable.
I believe the reason for the difference is why the sin(x) is calculated: The SPIN float library uses look up tables resident in the propeller. I suspect the C Math library uses the a series to do calculate the sine but with a convergence criteria that will affect the number of iterations required for convergence.
In short, SPIN Float library is considerably faster that the C Math library BUT the C Math library will be yield a higher precision result, especially for very small angles.
I haven't looked the Log/Ln calculations but I suspect a similar result would be obtained.
I recently had the occasion to port some specific code from SPIN to C using the Simple IDE. The target was an old Demo board using 5MHz crystal. The eventually target implementation depends more on the speed of the calculation and less on the precision. The basic timing concerned calculating the quantity
v = 1.65 *(1 + sin(x)) where x varied from 0 to PI/2.
I used the SPIN Float32Full library for the SPIN program and the C Math library for the C code program.
Basically I captured the clock counts before a and after the calculation. Incrementing the value of x after capturing the second count.
Further I ran the test in increments of 0.1794533 radians (5 degrees) -- an arbitrary choice
The results: The SPIN were consistent at 19504 counts for each calculations
The C Float results varied from 27152 at 0 radians (0 degrees) to max of 305488 counts at 0.785398 radians (45 degrees).
At 45 Degrees the results were 2.81672 from the SPIN float library and 2.816726 from the C float library.
It takes more lines of code to implement the desired calculations using the SPIN Float32Full library than with the C math library (3 lines vs 1 line) but the operations are the same i.e. function call to sin(x), addition, multiplication and final storage in the variable.
I believe the reason for the difference is why the sin(x) is calculated: The SPIN float library uses look up tables resident in the propeller. I suspect the C Math library uses the a series to do calculate the sine but with a convergence criteria that will affect the number of iterations required for convergence.
In short, SPIN Float library is considerably faster that the C Math library BUT the C Math library will be yield a higher precision result, especially for very small angles.
I haven't looked the Log/Ln calculations but I suspect a similar result would be obtained.
Comments
I downloaded the F32 library and plugged it into the same test program. The number of Spin counts went for 19504 to 12832-- about 33% increase in speed.
BTW. The prop was running at 5MHz X16 or 80MHZ.
David Zemon or David Betz would likely know for sure. You could run a simple test to find out though - compile in LMM and CMM and see how much of a difference there is in speed. If the difference is large, chances are the float functions are running as part of your code.
forums.parallax.com/discussion/142523/using-a-cog-as-a-floating-point-coprocessor/p1
Andy