This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Division by zero


On 2/10/07, Robert Dewar <dewar@adacore.com> wrote:
Ian Lance Taylor wrote:
> "Jie Zhang" <jzhang918@gmail.com> writes:
>
>> But now gcc seems to optimize it away. For the following function:
>>
>> $ cat t.c
>> #include <limits.h>
>> void foo (int rc)
>> {
>>   int x = rc / INT_MAX;
>>   x = 4 / x;
>> }
>
> I believe we still keep division by zero in general.  In your example
> it gets optimized away because it is dead code.  Nothing uses x.

And it is certainly reasonable to do this optimization given that
the result of the division is undefined in C. In Ada, such a
division has well defined semantics (raise an exception), but
it is interesting to note that the optimization is valid in
Ada as well, since there is a special rule that basically says
you don't need to evaluate an expression if the only reason for
doing so is to see if it raises a predefined exception. That
rule is precisely to deal with cases like this.

The code I posted in my first email is from libgloss/libnosys/_exit.c. It's used to cause an exception deliberately. From your replies, it seems it should find another way to do that.

Thanks,
Jie


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]