Data Types and Passing Data

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:

  1. the primitive types of both languages;
  2. compound data types; and,
  3. arrays and matricies.

The sections in this chapter are:

  1. Data Types
    1. A Note on Complex Types
    2. A Note on Floating Point Formats
      1. Single (32-bit) and Double (64-bit) Precision Types
        • VAX F-Float, D-Float, and G-Float
        • IBM HFP Short and Long
      2. Extended (80-bit) Precision
      3. Quadruple (128-bit) Precision
        • VAX H-FLoat
        • IBM HFP Extended
      4. Half (16-bit) Precision Types
    3. char and Other Primitives
  2. Compound Data Structures
    1. The COMMON Block and C structs
    2. The VAX FORTRAN STRUCTURE Extension and C structs
    3. The VAX FORTRAN UNION Extensions and C unions
  3. Arrays

Data Types

Both 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_t
signed char
INTEGER*1
LOGICAL*1
BYTE
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_t
short
INTEGER*2
LOGICAL*2
int32_t
int
INTEGER*4
INTEGER[3]
LOGICAL*4
LOGICAL[3]
int64_t
long
INTEGER*8
LOGICAL*8
uint8_t
unsigned char
bool/_Bool
UNSIGNED*1 J3/24-116 extension
uint16_t
unsigned short
UNSIGNED*2
uint32_t
unsigned int
UNSIGNED*4
UNSIGNED[3]
uint64_t
unsigned long
UNSIGNED*8
float
_Float32
REAL*4
REAL[3]
double
_Float64
REAL*8
DOUBLE PRECISION[3]
__float80
_Float64x
REAL*10 Compiler-specific extensions.
__float128
_Float128
REAL*16
_Float32 complex
float complex
COMPLEX*8
COMPLEX[3]
_Float64 complex
double complex
COMPLEX*16
DOUBLE 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:

  1. The __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.
  2. 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).
  3. For the floating point types, both the common form and form defined by ISO/IEC TS 18661-3:2015 (the _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.

A Note on Complex 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
struct fcomplex_s {
    _Float32 real;
    _Float32 imag;
};
_Float64 complex
struct dcomplex_s {
    _Float64 real;
    _Float64 imag;
};
_Float64x complex
struct ecomplex_s {
    _Float64x real;
    _Float64x imag;
};
_Float128 complex
struct qcomplex_s {
    _Float128 real;
    _Float128 imag;
};

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.

A Note on Floating Point Formats

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.

Single (32-bit) and Double (64-bit) Precision Types

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 F-Float, D-Float, and G-Float

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.

IBM HFP Short and Long

Extended (80-bit) Precision

“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 (128-bit) Precision

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.

VAX H-FLoat

IBM HFP Extended

Half (16-bit) Precision Types

char and Other Primitives

C'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.

Compound Data Structures

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.

The COMMON Block and C structs

The 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:

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.

The VAX FORTRAN STRUCTURE Extension and C structs

In 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.

The VAX FORTRAN UNION Extensions and C unions

Arrays


  1. By “intrinsic data structures” we mean compound types (e.g. the structures and unions).
  2. Though it should be noted that C is weakly typed, whereas FORTRAN 77 is strongly typed.
  3. Default type if the KIND parameter is left unspecified.
  4. The international standard ISO/IEC/IEEE 60559:2011 is identical to the IEEE 754:2008 standard, hence the macro calling out the declarations as ‘ISO 60559” types.
  5. On Intel 64, GCC and Clang when instructed to provide the ’18661 declarations, will also enable the use of the _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.
  6. IA-32, Intel 64, IA-64, PA-RISC, MIPS, IBM ESA/390 (G5 and above) and z/Architecture[7], Loongson, ARM (32- and 64-bit), Alpha[8], and POWER (including PowerPC)[9].
  7. IBM's mainframe offerings also support the HFP formats that originated on the System/360.
  8. Alpha supports the VAX F-, D-, and G-float formats for backwards compatibility. The ’754 types are, per the Alpha System Reference Manual (7E) are referred to as S-, T-, and X-float for 32-, 64-, and 128-bit floating point respectively.
  9. POWER supports the “Double Double” format for extended precision work.


Valid XHTML 1.0 Strict Valid CSS!
Fave icon Database CMYK © 2008 – Barry Mieny
Syntax highlighting using highlight.js by © 2006 – Ivan Sagalaev