3.59 128-bit Multiply
★★
The following code computes the 128-bit product of two 64-bit signed values x and y and stores the result in memory:
typedef __int128 int128_t;
void store_prod(int128_t *dest, int64_t x, int64_t y) {
*dest = x * (int128_t) y;
}GCC generates the following assembly code implementing the computation:
store_prod:
movq %rdx, %rax
cqto
movq %rsi, %rcx
sarq $63, %rcx
imulq %rax, %rcx
imulq %rsi, %rdx
addq %rdx, %rcx
mulq %rsi
addq %rcx, %rdx
movq %rax, (%rdi)
movq %rdx, 8(%rdi)
retThis code uses three multiplications for the multiprecision arithmetic required to implement 128-bit arithmetic on a 64-bit machine. Describe the algorithm used to compute the product, and annotate the assembly code to show how it realizes your algorithm. Hint: When extending arguments of x and y to 128 bits, they can be rewritten as and , where , , , and are 64-bit values. Similary, the 128-bit product can be written as , where , and are 64-bit values. Show how the code computes the values of and in terms of , , and .
Solve:
assume:
then:
overflows and don't care. So:
Last updated
Was this helpful?