Compaq Fortran
User Manual for
OpenVMS Alpha Systems


Previous Contents Index


Chapter 9
Converting Unformatted Numeric Data

This chapter describes how you can use Compaq Fortran to read and write unformatted numeric data in certain nonnative formats, including big endian IEEE and VAX floating-point formats.

On OpenVMS Alpha systems, you specify the floating-point format in memory with the /FLOAT qualifier. Compaq Fortran supports the following little endian floating-point formats in memory (default is /FLOAT=G_FLOAT):
Floating-Point Size /FLOAT=IEEE_FLOAT /FLOAT=D_FLOAT /FLOAT=G_FLOAT
KIND=4 S_float F_float F_float
KIND=8 T_float D_float G_float
KIND=16 X_float X_float X_float

If your program needs to read or write unformatted data files containing a floating-point format that differs from the format in memory for that data size, you can request that the unformatted data be converted.

For example, if your program primarily uses IEEE little endian floating-point data, specify /FLOAT=IEEE_FLOAT to specify use of the S_float, T_float, and X_float formats in memory. If your program needs to read a data file containing a different format (VAX or big endian), you need to specify which VAX or big endian floating-point format to use for conversion into the native IEEE memory format for that file.

Converting unformatted data is generally faster than converting formatted data and is less likely to lose precision for floating-point numbers.

9.1 Endian Order of Numeric Formats

Data storage in different computers use a convention of either little endian or big endian storage. The storage convention generally applies to numeric values that span multiple bytes, as follows:

Figure 9-1 shows the difference between the two byte-ordering schemes.

Figure 9-1 Little and Big Endian Storage of an INTEGER Value


Little and Big Endian Storage of an INTEGER

Moving data files between big endian and little endian computers requires that the data be converted.

9.2 Native and Supported Nonnative Numeric Formats

Compaq Fortran provides the capability for programs to read and write unformatted data (originally written using unformatted I/O statements) in several nonnative floating-point formats and in big endian INTEGER or floating-point format.

When reading a nonnative unformatted format, the nonnative format on disk must be converted to native format in memory. Similarly, native data in memory can be written to a nonnative unformatted format. If a converted nonnative value is outside the range of the native data type, a run-time message appears (listed in Section 7.1).

Supported native and nonnative floating-point formats include:

The native memory format uses little endian integers and little endian floating-point formats, as follows:

Table 9-1 lists the keywords for the supported unformatted file data formats. Use the appropriate keyword after the /CONVERT qualifier (such as /CONVERT=CRAY) or as an logical name value (see Section 9.4).

Table 9-1 Unformatted Numeric Formats, Keywords, and Supported Data Types
Recognized Keyword Description
BIG_ENDIAN Big endian integer data of the appropriate INTEGER size (one, two, or four bytes) and big endian IEEE floating-point formats for REAL and COMPLEX single- and double-precision numbers. INTEGER (KIND=1) or INTEGER*1 data is the same for little endian and big endian.
CRAY Big endian integer data of the appropriate INTEGER size (one, two, four, or eight bytes) and big endian CRAY proprietary floating-point format for REAL and COMPLEX single- and double-precision numbers.
IBM Big endian integer data of the appropriate INTEGER size (one, two, or four bytes) and big endian IBM proprietary floating-point format for REAL and COMPLEX single- and double-precision numbers.
FDX Native little endian integers of the appropriate INTEGER size (one, two, four, or eight bytes) and the following native little endian Compaq proprietary floating-point formats:
F_float for REAL (KIND=4) and COMPLEX (KIND=4)
D_float for REAL (KIND=8) and COMPLEX (KIND=8)
Alpha IEEE-like X_float for REAL (KIND=16) and COMPLEX (KIND=16)

For information on these native OpenVMS Alpha formats, see Table 8-1 and Section 8.4.

