Live data from Hacker News

Fast Inverse Square Root

timmmm.github.io

21–30 of 100 posts

Re: Fast Inverse Square Root

#21

FYI: Type punning like this doesn't work on all compilers i = * ( long * ) &y; // evil floating point bit level hacking Learned this recently with Arm AC6. [Also, this kind of genius analytic approximation of hot functions that do math makes me all tingly]

Would a union do the trick?

That's the Linux way to do it

    union { int i; float f } u { .f = 1.23f };
    int i = u.i; 
Another way is a memcpy, which I believe is the most defined way to do type punning

    int i; 
    float f; 
    memcpy(&i, &f, 4);
But you also have to assume the size of those primitive types but that's pretty safe in modern C/C++.

Re: Fast Inverse Square Root

#24
post #9

Earlier quoted context omitted.

Obligatory XKCD: https://xkcd.com/1053/

XKCD is legit shit. This one, the standards one, the Bobby Tables one. And the password one. Things that are barely worth a chuckle are repeated as gospel.

Wow, bruh, who hurt you?

Re: Fast Inverse Square Root

#25
post #4

It may be useful to mention that modern architectures often have vectorized instructions like vrsqrt14ps (accessible via the _mm512_rsqrt14_ps intrinsic) that provides a 14-bit approximation (there are more accurate variants too) in every lane with an inverse throughput of 2. These are faster than the integer bit hacks. https://software.intel.com/sites/landingpage/IntrinsicsGuide...

I imagine the bit hacks are still useful for microcontroller applications though.

Re: Fast Inverse Square Root

#26
post #21

Earlier quoted context omitted.

Would a union do the trick?

That's the Linux way to do it union { int i; float f } u { .f = 1.23f }; int i = u.i; Another way is a memcpy, which I believe is the most defined way to do type punning int i; float f; memcpy(&i, &f, 4); But you also have to assume the size of those primitive types but that's pretty safe in modern C/C++.

Do modern compilers optimize away the memcpy?

Re: Fast Inverse Square Root

#27
post #21

Earlier quoted context omitted.

Would a union do the trick?

That's the Linux way to do it union { int i; float f } u { .f = 1.23f }; int i = u.i; Another way is a memcpy, which I believe is the most defined way to do type punning int i; float f; memcpy(&i, &f, 4); But you also have to assume the size of those primitive types but that's pretty safe in modern C/C++.

In C++, type punning through pointers and unions is undefined behavior. Even `reinterpret_cast` isn’t allowed because of aliasing (IIRC). The only “defined” way to do type punning is a memcpy. A compiler targeting something like x86 would optimize out the memcpy.

For more information, see the C++20 final draft[0§7.6.1.9]

[0]: https://isocpp.org/files/papers/N4860.pdf

Re: Fast Inverse Square Root

#29
post #9

Earlier quoted context omitted.

Obligatory XKCD: https://xkcd.com/1053/

Not having heard of something is always understandable, and it's pretty immature to make fun or "feign surprise" when someone doesn't know something you know. But not searching to see if something had been posted before on a forum is a bit different[0] [0] https://hn.algolia.com/?q=inverse+square+root

> If a story has not had significant attention in the last year or so, a small number of reposts is ok. Otherwise we bury reposts as duplicates.

https://news.ycombinator.com/newsfaq.html

Re: Fast Inverse Square Root

#30
post #26
post #21

Earlier quoted context omitted.

That's the Linux way to do it union { int i; float f } u { .f = 1.23f }; int i = u.i; Another way is a memcpy, which I believe is the most defined way to do type punning int i; float f; memcpy(&i, &f, 4); But you also have to assume the size of those primitive types but that's pretty safe in modern C/C++.

Do modern compilers optimize away the memcpy?

yes
Post reply on HN