Fortran User's Guide |
Fortran 95 Features and Differences
This appendix shows some of the major features differences between:
- Standard Fortran 95 and Sun Fortran 95
- FORTRAN 77 and Fortran 95
Features and Extensions
Sun WorkShop 6 Fortran 95 provides the following features.
Continuation Line Limits
f95
andf77
allow 99 continuation lines (1 initial and 98 continuation lines). Standard Fortran 95 allows 19 for fixed-form and 39 for free-form.Fixed-Form Source Lines
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.
Directives
f95
allows directive lines starting withCDIR$
, !DIR$
,CMIC$
,C$PRAGMA
,C$OMP
, or !MIC$
. For a summary of directives, see Appendix E. Standard Fortran 95 does not consider directives.Tab Form
The tab form of
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 (page 55) 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.
In the example above, "
^I
" is a way of indicating 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.Source Form Assumed
The source form assumed by
f95
depends on options, directives, and suffixes.
TABLE C-1 F95 Source Form Command-line options -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 aFREE
orFIXED
directive is used, it overrides the option and file name suffix.Mixing Forms
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 directives.
- In the same program unit, tab form can be mixed with free or fixed form.
Case
Sun Fortran 95 is case insensitive by default. That means that a variable
AbcDeF
is treated as if it were spelledabcdef
. Compile with the-U
option to have the compiler treat upper and lower case as unique.Known Limits
A single Fortran 95 program unit can define up to 65,535 derived types and 16,777,215 distinct constants.
Boolean Type
f95
supports constants and expressions of Boolean type. There are no Boolean variables or arrays, and there is no Boolean type statement.
Miscellaneous Rules Governing Boolean Type
- Masking--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 noBOOLEAN
statement.- A logical variable or constant represents only one value. A Boolean constant can represent as many as 32 values.
- A logical expression yields one value. A Boolean expression can yield as many as 32 values.
- Logical entities are invalid in arithmetic, relational, or bitwise logical expressions. Boolean entities are valid in all three.
Alternate Forms of Boolean Constants
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.Octal
dddddd
B
, where d is any octal digit
- You can use the letter
B
orb
.- 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.Hexadecimal
X'
ddd'
orX"
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.
Hollerith
Accepted forms for Hollerith data are:
n H
...'
...'H
"
..."H
n L
...'
...'L
"
..."L
n R
...'
...'R
"
..."R
Above, "..." is a string of characters and n is the character count.
- A Hollerith constant is type Boolean.
- 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.
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.
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.
Alternate Contexts of Boolean Constants
f95
allows BOZ constants in the places other thanDATA
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.Abbreviated Size Notation for Numeric Data Types
f95
allows the following nonstandard type declaration forms in declaration statements, function statements, andIMPLICIT
statements.
The form in column one is nonstandard Fortran 95, though in common use. The kind numbers in column two can vary by vendor.
Cray Pointers
A Cray pointer is a variable whose value is the address of another entity, which is called the pointee.
f95
supports Cray pointers; Standard Fortran 95 does not.Syntax
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:
Example: Declare Cray pointers to two pointees.
POINTER ( p, b ), ( q, c )The above example declares Cray pointer
p
and its pointeeb
, and Cray pointerq
and its pointeec
.Example: Declare a Cray pointer to an array.
POINTER ( ix, x(n, 0:m) )The above example declares Cray pointer
ix
and its pointeex
; and declaresx
to be an array of dimensionsn
bym-1
.Purpose of Cray Pointers
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 do not provide convenient manipulation of linked lists because (for optimization purposes) it is assumed that no two pointers have the same value.
Cray Pointers and Fortran Pointers
Cray pointers are declared as follows:
POINTER (
pointer_name, pointee_name [array_spec])
Fortran pointers are declared as follows:
The two kinds of pointers cannot be mixed.
Features of Cray Pointers
- 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. Its range of values depends on the size of memory for the machine in use.
- 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:
- If the array declarator is in a subprogram, the dimensioning can refer to:
- The size of each dimension is evaluated on entrance to the subprogram, not when the pointee is referenced.
Restrictions on Cray Pointers
- 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:
Restrictions on Cray Pointees
- A Cray pointee cannot appear in a
SAVE
,DATA
,EQUIVALENCE
,COMMON
, orPARAMETER
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.
Usage of Cray Pointers
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.
Remarks about the above example:
word64
refers to the contents of absolute address 64blk
is an array that occupies the first 128 words of memorya
is an array of length 1000 located in blank commonb
followsa
and is of lengthn
c
followsb
a
,b
, andc
are associated withpool
word64
is the same asblk(17)
because Cray pointers are byte address and the integer elements ofblk
are each 4 bytes longOptimization and Cray Pointers
For purposes of optimization,
f95
assumes the storage of a pointee is never overlaid on the storage of another variable--it assumes that a pointee is not associated with another variable.Such association could occur in either of two ways:
- A Cray pointer has two pointees, or
- Two Cray pointers are given the same value
Note The programmer is responsible for preventing such association.
These kinds of association are sometimes done deliberately, as in array equivalencing, but then results can differ depending on whether optimization is turned on or off.
Example:
b
andc
have the same pointer.
POINTER ( p, b ), ( p, c )REAL x, b, cp = LOC( x )b = 1.0c = 2.0PRINT *, b...Above, because
b
andc
have the same pointer, assigning 2.0 toc
gives the same value tob
. Thereforeb
prints out as 2.0, even though it was assigned 1.0.Intrinsics
f95
supports some intrinsic procedures which are extensions beyond the standard.
I/O Extensions
Some I/O extensions that appear in Sun Fortran 77 have been added to the Fortran 95 compiler:
- NAMELIST Input Format:
- 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 isCHARACTER
data, in which case the$
is treated as input data.- Allows NAMELIST input to start in the first column of a record.
OPEN(...,FORM='BINARY')
treats the file as binary data without record marks:
- Opening a file with
FORM='BINARY'
has roughly the same effect asFORM='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 toBACKSPACE
aFORM='BINARY'
file, because there is no way of telling where to backspace to. AREAD
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 withFORM="BINARY"
returns:
FORM="BINARY"
are undefined
ACCESS="SEQUENTIAL"
DIRECT="NO"
FORMATTED="NO"
UNFORMATTED="YES"
RECL= AND NEXTREC=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
andDECODE
are recognized and implemented as described in the FORTRAN 77 Language Reference Manual.- Naming of scratch files is the same as with
f77
.- Non-advancing I/O is enabled with
ADVANCE='NO'
, as in:
write(*,'(a)',ADVANCE='NO') 'n= '
read(*,*) nDirectives
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 directives are the default with
f95
(andf77
). To switch to Cray-style directives, use the-mp=cray
compiler command-line flag.A complete summary of Fortran directives appears in Appendix E.
Form of Special
f95
Directive Linesf95 recognizes its own special directives in addition to the general
f95/f77
directives described in Chapter 2. These have the following syntax:
!DIR$ d1, d2, ...Fixed-Form Source
- Put
CDIR$
or!DIR$
in columns 1 through 5.- 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.
Free-Form Source
- 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.
FIXED
andFREE
Directives
These directives specify the source form of lines following the directive line.
Scope
They apply to the rest of the file in which they appear, or until the next
FREE
orFIXED
directive is encountered.Uses
- 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 theINCLUDE
file. After theINCLUDE
file has been processed, the source form reverts back to the form being used prior to processing theINCLUDE
file.Restrictions
- 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$ FREEDO i = 1, na(i) = b(i) * c(i)END DOParallelization Directives
A parallelization directive is a special comment that directs the compiler to attempt to parallelize the next DO loop. These are summarized in Appendix E and described in the Fortran Programming Guide.
f95
recognizes bothf77
Sun and Cray style parallelization directives, as well as the OpenMP Fortran API directives.
Note Fortran parallelization features require a Sun WorkShop HPC license.
Compatibility with FORTRAN 77
Source
Standard-conforming, fixed-format (filename
.f
) FORTRAN 77 source code is compatible with Sun Fortran 95. Use of non-standard extensions, such as VMS Fortran features, are not compatible and may not compile with Sun Fortran 95.Limits
The
f77
compiler allows up to 20 array subscripts whilef90
only allows 7.Linking with
f77
-Compiled Routines
- To mix
f77
andf95
object binaries, link withf95
and thef77
compatibility library,libf77compat
, and not withlibF77
. For example, perform the link step with f95 even even if the main program is anf77
program:
f95
..files..-lf77compat
- Example:
f95
main andf77
subroutine.
- The FORTRAN 77 library is generally compatible with
f95
.Example:
f95
main calls a routine from the FORTRAN 77 library.
I/O
f77
andf95
are generally I/O compatible for binary I/O, sincef95
links to thef77
compatibility library.
Such compatibility includes the following two situations:
- In the same program, you can write some records in
f95
, then read them inf77
.- An
f95
program can write a file. Then anf77
program can read it.The numbers read back in may or may not equal the numbers written out.
- Unformatted
- The numbers read back in do equal the numbers written out.
- Floating-point formatted
- The numbers read back in can be different from the numbers written out. This is caused by slightly different base conversion routines, or by different conventions for uppercase/lowercase, spaces, plus or minus signs, and so forth.
- Examples:
1.0e12
,1.0E12
,1.0E+12
- List-directed
- The numbers read back in can be different from the numbers written out. This can be caused by various layout conventions with commas, spaces, zeros, repeat factors, and so forth.
- Example: '0.0' as compared to '.0'
- Example: ' 7' as compared to '7'
- Example: '3, 4, 5' as compared to '3 4 5'
- Example: '3*0' as compared to '0 0 0'
- The above results are from:
integer::v(3)=(/0,0,0/);
*,v
- Example: '0.333333343' as compared to '0.333333'
- The above results are from
*,
1.0/3.0
Intrinsics
The Fortran 95 standard supports the following new intrinsic functions that FORTRAN 77 does not have.
If you use one of these names in your program, you must add an
EXTERNAL
statement to makef95
use your function rather than the intrinsic one.
- ADJUSTL,ADJUSTR,ALL,ALLOCATED,ANY,BIT_SIZE,COUNT,CSHIFT, DIGITS,DOT_PRODUCT,EOSHIFT,EPSILON,EXPONENT,HUGE,KIND, LBOUND,LEN_TRIM,MATMUL,MAXEXPONENT,MAXLOC,MAXVAL,MERGE, MINEXPONENT,MINLOC,MINVAL,NEAREST,PACK,PRECISION,PRESENT, PRODUCT,RADIX,RANGE,REPEAT,RESHAPE,RRSPACING,SCALE,SCAN, SELECTED_INT_KIND,SELECTED_REAL_KIND,SET_EXPONENT,SHAPE, SIZE,SPACING,SPREAD,SUM,TINY,TRANSFER,TRANSPOSE,UBOUND, UNPACK,VERIFY
Forward Compatibility
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.Mixing Languages
On Solaris systems, routines written in C can be combined with Fortran programs, since these languages have common calling conventions.
Module Files
Compiling a file containing a Fortran 95
MODULE
generates a module file (.mod
file) for everyMODULE
encountered in the source. The file name is derived from the name of theMODULE
; filexyz.mod
(all lowercase) will be created forMODULE
xyz
.By default, such files are usually sought in the current working directory. The
-M
dir option allows you to tellf95
to seek them in an additional location.The
.mod
files cannot be stored into an archive file, or concatenated into a single file.
Sun Microsystems, Inc. Copyright information. All rights reserved. Feedback |
Library | Contents | Previous | Next | Index |