问题
I would like to know if current cpus avoid multiplying two numbers when at least one of them is a zero. Thanks
回答1:
Modern CPUs - what do you mean by that? Do you mean most commonly used (like x86, AMD64, ARM) or most recently developed. Every processor architecture has it's own properties. Moreover, each company (like Intel or AMD) can make processor different way (that usually being company secret).
As you question goes, I doubt that. You know, even checking if number is equal to zero twice prior EVERY multiplication is too much overhead, if you account how low percentage of multiply operations actually are optimized.
Optimisation like that would make the CPU more expensive.
Let's say there are 1% of multiplications by zero (and is probably much lower) in an average program. That would mean that comparision to zero would have to 200 times faster than multiplying to be just account for overhead (and much more for this to be useful in practice).
I think you are looking at this question too much from human perspective. When you multiply, you clearly see that one of the multiplicands is zero and conclude. But however, things are very different with computers. Computer actually has to check all 64 or 32 bits to be sure that something is equal to zero.
- I wouldn't worry if I were you. Processor manufacturers and compiler writers are doing their best to maximize performance. They have literary thought of everything.
回答2:
This varies widely depending on the CPU and (in some cases) the type(s) of the operands.
Older/simpler CPUs typically use a multiplication algorithm something like this:
integer operator*(integer const &other) {
unsigned temp1 = other.value;
unsigned temp2 = value;
unsigned answer = 0;
while (temp1 != 0) {
if (temp1 & 1)
answer += temp2;
temp2 <<= 1;
temp1 >>=1;
}
return integer(answer);
}
Since the loop executes only when/if temp1 != 0
, the loop obviously won't execute if temp1
starts out as 0 (but as written here, won't attempt any optimization for the other operand being 0).
That, however, is fundamentally a one bit at a time algorithm. For example, when multiplying 32-bit operands, if each bit has a 50:50 chance of being set, we expect approximately 16 iterations on average.
A newer, high-end CPU will typically work with at least two bits at a time, and perhaps even more than that. Instead of a single piece of hardware executing multiple iterations, it'll typically pipeline the operation with separate (albeit, essentially identical) hardware for each stage of the multiplication (though these won't normally show up as separate stages on a normal pipeline diagram for the processor).
This means the execution will have the same latency (and throughput) regardless of the operands. On average it improves latency a little bit and throughput a lot, but does lead to every operation happening at the same speed, regardless of operands.
回答3:
I would expect that a modern desktop CPU would have such a thing in it.
来源:https://stackoverflow.com/questions/9332670/do-modern-cpus-skip-multiplications-by-zero