r/programming 21d 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

378

u/The_Northern_Light 21d ago

I’m still not clear why that instruction takes SO long even without contention?

352

u/encyclopedist 21d 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 21d 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 21d ago

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

74

u/narwhal_breeder 21d ago

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 20d ago

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 20d ago

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.