Divide by zero works on ia64!!!
Following program causes floating point exception on x86 (compiled with gcc 3.3.5 (Debian 1:3.3.5-8)) as expected, but runs fine on ia64 when compiled with gcc (GCC) 3.3.5 (Debian 1:3.3.5-8).
main()
{
int a, b;
a = b = 0;
printf("a = %d, b = %d, a/b = %d\n", a, b, a/b);
a = 1;
b = 0;
printf("a = %d, b = %d, a/b = %d\n", a, b, a/b);
}Here is what I get on a machine running 2.6.10 kernel:
# ./tst a = 0, b = 0, a/b = 0 a = 1, b = 0, a/b = 0
Someone else tried Intel compiler and it generated code that caused floating point exception as it should.
Something is busted in gcc.
Signature · 9 lines
Khalid ================================== Khalid Aziz Open Source and Linux Organization (970)898-9214 Hewlett-Packard [email protected] Fort Collins, CO "The Linux kernel is subject to relentless development" - Alessandro Rubini
Quoted 1 line · Khalid Aziz
Something is busted in gcc.
Nothing. Divide by zero is undefined.
Andreas.
Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected] SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5 "And now for something completely different."
Quoted 6 lines · Khalid Aziz
Following program causes floating point exception on x86 (compiled with gcc 3.3.5 (Debian 1:3.3.5-8)) as expected, but runs fine on ia64 when compiled with gcc (GCC) 3.3.5 (Debian 1:3.3.5-8).
[...]
Here is what I get on a machine running 2.6.10 kernel:
Quoted 8 lines
# ./tst a = 0, b = 0, a/b = 0 a = 1, b = 0, a/b = 0Someone else tried Intel compiler and it generated code that caused floating point exception as it should.
Something is busted in gcc.
As Andreas pointed out, this code invokes undefined behavior. Undefined here means that the result of an operation is not defined by the language standard, and 0 is a valid result when invoking an undefined operation.
Code relying on undefined behavior is non-portable (or busted, if you want) by design.
To quote the standard:
6.5.5 #3:
The result of the / operator is the quotient from the division of the first operand by the second; the result of the % operator is the remainder. In both operations, if the value of the second operand is zero, the behavior is undefined.
Regarding what is undefined behaviour, see 3.4.3: undefined behavior behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements
NOTE Possible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).
I believe the HP-UX compiler works around this by adding an explicit test for every integer divide, and I am not aware of any plans to make a change like this to GCC.
Gerald
Quoted 6 lines · Andreas Schwab
Quoted 1 line · Khalid Aziz
Something is busted in gcc.
Nothing. Divide by zero is undefined.
So, is the inconsistency between x86 and ia64 okay?
Signature · 9 lines
Khalid ================================== Khalid Aziz Open Source and Linux Organization (970)898-9214 Hewlett-Packard [email protected] Fort Collins, CO "The Linux kernel is subject to relentless development" - Alessandro Rubini
Quoted 2 lines · Khalid Aziz
Quoted 1 line
Nothing. Divide by zero is undefined.
So, is the inconsistency between x86 and ia64 okay?
Why not? Even an application as simple as the following will give a different output on x86 and ia64:
#include <iostream> #include <climits>
using namespace std;
int main() {
cout << "sizeof long = " << sizeof(long) << endl
<< "CHAR_MAX = " << CHAR_MAX << endl;
}And when you run this on ppc64, the output will not match either one of the above, and on s390 it's different again, yet products like SLES support all three (and further) architectures with a common codebase.
Gerald
Quoted 3 lines · Gerald Pfeifer
As Andreas pointed out, this code invokes undefined behavior. Undefined here means that the result of an operation is not defined by the language standard, and 0 is a valid result when invoking an undefined operation.
It is undefined in C and C++, but what about other stricter languages like Java and Ada? I don't have language standards for either of them, but I believe they both require exceptions here. Then there is the question of Fortran, which may also have something to say about this issue.
Also, while C doesn't require an exception, it is expected behaviour for a POSIX system, and other gcc ports make sure we get an exception. The MIPS gcc port for instance emits a conditional trap instruction, because the MIPS integer divide instruction does not trap. The IA-64 gcc port could do likewise.
I'd call this an oversight, a quality of implementation problem for C, a probable bug for Java and Ada, and worthy of a gcc bugzilla bug report so that we don't forget about it.