Shop OBEX P1 Docs P2 Docs Learn Events
Spin Float Vs Simple IDE Float — Parallax Forums

Spin Float Vs Simple IDE Float

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.


Comments

  • There is a newer, faster, more accurate floating point library in the Object Exchange (F32) that uses a CORDIC engine for the transcendentals. It's intended as a substitute for Float32Full. Try it.
  • Thanks, I will.
  • greybeard, thanks for providing this benchmark. Could you also tell us what memory model you used and how large the program was for each test?
  • Update.
    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.
  • The accuracy should be better too
  • In Spin, the Float engine runs in pure ASM in a dedicated cog. I'm not sure if the same is true for C. It might actually be doing the computation as compiled code.

    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.
  • I would bet you are correct Jason - the C version almost certainly runs in the same cog. I won't have a chance to test it for a few days though.
  • AribaAriba Posts: 2,682
    See this thread for faster Float in C. It uses a cog as float coprocessor:
    forums.parallax.com/discussion/142523/using-a-cog-as-a-floating-point-coprocessor/p1

    Andy
  • Andy, that's awesome! Thanks for pointing me toward that. I have a port of F32 in the flight controller object, and I love the idea of being able to subvert the built-in float lib to call my functions instead.
Sign In or Register to comment.