Performance comparison: 64-bit versus 32-bit multiplication

I'm using an Intel i5-4200U with an Intel (R) Core 1.60GHz processor and wondering why the multiplication of 64-bit numbers is slower than 32-bit numbers. I did a test run in C and it turns out it takes twice as long.

I expected it to take the same amount of time because the processor operates on its own 64-bit registers, and it doesn't matter how wide the numbers are (as long as they fit into a 64-bit register).

Can someone explain this?

+3


source to share


2 answers


There are special instructions in the x86-64 instruction set to express that you only want to multiply two quantities 32-bit

. One instruction may look like it is IMUL %EBX, %ECX

in a specific dialect for x86-64 assembly, as opposed to 64-bit multiplication IMUL %RBX, %RCX

.

So the processor knows that you only want to multiply 32-bit values. This happens often enough that the processor designers have made sure that the internal circuitry will be optimized to provide a faster response in this easier case, just as you can multiply 3-digit numbers more easily than 6-digit numbers. The difference can be seen in the timings as measured by Agner Fog and documented in his comprehensive build optimization resources .



If your compiler targets the older 32-bit IA-32 instruction set, the difference between 32-bit and 64-bit multiplication is even wider. The compiler must implement 64-bit multiplication with instructions for 32-bit multiplication, using four of them (three if only the least significant 64 bits of the result are computed). In this case, 64-bit multiplication can be about three to four times slower than 32-bit multiplication.

+6


source


I can think of a problem here due to 64 bit multiplication.

Actually, for multiplying two 32-bit numbers, the result will be no more than 64 bits. But in the case of multiplying two 64-bit numbers, the product can be 128 bits, and in all cases it will be more than 64 bits!



As a similar example in the 8086 microprocessor, if you do the same with 8-bit numbers and 16-bit numbers, you will run into a situation where the CPU registers have to store them from the AX register and DX register as well (if you know assembly language abbreviations).

So, I believe that it is possible to increase the computation time !!! I feel like this is what makes your 64 bit multiplication slow!

0


source







All Articles