Elektrine lite

← Feed

@ela@infosec.exchange

Post #2384223

2025-05-15 13:48 UTC

You might not like it, but this is what peak compiler performance looks like.

Replies (21)

  • @tante@tldr.nettime.org 2025-05-15 13:51

    @ela@infosec.exchange I love how the ALT text really explains what's going on even for people not super familiar with machine code. Shows how helpful that feature is for everyone. Thank you!

    Open ##2639679

  • @plusmid@chaos.social 2025-05-15 14:19

    @ela@infosec.exchange no way...

    Open ##2639681

  • @viduq@mstdn.social 2025-05-15 14:38

    @ela@infosec.exchange how can the compiler break down a recursive function to a mathematical statement? There is no brute force involved or is there? And does it do that only when optimizations are on?

    Open ##2639686

  • @ljrk@todon.eu 2025-05-15 15:01

    @ela@infosec.exchange Nice! I wonder how this feat is performed? I suppose the tail-call is eliminated, and the resulting loop is reasoned about, perhaps unrolled even and then some analysis turns up this?

    Open ##2639695

  • @mica@mk.absturztau.be 2025-05-15 16:23

    @ela@infosec.exchange so from now on write your isEven function recursive? Got it ​:noted:​

    Open ##2639699

  • @ela@infosec.exchange Actually, this is what bad code writing looks like.

    Open ##2639700

  • @yama@tech.lgbt 2025-05-15 18:58

    @ela@infosec.exchange Or maybe a raw bitshift to take the last digit and check if its 0,2,4,6 or 8 :3

    Open ##2639702

  • @nakal@mastodon.social 2025-05-15 19:56

    @ela@infosec.exchange This is nice. I knew that C and C++ compiler pipelines and toolchains are very advanced, but sometimes they still surprise me.

    Open ##2639703

  • @trcwm@mastodon.social 2025-05-15 21:05

    @ela@infosec.exchange Love it!

    Open ##2639704

  • @gilesgoat@toot.wales 2025-05-15 21:08

    @ela@infosec.exchange This is INSANE .. I mean it's insane the C code written could not be worse than that !! .. But it's 10 times more insane the compiler realized what was meant to be and put the best solution ( i.e. 'mod 2' ) .. Did it guess by the NAME of the function the intended purpose maybe ??

    Open ##2639705

  • @mirabilos@toot.mirbsd.org 2025-05-15 21:20

    @ela@infosec.exchange this stumped me until I saw clang decides to return bool in al, not eax, for some reason.

    Open ##2639710

  • @xenotar@mastodon.social 2025-05-15 21:46

    @ela@infosec.exchange @naoEhAlexa@capivarinha.club

    Open ##2639720

  • @porglezomp@mastodon.social 2025-05-15 21:47

    @ela@infosec.exchange I wrote a blog post years ago that shows all the intermediate steps of performing this optimization: https://blog.witchoflight.com/2018/llvm-hearts-peano-addition/

    Open ##2639721

  • @jaj@mastodon.social 2025-05-15 21:48

    @ela@infosec.exchange Wow

    Open ##2639725

  • @ela@infosec.exchange “The computers rebelled but quietly and very performantly so no one noticed.”

    Open ##2639726

  • @ela@infosec.exchange This is the “give them something better than they would choose to give themselves” philosophy of compiler implementation.

    Open ##2639727

  • @josch@floss.social 2025-05-15 22:21

    @ela@infosec.exchange This is brilliant! I'd like to put this into my uni lecture on C/C++. If you would like to be credited for this find, how would you like to be attributed? Thanks!

    Open ##2639728

  • @realleif@social.sdfeu.org 2025-05-15 22:42

    @ela@infosec.exchange Unfortunately this does not work for the mutually recursive isEven and isOdd functions. 😔

    Open ##2639730

  • @sergi@floss.social 2025-05-15 23:37

    @ela@infosec.exchange It seems rustc is doing the same thing:

    Open ##2639731

  • @minego@pdx.social 2025-05-16 02:03

    @ela@infosec.exchange holly shit...

    Open ##2639732

  • @loke@functional.cafe 2025-05-16 04:47

    @ela@infosec.exchange This is quite impressive, and I wonder how complex you could make these kinds of functions while still having this kind of optimisation happening. But it does make me ask: Should perhaps the compiler warn about these cases? I mean, it's great that the compiler can optimise it, but when it can, the code should probably be simplified, and that would be better than relying on the compiler.

    Open ##2639733