FGX Native little endian integers of the appropriate INTEGER size (one, two, four, or eight bytes) and the following native little endian Compaq proprietary floating-point formats:
F_float for REAL (KIND=4) and COMPLEX (KIND=4)
G_float for REAL (KIND=8) and COMPLEX (KIND=8)
Alpha IEEE-like X_float for REAL (KIND=16) and COMPLEX (KIND=16)

For information on these native OpenVMS Alpha formats, see Table 8-1 and Section 8.4.

LITTLE_ENDIAN Little endian integers of the appropriate INTEGER size (one, two, four, or eight bytes) and the following native little endian IEEE floating-point formats:
S_float for REAL (KIND=4) and COMPLEX (KIND=4)
T_float for REAL (KIND=8) and COMPLEX (KIND=8)
Alpha IEEE-like X_float for REAL (KIND=16) and COMPLEX (KIND=16)

For information on these native OpenVMS Alpha formats, see Table 8-1 and Section 8.4.

NATIVE No conversion occurs between memory and disk. This is the default for unformatted files.
VAXD Native little endian integers of the appropriate INTEGER size (one, two, four, or eight bytes) and the following little endian VAX Compaq proprietary floating-point formats:
F_float for REAL (KIND=4) and COMPLEX (KIND=4)
D_float for REAL (KIND=8) and COMPLEX (KIND=8)
H_float for REAL (KIND=16)

For information on the F_float and D_float formats, see Table 8-1 and Section 8.4. For information on the H_float format (available only on VAX systems), see Section A.8.

VAXG Native little endian integers of the appropriate INTEGER size (one, two, four, or eight bytes) and the following little endian VAX Compaq proprietary floating-point formats:
F_float format for REAL (KIND=4) and COMPLEX (KIND=4)
G_float format for REAL (KIND=8) and COMPLEX (KIND=8)
H_float format for REAL (KIND=16)

For information on the F_float and D_float formats, see Table 8-1 and Section 8.4. For information on the H_float format (available only on VAX systems), see Section A.8.

While this solution is not expected to fulfill all floating-point conversion needs, it provides the capability to read and write various types of unformatted nonnative floating-point data.

For More Information:

Note

1 IEEE floating-point formats are defined in the IEEE Standard for Binary Floating-Point Arithmetic, ANSI/IEEE Standard 754-1985, Institute of Electrical and Electronics Engineers, August 1985.

9.3 Limitations of Numeric Conversion

The Compaq Fortran floating-point conversion solution is not expected to fulfill all floating-point conversion needs.

Data (variables) in record structures (specified in a STRUCTURE statement) and data components of derived types (TYPE statement) are not converted. When variables are later examined as separate fields by the program, they will remain in the binary format they were stored in on disk, unless the program is modified.

If a program reads an I/O record containing multiple floating-point fields into an integer array (instead of their respective variables), the fields will not be converted. When they are later examined as separate fields by the program, they will remain in the binary format they were stored in on disk, unless the program is modified. To convert floating-point formats for individual fields, consider using the CVT$CONVERT_FLOAT routine (see Example A-1).

With EQUIVALENCE statements, the data type of the variable named in the I/O statement is used.

9.4 Methods of Specifying the Unformatted Numeric Format

The methods you can use to specify the type of numeric floating-point format are as follows:

If you specify more than one method, the order of precedence when you open a file with unformatted data is:

  1. Check for a logical name for a specific unit number
  2. Check for a FOR$CONVERT.ext logical name and then for a FOR$CONVERT_ext logical name (if the former logical name is not found)
  3. Check for the OPEN statement CONVERT specifier
  4. Check whether an OPTIONS statement with a /CONVERT=(keyword) qualifier was present when the program was compiled
  5. Check whether the FORTRAN command /CONVERT=(keyword) qualifier was used when the program was compiled

If none of these methods are specified, no conversion occurs between disk and memory. Data should therefore be in the native memory format (little endian integer and little endian IEEE or VAX format) or otherwise translated by the application program.

Any keyword listed in Table 9-1 can be used with any of these methods.

