Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Any way to TRAP integer overflows ?

WSinc
New Contributor I
799 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
799 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 ?

0 Kudos
Steve_Lionel
Honored Contributor III
798 Views

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

0 Kudos
andrew_4619
Honored Contributor II
799 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.

0 Kudos
jimdempseyatthecove
Honored Contributor III
799 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

0 Kudos
Reply