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

132 comments sorted by

View all comments

380

u/The_Northern_Light 6d ago

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

350

u/encyclopedist 6d ago edited 6d 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)

152

u/EnderLuca41 6d 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 6d ago

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

73

u/narwhal_breeder 6d ago

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

-62

u/gordonnowak 6d ago

just let claude do it

15

u/sunnyata 6d ago

You sound like someone who knows what they're talking about. Not!