unsigned int multiply, x86-64
Bob Plantz
plantz@cds1.net
Tue Apr 15 17:15:00 GMT 2008
I wrote some C code to multiply two unsigned (32-bit) ints and store the
result in a 64-bit unsigned int.
int main(void)
{
unsigned int x, y;
unsigned long int z;
printf("Enter two integers: ");
scanf("%u %u", &x, &y);
z = (long int)(x * y);
printf("%u * %u = %lu\n", x , y, z);
exit(0);
}
For the multiply, gcc produces (with -O0):
movl -4(%rbp), %edx
movl -8(%rbp), %eax
imull %edx, %eax
mov %eax, %eax
movq %rax, -16(%rbp)
First, it uses signed multiply (imull). Second, it only keeps the
low-order 32 bits of the result.
I would do something like:
movl -4(%rbp), %edx
movl -8(%rbp), %eax # zeros high-order 32 bits
mull %edx # 64-bit result in edx:eax
shlq $32, %rdx # shift to high-order
addq %rdx, %rax # combine into 64-bit result
movq %rax, -16(%rbp)
Am I missing something here?
My "application" is that I'm writing a book and want to make sure I have
a clear understanding so I don't say stupid things.
-- Bob
More information about the Gcc-help
mailing list