Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!

Any way to TRAP integer overflows ?

WSinc
New Contributor I
182 Views

This has probably been covered - maybe there is an article somewhere ?

I have a program that makes use of INTEGER arithmetic in quite a few places.

 

I would like to KNOW when an integer operation causes a bad result.

I was wondering if there is a way to automatically find out when this occurs,

other than putting in dozens of IF tests.

For example, if I have m=N*7, if there is an overflow, m and N will have different signs

about half the time. In the old IBM 7094 CPUs, this would trap if that occurs, but apparently the INTEL

does not give us that option.

 

any thoughts on this ?

0 Kudos
4 Replies
WSinc
New Contributor I
182 Views

I was thinking - it might be a useful addition to TELL the complier to CHECK the result,

without having to insert an if test in the code. Has this been addresed ?

Steve_Lionel
Black Belt Retired Employee
182 Views

Run-time integer overflow detection has been on the "wish list" for many years. Unfortunately, it is not currently available.

andrew_4619
Honored Contributor I
182 Views

It has been covered on the previous times you asked the same question. If it is important to your code write a couple of subroutines to operate on two integers passed in,  one to add and one to multiply.  You can then extensively test the args for overflow / truncation etc and return an error status..... It won't take a great deal of time to do.

jimdempseyatthecove
Black Belt
182 Views

Additional comments on Andrew's suggestion:

If you are really a tickler, you may also need to add unary negate (protect against -Z"80000000" being Z"80000000").
... or ,,,
You might want to extend the feature to use Z"80000000" as an integer equivalent to NaN (which can also be used a indicator for uninitialized vriable).

Then using the Fortran Preprocessor, you function calls to overflow detection routines can alternately expand to inline statements.

! some header #included
#if defined(USE_integer_overflow)
#define ADD(a,b) iov_ADD(a,b)
#else
#define ADD(a,b) (a+b)
#endif

...
C = ADD(A,B)

Jim Dempsey

Reply