9.4.1 Logical Name FOR$CONVERTnnn Method

You can use the logical name method to specify multiple formats in a single program, usually one format for each unit number. You specify the numeric format at run time by setting the appropriate logical name before you open that unit number. For unit numbers that contain fewer than three digits, use leading zeros.

For example, to specify the numeric format for unit 9, set logical name FOR$CONVERT009 to the appropriate value (such as BIG_ENDIAN) before you run the program. For unit 125, set the logical name FOR$CONVERT125 before you run the program.

When you open the file, the logical name is always used, since this method takes precedence over the FORTRAN command qualifier methods. For instance, you might use this method to specify that different unformatted numeric formats for different unit numbers (perhaps in a command procedure that sets the logical name before running the program).

For example, assume you have a previously compiled program that reads numeric data from unit 28 and writes it to unit 29 using unformatted I/O statements. You want the program to read nonnative big endian (IEEE floating-point) format from unit 28 and write that data in native little endian format to unit 29.

In this case, the data is converted from big endian IEEE format to native little endian IEEE memory format (S_float and T_float) when read from unit 28, and then written without conversion in native little endian IEEE format to unit 29. The FORTRAN command qualifier /FLOAT specifies the IEEE floating-point format in memory and the LINK command creates the executable program:


$ FORTRAN/FLOAT=IEEE_FLOAT CONV_IEEE
$ LINK CONV_IEEE

Without requiring source code modification of recompilation of this program, the following DCL command sequence sets the appropriate logical name and then runs the program CONV_IEEE.EXE:


$ DEFINE FOR$CONVERT028 BIG_ENDIAN
$ DEFINE FOR$CONVERT029 NATIVE
$ RUN CONV_IEEE

Figure 9-2 shows the data formats used on disk and in memory when the example file conv_ieee.exe is run after the logical names are set with DCL commands.

Figure 9-2 Sample Unformatted File Conversion


Sample Unformatted File Conversion

For information on the DCL commands you can use to define and deassign logical names, see Appendix C.

9.4.2 Logical Name FOR$CONVERT.ext (and FOR$CONVERT_ext) Method

You can use this method to specify formats in a single program, usually one format for each specified file name extension (suffix). You specify the numeric format at run time by setting the appropriate logical name before an implicit or explicit OPEN statement to one or more unformatted files.

For example, assume you have a previously compiled program that reads floating-point numeric data from one file and writes to another file using unformatted I/O statements. You want the program to read nonnative big endian (IEEE floating-point) format from a file with a .dat file extension suffix and write that data in native little endian format to a file with a suffix of .data. In this case, the data is converted from big endian IEEE format to native little endian IEEE memory format when read from file.dat, and then written without conversion in native little endian IEEE format to the file with a suffix of .data, assuming that logical names FOR$CONVERT.DATA and FOR$CONVERTnnn (for that unit number) are not defined.

The FOR$CONVERTnnn method takes precedence over this method. When the appropriate logical name is set when you open the file, the FOR$CONVERT.ext logical name is used if a FOR$CONVERTnnn logical name is not set for the unit number.

The FOR$CONVERTnnn and FOR$CONVERT.ext (or FOR$CONVERT_ext) logical name methods take precedence over the other methods. For instance, you might use this method to specify that a unit number will use a particular format instead of the format specified in the program (perhaps for a one-time file conversion).

9.4.3 OPEN Statement CONVERT='keyword' Method

You can use the OPEN statement method to specify multiple formats in a single program, usually one format for each specified unit number. This method requires an explicit file OPEN statement to specify the numeric format of the file for that unit number.

This method takes precedence over the /CONVERT=keyword method (see Section 9.4.5), but has a lower precedence than the logical name method.

