This appendix shows some of the major features differences between standard Fortran 95 and the Fortran 95 compiler, f95.
The Fortran 95 compiler provides the following source langauge features and extensions to the Fortran 95 standard.
f95 allows 999 continuation lines (1 initial and 999 continuation lines). Standard Fortran 95 allows 19 for fixed-form and 39 for free-form.
In fixed-form source, lines can be longer than 72 characters, but everything beyond column 73 is ignored. Standard Fortran 95 only allows 72-character lines.
The f95 fixed-format source text is defined as follows:
A tab in any of columns 1 through 6 makes the line as a tab form source line.
A comment indicator or a statement number may precede the tab.
If a tab is the first nonblank character, then:
If the character after the tab is anything other than a nonzero digit, then the text following the tab is an initial line.
If there is a nonzero digit after the first tab, the line is a continuation line. The text following the nonzero digit is the next part of the statement.
The f95 default maximum line length is 72 columns for fixed form and 132 for free form. Use the -e compiler option to extend the lines in fixed-format source to 132 columns.
Example: The tab form source on the left is treated as shown on the right.
!^IUses of tabs ^ICHARACTER *3 A = ’A’ ^IINTEGER B = 2 ^IREAL C = 3.0 ^IWRITE(*,9) A, B, C 9^IFORMAT(1X, A3, ^I1 I3, ^I2 F9.1 ) ^IEND ! Uses of tabs CHARACTER *3 A = ’A’ INTEGER B = 2 REAL C = 3.0 WRITE(*,9) A, B, C 9 FORMAT(1X, A3, 1 I3, 2 F9.1 ) END |
In the example above, ”^I” stands for the tab character, and the line starting with “1” and “2” are continuation lines. The coding is shown to illustrate various tab situations, and not to advocate any one style.
Tabs in f95 force the rest of the line to be padded out to column 72. This may cause unexpected results if the tab appears within a character string that is continued onto the next line:
Source file:
^Iprint *, "Tab on next line ^I1this continuation line starts with a tab." ^Iend |
Running the code:
Tab on next line this continuation line starts with a tab. |
The source form assumed by f95 depends on options, directives, and suffixes.
Files with a .f or .F suffix are assumed to be in fixed format. Files with a .f90, .f95, .F90, or .F95 suffix are assumed to be in free format.
Table 4–1 F95 Source Form Command-line Options
Option |
Action |
---|---|
-fixed |
Interpret all source files as Fortran fixed form |
-free |
Interpret all source files as Fortran free form |
If the -free or -fixed option is used, it overrides the file name suffix. If either a !DIR$ FREE or !DIR$ FIXED directive is used, it overrides the option and file name suffix.
Some mixing of source forms is allowed.
In the same f95 command, some source files can be fixed form, some free.
In the same file, free form can be mixed with fixed form by using !DIR$ FREE and !DIR$ FIXED directives.
In the same program unit, tab form can be mixed with free or fixed form.
Sun Fortran 95 is case insensitive by default. That means that a variable AbcDeF is treated as if it were spelled abcdef. Compile with the -U option to have the compiler treat upper and lower case as unique.
A single Fortran 95 program unit can define up to 65,535 derived types and 16,777,215 distinct constants.
Names of variables and other objects can be up to 127 characters long. 31 is standard.
This section describes features and extensions to the Fortran 95 data types.
f95 supports constants and expressions of Boolean type. However, there are no Boolean variables or arrays, and there is no Boolean type statement.
For masking operations, a bitwise logical expression has a Boolean result; each of its bits is the result of one or more logical operations on the corresponding bits of the operands.
For binary arithmetic operators, and for relational operators:
If one operand is Boolean, the operation is performed with no conversion.
If both operands are Boolean, the operation is performed as if they were integers.
No user-specified function can generate a Boolean result, although some (nonstandard) intrinsics can.
Boolean and logical types differ as follows:
Variables, arrays, and functions can be of logical type, but they cannot be Boolean type.
There is a LOGICAL statement, but no BOOLEAN statement.
A logical variable, constant, or expression represents only two values, .TRUE. or .FALSE. A Boolean variable, constant, or expression can represent any binary value.
Logical entities are invalid in arithmetic, relational, or bitwise logical expressions. Boolean entities are valid in all three.
f95 allows a Boolean constant (octal, hexadecimal, or Hollerith) in the following alternate forms (no binary). Variables cannot be declared Boolean. Standard Fortran does not allow these forms.
ddddddB, where d is any octal digit
You can use the letter B or b.
There can be 1 to 11 octal digits (0 through 7).
11 octal digits represent a full 32-bit word, with the leftmost digit allowed to be 0, 1, 2, or 3.
Each octal digit specifies three bit values.
The last (right most) digit specifies the content of the right most three bit positions (bits 29, 30, and 31).
If less than 11 digits are present, the value is right-justified—it represents the right most bits of a word: bits n through 31. The other bits are 0.
Blanks are ignored.
Within an I/O format specification, the letter B indicates binary digits; elsewhere it indicates octal digits.
X’ddd’ or X"ddd", where d is any hexadecimal digit
There can be 1 to 8 hexadecimal digits (0 through 9, A-F).
Any of the letters can be uppercase or lowercase (X, x, A-F, a-f).
The digits must be enclosed in either apostrophes or quotes.
Blanks are ignored.
The hexadecimal digits may be preceded by a + or - sign.
8 hexadecimal digits represent a full 32-bit word and the binary equivalents correspond to the contents of each bit position in the 32-bit word.
If less than 8 digits are present, the value is right-justified—it represents the right most bits of a word: bits n through 31. The other bits are 0.
Accepted forms for Hollerith data are:
nH… |
’…’H |
"…"H |
nL… |
’…’L |
"…"L |
nR… |
’…’R |
"…"R |
Above, “…” is a string of characters and n is the character count.
If any character constant is in a bitwise logical expression, the expression is evaluated as Hollerith.
A Hollerith constant can have 1 to 4 characters.
Examples: Octal and hexadecimal constants.
Boolean Constant |
Internal Octal for 32-bit Word |
---|---|
0B |
00000000000 |
77740B |
00000077740 |
X"ABE" |
00000005276 |
X"-340" |
37777776300 |
X’1 2 3’ |
00000000443 |
X’FFFFFFFFFFFFFFFF’ |
37777777777 |
Examples: Octal and hexadecimal in assignment statements.
i = 1357B j = X"28FF" k = X’-5A’ |
Use of an octal or hexadecimal constant in an arithmetic expression can produce undefined results and do not generate syntax errors.
f95 allows BOZ constants in the places other than DATA statements.
B’bbb’ |
O’ooo’ |
Z’zzz’ |
B"bbb" |
O"ooo" |
Z"zzz" |
If these are assigned to a real variable, no type conversion occurs.
Standard Fortran allows these only in DATA statements.
f95 allows the following nonstandard type declaration forms in declaration statements, function statements, and IMPLICIT statements. The form in column one is nonstandard Fortran 95, though in common use. The kind numbers in column two can vary by vendor.
Table 4–2 Size Notation for Numeric Data Types
Nonstandard |
Declarator |
Short Form |
Meaning |
---|---|---|---|
INTEGER*1 |
INTEGER(KIND=1) |
INTEGER(1) |
One-byte signed integers |
INTEGER*2 |
INTEGER(KIND=2) |
INTEGER(2) |
Two-byte signed integers |
INTEGER*4 |
INTEGER(KIND=4) |
INTEGER(4) |
Four-byte signed integers |
LOGICAL*1 |
LOGICAL(KIND=1) |
LOGICAL(1) |
One-byte logicals |
LOGICAL*2 |
LOGICAL(KIND=2) |
LOGICAL(2) |
Two-byte logicals |
LOGICAL*4 |
LOGICAL(KIND=4) |
LOGICAL(4) |
Four-byte logicals |
REAL*4 |
REAL(KIND=4) |
REAL(4) |
IEEE single-precision four-byte floating-point |
REAL*8 |
REAL(KIND=8) |
REAL(8) |
IEEE double-precision eight-byte floating-point |
REAL*16 |
REAL(KIND=16) |
REAL(16) |
IEEE quad-precision sixteen-byte floating-point |
COMPLEX*8 |
COMPLEX(KIND=4) |
COMPLEX(4) |
Single-precision complex (four bytes each part) |
COMPLEX*16 |
COMPLEX(KIND=8) |
COMPLEX(8) |
Double-precision complex (eight bytes each part) |
COMPLEX*32 |
COMPLEX(KIND=16) |
COMPLEX(16) |
Quad-precision complex (sixteen bytes each part) |
Storage and alignment are always given in bytes. Values that can fit into a single byte are byte-aligned.
The size and alignment of types depends on various compiler options and platforms, and how variables are declared. The default maximum alignment in COMMON blocks is to 4-byte boundaries.
Default data alignment and storage allocation can be changed by compiling with special options, such as -aligncommon, -f, -dalign, -dbl_align_all,- xmemalign,, and- xtypemap. The default descriptions in this manual assume that these options are not in force.
There is additional information in the Fortran Programming Guide regarding special cases of data types and alignment on certain platforms.
The following table summarizes the default size and alignment, ignoring other aspects of types and options.
Table 4–3 Default Data Sizes and Alignments (in Bytes)
Fortran 95 Data Type |
Size |
DefaultAlignment |
Alignment inCOMMON |
---|---|---|---|
BYTE X CHARACTER X CHARACTER*n X |
1 1 n |
1 1 1 |
1 1 1 |
COMPLEX X COMPLEX*8 X DOUBLE COMPLEX X COMPLEX*16 X COMPLEX*32 X |
8 8 16 16 32 |
4 4 8 8 8/16 |
4 4 4 4 4 |
DOUBLE PRECISION X REAL X REAL*4 X REAL*8 X REAL*16 X |
8 4 4 8 16 |
8 4 4 8 8/16 |
4 4 4 4 4 |
INTEGER X INTEGER*2 X INTEGER*4 X INTEGER*8 X |
4 2 4 8 |
4 2 4 8 |
4 2 4 4 |
LOGICAL X LOGICAL*1 X LOGICAL*2 X LOGICAL*4 X LOGICAL*8 X |
4 1 2 4 8 |
4 1 2 4 8 |
4 1 2 4 4 |
Note the following:
REAL*16 and COMPLEX*32: in 64-bit environments (compiling with -m64) the default alignment is on 16-byte (rather than 8-byte) boundaries, as indicated by 8/16 in the table. This data type, “quad precision”, is not available on x86 platforms.
Arrays and structures align according to their elements or fields. An array aligns the same as the array element. A structure aligns the same as the field with the widest alignment.
Options -f or -dalign force alignment of all 8, 16, or 32-byte data onto 8-byte boundaries. Option -dbl_align_all causes all data to be aligned on 8-byte boundaries. Programs that depend on the use of these options may not be portable.
A Cray pointer is a variable whose value is the address of another entity, called the pointee.
f95 supports Cray pointers; Standard Fortran 95 does not.
The Cray POINTER statement has the following format:
POINTER ( pointer_name, pointee_name [array_spec] ), … |
Where pointer_name, pointee_name, and array_spec are as follows:
Pointer to the corresponding pointee_name.
pointer_name contains the address of pointee_name.Must be a scalar variable name (but not a derived type).Cannot be: a constant, a name of a structure, an array, or a function.
Pointee of the corresponding pointer_name
Must be: a variable name, array declarator, or array name
If array_spec is present, it must be explicit shape, (constant or non-constant bounds), or assumed-size.
For example, we can declare Cray pointers to two pointees:
POINTER ( p, b ), ( q, c ) |
The above example declares Cray pointer p and its pointee b, and Cray pointer q and its pointee c.
We can also declare a Cray pointer to an array:
POINTER ( ix, x(n, 0:m) ) |
The above example declares Cray pointer ix and its pointee x; and declares x to be an array of dimensions n by m+1.
You can use pointers to access user-managed storage by dynamically associating variables to particular locations in a block of storage.
Cray pointers allow accessing absolute memory locations.
Cray pointers are declared as follows:
POINTER ( pointer_name, pointee_name [array_spec] )
Fortran 95 pointers are declared as follows:
POINTER object_name
The two kinds of pointers cannot be mixed.
Whenever the pointee is referenced, f95 uses the current value of the pointer as the address of the pointee.
The Cray pointer type statement declares both the pointer and the pointee.
The Cray pointer is of type Cray pointer.
The value of a Cray pointer occupies one storage unit on 32-bit processors, and two storage units on 64-bit processors.
The Cray pointer can appear in a COMMON list or as a dummy argument.
The Cray pointee has no address until the value of the Cray pointer is defined.
If an array is named as a pointee, it is called a pointee array.
Its array declarator can appear in:
A separate type statement
A separate DIMENSION statement
The pointer statement itself
If the array declarator is in a subprogram, the dimensioning can refer to:
Variables in a common block, or
Variables that are dummy arguments
The size of each dimension is evaluated on entrance to the subprogram, not when the pointee is referenced.
pointee_name must not be a variable typed CHARACTER*(*).
If pointee_name is an array declarator, it must be explicit shape, (constant or non-constant bounds), or assumed-size.
An array of Cray pointers is not allowed.
A Cray pointer cannot be:
Pointed to by another Cray pointer or by a Fortran pointer.
A component of a structure.
Declared to be any other data type.
A Cray pointer cannot appear in:
A PARAMETER statement or in a type declaration statement that includes the PARAMETER attribute.
A DATA statement.
A Cray pointee cannot appear in a SAVE, DATA, EQUIVALENCE, COMMON, or PARAMETER statement.
A Cray pointee cannot be a dummy argument.
A Cray pointee cannot be a function value.
A Cray pointee cannot be a structure or a structure component.
A Cray pointee cannot be of a derived type.
Cray pointers can be assigned values as follows:
Set to an absolute address
Example: q = 0
Assigned to or from integer variables, plus or minus expressions
Example: p = q + 100
Cray pointers are not integers. You cannot assign them to a real variable.
The LOC function (nonstandard) can be used to define a Cray pointer.
Example: p = LOC( x )
Example: Use Cray pointers as described above.
SUBROUTINE sub ( n ) COMMON pool(100000) INTEGER blk(128), word64 REAL a(1000), b(n), c(100000-n-1000) POINTER ( pblk, blk ), (ia, a ), ( ib, b ), & ( ic, c ), ( address, word64 ) DATA address / 64 / pblk = 0 ia = LOC( pool ) ib = ia + 4000 ic = ib + n ... |
Remarks about the above example:
word64 refers to the contents of absolute address 64
blk is an array that occupies the first 128 words of memory
a is an array of length 1000 located in blank common
b follows a and is of length n
c follows b
a, b, and c are associated with pool
word64 is the same as blk(17) because Cray pointers are byte address and the integer elements of blk are each 4 bytes long
To aid the migration of programs from f77, f95 accepts VAX Fortran STRUCTURE and UNION statements, a precursor to the “derived types” in Fortran 95. For syntax details see the FORTRAN 77 Language Reference manual.
The field declarations within a STRUCTURE can be one of the following:
A substructure— either another STRUCTURE declaration, or a record that has been previously defined.
A UNION declaration.
A TYPE declaration, which can include initial values.
A derived type having the SEQUENCE attribute. (This is particular to f95 only.)
As with f77, a POINTER statement cannot be used as a field declaration.
f95 also allows:
Either ”.’ or ”%’ can be used as a structure field dereference symbol:struct.field or struct%field.
Structures can appear in a formatted I/O statement.
Structures can be initialized in a PARAMETER statement; the format is the same as a derived type initialization.
Structures can appear as components in a derived type, but the derived type must be declared with the SEQUENCE attribute.
The Fortran 95 compiler accepts a new data type, UNSIGNED, as an extension to the language. Four KIND parameter values are accepted with UNSIGNED: 1, 2, 4, and 8, corresponding to 1-, 2-, 4-, and 8-byte unsigned integers, respectively.
The form of an unsigned integer constant is a digit-string followed by the upper or lower case letter U, optionally followed by an underscore and kind parameter. The following examples show the maximum values for unsigned integer constants:
255u_1 65535u_2 4294967295U_4 18446744073709551615U_8 |
Expressed without a kind parameter (12345U), the default is the same as for default integer. This is U_4 but can be changed by the -xtypemap option, which will change the kind type for default unsigned integers.
Declare an unsigned integer variable or array with the UNSIGNED type specifier:
UNSIGNED U UNSIGNED(KIND=2) :: A UNSIGNED*8 :: B |
Binary operations, such as + - * / cannot mix signed and unsigned operands. That is, U*N is illegal if U is declared UNSIGNED, and N is a signed INTEGER.
Use the UNSIGNED intrinsic function to combine mixed operands in a binary operation, as in U*UNSIGNED(N)
An exception is when one operand is an unsigned integer and the other is a signed integer constant expression with positive or zero value; the result is an unsigned integer.
The kind of the result of such a mixed expression is the largest kind of the operands.
Exponentiation of a signed value is signed while exponentiation of an unsigned value is unsigned.
Unary minus of an unsigned value is unsigned.
Unsigned operands may mix freely with real, complex operands. (Unsigned operands cannot be mixed with interval operands.)
Signed and unsigned integer operands may be compared using intrinsic relational operations. The result is based on the unaltered value of the operands.
The CASE construct accepts unsigned integers as case-expressions.
Unsigned integers are not permitted as DO loop control variables, or in arithmetic IF control expressions.
Unsigned integers can be read and written using the I, B, O, and Z edit descriptors.
They can also be read and written using list-directed and namelist I/O. The written form of an unsigned integer under list-directed or namelist I/O is the same as is used for positive signed integers.
Unsigned integers can also be read or written using unformatted I/O.
Unsigned integers are allowed in intrinsic functions, except for SIGN and ABS.
A new intrinsic function, UNSIGNED, is analogous to INT but produces a result of unsigned type. The form is
UNSIGNED(v [,kind] ).
Another new intrinsic function, SELECTED_UNSIGNED_KIND( var), returns the kind parameter for var.
Intrinsic functions do not allow both signed and unsigned integer operands, except for the MAX and MIN functions, which allow both signed and unsigned integer operands only if there is at least one operand of REAL type.
Unsigned arrays cannot appear as arguments to array intrinsic functions.
A number of new features in the Fortran 2003 standard appear in this release of the f95 compiler. For details, refer to the Fortran 2003 standard.
The new standard for Fortran provides:
a means of referencing C language procedures and, conversely, a means of specifying that a Fortran subprogram can be referenced from a C function, and
a means of declaring global variables that are linked with external C variables
The ISO_C_BINDING module provides access to named constants that are kind type parameters representing data that is compatible with C types.
The draft standard also introduces the BIND(C) attribute. A Fortran derived type is interoperable with C if it has the BIND attribute.
This release of the Fortran 95 compiler implements these features as described in the chapter 15 of the draft standard. Fortran also provides facilities for defining derived types and enumerations that correspond to C types, as described in chapter 4 of the standard.
New intrinsic modules IEEE_ARITHMETIC, and IEEE_FEATURES provide support for exceptions and IEEE arithmetic in the Fortran language. Full support of these features is provided by:
USE, INTRINSIC :: IEEE_ARITHMETIC
USE, INTRINSIC :: IEEE_FEATURES
The INTRINSIC keyword is new in Fortran 2003.These modules define a set of derived types, constants, rounding modes, inquiry functions, elemental functions, kind functions, and elemental and non-elemental subroutines. The details are contained in Chapter 14 of the draft standard for Fortran 2003.
The Fortran 2003 standard introduces three new intrinsics for processing command-line arguments and environment variables. These are:
GET_COMMAND(command, length, status)
Returns in command the entire command line that invoked the program.
GET_COMMAND_ARGUMENT(number, value, length, status)
Returns a command-line argument in value.
GET_ENVIRONMENT_VARIABLE(name, value, length, status, trim_name)
Return the value of an environment variable.
The Fortran 95 compiler now accepts the Fortran 2003 PROTECTED attribute. PROTECTED imposes limitations on the usage of module entities. Objects with the PROTECTED attribute are only definable within the module that declares them.
The compiler recognizes the ASYNCHRONOUS specifier on I/O statements:
ASYNCHRONOUS=[’YES’ | ’NO’]
This syntax is as proposed in the Fortran 2003 standard, Chapter 9. In combination with the WAIT statement it allows the programmer to specify I/O processes that may be overlapped with computation. While the compiler recognizes ASYNCHRONOUS=’YES’, the draft standard does not require actual asynchronous I/O. In this release of the compiler, I/O is always synchronous.
Fortran 2003 extends the data entities allowed for the ALLOCATABLE attribute. Previously this attribute was limited to locally stored array variables. It is now allowed with:
array components of structures
dummy arrays
array function results
Allocatable entities remain forbidden in all places where they may be storage-associated: COMMON blocks and EQUIVALENCE statements. Allocatable array components may appear in SEQUENCE types, but objects of such types are then prohibited from COMMON and EQUIVALENCE.
The f95 compiler accepts the Fortran 2003 VALUE type declaration attribute.
Specifying a subprogram dummy input argument with this attribute indicates that the actual argument is passed “by value”. The following example demonstrates the use of the VALUE attribute with a C main program calling a Fortran 95 subprogram with a literal value as an argument:
C code: #include <stdlib.h> int main(int ac, char *av[]) { to_fortran(2); } Fortran code: subroutine to_fortran(i) integer, value :: i print *, i end |
The Fortran 2003 standard defines a new “stream” I/O scheme. Stream I/O access treats a data file as a continuous sequence of bytes, addressable by a positive integer starting from 1. The data file can be connected for either formatted or unformatted access.
Declare a stream I/O file with the ACCESS=’STREAM’ specifier on the OPEN statement. File positioning to a byte address requires a POS=scalar_integer_expression specifier on a READ or WRITE statement. The INQUIRE statement accepts ACCESS=’STREAM’, a specifier STREAM=scalar_character_variable, and POS=scalar_integer_variable.
Three new Fortran 2003 formatted I/O specifiers have been implemented in f95. They may appear on OPEN, READ, WRITE, PRINT, and INQUIRE statements:
DECIMAL=[’POINT’|’COMMA’]
Change the default decimal editing mode. The default uses a period to separate the whole number and decimal parts of a floating-point number formatted with D, E, EN, ES, F, and G editing. ’COMMA’ changes the default to use comma instead of a period, to print, for example, 123,456. The default is ’POINT’, which uses a period, to print, for example, 123.456.
ROUND=[’PROCESSOR_DEFINED’ | ’COMPATIBLE’]
Set the default rounding mode for formatted I/O D, E, EN, ES, F, and G editing. With ’COMPATIBLE’, the value resulting from data conversion is the one closer to the two nearest represetnations, or the value away from zero if the value is halfway between them. With ’PROCESSOR_DEFINED’, the rounding mode is dependent on the processor’s default mode, and is the compiler default if ROUND is not specified.
As an example, WRITE(*,’(f11.4)’) 0.11115 prints 0.1111 in default mode, and 0.1112 in ’COMPATIBLE’ mode.
IOMSG=character-variable
Returns an error message as a string in the specified character variable. This is the same error message that would appear on standard output. Users should allocate a character buffer large enough to hold the longest message. (CHARACTER*256 should be sufficient.)
When used in INQUIRE statements, these specifiers declare a character variable for returning the current values.
New edit descriptors DP, DC, RP, and RC change the defaults within a single FORMAT statement to decimal point, decimal comma, processor-defined rounding, and compatible rounding respectively. For example:
WRITE(*,’(I5,DC,F10.3)’) N, W
prints a comma instead of a period in the F10.3 output item.
See also the -iorounding compiler command-line option for changing the floating-point rounding modes on formatted I/O. (3.4.49 –iorounding[={compatible|processor-defined}].)
The f95 compiler accepts the Fortran 2003 FLUSH statement. The FLUSH statement makes data written to an external file available to other processes, or causes data placed in an external file by means other than Fortran to be available to a READ statement.
The section describes extensions to Fortran 95 Input/Output handling that are accepted by the f95 compiler that are not part of the Fortran 2003 draft standard. Some are I/O extensions that appeared in the Fortran 77 compiler, f77, and are now part of the Fortran 95 compiler.
Two new functions enable the user to specify their own error handling routine for formatted input on a logical unit. When a formatting error is detected, the runtime I/O library calls the specified user-supplied handler routine with data pointing at the character in the input line causing the error. The handler routine can supply a new character and allow the I/O operation to continue at the point where the error was detected using the new character; or take the default Fortran error handling.
The new routines, SET_IO_ERR_HANDLER(3f) and GET_IO_ERR_HANDLER(3f), are module subroutines and require USE SUN_IO_HANDLERS in the routine that calls them. See the man pages for these routines for details.
Fortran 77 allowed any integer constant in a format to be replaced by an arbitrary expression enclosed in angle brackets:
1 FORMAT( … < expr > … )
Variable format expressions cannot appear as the n in an nH… edit descriptor, in a FORMAT statement that is referenced by an ASSIGN statement, or in a FORMAT statement within a parallel region.
This feature is enabled natively in f95, and does not require the -f77 compatibility option flag.
The group name may be preceded by $ or & on input. The & is the only form accepted by the Fortran 95 standard, and is what is written by NAMELIST output.
Accepts $ as the symbol terminating input except if the last data item in the group is CHARACTER data, in which case the $ is treated as input data.
Allows NAMELIST input to start in the first column of a record.
Opening a file with FORM=’BINARY’ has roughly the same effect as FORM=’UNFORMATTED’, except that no record lengths are embedded in the file. Without this data, there is no way to tell where one record begins, or ends. Thus, it is impossible to BACKSPACE a FORM=’BINARY’ file, because there is no way of telling where to backspace to. A READ on a ’BINARY’ file will read as much data as needed to fill the variables on the input list.
WRITE statement: Data is written to the file in binary, with as many bytes transferred as specified by the output list.
READ statement: Data is read into the variables on the input list, transferring as many bytes as required by the list. Because there are no record marks on the file, there will be no “end-of-record” error detection. The only errors detected are “end-of-file” or abnormal system errors.
INQUIRE statement: INQUIRE on a file opened with FORM=”BINARY” returns:
FORM=”BINARY”ACCESS=”SEQUENTIAL”DIRECT=”NO”FORMATTED=”NO”UNFORMATTED=”YES”RECL= AND NEXTREC= are undefined
BACKSPACE statement: Not allowed—returns an error.
ENDFILE statement: Truncates file at current position, as usual.
REWIND statement: Repositions file to beginning of data, as usual.
Recursive I/O possible on different units (this is because the f95 I/O library is "MT-Warm").
RECL=2147483646 (231-2) is the default record length on sequential formatted, list directed, and namelist output.
ENCODE and DECODE are recognized and implemented as described in the FORTRAN 77 Language Reference Manual.
Non-advancing I/O is enabled with ADVANCE=’NO’, as in:
write(*,’(a)’,ADVANCE=’NO’) ’n= ’ read(*,*) n
A compiler directive directs the compiler to do some special action. Directives are also called pragmas.
A compiler directive is inserted into the source program as one or more lines of text. Each line looks like a comment, but has additional characters that identify it as more than a comment for this compiler. For most other compilers, it is treated as a comment, so there is some code portability.
Sun-style parallelization directives are the default with f95 -explicitpar. To switch to Cray-style directives, use the -mp=cray compiler command-line flag. Explicit parallelization with OpenMP directives requires compiling with -openmp.
A complete summary of Fortran directives appears in Table C–1.
f95 recognizes its own special directives in addition to those described in 1.9 Command-Line Help. These have the following syntax:
!DIR$ d1, d2, … |
Directives are listed in columns 7 and beyond.
Columns beyond 72 are ignored.
An initial directive line has a blank in column 6.
A continuation directive line has a nonblank in column 6.
Put !DIR$ followed by a space anywhere in the line.
The !DIR$ characters are the first nonblank characters in the line (actually, non-whitespace).
Directives are listed after the space.
An initial directive line has a blank, tab, or newline in the position immediately after the !DIR$.
A continuation directive line has a character other than a blank, tab, or newline in the position immediately after the !DIR$.
Thus, !DIR$ in columns 1 through 5 works for both free-form source and fixed-form source.
These directives specify the source form of lines following the directive line.
They apply to the rest of the file in which they appear, or until the next FREE or FIXED directive is encountered.
They allow you to switch source forms within a source file.
They allow you to switch source forms for an INCLUDE file. You insert the directive at the start of the INCLUDE file. After the INCLUDE file has been processed, the source form reverts back to the form being used prior to processing the INCLUDE file.
Each must appear alone on a compiler directive line (not continued).
Each can appear anywhere in your source code. Other directives must appear within the program unit they affect.
Example: A FREE directive.
!DIR$ FREE DO i = 1, n a(i) = b(i) * c(i) END DO |
A parallelization directive is a special comment that directs the compiler to attempt to parallelize the next DO loop. These are summarized in Appendix D and described in the chapter on parallelization in the Fortran Programming Guide. f95 recognizes both Sun and Cray style parallelization directives, as well as the OpenMP Fortran API directives. OpenMP parallelization is described in the OpenMP API User’s Guide.
Compiling a file containing a Fortran 95 MODULE generates a module interface file (.mod file) for every MODULE encountered in the source. The file name is derived from the name of the MODULE; file xyz.mod (all lowercase) will be created for MODULE xyz.
Compilation also generates a .o module implementation object file for the source file containing the MODULE statements. Link with the module implementation object file along with the all other object files to create an executable.
The compiler creates module interface files and implementation object files in the directory specified by the -moddir=dir flag or the MODDIR evironment variable. If not specified, the compiler writes .mod files in the current working directory.
The compiler looks in the current working directory for the interface files when compiling USE modulename statements. The- Mpath option allows you to give the compiler an additional path to search. Module implementation object files must be listed explicitly on the command line for the link step.
Typically, programmers define one MODULE per file and assign the same name to the MODULE and the source file containing it. However, this is not a requirement.
In this example, all the files are compiled at once. The module source files appear first before their use in the main program.
demo% cat mod_one.f90 MODULE one ... END MODULE demo% cat mod_two.f90 MODULE two ... END MODULE demo% cat main.f90 USE one USE two ... END demo% f95 -o main mod_one.f90 mod_two.f90 main.f90 |
Compilation creates the files:
mainmain.oone.modmod_one.otwo.modmod_two.o
The next example compiles each unit separately and links them together.
demo% f95 -c mod_one.f90 mod_two.f90 demo% f95 -c main.f90 demo% f95 -o main main.o mod_one.o mod_two.o |
When compiling main.f90, the compiler searches the current directory for one.mod and two.mod. These must be compiled before compiling any files that reference the modules on USE statements. The link step requires the module implementation object files mod_one.o and mod_two.o appear along with all other object files to create the executable.
With the release of the version 7.0 of the Fortran 95 compiler, .mod files can be stored into an archive (.a) file. An archive must be explicitly specified in a -Mpath flag on the command line for it to be searched for modules. The compiler does not search archive files by default.
Only .mod files with the same names that appear on USE statements will be searched. For example, the Fortran 95 statement USE mymod causes the compiler to search for the module file mymod.mod by default.
While searching, the compiler gives higher priority to the directory where the module files are being written. This can be controlled by the -moddir=dir option flag and the MODDIR environment variable. This implies that if only the -Mpath option is specified, the current directory will be searched for modules first, before the directories and files listed on the -M flag.
The -use=list flag forces one or more implicit USE statements into each subprogram or module subprogram compiled with this flag. By using the flag, it is not necessary to modify source programs when a module or module file is required for some feature of a library or application.
Compiling with -use=module_name has the effect of adding a USE module_name to each subprogram or module being compiled. Compiling with -use=module_file_name has the effect of adding a USE module_name for each of the modules contained in the module_file_name file.
Use the fdumpmod(1) command to display information about the contents of a compiled module information file.
demo% fdumpmod x.mod group.mod x 1.0 v8,i4,r4,d8,n16,a4 x.mod group 1.0 v8,i4,r4,d8,n16,a4 group.mod |
The fdumpmod command will display information about modules in a single .mod file, files formed by concatenating .mod files, and in .a archives of .mod files. The display includes the name of the module, a version number, the target architecture, and flags indicating compilation options with which the module is compatible. See the fdumpmod(1) man page for details.
f95 supports some intrinsic procedures that are extensions beyond the standard.
Table 4–4 Nonstandard Intrinsics
Name |
Definition |
Function Type |
Argument Types |
Arguments |
Notes |
---|---|---|---|---|---|
COT |
Cotangent |
real |
real |
([X=]x) |
P, E |
DDIM |
Positive difference |
double precision |
double precision |
([X=]x,[Y=]y) |
P, E |
LEADZ |
Get the number of leading 0 bits |
integer |
Boolean, integer, real, or pointer |
([I=]i) |
NP, I |
POPCNT |
Get the number of set bits |
integer |
Boolean, integer, real, or pointer |
([I=]i) |
NP, I |
POPPAR |
Calculate bit population parity |
integer |
Boolean, integer, real, or pointer |
([X=]x) |
NP, I |
Notes:P: The name can be passed as an argument. NP: The name cannot be passed as an argument. E: External code for the intrinsic is called at run time. I: f95 generates inline code for the intrinsic procedure.
See the Fortran Library Reference for a more complete discussion of intrinsics, including those from Fortran 77 that are recognized by the Fortran 95 compiler.
Future releases of f95 are intended to be source code compatible with this release.
Module information files generated by this release of f95 are not guaranteed to be compatible with future releases.
Routines written in C can be combined with Fortran programs, since these languages have common calling conventions. See the C/Fortran Interface chapter in the Fortran Programming Guide for details on how to interoperate between C and Fortran routines.