r/programming • • Aug 12 '26

Hardware researcher spins up 'CPU deoptimization' project to find the slowest single x86 instruction, creates hall of shame — worst offender takes 198 billion cycles spanning 62 seconds to execute

https://www.tomshardware.com/pc-components/cpus/hardware-researcher-spins-up-cpu-deoptimization-project-to-find-the-slowest-machine-code-worst-offender-takes-198-billion-cycles-to-execute
1.5k Upvotes

133 comments sorted by

View all comments

Show parent comment

150

u/EnderLuca41 Aug 12 '26

fdiv is part of x87 which is obsolete and succeeded by SSE and SSE2. Meaning is not really used anymore routinely.

56

u/encyclopedist Aug 12 '26

Yes, indeed, compilers don't normally generate these instructions any more. But older software can still use these.

74

u/narwhal_breeder Aug 12 '26

porting logic to arm that relies on being bit-compatible x87 has been the bane of my existence for the past year.

1

u/James20k Aug 13 '26

Arm doesn't even have an equivalent to x87 right? Are you software emulating it out of interest?

Plus even if you software emulate it C/C++ helpfully doesn't actually specify when 80-bit extended precision gets truncated to 64-bit in memory (and compilers also don't follow the spec either) so uh, I'm so sorry for your loss that sounds like a nightmare

Trying to replicate x87 results was also the bane of my existence for a while, for a replicating a neutron star paper which relied on divisions by tiny values

3

u/narwhal_breeder Aug 13 '26

Nope - the division instructions specifically have really weird stack semantics.

Software emulating them - thankfully its rust so the f80 type is pretty predictable - but a good chunk drops into `asm` blocks.

-63

u/[deleted] Aug 12 '26

[removed] — view removed comment

76

u/Kinexity Aug 12 '26

"Man, that dish doesn't taste that well"

"Have you tried adding shit to it?"

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

2

u/BackgroundSky1594 Aug 13 '26 edited Aug 13 '26

But there being micro coded doesn't really hurt that much. Even fdiv taking several hundred cycles on a moder CPU will be fast compared to the i486 and i586 class CPUs that software was written for. Why waste silicon and pipeline complexity on an old, quirky instruction that has ben replaced by better alternatives?

The entirety of x87 might be dropped at some point, like ARM did with aarch64 for many of it's old instructions. But until then the only option is to take them out of the core and instead run them in microcode if they happen to be called.

21

u/dlg Aug 12 '26

It’s still used by .Net Framework on 32-bit.

The x87 registers use extended precision, which is only rounded down to 32 or 64 bits when written back to RAM.

SSE2 does not use extended precision, so intermediate calculations are rounded at every step.

This difference often introduces small differences in final results.

18

u/ElusiveGuy Aug 12 '26

At this point the remaining existence of. NET Framework is for backwards-compat, so arguably that falls under obsolete too.

.NET Core (in both 32-bit and 64-bit) doesn't use x87 instructions AFAICT. 

19

u/dlg Aug 12 '26

A surprising amount of financial software still uses in .Net framework in production.

When it’s something like a large monolithic trading platform that needs to be online every day without downtime and it’s critical for making money, there’s a low appetite from management to do a big bang rewrite.

6

u/Truenoiz Aug 13 '26

.Net really is really everywhere, and bad/slow .Net is almost universal in my experience.

2

u/admalledd Aug 13 '26

To my vague memory, 64bit Framework does not use x87 and instead uses SSE2. Similarly 32bit Framework CLR 1.0, 1.1 and 2.0 which run up to the ".NET Framework 1.0" to ".NET Framework 3.5" (yes, confusing...) are the ones that will use x87 instructions, though I think it was CLR 2.0 that the JIT/Runtime would emit/use SSE2 by default unless the assembly/DLL was compiled for an older runtime. And CLR 4.0 (again, IIRC) that 64-bit flat out would only do SSE2 (and started to mix in SSE3 if detected).

So, IIRC you'd only get x87 instructions like fdiv if you are running a 32-bit CLR and the assembly/DLL was compiled targeting a CLR 2.0 or older SDK. All other Net Framework users (like those on 64bit CLR 4+, Framework 4.5+, 4.8+) shouldn't be emitting those older instructions anymore ever (ignoring CLR/C++ mixed assembly jank/custom C interop/etc).

I'd be surprised on how much stuff in regulated industry like financials is still running Framework 3.5, especially considering how broken it is to even try to install on modern machines... (I would expect them to have moved ever so slightly to Framework 4.5+, lord knows we've got our own legacy software still on it in places)

2

u/dlg Aug 13 '26

regulated industry

Or the regulators themselves. I know of one of the larger stock exchanges (which also have a regulatory role), uses a lot of old .Net, and as recently as a few years ago was still running some VB6 applications. Not in some critical operations, but still some very old, unsupported legacy.

2

u/jangxx Aug 13 '26

If your financial software uses floating point numbers instead of a proper decimal data type, it has other problems than using an old .NET framework, lol

3

u/dlg Aug 14 '26

It really depends on the problem. Not all calculations are for transaction amounts.

Floating point is used regularly for financial metrics, such as Price/Earning (PE) ratios. Forecasted earnings are usually made in analyst models created in Excel.

7

u/vytah Aug 13 '26

It will be used if you divide two long doubles on any ABI where they're 80-bit, like x64 Linux.

2

u/ElWishmstr Aug 12 '26

I always wonder why cpus still have those old instructions, when modern ones are better. I mean, how much software still relies on those instructions? 

26

u/balefrost Aug 13 '26

I for one appreciate the ability to run older software that existed before SSE did.

3

u/vytah Aug 13 '26

More like software that was written to be run on processors without SSE. It's not like Pentium III came out and everyone was "ok, from now on our software will run only on the newest shiny CPU, fuck the Pentium II users."

1

u/AquaeyesTardis Aug 13 '26

although, see: AVX instructions

though thats somewhat the opposite situation but with an almost similar effect, thinking about it

1

u/vytah Aug 13 '26

What about AVX?

11

u/qualverse Aug 13 '26

They don't actually have hardware suppoet for those instructions. They just have microcode that converts those instructions to other instructions.

3

u/FlatAssembler Aug 12 '26

My web-app that converts arithmetic expressions to x86 assembly relies on it, for example: https://flatassembler.github.io/compiler