It is safe to assume that all non-trivial programs do involve some form of data manipulation. Consequently one of, if not the most important, areas to discuss in interfacing C and FORTRAN is accessing and manipulating data. To this end, the first thing to be covered will be discussing primitive types and intrinsic data structures[1] available in C and FORTRAN 77.
Three particular areas will be covered in this chapter of the guide:
The sections in this chapter are:
char and Other
PrimitivesBoth C and FORTRAN 77 are statically typed languages[2], and conveniently their primitive types are relatively compatible with a few caveats that will be expounded upon in subsections further on in this chapter. Before we go further, let us first take a look at the simplest primitive type concordances between C and FORTRAN 77:
| C | FORTRAN 77 | Notes |
|---|---|---|
int8_tsigned char |
INTEGER*1LOGICAL*1BYTE |
The LOGICAL types are equivalent to the
INTEGER type with the same KIND size parameter.
However, the LOGICAL type, allows for comparisons with the logical
operators and, when printed, is interpreted as a logical value. |
int16_tshort |
INTEGER*2LOGICAL*2 |
|
int32_tint |
INTEGER*4INTEGER[3]LOGICAL*4LOGICAL[3] |
|
int64_tlong |
INTEGER*8LOGICAL*8 |
|
uint8_tunsigned charbool/_Bool |
UNSIGNED*1 |
J3/24-116 extension |
uint16_tunsigned short |
UNSIGNED*2 |
|
uint32_tunsigned int |
UNSIGNED*4UNSIGNED[3] |
|
uint64_tunsigned long |
UNSIGNED*8 |
|
float_Float32 |
REAL*4REAL[3] |
|
double_Float64 |
REAL*8DOUBLE PRECISION[3] |
|
__float80_Float64x |
REAL*10 |
Compiler-specific extensions. |
__float128_Float128 |
REAL*16 |
|
_Float32 complexfloat complex |
COMPLEX*8COMPLEX[3] |
|
_Float64 complexdouble complex |
COMPLEX*16DOUBLE COMPLEX[3] |
|
_Float64x complex__float80 complex |
COMPLEX*20 |
Compiler-specific extensions. |
_Float128 complex__float128 complex |
COMPLEX*32 |
|
char |
CHARACTER |
Some FORTRAN 77 types added by extensions are included in the above table of primitive types, as the assorted notes indicate; these will be discussed further in greater depth. First, however, a moment should be taken to address some of the primitive types listed for C. The main caveats in the realm of C types are:
__float80 and __float128 types are
compiler extensions, that may or may not be available. For instance, both are
available in GCC on Debian (on Intel 64), neither are available in GCC on
OpenBSD/VAX, and quadruple-precision __float128 is available while
extended-precision __float80 is not in both Clang on Debian and
FreeBSD (on Intel 64), and GCC on z/Architecture.bool (and the intrinsice _Bool) as well as
the x complex types are from the C99 standard; from the
stdbool.h and complex.h headers, respectively. In C89
(and older K&R) style, Boolean flags are most usually implemented by using a
simple undecorated int; this styling does work somewhat better with
FORTRAN 77, given that the default FORTRAN LOGICAL corresponds
to the default C int whereas bool requires one use a
FORTRAN CHARACTER type, or have access to the UNSIGNED
extension. The x complex derived types are addressed in
greater detail, but suffice to say that they may be—and depending on the
compiler may require they be replaced by a compound data type (that is,
a struct)._Floatx
declarations) are listed, though the latter format may not be supported by one's
compiler of choice—or they may require additional work to use. As an
example, using GCC or Clang (of a sufficiently recent version), if one defines
__STDC_WANT_IEC_60559_TYPES_EXT__[4]
as a preprocessor macro the ’18661 declarations will be enabled for
use[5].FORTRAN 77 does not, by default, support unsigned data types. However,
the US Fortran Programming Language Standards Technical Committee promulgated
the J3/24-116 extension in February of 2024. A sufficiently modern version of
GFortran and Flang supports—if passed the -funsigned flag at
compilation—the UNSIGNED*n types.
As mentioned, the x complex C declarations are
available only if one is making use of C99 or later, in C89 or a K&R variant
the following set of structs may be used in place of the
complex.h type declarations:
complex.h Declaration |
Equivalent Declaration |
|---|---|
_Float32 complex |
|
_Float64 complex |
|
_Float64x complex |
|
_Float128 complex |
|
The struct definitions are prototypical, one need not use those
specific definitions—the structure names, or using ’18661 style type
declarations within the struct—what matters only is that the
structure consists of a pair of the floating point type.
On most architectures[6], the floating point primitive types correspond to the binary formats of the IEEE 754:2008 standard. The major exception—in relation to this guide, at the very least—being VAX.
The two commonly supported floating point formats—single-precision and
double-precision—correspond to the IEEE types of binary32 for
single-precision (float, _Float32, REAL,
and REAL*4), and binary64 for double-precision
(double, _Float64, DOUBLE PRECISION, and
REAL*8).
VAX predates the IEEE 754 standard by several years—the VAX-11/780 being launched in October 1977, while the first version of the IEEE 754 standard was promulgated in July195, and unfortunately for data interchange, its floating point formats are not binary compatible with the ’754 binary formats. The VAX 32-bit floating point format—F-float—is actually the same as that as the single-precision format of DEC's venerable PDP-11 series. The two VAX 64-bit floating point formats—D-float and G-float, respectively—differ; D-float, similar to F-float, is based upon the double-precision format of the PDP-11's floating point processor, whereas G-float has an extended exponent range with a correspondingly reduced precision of the mantissa. Given this fact, calculations on VAX will naturally have potentially different results—though they should correspond, within a reasonable margin of error, to results obtained on other architectures.
“Extended-precision” floating point originates on the Intel 8087 floating point coprocessor. Whilst the format is not defined by the ’754 standard, Intel did design their format to have the same capabilities as, with the same general construction as, the ’754 standard binary formats. Per the GCC documentation, extended-precision floating point is supported on IA-32, Intel 64, and IA-64 architectures—obviously, as they are architectures developed by Intel. This format is not universally supported by all compilers, even on the Intel platforms that have hardware support for it—as evidenced by the fact that GCC and GFortran support it, whilst Clang and Flang do not.
Quadruple-precision floating point corresponds to the binary128
type of the IEEE 754 standard. It is supported on several architectures,
namely IA-32, Intel 64, IA-64, POWER/PowerPC, Loongson, and PA-RISC; though it
is possible that the quadruple-precision operations are implemented in software
as “soft” floating point.
char and Other PrimitivesC's char type, and FORTRAN's CHARACTER type are, as
should be obvious, equivalent. However their particulars are rather annoyingly
implementation defined—at least, in terms of whether they are a signed or
unsigned type. These particulars don't particularly matter at the FORTRAN end
of thing, as attempting to use a CHARACTER type in an arithmetical
expression will not work, but from the C side of things it does matter as one
can, and often does, use char in arithmetic.
The other miscellaneous types that C's standard and system libraries
provide—size_t, time_t, pid_t, et
cetera—may be compatible with FORTRAN types—time_t for
example would be equivalent to INTEGER*8 or INTEGER*4
depending on the system—whereas some types—opaque pointers to
structs—would not. For those which are compatible, often it
can be very system dependent, such as the given example of time_t,
where, depending on the system, it could be 32- or 64-bit.
With the possible exception of complex types, all the above information
applies to primitive, “unitary” types. And whilst it is possible to
pass only individual variables, it is sometimes necessary to pass a collection
of variables that have been structured into a group, hence the existence of
compound types. Per the FORTRAN standard, there is only one formal method of
combining a set of variables into a compound unit, the COMMON
block, however GFortran supports extensions from VAX FORTRAN for a method
of constructing compound types more akin to that employed by C.
COMMON Block and C structsThe COMMON block is, in C parlance, a struct which
has been declared globally; but where a globally defined struct in
C is visible to all functions in the translation unit where it is defined, a
COMMON block is only visible within the functions where it has
been defined. As a contrived example, in the following code:
REAL FUNCTION FOO(T)
REAL T, U, V, W, X, Y, Z
COMMON /A/ U, V, W
COMMON /B/ X, Y, Z
END
REAL FUNCTION BAR(T)
REAL T, U, V, W
COMMON /A/ U, V, W
END
SUBROUTINE BAZ(T)
REAL T, X, Y, Z
COMMON /B/ X, Y, Z
END
SUBROUTINE QUX(T)
REAL T, RED, BLUE, GREEN, LARRY, MOE, CURLY
COMMON /A/ RED, BLUE, GREEN
COMMON /B/ LARRY, MOE, CURLY
END
Two COMMON blocks have been defined, A and
B. Block A is visible within the functions
FOO(T), BAR(T), and the QUX(T)
subroutine; block B is visible within the functions
FOO(T), BAZ(T), and the QUX(T). Note the
delightfully cursèd—from the perspective of a programmer
who usually works with C—property of the COMMON block that,
so long as the types and ordering remain the same, the variables within a
COMMON block need not maintain the same name between functions.
To access a COMMON block from C, is actually relatively simple.
Recall that the COMMON block was stated to be equivalent to a
globally defined struct; that is precisely how one would
declare the COMMON block for access within a C translation unit.
Taking our contrived exemplar from above, within a C translation unit, the two
blocks A and B may be accessed as:
extern struct {
float u, v, w;
} a_;
extern struct {
float x, y, z;
} b_;
The usual methods of accessing members of a struct then apply,
for example, accessing the variable V in COMMON block
A, you would refer to it as a_.v. Note that, just as
how in a FORTRAN function or subroutine the names of the COMMON
block member variables can by chosen freely so long as the organization of the
COMMON block and its name remain the same, the struct
definition may also use any names for the member variables so long as they also
maintain the same organization and name as the COMMON block; note
that GFortran and Flang name compiled objects[X]
with a trailing underscore, as well as making the object name all lower case.
Therefore, let us look at a few examples of COMMON blocks and their
equivalent C structs:
MESG → mesg_DATA$CONSTANTS → data$constants_Note that the example COMMON blocks shown above—the blocks
A and B—being identical in organization can have
their definitions simplified within the scope of a C codebase. Namely, one can
define a structure of the form:
struct common_block {
float f1, f2, f3;
};
Then the COMMON blocks A and B could
then be referenced by defining extern struct common_block a_; and
extern struct common_block b_; at whichever part of the translation
unit is most convenient. Note that the name of the structure definition (e.g.
common_block) can be whatever you choose, as it is not visible to
the FORTRAN 77 objects.
STRUCTURE Extension and C
structsIn the section on the COMMON block, the C equivalent of said was
implemented as a struct (with external linkage). FORTRAN 77,
as per the language standard, does not have a method of defining a compound
type—in a similar manner as a C struct; COMMON
blocks are the intended manner of compounding the primitive types. However,
Digital's VAX FORTRAN provided an extension to the language that allows the
definition of compound types as the C programmer would envisage them. This
section will discuss that extension—as both GFortran and Flang implement
it.
struct product {
int32_t id, model;
char name[16];
float cost, markup;
};
STRUCTURE /PRODUCT/
INTEGER*4 ID
INTEGER*4 MODEL
CHARACTER*16 NAME
REAL*4 COST
REAL*4 MARKUP
END STRUCTURE
An example of a C struct is provided above, with
the equivalent definition using the VAX FORTRAN STRUCTURE
extension beside it. The identifier used is not preserved, and if one so
wishes, it is possible to give the struct and corresponding
STRUCTURE different identifiers. What is important, is
that the structures have the same layout of variables, and that any padding used
for memory alignment is also identitical.
struct product unit;
RECORD /PRODUCT/ ITEM
The above illustrates how one defines a variable that uses a
struct/STRUCTURE. The following shows how one may
access the members of a struct/STRUCTURE:
unit.id = 1729;
unit.model = INT32_C(0x439B36);
strncpy(unit.name, "Checker A11E", 15);
unit.cost = 15650.00;
unit.markup = 0.05;
ITEM.ID = 600
ITEM.MODEL = Z'578FC9'
ITEM.NAME = 'Mercedes 600'
ITEM.COST = 172400.00
ITEM.MARKUP = 0.15
As can be seen, the method of accessing member variables is the same in both C and FORTRAN 77.
UNION Extensions and C
unionsKIND parameter is left
unspecified._Float16
type— that is 16-bit—half-precision—floating point. It should
be noted that GCC explicitly requires that the CPU support SSE2 at a minimum,
and that one also explicitly target SSE2 instructions as well (by using
the -msse2 target flag); both GCC and Clang support hardware
acceleartion of half-precision floating point if AVX512 available (and targeted
during compilation). Whether GFortran and Flang support REAL*2 or
COMPLEX*4, is currently unknwon to the author.
Fave icon Database CMYK © 2008 – Barry Mieny
Syntax highlighting using highlight.js by © 2006 – Ivan Sagalaev