Is rsqrt the same as Fast inverse square root?

Hello,
I would like to us sqrt() and run it on a embeded controller. I was wondering sqrt() is the same as Fast inverse square root ( https://en.wikipedia.org/wiki/Fast_inverse_square_root ). if not, is it efficent as Fast inverse square root. Does it support code generation? Can I use it in embeded control software?
Thanks,
Hongbo

4 Comments

The sqrt is not an inverse square root, but the normal square root. The implementation is hidden, but you can assume Mathworks engineers are aware of this method and will have tried to use it in an optimization if possible.
I would be highly surprised if this wouldn't be supported for code generation.
@Rik, Sorry, a typo, it's rsqrt(). The help document says rsqrt() is faster than sqrt(). From the name, it looks like rsqrt() is Fast_inverse_square_root. Do you know if the implementation of rsqrt() is the same as Fast_inverse_square_root as shown below. I'm also trying to test how fast is simulink rsqrt() in a embedded controller. if it's slow than my expectation, I might need to implemente Fast_inverse_square_root.
And I have one more question is how to implemente i = * ( long * ) &y; in simulink.
Thank you very much,
Hongbo
Fast_inverse_square_root c++ code.
float Q_rsqrt( float number ){
long i; float x2, y;
const float threehalfs = 1.5F;
x2 = number * 0.5F;
y = number;
i = * ( long * ) &y; // evil floating point bit level hacking
i = 0x5f3759df - ( i >> 1 ); // what the heck?
y = * ( float * ) &i;
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration//
y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
return y;}
It might be, although it doesn't have to be. Implementing this C code as a mex function might speed up what you want to do, or might slow it down.
If you don't know what you're working with well enough to know this code is not Matlab but C, I am not sure you would be able to write something faster than the engineers working for Mathworks. This algorithm is famous, I would expect them to know it. It would highly surprise me if they haven't tried implementing it to see the relative performance.
What is the reason for your concern about performance? If performance is so important, why aren't you doing everything in C anyway (or in a language even closer to your bare metal of choice)? I personally have the view that Matlab (and Simulink) are great tools for rapid prototyping and doing complex things. However, they tend to be the slower choice if performance is absolutely critical (it often isn't).
@Rik Thank you very much for your quick response and explanation. It will be great if someone from MathWork can confirm rsqrt() is efficient for embeded control system.

Sign in to comment.

 Accepted Answer

Hi,
The Simulink sqrt block supports rsqrt.
The algorithm is Newton-Raphson, so it can be considered the same as the one described on the wiki.
With that setting, C code generation is possible.

More Answers (1)

i = * ( long * ) &y
This is equivalent to:
y = single(pi);
i = typecast(y, 'int32');
The shown code of Q_rsqrt is an approximation of 1/sqrt(x) for single precision floating point values. It might have a fair speed on CPUs without a floating point unit, but it is less accurate then the direct calculation.

1 Comment

Hi @Jan, Thank you very much. I noticed there is also a simulink block named Float Typecast. Does it supported by code generation for embeded controller?

Sign in to comment.

Categories

Find more on Simulink Coder in Help Center and File Exchange

Products

Release

R2018b

Asked:

on 13 Sep 2021

Edited:

on 31 Jul 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!