public inbox for linux-ia64@vger.kernel.org
 help / color / mirror / Atom feed
* Divide by zero works on ia64!!!
@ 2005-05-09 15:23 Khalid Aziz
  2005-05-09 15:56 ` Andreas Schwab
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Khalid Aziz @ 2005-05-09 15:23 UTC (permalink / raw)
  To: linux-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.

-- 
Khalid

==================================
Khalid Aziz                       Open Source and Linux Organization
(970)898-9214                                        Hewlett-Packard
khalid.aziz@hp.com                                  Fort Collins, CO

"The Linux kernel is subject to relentless development" 
                                - Alessandro Rubini


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Divide by zero works on ia64!!!
  2005-05-09 15:23 Divide by zero works on ia64!!! Khalid Aziz
@ 2005-05-09 15:56 ` Andreas Schwab
  2005-05-09 20:37 ` Gerald Pfeifer
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Andreas Schwab @ 2005-05-09 15:56 UTC (permalink / raw)
  To: linux-ia64

Khalid Aziz <khalid_aziz@hp.com> writes:

> Something is busted in gcc.

Nothing.  Divide by zero is undefined.

Andreas.

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
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."

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Divide by zero works on ia64!!!
  2005-05-09 15:23 Divide by zero works on ia64!!! Khalid Aziz
  2005-05-09 15:56 ` Andreas Schwab
@ 2005-05-09 20:37 ` Gerald Pfeifer
  2005-05-09 21:25 ` Khalid Aziz
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Gerald Pfeifer @ 2005-05-09 20:37 UTC (permalink / raw)
  To: linux-ia64

On Mon, 9 May 2005, Khalid Aziz wrote:
> 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:
> 
> # ./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.

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Divide by zero works on ia64!!!
  2005-05-09 15:23 Divide by zero works on ia64!!! Khalid Aziz
  2005-05-09 15:56 ` Andreas Schwab
  2005-05-09 20:37 ` Gerald Pfeifer
@ 2005-05-09 21:25 ` Khalid Aziz
  2005-05-09 21:54 ` Gerald Pfeifer
  2005-05-11 18:36 ` James E Wilson
  4 siblings, 0 replies; 6+ messages in thread
From: Khalid Aziz @ 2005-05-09 21:25 UTC (permalink / raw)
  To: linux-ia64

On Mon, 2005-05-09 at 17:56 +0200, Andreas Schwab wrote:
> Khalid Aziz <khalid_aziz@hp.com> writes:
> 
> > Something is busted in gcc.
> 
> Nothing.  Divide by zero is undefined.
> 

So, is the inconsistency between x86 and ia64 okay?

-- 
Khalid

==================================
Khalid Aziz                       Open Source and Linux Organization
(970)898-9214                                        Hewlett-Packard
khalid.aziz@hp.com                                  Fort Collins, CO

"The Linux kernel is subject to relentless development" 
                                - Alessandro Rubini


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Divide by zero works on ia64!!!
  2005-05-09 15:23 Divide by zero works on ia64!!! Khalid Aziz
                   ` (2 preceding siblings ...)
  2005-05-09 21:25 ` Khalid Aziz
@ 2005-05-09 21:54 ` Gerald Pfeifer
  2005-05-11 18:36 ` James E Wilson
  4 siblings, 0 replies; 6+ messages in thread
From: Gerald Pfeifer @ 2005-05-09 21:54 UTC (permalink / raw)
  To: linux-ia64

On Mon, 9 May 2005, Khalid Aziz wrote:
>> 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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Divide by zero works on ia64!!!
  2005-05-09 15:23 Divide by zero works on ia64!!! Khalid Aziz
                   ` (3 preceding siblings ...)
  2005-05-09 21:54 ` Gerald Pfeifer
@ 2005-05-11 18:36 ` James E Wilson
  4 siblings, 0 replies; 6+ messages in thread
From: James E Wilson @ 2005-05-11 18:36 UTC (permalink / raw)
  To: linux-ia64

On Mon, 2005-05-09 at 13:37, Gerald Pfeifer wrote:
> 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.



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2005-05-11 18:36 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-05-09 15:23 Divide by zero works on ia64!!! Khalid Aziz
2005-05-09 15:56 ` Andreas Schwab
2005-05-09 20:37 ` Gerald Pfeifer
2005-05-09 21:25 ` Khalid Aziz
2005-05-09 21:54 ` Gerald Pfeifer
2005-05-11 18:36 ` James E Wilson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox