Next | Prev | Up | Top | Contents | Index

Examples of Fortran Portability Issues

The following examples illustrate the variable size issues outlined above:


Example 1: Changing Integer Variables

Integer variables used to hold addresses must be changed to INTEGER*8.

32-bit code:

integer iptr, asize iptr = malloc(asize)

64-bit code:

integer asize integer*8 iptr iptr = malloc(asize)


Example 2: Enlarging Tables

Tables which hold integers used as pointers must be enlarged by a factor of two.

32-bit code:

integer tableptr, asize, numptrs numptrs = 100 asize = 100 * 4 tableptr = malloc(asize)

64-bit code:

integer asize, numptrs integer*8 tableptr numptrs = 100 asize = 100 * 8 tableptr = malloc(asize)


Example 3: Using #if Directives with Predefined Variables.

You should use #if directives so that your source code can be compiled either -32 or -64. The compilers support predefined variables such as _MIPS_SZPTR or _MIPS_SZLONG, which can be used to differentiate 32-bit and 64-bit source code. A later section provides a more complete list of predefined compiler variables and their values for 32-bit and 64-bit operation. For example, the set of changes in the previous example could be coded:

integer asize, numptrs

#if (_MIPS_SZPTR==64)

integer*8 tablept
asize = 100 * 8

#else 

integer*4 tableptr
asize = 100 * 4

#endif

tableptr = malloc(asize)

Example 4: Storing %LOC Return Values

%LOC returns 64-bit addresses. You need to use an INTEGER*8 variable to store the return value of a %LOC call.

#if (_MIPS_SZLONG==64)

INTEGER*8 HADDRESS

#else

INTEGER*4 HADDRESS

#endif

C determine memory location of dummy heap array

HADDRESS = %LOC(HEAP)


Example 5: Modifying C Routines Called by Fortran

C routines which are called by Fortran where variables are passed by reference must be modified to hold 64-bit addresses.Typically, these routines used ints to contain the addresses in the past. For 64-bit use, at the very least, they should use long ints. There are no problems if the original C routines simply define the parameters as pointers.

Fortran:

call foo(i,j)
C:

foo_( int *i, int *j) or at least
foo_( long i, long j)    

Example 6: Declaring Fortran Arguments as long ints

Fortran arguments passed by %VAL calls to C routines should be declared as long ints in the C routines.

Fortran:

call foo(%VAL(i))
C:

foo_( long i )

Example 7: Changing Argument Declarations in Fortran Subprograms

Fortran subprograms called by C where long int arguments are passed by address need to change their argument declarations.

C:

long l1, l2;
foo_(&l1, &l2);
Fortran:

subroutine foo(i, j)
#if (_MIPS_SZLONG==64)

INTEGER*8 i,j

#else

INTEGER*4 i,j

#endif

Next | Prev | Up | Top | Contents | Index