unsigned int multiply, x86-64
Tue Apr 15 23:41:00 GMT 2008
On Tue, 2008-04-15 at 13:16 -0400, John Fine wrote:
> When x and y are both unsigned int (x*y) is also an unsigned int.
> (long int)(x*y) means first compute the unsigned in (x*y) then promote
> it to long int.
> To get the answer you want, you need to promote one of the arguments of
> the multiply before multiplying. I don't know whether the optimizer
> will figure out to do the 32 bit multiply you want and store the 64 bit
> result or whether it would do a 64 bit multiply.
Thank you, John, for your remarks.
I knew about multiplying first, then the promotion, but it slipped my
mind. So I tried:
unsigned int x;
unsigned long int y;
printf("Enter two integers: ");
scanf("%u %lu", &x, &y);
y = x * y;
In this case 32-bit * 64-bit -> 96-bit result. But the compiler does:
movl -4(%rbp), %eax # load x
mov %eax, %edx # promote to 64-bit (zeroes high
movq -16(%rbp), %rax # load y (64-bit value)
imulq %rdx, %rax # truncate high-order 32 bits of
movq %rax, -16(%rbp)
I've also tried using 64-bit for all the ints. Basically, if the result
is wider than the widest int in the multiplication, there is overflow.
My thought is that this is a place where assembly language is needed if
the high-order bits need to be preserved. At least one needs to check
the CF and OF. (They get set to one if there is signed multiply
I guess I need to look this up in the C standard. I suspect that it's
okay to ignore multiply overflow.
> Also, are you sure (long int) is 64 bit? I thought it was just 32.
According to the ABI a long int in 32-bit mode is 32 bits, but in 64-bit
mode it is 64 bits. The code above verifies that a long int is 64 bits
in 64-bit mode.
I'm learning why converting to 64-bit is not as simple as I first
More information about the Gcc-help