LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Fast Reciprocal Square Root with Labview

Solved!
Go to solution

Hello,

Could you help me to call and execute the following function C++ with Labview?

The following code is the fast inverse square root implementation/approximation.

(https://en.wikipedia.org/wiki/Fast_inverse_square_root)

 

I don't understand the way to call this library/function with Labview, after having read :

1. How Do I Call a Dynamic Link Library (DLL) from LabVIEW?

2. Using External Code in LabVIEW

Thanks in advance.

 

// CPP program for fast inverse square root. 
#include<bits/stdc++.h> 
using namespace std; 
  
// function to find the inverse square root 
float inverse_rsqrt( float number ) 
{ 
    const float threehalfs = 1.5F; 
  
    float x2 = number * 0.5F; 
    float y = number; 
      
    // evil floating point bit level hacking 
    long i = * ( long * ) &y; 
      
    // value is pre-assumed 
    i = 0x5f3759df - ( i >> 1 ); 
    y = * ( float * ) &i; 
      
    // 1st iteration 
    y = y * ( threehalfs - ( x2 * y * y ) ); 
      
    // 2nd iteration, this can be removed 
    // y = y * ( threehalfs - ( x2 * y * y ) ); 
  
    return y; 
} 
  
// driver code 
int main(){ 
      
    int n = 256; 
    float f = inverse_rsqrt(n); 
    cout << f << endl; 
      
    return 0; 
} 
0 Kudos
Message 1 of 12
(4,731 Views)

Why not just code it using LabVIEW directly? 😮

0 Kudos
Message 2 of 12
(4,722 Views)

Because some data manipulations in the C code with pointers are obscur for me. The code translation is not trivial.

I tried to use the functions in the palette "Data Manipulation Functions" like the logical shift function, but I dont succeed to obtain the same result than the example in the wikipedia page.

0 Kudos
Message 3 of 12
(4,711 Views)
Solution
Accepted by topic author Ubik)

Here's how to get the first guess. Works great! (all code for the lower 3 terminals can be deleted, of course)

 

FastSGLSQRT.png

 

 

... and yes, it reproduces the wikipedia example exactly:

 

FastSGLSQRTwikiexample.png

Message 4 of 12
(4,701 Views)

As you can see, the error of the first guess is <4%.

 

FastSGLSQRTerror.png

Message 5 of 12
(4,687 Views)

Thank you very much altenbach for the LV code and your comparison. It's interesting.

 

I have just compared in term of  time passed, and I am disappointed with the slowness of this approximation because I expected this algorithm was faster than the right calculation of the reciprocal square root of number.

 

I attached the function in order to compare the two implementations in term of time elapsed (by switching the code in the Disable structure).

 

0 Kudos
Message 6 of 12
(4,679 Views)

Yes, it is slow (~450ns in my own tests), but your benchmark is meaningless. Since debugging is enabled, your loop contains additional debugging code that allows probing of wires and potentially slows it down. However, if you disable debugging, the entire loop code gets constant folded and completes in nanoseconds, no matter what N is. Both scenarios are not ideal.

 

(Also, don't label the elapsed time as ms. The time is in seconds with the current relative timer! I usually use a display format similar to "%.03ps", giving the result in SI units (e.g. "452.781ns").

 

Yes, I would have expected it to be faster, could be due to implicit endian conversions. Not sure....

0 Kudos
Message 7 of 12
(4,662 Views)

(nevermind)

 

 

 

Message 8 of 12
(4,658 Views)

I think you missed this bit

 

x2 = number * 0.5F;

and also the 1.5 stuff at the end.

 

I get quite a lot more accurate results on the first iteration, something like within .1 %

 

NeilPate_0-1663431007621.png

 

Apologies for the messy code, I am sick in bed with my laptop!

 

Message 9 of 12
(2,665 Views)

My code was based on the worked example. Did not study the rest in detail.

0 Kudos
Message 10 of 12
(2,647 Views)