取消
显示结果 
搜索替代 
您的意思是: 

Fast Reciprocal Square Root with Labview

已解决!
转到解答

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 项奖励
1 条消息(共 12 条)
6,125 次查看

Why not just code it using LabVIEW directly? 😮

0 项奖励
2 条消息(共 12 条)
6,116 次查看

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 项奖励
3 条消息(共 12 条)
6,105 次查看
解答
已被主题作者 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

4 条消息(共 12 条)
6,095 次查看

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

 

FastSGLSQRTerror.png

5 条消息(共 12 条)
6,081 次查看

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 项奖励
6 条消息(共 12 条)
6,073 次查看

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 项奖励
7 条消息(共 12 条)
6,056 次查看

(nevermind)

 

 

 

8 条消息(共 12 条)
6,052 次查看

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!

 

9 条消息(共 12 条)
4,059 次查看

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

0 项奖励
10 条消息(共 12 条)
4,041 次查看