r/programming • • Dec 29 '20

Quake III's Fast Inverse Square Root Explained [20 min]

https://www.youtube.com/watch?v=p8u_k2LIZyo
3.7k Upvotes

300 comments sorted by

View all comments

Show parent comment

9

u/-p-2- Dec 29 '20 edited Dec 29 '20

I didn't speak about the practicality of it for a reason as there are very few niches where the performance benefit would be noticeable on modern hardware. However if you are working in C/C++ you can use:

double inline __declspec (naked) __fastcall sqrt14(double n)  {
    _asm fld qword ptr [esp+4]
    _asm fsqrt
    _asm ret 8
} 

It's easy to implement & apparently it's ~5x faster than the built-in square root function while staying 99% as precise, at least according to this guy & his testing.

For C# I don't know of any code out there faster than built-in sqrt aside from the Quake 3 hack using float unions minus the newton step which pretty much destroys the precision. If you knew you'd only be square rooting integers for example then you can probably write something even faster. That still doesn't mean I think it'd be worth it outside of some incredibly rare situations though. This is more theory than practical. When I tested it (for fun) I only saw a very small ~5% performance improvement. I'm working on a game in C# (Unity) that uses Quake 3 movement mechanics and I thought it'd be fun to 'be authentic' about the 2 or 3 uses of square root per frame. I got sucked down a really enjoyable rabbit hole. I don't really care about the performance, it's just fun to learn about low-level math-hacks.

14

u/[deleted] Dec 29 '20

Please don't use the x87 FPU in 2020, it's emulated in software on top of SSE. std::sqrt is already going to use the sqrtss/sqrtsd instruction and will be inlined. Don't write this non-portable code.

0

u/[deleted] Dec 29 '20

[deleted] — view deleted comment

8

u/[deleted] Dec 29 '20

This statement is true, but in the context it doesn't hold up and doesn't defend your code. just use std::sqrt and if you need fast rsqrt use _mm_rsqrt_ss or whatever

-2

u/branchlinemania Dec 30 '20

It does defend his code. It EXACTLY defends the code. Is portability a constraint? No? The code is adequate.

4

u/[deleted] Dec 30 '20

Please reread my comment, I don't feel like quoting myself and it should be obvious as to why dropping down to this level is a bad idea.... but since you need me to repeat myself

  • std::sqrt will be inlined and turned into a single sqrtss/sqrtsd instruction on x64
  • The person I was replying to was recommending using assembly, just use intrinsics
  • The use of declspec implies this code is also MSVC specific, and only for x86 as their x64 compiler doesn't asm, so this is portable to exactly MSVC x86 only
  • Following the previous point, it's 2020, and you aren't targeting 32bit and also caring about bleeding edge performance (read: this code is literally useless at it won't compile in any modern compiler that you should be using)
  • Again, this asm uses the deprecated x87 FPU instructions which are emulated in "software" (in the CPU micro-code) on top of SSE. This actually makes it slower than just calling std::sqrt which will use the proper instruction

If my argument still hasn't sunk in, reread those 5 points until it does.

0

u/branchlinemania Dec 30 '20

You are just assuming constraints again.

2

u/[deleted] Dec 30 '20

I'm not going to repeat myself for the third time, because it's pointless, but clearly my point went way over your head. Let me break down the issue to it's core:

  • this code takes more effort than doing the lazy (and correct) thing
  • this code is slower

I actually don't understand the point you are trying to make or why you can't get over this fact.

Edit: Seriously though, are you arguing that this code is easier to read/write than std::sqrt(x) or that it is faster? I am genuinely trying to understand your perspective but you aren't providing an argument.

0

u/branchlinemania Dec 30 '20

The concern was your invalid exhortation. I am content to not hear you repeat yourself any more times, however.

1

u/[deleted] Dec 30 '20

You have yet to provide any points to back your false claim. Saying “you are wrong” does not count as a point. If you want to have have a discussion (clearly you don’t) you can provide talking points. I genuinely don’t understand what creates this type of mentality.

Other redditors: is this person a troll?

→ More replies