Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do modern cpus skip multiplications by zero?

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

like image 249
user1214927 Avatar asked Feb 17 '12 17:02

user1214927


People also ask

Can CPU do multiplication?

This method is mathematically correct and has the advantage that a small CPU may perform the multiplication by using the shift and add features of its arithmetic logic unit rather than a specialized circuit. The method is slow, however, as it involves many intermediate additions.

How many ALU does a modern CPU have?

The processor contains three sections called the Arithmetic Logic Unit (ALU), the Control Unit and Registers. Control unit: The Control Unit makes decisions and sends the appropriate signal down its lines to other parts of the computer.

Do all cpus have an ALU?

The Arithmetic Logic Unit (ALU) is the heart of the CPU operation. It takes values in registers and performs any of the multitude of operations the CPU is capable of. All modern processors have a number of ALUs so each can be working independently.


1 Answers

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.

like image 181
Jerry Coffin Avatar answered Sep 20 '22 23:09

Jerry Coffin