02-02-2019 01:38 PM
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; }
Solved! Go to Solution.
02-02-2019 01:53 PM
Why not just code it using LabVIEW directly? 😮
02-02-2019 02:06 PM - edited 02-02-2019 02:08 PM
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.
02-02-2019 02:35 PM - edited 02-02-2019 03:07 PM
Here's how to get the first guess. Works great! (all code for the lower 3 terminals can be deleted, of course)
... and yes, it reproduces the wikipedia example exactly:
02-02-2019 03:01 PM
02-02-2019 03:33 PM - edited 02-02-2019 03:51 PM
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).
02-02-2019 04:09 PM
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....
02-02-2019 04:21 PM - edited 02-02-2019 04:25 PM
09-17-2022 11:06 AM - edited 09-17-2022 11:14 AM
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 %
Apologies for the messy code, I am sick in bed with my laptop!
09-17-2022 12:15 PM
My code was based on the worked example. Did not study the rest in detail.