Skip navigation
I compiled Quake III's fast inverse square root on 64-bit Linux with gcc -O1 and it returned…
0:00 / 12:11

I compiled Quake III's fast inverse square root on 64-bit Linux with gcc -O1 and it returned…

tetsuo

@tetsuoai247K followers

Visit on X
547 likesDownloadOriginal post

37,264 views

I compiled Quake III's fast inverse square root on 64-bit Linux with gcc -O1 and it returned negative numbers. The long cast reads past the float into the stack, and the low bit of the return address ends up in the sign bit.

23 Comments

Comments from the original post on X (Twitter)

  • tetsuo's profile picture

    @tetsuoai

    Quake III's Q_rsqrt as shipped, on 64-bit Linux. gcc -O1 q.c -o q && ./q gcc 13 prints -0.499154. clang 18 -O1 prints 0.000000, because reading 8 bytes from a 4-byte float is undefined behavior and clang compiles Q_rsqrt to a single ret. Your result depends on your compiler version.

    15 likes

  • Frederico Pissarra's profile picture

    @fred_pissarra

    x86-64 SysV ABI? If that is the case `long int` is 64 bits long, not 32... maybe this is the reason. Change `long int` to `int`.

    6 likes

  • tetsuo's profile picture

    @tetsuoai

    Yep, that's the bug. On x86-64 SysV long is 8 bytes so *(long *)&y reads 4 bytes past y into the stack. The fix is at ~10:30 you went straight to the replies 😄

    10 likes

  • Frederico Pissarra's profile picture

    @fred_pissarra

    Yep... I've got straight to the replies (sorry!) and watched the video now... as said before, always an AMAZING analysis. I wish I had the patience and skills to do amazing videos like this one!

    4 likes

  • tetsuo's profile picture

    @tetsuoai

    I'll jump on discord this weekend and give you my work flow.

    3 likes

  • Frederico Pissarra's profile picture

    @fred_pissarra

    As always, this is an AWESOME analysis...

    3 likes

  • Traveler's profile picture

    @Traveler2000AD

    Quake III was made in 1999. For 32-bit CPU. So kids, if you think using explicit types like uin32_t,uin64_t,int32_t,int64_t and co is just meaningless old farts fad then you are in a world of pain if you try porting shit from 1981 to 2002 or so (AMD Opteron in 2003 was first x86 compatible 64-bit CPU) to modern day.

    3 likes

  • X Girls's profile picture

    @thesoragirls

    woah 🤯

    2 likes

  • why's profile picture

    @NullContex1s

    A perfect example of code that was “clever” on 32-bit and undefined on 64-bit. `long` being 8 bytes turns that type pun into an out-of-bounds read.

    2 likes

  • patrick mcqueeny's profile picture

    @humansandaiboss

    interesting! but why?

    2 likes

  • Andrew's profile picture

    @AndrewOctal

    Ya ..W/e use like Java script or something

    1 like

  • Wow Cool Thanks's profile picture

    @WowCoolTh4nks

    NOTHING ELSE MATTERS UNLESS STRAFE JUMP GIVES ME ACCELERATION.

  • MancerAI's profile picture

    @MancerAI_

    I understand this is mostly out of curiosity, so maybe a stupid comment but; doesn't modern CPUs have fast inverse square root built in/as an OP

  • ai(Key)骆驼's profile picture

    @biquan88888

    Good

  • ヴァルカン's profile picture

    @VulcanIgnis

    how do you make these vids? cool

  • Ben's profile picture

    @BenSchneiderX

    Yep that one also drove me nuts, genius math in the game.

  • Time's profile picture

    @rigel3in6orion9

    てつお!元気?何言ってるのか全くわからない😊✨

  • Necati Demir, PhD's profile picture

    @ndemir

    Just curious. Did you generate this visualization with Opus 5.5? If so that looks great.

  • Pollino Geroni's profile picture

    @IlPogeoni

    @grok sono ignorante a cosa servirebbe quello che l’Op ha fatto?

  • A. I. (Macro Photography)🔬's profile picture

    @MacroWorld21

    Magic.