Divide by zero does not crash - Powerpc

This is a discussion on Divide by zero does not crash - Powerpc ; I noticed that my application does not crash when there is a divide by zero. For example, the following test code: int i; i = 10/0; printf("i = %d\n", i); gives: i = 0 It does not crash. Is there ...

+ Reply to Thread
Results 1 to 10 of 10

Thread: Divide by zero does not crash

  1. Divide by zero does not crash

    I noticed that my application does not crash when there is a divide by
    zero. For example, the following test code:

    int i;
    i = 10/0;
    printf("i = %d\n", i);

    gives:

    i = 0

    It does not crash.

    Is there a way to make the application crash when there is a divide by
    zero? I am running Linux 2.4.26 on a powerpc (MPC8248). I compile
    with powerpc-linux-gcc.

  2. Re: Divide by zero does not crash

    Bill wrote:

    > I noticed that my application does not crash when there is a divide by
    > zero. For example, the following test code:
    >
    > int i;
    > i = 10/0;
    > printf("i = %d\n", i);
    >
    > gives:
    >
    > i = 0
    >
    > It does not crash.


    Because it's a "div" operation on integer.

    > Is there a way to make the application crash when there is a divide by
    > zero? I am running Linux 2.4.26 on a powerpc (MPC8248). I compile
    > with powerpc-linux-gcc.


    Use float instead of int.

    HTH...

    Dirk
    --
    Dirk Heinrichs | Tel: +49 (0)162 234 3408
    Configuration Manager | Fax: +49 (0)211 47068 111
    Capgemini Deutschland | Mail: dirk.heinrichs@capgemini.com
    Wanheimerstraße 68 | Web: http://www.capgemini.com
    D-40468 Düsseldorf | ICQ#: 110037733
    GPG Public Key C2E467BB | Keyserver: www.keyserver.net

  3. Re: Divide by zero does not crash

    Dirk Heinrichs wrote:
    > Bill wrote:
    >
    >> I noticed that my application does not crash when there is a divide by
    >> zero. For example, the following test code:
    >>
    >> int i;
    >> i = 10/0;
    >> printf("i = %d\n", i);
    >>
    >> gives:
    >>
    >> i = 0
    >>
    >> It does not crash.

    >
    > Because it's a "div" operation on integer.
    >
    >> Is there a way to make the application crash when there is a divide by
    >> zero? I am running Linux 2.4.26 on a powerpc (MPC8248). I compile
    >> with powerpc-linux-gcc.

    >
    > Use float instead of int.
    >
    > HTH...
    >
    > Dirk

    You will not get an abend with float either:

    float f;
    f = 10.0/0;
    printf("f = %f\n", f);

    ./a.out
    f = INF

    An Abend is not good programming either, you should set a signal handle
    for integer and floating point exceptions: see

    /* SIGFPE */
    #define FPE_INTDIV 20
    #define FPE_INTOVF 21
    #define FPE_FLTDIV 22
    #define FPE_FLTOVF 23
    #define FPE_FLTUND 24
    #define FPE_FLTRES 25
    #define FPE_FLTINV 26
    #define FPE_FLTSUB 27

    Cheers, John.


  4. Re: Divide by zero does not crash

    John W. Gillikin wrote:

    > Dirk Heinrichs wrote:
    >> Bill wrote:
    >>
    >>> I noticed that my application does not crash when there is a divide by
    >>> zero. For example, the following test code:
    >>>
    >>> int i;
    >>> i = 10/0;
    >>> printf("i = %d\n", i);
    >>>
    >>> gives:
    >>>
    >>> i = 0
    >>>
    >>> It does not crash.

    >>
    >> Because it's a "div" operation on integer.
    >>
    >>> Is there a way to make the application crash when there is a divide by
    >>> zero? I am running Linux 2.4.26 on a powerpc (MPC8248). I compile
    >>> with powerpc-linux-gcc.

    >>
    >> Use float instead of int.
    >>

    > You will not get an abend with float either:
    >
    > float f;
    > f = 10.0/0;
    > printf("f = %f\n", f);
    >
    > ./a.out
    > f = INF


    Hmm, you're right. However, I get at least a warning at compile time.

    Bye...

    Dirk
    --
    Dirk Heinrichs | Tel: +49 (0)162 234 3408
    Configuration Manager | Fax: +49 (0)211 47068 111
    Capgemini Deutschland | Mail: dirk.heinrichs@capgemini.com
    Wanheimerstraße 68 | Web: http://www.capgemini.com
    D-40468 Düsseldorf | ICQ#: 110037733
    GPG Public Key C2E467BB | Keyserver: www.keyserver.net

  5. Re: Divide by zero does not crash

    > int i;
    > i = 10/0;


    It's evaluated on compile time. So it can't crash (but it should result
    in a compiler error or warning).

    -Michael

  6. Re: Divide by zero does not crash

    On Feb 8, 7:48 am, Michael Schnell
    wrote:
    > > int i;
    > > i = 10/0;

    >
    > It's evaluated on compile time. So it can't crash (but it should result
    > in a compiler error or warning).
    >
    > -Michael


    This does give a warning at compile time because the constant zero is
    there. What concerns me is when the denominator is a variable that
    takes the value of zero while the application is running. There is
    neither a compilation warning nor any indication that a divide by zero
    is occurring when the application is running.

  7. Re: Divide by zero does not crash

    Bill wrote in
    news:68a88e30-09b5-439e-b9ca-b5a4cbbda73a@e10g2000prf.googlegroups.com:

    > On Feb 8, 7:48 am, Michael Schnell
    > wrote:
    >> > int i;
    >> > i = 10/0;

    >>
    >> It's evaluated on compile time. So it can't crash (but it should
    >> result in a compiler error or warning).


    > This does give a warning at compile time because the constant zero is
    > there. What concerns me is when the denominator is a variable that
    > takes the value of zero while the application is running. There is
    > neither a compilation warning nor any indication that a divide by zero
    > is occurring when the application is running.


    The behavior of integer division by 0 is undefined per the C Language
    standard and, hence, implementation-dependent. On x86, division by 0
    produces a hardware exception. On x86 linux, this exception is presented
    to the program as a signal. However, a conforming implementation may
    choose to do anything else that is convenient: nothing (leave quotient
    unmodified), produce random value for the quotient, kill the process.

    GH

  8. Re: Divide by zero does not crash

    Bill writes:

    >On Feb 8, 7:48 am, Michael Schnell
    > wrote:
    >> > int i;
    >> > i = 10/0;

    >>
    >> It's evaluated on compile time. So it can't crash (but it should result
    >> in a compiler error or warning).
    >>
    >> -Michael


    >This does give a warning at compile time because the constant zero is
    >there. What concerns me is when the denominator is a variable that
    >takes the value of zero while the application is running. There is
    >neither a compilation warning nor any indication that a divide by zero
    >is occurring when the application is running.


    So PUT IN A TEST!!!
    if (x==0)
    {
    fprintf(stderr,"Divide by zero.\n");
    exit;
    }
    else
    i=10/x;


  9. Re: Divide by zero does not crash

    Bill writes:
    >On Feb 8, 7:48 am, Michael Schnell
    > wrote:
    >> > int i;
    >> > i = 10/0;

    >>
    >> It's evaluated on compile time. So it can't crash (but it should result
    >> in a compiler error or warning).


    The compiler could insert code that crashes (or produces a signal or
    whatever behaviour is deemed appropriate) at run-time. But it does
    not, because the behaviour when not doing an evaluation at
    compile-time is not to crash or produce a signal. In contrast, on an
    AMD64 box I also get a warning at compile-time, and a SIGFPE at
    run-time.

    >This does give a warning at compile time because the constant zero is
    >there. What concerns me is when the denominator is a variable that
    >takes the value of zero while the application is running. There is
    >neither a compilation warning nor any indication that a divide by zero
    >is occurring when the application is running.


    Yes, that's a property of the PowerPC archictecture. AFAIK the theory
    of the hardware designers was that the compiler should produce code
    equivalent to:

    q=n/d;
    if (d == 0)
    raise(SIGFPE);

    The idea is that the check would be performed during the latency of
    the division, so it would usually not cost extra time. So one could
    make the hardware simpler by not putting in the check there. I may be
    confusing PowerPC with another RISC architecture (most likely MIPS)
    wrt this aspect, though.

    But obviously gcc does not do this (there does not even seem to be an
    option to turn this on). Do other C compilers for PowerPC (from IBM,
    Apple or Motorola generate such code)?

    Followups set to comp.os.linux.powerpc.

    - anton
    --
    M. Anton Ertl Some things have to be seen to be believed
    anton@mips.complang.tuwien.ac.at Most things have to be believed to be seen
    http://www.complang.tuwien.ac.at/anton/home.html

  10. Re: Divide by zero does not crash

    Bill wrote:
    > I noticed that my application does not crash when there is a divide
    > by zero.


    I'd suggest to download the PowerPC ISA 2.05 from power.org.

    On p.68 it states for integer divides, e.g. "divw":
    "If an attempt is made to perform [...] 0 then the contents
    of register RT are undefined [...]"

    => no exception

    For _floating_point_ operations, on p.111 it states:
    "A Zero Divide Exception occurs when a Divide instruction is executed
    with a zero divisor value and a finite nonzero dividend value."



+ Reply to Thread