[FRIAM] Q_rsqrt() vs 1/sqrt()

Marcus Daniels marcus at snoutfarm.com
Thu Jan 7 19:32:47 EST 2021


It only seems to happen with single precision, like:

#include <math.h>
float rsqrt1(float a) { return 1.0f/sqrtf(a); }

If the compiler doesn’t do what one expects, one probably expects wrong.  (It isn't favorable to do so.)

Marcus

-----Original Message-----
From: Friam <friam-bounces at redfish.com> On Behalf Of u?l? ???
Sent: Thursday, January 7, 2021 4:09 PM
To: FriAM <friam at redfish.com>
Subject: Re: [FRIAM] Q_rsqrt() vs 1/sqrt()

Great idea! Thanks. I'd tried gcc -S. Whew is my ASM rusty. But objdump -d doesn't show any rsqrt instructions. 

On 1/7/21 3:46 PM, Marcus Daniels wrote:
> There's a reciprocal sqrt instruction in SSE (x86) that your compiler may be pattern matching to, depending on your compiler options.   You could disassemble to see what you get from the latter.
> 
> -----Original Message-----
> From: Friam <friam-bounces at redfish.com> On Behalf Of u?l? ???
> Sent: Thursday, January 7, 2021 3:24 PM
> To: FriAM <friam at redfish.com>
> Subject: [FRIAM] Q_rsqrt() vs 1/sqrt()
> 
> https://en.wikipedia.org/wiki/Fast_inverse_square_root
> 
> So, why is Q_rsqrt() *slower* than 1/sqrt()?
> 
> 1/sqrt() took 0.294771 s
> Q_rsqrt() took 0.51579 s
> 

--
↙↙↙ uǝlƃ

- .... . -..-. . -. -.. -..-. .. ... -..-. .... . .-. .
FRIAM Applied Complexity Group listserv
Zoom Fridays 9:30a-12p Mtn GMT-6  bit.ly/virtualfriam un/subscribe http://redfish.com/mailman/listinfo/friam_redfish.com
archives: http://friam.471366.n2.nabble.com/
FRIAM-COMIC http://friam-comic.blogspot.com/ 


More information about the Friam mailing list