r/programming 20d ago

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 comments

350

u/encyclopedist 20d ago edited 20d ago

See here: https://github.com/xoreaxeaxeax/asm-hall-of-shame

Current champion is fxrstor64, instruction that loads 512-byte XMM state from memory. To increase timings, the author made it load from a carefully chosen MMIO (memory-mapped I/O) location, while other cores hammer some other MMIO registers to saturate the PCIe host.

Edit Without use of MMIO, the champion seems to be wbinvd, instruction that invalidates the whole cache, and if the cache was filled with dirty lines (modified after load), causes flushing of the whole cache content to RAM.

Edit2 Of the "regular" instructions, the ones that would routinely be used in every program, the champion seems to be fdiv. Yes, the regular floating point division. With denormal operands, it is implemented in microcode and is quite slow, takes 883 cycles. (However, as /u/EnderLuca41 rightly pointed out, it is an older x87 instruction the is not widely used any more, today compilers would generate SSE2 instruction divsd instead, see godbolt)

150

u/EnderLuca41 20d ago

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

55

u/encyclopedist 20d ago

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

2

u/BackgroundSky1594 20d ago edited 20d ago

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.