[cfe-dev] Divide by zero - no floating point exception thrown

Cameron McInally cameron.mcinally at nyu.edu
Tue Sep 17 07:46:47 PDT 2013


On Tue, Sep 17, 2013 at 10:13 AM, SENTHIL KUMAR THANGAVELU <
senthil.t at samsung.com> wrote:

>  Hello all,
>
>     I saw some odd behaviour in clang for the following test case. g++ and
> intel compiler produce binary which dumps "Floating point exception (core
> dumped)" on execution. Incase of clang random values are returned for each
> execution. Originally I had written a basic template specialization code
> which does a divide by zero and saw this bug. However clang warns of divide
> by zero similar to g++ and icc.
>
>
>
> clang version is some trunk  head revision on 12 sep 2013.
>
>
>
> #include <iostream>
>
> void F002() {
>   int b = 23/0;
>   std::cout << "\n b == " <<
>     b << std::endl;
> }
>
> int main() {
>   F002();
> }
>
>
>
> ======= Following code also executes fine in clang without floating point
> exception. g++ & intel compiler binary emits floating point exception
>  ======
>
> template <int d, int e>
> int F004() {
>   return d/e;
> }
>
> int main() {
>   F004<4,5>();
>   F004<4,0>();
> }
>
> ============ clang version info ============
>
> test$ clang++ -v sf2.cpp
> clang version 3.4
> Target: x86_64-unknown-linux-gnu
> Thread model: posix
> Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.7
> Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.7.2
> Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.7
>  "/usr/local/bin/clang-3.4" -cc1 -triple x86_64-unknown-linux-gnu
> -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name
> sf2.cpp -mrelocation-model static -mdisable-fp-elim -fmath-errno
> -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array
> -target-cpu x86-64 -v -resource-dir /usr/local/bin/../lib/clang/3.4
> -internal-isystem
> /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7
> -internal-isystem
> /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/x86_64-linux-gnu
> -internal-isystem
> /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/backward
> -internal-isystem
> /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/x86_64-linux-gnu/c++/4.7
> -internal-isystem /usr/local/include -internal-isystem
> /usr/local/bin/../lib/clang/3.4/include -internal-externc-isystem
> /usr/include/x86_64-linux-gnu -internal-externc-isystem /include
> -internal-externc-isystem /usr/include -fdeprecated-macro
> -fdebug-compilation-dir /opt/SISO_BDP_SENTHIL_LLVM_JUL25/test -ferror-limit
> 19 -fmessage-length 135 -mstackrealign -fobjc-runtime=gcc
> -fobjc-default-synthesize-properties -fcxx-exceptions -fexceptions
> -fdiagnostics-show-option -vectorize-slp -o /tmp/sf2-0ecc11.o -x c++ sf2.cpp
> clang -cc1 version 3.4 based upon LLVM 3.4svn default target
> x86_64-unknown-linux-gnu
>
Hey Senthil,

Maybe I can save you some trouble...

http://llvm.1065342.n5.nabble.com/Integer-divide-by-zero-tt56495.html

To sum it up, integer division by zero is undefined behaviour and the
compiler is free to do what it would like to do. Although, I am still not
thrilled with this behaviour in Clang/LLVM. My preferred handling of this
undefined behaviour is to fall back to the processor's expected behaviour.
I believe that this is the "nice" thing to do.

Hope that helps,
Cameron
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130917/352a3539/attachment.html>


More information about the cfe-dev mailing list