Hi all,

I'd like to catch automatically over/underflows on floating point and
integer arithmetic. I thought -ftrapv would do the trick but I don't
really understand how it works. From the latest manual online:
-ftrapv
   This option generates traps for signed overflow on addition,
subtraction, multiplication operations.

I don't understand what's _a trap_. Does it send a signal or something
I can catch?
My idea in C++ is to catch whatever this sends and through a custom
exception. A little after in the manual I have:
-fnon-call-exceptions
   Generate code that allows trapping instructions to throw
exceptions. Note that this requires platform-specific runtime support
that does not exist everywhere. Moreover, it only allows trapping
instructions to throw exceptions, i.e. memory references or floating
point instructions. It does not allow exceptions to be thrown from
arbitrary signal handlers such as SIGALRM.

Does it mean that if I use this, exceptions are thrown when I have an
over/underflow? Could someone please explain me what happens inside
g++?
Another issue is if you think this is better than implementing a new
number type (consider it well implemented) which overloads all
arithmetic operators and throws exceptions when needed.

for example,
#include <iostream>
#include <limits>

int main() {

 unsigned int maxi = std::numeric_limits<unsigned int>::max();

 maxi++;

 std::cerr <<  "Value of maxi after sum: " << maxi << std::endl;

 return 0;

}

after compiled with or with -ftrapv g++ generates binary executables
which are exactly the same:
[EMAIL PROTECTED] ~ $ g++ -o ftrapvw ftrapv.cc
[EMAIL PROTECTED] ~ $ g++ -ftrapv -o ftrapv ftrapv.cc
[EMAIL PROTECTED] ~ $ md5sum ftrapv
fa2b15c2dccf7d721ae883cea1e5190e  ftrapv
[EMAIL PROTECTED] ~ $ md5sum ftrapvw
fa2b15c2dccf7d721ae883cea1e5190e  ftrapvw

Thanks in advance for your comments,

--
Paulo Jorge Matos - pocm at sat inesc-id pt
Web: http://sat.inesc-id.pt/~pocm
Computer and Software Engineering
INESC-ID - SAT Group

Reply via email to