5 ms·
Nobody has any idea what a modern x86 cpu does under the hood because we have no access to the microcode. The only thing you can do is run your code and get an
by antt 8y ago
Nobody has any idea what a modern x86 cpu does under the hood because we have no access to the microcode.
The only thing you can do is run your code and get an empirical answer to the question 'Is it fast enough'.
- firethief 8y agoCompilers almost never emit instructions that are microcoded on any modern processor
- codetrotter 8y agoPlease explain what you mean. The microcode is the “firmware” of sorts of the CPU. What instructions we tell the CPU to execute and what the CPU decides to actually do to achieve the same result you are asking for can be different. What do you mean by the instructions not being microcoded?
- imtringued 8y agoThere is no rule that every instruction has to be implemented with microcode. Simple instructions like adding two numbers don't need it. Microcode is generally used for more complicated instructions, often those with a variable number of cycles. They are also usually not the fastest way to achieve a result.
- cesarb 8y ago> What do you mean by the instructions not being microcoded? Most common instructions in modern CPU cores are decoded directly into the corresponding micro-operation(s), without involving the microcode ROM. Only the most complex instructions, involving many micro-operations, will use the microcode decoder. Also, there are often several copies of the simpler instruction decoder, so the CPU can decode several instructions in a single cycle, while there's normally a single copy of the complex (microcode-using) instruction decoder. A good resource if you want to read more is part 3 of https://www.agner.org/optimize/ https://www.agner.org/optimize/ which describes the decoder (and other parts) of several families of x86 processors.
- codetrotter 8y agoThanks
- TeMPOraL 8y agoTechnically true, but also useless point of view, because it's essentially saying "it's all arbitrary chaos underneath". But it isn't. You can get 90% there with basic understanding of x86 assembly and some CPU-level abstractions. (I too execute the code in my head, but these days usually a couple layers above x86 ASM, in my own mental "bytecode" that tracks how expensive are some of programming language's operations and stdlib functions.)
- lmm 8y agoWhether relevant things fit into L1 cache will make an order of magnitude more difference than whether you're using a nominally 2-cycle or 4-cycle assembly instruction. I don't believe any human programmer on a modern CPU is able to keep track of what cache level their code operates at entirely in their head; certainly such a human would not be thinking in terms of x86 assembly or normal language stdlib functions. So attempting to execute the code in one's head is overwhelmingly likely to be a waste of time.
- jdsully 8y agoA lot of the relevant low level optimizations are documented - like micro and macro op fusion. The published optimization guide is thousands of pages long.
- leibnitz27 8y agoSometimes, you have to. An example - some code I was working with gave subtly different results on two processors. It turned out this was because one of them implemented FMA - Fused Multiply Add. https://en.wikipedia.org/wiki/Multiply%E2%80%93accumulate_operation https://en.wikipedia.org/wiki/Multiply%E2%80%93accumulate_op... It wasn't particularly painful to find this, but if you were attempting to debug this kind of problem without considering lower level issues, you'd spend a whole lot of time banging your head against a wall.
- chrisseaton 8y agoIf the explicit fused instruction (so VFMADD..., we're not talking micro-code fusing, are we?) doesn't have the same semantics as a separate multiply and add then it's a compiler bug to select it, isn't it? I think if you're talking micro-code fusing then Intel does guarantee it has exactly the same semantics as a separate multiply and add. Interesting fact - I believe modern Intel architectures actually only have fused multiply add. If you do just a multiply it'll do a fused multiply and add zero.