unsigned int multiply, x86-64

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux