Are modern cpus skipping multiplications by zero?

I would like to know if the current processor cannot avoid multiplying two numbers when at least one of them is zero. Thanks

+6
source share
3 answers
  • Modern processors - what do you mean by that? You mean the most commonly used ones (for example, x86, AMD64, ARM) or most recently developed ones. Each processor architecture has its own properties. Moreover, each company (for example, Intel or AMD) can make the processor differently (usually this is the secret of the company).

  • As you doubt, I doubt it. You know, even checking if a number is zero two times before EVERY multiplication is too much overhead if you consider how optimized the low percentage of multiplication operations is.

  • Optimization, as it would make the CPU more expensive.

Say there are 1% multiplications by zero (and probably much lower) in the average program. This would mean that a comparison with zero should be 200 times faster than multiplying by only overhead (and much more is useful in practice).

I think you look at this question too much from a human point of view. When you multiply, you clearly see that one of the factors is zero and ends. But, however, with computers everything is very different. In fact, the computer should check all 64 or 32 bits to make sure that something is equal to zero.

  • I would not worry if I were you. Processor manufacturers and compiler compilers do their best to maximize performance. They have a literary thought about everything.
+8
source

This varies greatly depending on the processor and (in some cases) the type (s) of the operands.

Older / simpler processors typically use a multiplication algorithm 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 is executed only when / if temp1 != 0 , the loop will obviously not be executed if temp1 starts at 0 (but, as it is written here, it will not try to optimize for the other operand 0).

This, however, is basically one bit in the time algorithm. For example, when multiplying 32-bit operands, if each bit has a chance to set 50:50, we expect an average of about 16 iterations.

A new, high-performance CPU typically works with at least two bits at a time, and possibly more. Instead of having separate hardware performing several iterations, it usually associates the operation with separate (albeit essentially identical) equipment for each stage of multiplication (although they usually do not appear as separate stages in the normal pipeline diagram for the processor).

This means that execution will have the same delay (and throughput) regardless of the operands. On average, this slightly improves latency and throughput, but leads to every operation occurring at the same speed, regardless of the operands.

+1
source

I would expect this modern processor to have such a thing.

-2
source

Source: https://habr.com/ru/post/908728/


All Articles