Q_rsqrt() vs 1/sqrt()

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Q_rsqrt() vs 1/sqrt()

gepr
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/ 

Q_rsqrt.c (836 bytes) Download Attachment
uǝʃƃ ⊥ glen
Reply | Threaded
Open this post in threaded view
|

Re: Q_rsqrt() vs 1/sqrt()

Marcus G. Daniels
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 <[hidden email]> On Behalf Of u?l? ???
Sent: Thursday, January 7, 2021 3:24 PM
To: FriAM <[hidden email]>
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/ 
Reply | Threaded
Open this post in threaded view
|

Re: Q_rsqrt() vs 1/sqrt()

gepr
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 <[hidden email]> On Behalf Of u?l? ???
> Sent: Thursday, January 7, 2021 3:24 PM
> To: FriAM <[hidden email]>
> 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/ 
uǝʃƃ ⊥ glen
Reply | Threaded
Open this post in threaded view
|

Re: Q_rsqrt() vs 1/sqrt()

Marcus G. Daniels
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 <[hidden email]> On Behalf Of u?l? ???
Sent: Thursday, January 7, 2021 4:09 PM
To: FriAM <[hidden email]>
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 <[hidden email]> On Behalf Of u?l? ???
> Sent: Thursday, January 7, 2021 3:24 PM
> To: FriAM <[hidden email]>
> 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/ 
- .... . -..-. . -. -.. -..-. .. ... -..-. .... . .-. .
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/