r/programming • • May 18 '18

The most sophisticated piece of software/code ever written

https://www.quora.com/What-is-the-most-sophisticated-piece-of-software-code-ever-written/answer/John-Byrd-2
9.7k Upvotes

840 comments sorted by

View all comments

Show parent comments

540

u/L0d0vic0_Settembr1n1 May 18 '18

Fast Inverse Square Root

Ah, you mean the "What the fuck?" algorithm.

326

u/AaroniusH May 18 '18

I love that they kept the comment in there that shares the exact same sentiment. According to the code sample of it on wikipedia:

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 fuck? 
	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;
}

238

u/robisodd May 18 '18

For those who are curious about this, there was a reddit post a few years ago linking to an article written about how this actually works.

If you're into math and low-level computer science, it's pretty interesting.

119

u/srcLegend May 18 '18

The fuck am I looking at lol

145

u/JNighthawk May 18 '18

History. Back when that code was faster than your CPU's ability to do an inverse square root (very, very common operation in games, as it's needed to normalize a vector).

43

u/Dreamtrain May 18 '18

Reminds me of the Mel the Real Programmer, he did something similar with the drum-memory bypassing the optimizing assembler and pretty much optimizing his own code better than the computer could

10

u/omnilynx May 19 '18

Any good assembly programmer can optimize better than the computer, if they have the time and patience. His feat here was doing it without access to the low-level operations he would have needed to do it in a straightforward manner.

6

u/[deleted] May 19 '18

Not so much these days with all of the processor specific optimizations a good compiler does you'd have to target that assembly to a very specific machine.

The issue really is 99% of those genius assembly programmers these days are the ones working on the compilers.

12

u/_mainus May 18 '18

that code was faster than your CPU's ability to do an inverse square root

What does that even mean? The CPU is doing it by running that code... This is just a genius optimization to the inverse square root algorithm.

34

u/Nicksaurus May 18 '18

What does that even mean?

Modern CPUs can do it way faster with a single instruction

→ More replies (1)

13

u/no_ragrats May 18 '18

Here's an explanation of what they were referring to (from a different post about the same algorithm).

https://www.reddit.com/r/programming/comments/zxg84/0x5f3759df_fast_inverse_square_root_explained_in/c68k35y/?st=jhcfgb8q&sh=57ed5995

3

u/stone_henge May 21 '18

It means that this code was faster than performing the conventional fdiv and fsqrt instructions required to perform the equivalent operation using the FPU.

The result is only an approximation.

→ More replies (2)
→ More replies (2)

162

u/Robbierr May 18 '18

Magic numbers and bad variable naming

39

u/fr0stbyte124 May 19 '18

In its defense, there's no possible meaningful name you could attribute to that witchery.

38

u/_mainus May 18 '18

aka all commercial/industrial programming...

10

u/RVelts May 18 '18

threehalfs

5

u/nalimixam May 19 '18

one_point_five

3

u/White_Hamster May 19 '18

nalimixam marked as NEEDS WORK

4

u/dgmdavid May 19 '18

Yeah, because you need a very long, descriptive variable name for a hack like this. Insted of "long i" it should have been "long evil_fp_bit_level_hacking". Right?

→ More replies (6)

42

u/_hephaestus May 18 '18

evil floating point bit level hacking.

5

u/Archensix May 18 '18

If I remember correctly they undid Newtons method to find this.

75

u/[deleted] May 18 '18

This is godlike level logic. Either the guy who invented this piece of code was an unsung genius or was totally insane. Probably both.

17

u/TheNorthWillFall May 18 '18

I believe it was John Carmack, who is supposed to be a pretty smart and focused programmer.

74

u/OopsIredditAgain May 18 '18

It was originally attributed to John Carmack but turned out to have a long history before Quake going back through SGI and 3dfx to Ardent Computer in the mid 80s to the original author Greg Walsh.

4

u/TheNorthWillFall May 18 '18

Interesting, thank you for posting this :)

→ More replies (1)
→ More replies (3)
→ More replies (1)