GCC 64 bit arithmetic bugs?
Posted: Fri Apr 15, 2005 9:06 am
Hi,
And the test environment is:
[tt]bash-2.05b$ gcc --version
gcc (GCC) 3.3.5-20050130 (Gentoo Linux 3.3.5.20050130-r1, ssp-3.3.5.20050130-1, pie-8.7.7.1)
bash-2.05b$ cat /proc/version
Linux version 2.6.9-gentoo-r1 (root@grunt) (gcc version 3.3.4 20040623 (Gentoo Linux 3.3.4-r1, ssp-3.3.2-2, pie-8.7.6)) #7 SMP Wed Jan 12 20:09:39 CST 2005
[/tt]
I'm not sure what the difference is between "long long" and "long long int" but it doesn't seem to make a difference. I changed the values for both a and b to see how they effect the results. The results are as follows...
Test 1 - simple, everything correct
[tt]Results from 1 * 5:
As signed 64 bit decimal: 5
As unsigned 64 bit hex: 0x0000000000000005
Low dword in hex: 0x00000005
High dword in hex: 0x00000000[/tt]
Test 2 - 32 bit result, problem displaying 64 bit decimal only
[tt]Results from 1000000 * 500000:
As signed 64 bit decimal: 1783793664
As unsigned 64 bit hex: 0x000000006A528800
Low dword in hex: 0x6A528800
High dword in hex: 0x00000000[/tt]
Result should be 500000000000 or 0x000000006A528800
Test 3 - 64 bit result, everything wrong
[tt]Results from 1000000 * 2000000:
As signed 64 bit decimal: -1454759936
As unsigned 64 bit hex: 0xFFFFFFFFA94A2000
Low dword in hex: 0xA94A2000
High dword in hex: 0xFFFFFFFF[/tt]
Result should be 2000000000000 or 0x000001D1A94A2000
Test 4 - largest possbile (within range) inputs, everything wrong
[tt]Results from 2147483647 * 2147483647:
As signed 64 bit decimal: 1
As unsigned 64 bit hex: 0x0000000000000001
Low dword in hex: 0x00000001
High dword in hex: 0x00000000[/tt]
Result should be 4611686014132420609 or 0x3FFFFFFF00000001
Then I thought I'd try the following code instead:
This didn't compile properly ("warning: integer constant is too large for "long" type") so I figured I'd check with a "printf("Size %d\n", sizeof(c));" which reported that c was indeed an 8 byte variable.
I ignored the silly warning and the result was:
[tt] As signed 64 bit decimal: 1311768467463790320
As unsigned 64 bit hex: 0x123456789ABCDEF0
Low dword in hex: 0x9ABCDEF0
High dword in hex: 0x12345678[/tt]
Which is all entirely correct (even though it'd failed to print a 64 bit decimal number before)....
So, am I doing something wrong, or is GCC buggy?
Cheers,
Brendan
Ok I was curious, so I decided to try a little test program. Basic test code is:Solar wrote: Should you happen to find that "bug" again, I'd like to hear about it.
Code: Select all
int main(void) {
int a = 1;
int b = 5;
long long int c;
c = a * b;
printf("Results from %d * %d:\n", a, b);
printf(" As signed 64 bit decimal: %Ld\n", c);
printf(" As unsigned 64 bit hex: 0x%016LX\n", c);
printf(" Low dword in hex: 0x%08X\n", c);
printf(" High dword in hex: 0x%08X\n", c >> 32);
return 0;
}[tt]bash-2.05b$ gcc --version
gcc (GCC) 3.3.5-20050130 (Gentoo Linux 3.3.5.20050130-r1, ssp-3.3.5.20050130-1, pie-8.7.7.1)
bash-2.05b$ cat /proc/version
Linux version 2.6.9-gentoo-r1 (root@grunt) (gcc version 3.3.4 20040623 (Gentoo Linux 3.3.4-r1, ssp-3.3.2-2, pie-8.7.6)) #7 SMP Wed Jan 12 20:09:39 CST 2005
[/tt]
I'm not sure what the difference is between "long long" and "long long int" but it doesn't seem to make a difference. I changed the values for both a and b to see how they effect the results. The results are as follows...
Test 1 - simple, everything correct
[tt]Results from 1 * 5:
As signed 64 bit decimal: 5
As unsigned 64 bit hex: 0x0000000000000005
Low dword in hex: 0x00000005
High dword in hex: 0x00000000[/tt]
Test 2 - 32 bit result, problem displaying 64 bit decimal only
[tt]Results from 1000000 * 500000:
As signed 64 bit decimal: 1783793664
As unsigned 64 bit hex: 0x000000006A528800
Low dword in hex: 0x6A528800
High dword in hex: 0x00000000[/tt]
Result should be 500000000000 or 0x000000006A528800
Test 3 - 64 bit result, everything wrong
[tt]Results from 1000000 * 2000000:
As signed 64 bit decimal: -1454759936
As unsigned 64 bit hex: 0xFFFFFFFFA94A2000
Low dword in hex: 0xA94A2000
High dword in hex: 0xFFFFFFFF[/tt]
Result should be 2000000000000 or 0x000001D1A94A2000
Test 4 - largest possbile (within range) inputs, everything wrong
[tt]Results from 2147483647 * 2147483647:
As signed 64 bit decimal: 1
As unsigned 64 bit hex: 0x0000000000000001
Low dword in hex: 0x00000001
High dword in hex: 0x00000000[/tt]
Result should be 4611686014132420609 or 0x3FFFFFFF00000001
Then I thought I'd try the following code instead:
Code: Select all
int main(void) {
long long int c = 0x123456789ABCDEF0;
printf(" As signed 64 bit decimal: %Ld\n", c);
printf(" As unsigned 64 bit hex: 0x%016LX\n", c);
printf(" Low dword in hex: 0x%08X\n", c);
printf(" High dword in hex: 0x%08X\n", c >> 32);
return 0;
}I ignored the silly warning and the result was:
[tt] As signed 64 bit decimal: 1311768467463790320
As unsigned 64 bit hex: 0x123456789ABCDEF0
Low dword in hex: 0x9ABCDEF0
High dword in hex: 0x12345678[/tt]
Which is all entirely correct (even though it'd failed to print a 64 bit decimal number before)....
So, am I doing something wrong, or is GCC buggy?
Cheers,
Brendan