The following source code shows an OPEN statement coded for unformatted VAXD numeric data (read from unit 15), and an OPEN statement coded for unformatted native little endian format (written to unit 20). The absence of the CONVERT specifier (in the second OPEN statement) or logical name FOR$CONVERT020 indicates native little endian data for unit 20:


  OPEN (CONVERT='VAXD', FILE='graph3.dat', FORM='UNFORMATTED', UNIT=15) 
  . 
  . 
  . 
  OPEN (FILE='graph3_ieee.dat', FORM='UNFORMATTED', UNIT=20) 

A hard-coded OPEN statement CONVERT specifier keyword value cannot be changed after compile time. However, to allow selection of a particular format at run time, you can equate the CONVERT specifier to a variable and provide the user with a menu that allows selection of the appropriate format (menu choice sets the variable) before the OPEN occurs.

You can also select a particular format for a unit number at run time by using the logical name method (see Section 9.4.1), which takes precedence over the OPEN statement CONVERT specifier method.

You can issue an INQUIRE statement (by unit number) to an opened file to obtain the current CONVERT method in use.

9.4.4 OPTIONS Statement /CONVERT=keyword Method

You can only specify one numeric file format for all unit numbers using this method, unless you also use the logical name or OPEN statement CONVERT specifier method.

You specify the numeric format at compile time and must compile all routines under the same OPTIONS statement CONVERT=keyword qualifier. You could use one source program and compile it using different FORTRAN commands to create multiple executable programs that each read a certain format.

The logical name or OPEN CONVERT specifier methods take precedence over this method. For instance, you might use the logical name or OPEN CONVERT specifier method to specify each unit number that will use a format other than that specified using the FORTRAN command qualifier method. This method takes precedence over the FORTRAN command /CONVERT qualifier method.

You can use OPTIONS statements to specify the appropriate floating-point formats (in memory and in unformatted files) instead of using the corresponding FORTRAN command qualifiers. For example, to use G_float as both the memory format and as the unformatted file format, specify the following OPTIONS statements:


  OPTIONS /FLOAT=G_FLOAT 
  OPTIONS /CONVERT=NATIVE 

Because this method affects all unit numbers, you cannot read data in one format and write it in another format using the FORTRAN/CONVERT=keyword method alone, unless you use it in combination with the logical name method or the OPEN statement CONVERT keyword method to specify a different format for a particular unit number.

For More Information:

On the OPTIONS statement, see the Compaq Fortran Language Reference Manual.

9.4.5 FORTRAN Command /CONVERT=keyword Qualifier Method

You can specify only one numeric format for all unit numbers by using the FORTRAN command qualifier method, unless you also use the logical name method or CONVERT specifier method. You specify the numeric format at compile time and must compile all routines under the same /CONVERT=keyword qualifier (or the equivalent OPTIONS statement). If needed, you can use one source program and compile it using different FORTRAN commands to create multiple executable programs that each read a certain format.

The other methods take precedence over this method. For instance, you might use the logical name or OPEN CONVERT specifier method to specify each unit number that will use a format other than that specified using the FORTRAN command qualifier method.

For example, the following DCL commands compile program FILE.F90 to use VAX D_float and F_float data. Data is converted between the file format and the little endian memory format (little endian integers, S_float and T_float little endian IEEE floating-point format). The created file, VAXD_CONVERT.EXE, is then run:


$ FORTRAN/FLOAT=IEEE_MODE /CONVERT=VAXD/OBJECT=VAXD_CONVERT.OBJ FILE.F90
$ LINK VAXD_CONVERT
$ RUN VAXD_CONVERT

Because this method affects all unit numbers, you cannot read or write data in different formats if you only use the FORTRAN /CONVERT=keyword method. To specify a different format for a particular unit number, use the FORTRAN /CONVERT=keyword method in combination with the logical name method or the OPEN statement CONVERT specifier method.

9.4.6 Additional Notes on Nonnative Data

The following notes apply to porting nonnative data:

For More Information:

Note

1 American National Standard Fortran 90, ANSI X3.198-1991, and International Standards Organization standard ISO/IEC 1539:1991


Previous Next Contents Index