[MPIWG Fortran] MPI-3 ticket 349: Fortran question

Dave Goodell (dgoodell) dgoodell at cisco.com
Thu Jan 2 10:36:26 CST 2014


On Dec 24, 2013, at 11:36 AM, Jim Dinan <james.dinan at gmail.com> wrote:

> Given that MPI_Aint will be at least the same size as ptrdiff_t, do we
> actually have a problem with subtracting MPI_Aints to find pointer diffs?
> It seems like this may actually be ok.

There's still a problem.  I don't think that ptrdiff_t has anything to do with it since that normally only comes into play when you subtract two _pointers_, not two integers acting sort of like pointers.

As an example, take a look at this little test program.  It subtracts two plausible addresses from the middle of the address space but underflows the signed arithmetic anyway.  When I compile this with '-ftrapv' under clang then I get a core dump when executing this program.

----8<----
#include <stdio.h>
#include <limits.h>

typedef long long MPI_Aint;

int main(int argc, char **argv)
{
    MPI_Aint a = LLONG_MIN; /* 0x8000000000000000LL */
    MPI_Aint b = 0x7ffffffffffffffcLL;
    MPI_Aint c = a - b; /* expect 0x4LL if signed overflow were defined */
    printf("a=%#llx b=%#llx c=%#llx\n", a, b, c);
    return 0;
}
----8<----

The overflow occurs because "(a - b)" is really "(-9223372036854775808 - 9223372036854775804)" in signed integer arithmetic.  Note that most implementations will give you the expected value if you don't set '-ftrapv', but that's *undefined behavior* according to the C standard and should not be relied upon (see http://www.airs.com/blog/archives/120).

This is all just the C side of things.  I have no idea whether Fortran integer overflow is defined.

-Dave




More information about the mpiwg-fortran mailing list