Fortran 2008 Overview

Fortran 2008 Overview

Fortran 2008 Overview

Table of Contents

1 Introduction

This document describes those parts of the Fortran 2008 language which are not in Fortran 2003, and which are supported by the latest release of the NAG Fortran Compiler.

Features marked as ‘[5.3.1]’ are newly available in release 5.3.1, and those marked as ‘[5.3]’ were available in release 5.3.

2 Overview of Fortran 2008

The new features of Fortran 2008 that are supported by the NAG Fortran Compiler can be grouped as follows:

  • the BLOCK construct,
  • additional intrinsic functions for bit manipulation,
  • additional mathematical intrinsic functions,
  • other new intrinsic procedures,
  • changes to existing intrinsic procedures,
  • ISO_FORTRAN_ENV additions,
  • miscellaneous and convenience features.

3 The BLOCK construct [5.3]

This construct allows declarations of entities within executable code. For example,

  Do i=1,n
    Block
      Real tmp
      tmp = a(i)**3
      If (tmp>b(i)) b(i) = tmp
    End Block
  End Do
Here the variable tmp has its scope limited to the BLOCK construct, so will not affect anything outside it. This is particularly useful when including code by INCLUDE or by macro preprocessing.

All declarations are allowed within a BLOCK construct except for COMMON, EQUIVALENCE, IMPLICIT, INTENT, NAMELIST, OPTIONAL and VALUE; also, statement function definitions are not permitted.

BLOCK constructs may be nested; like other constructs, branches into a BLOCK construct from outside are not permitted. A branch out of a BLOCK construct “completes” execution of the construct.

Entities within a BLOCK construct that do not have the SAVE attribute (including implicitly via initialisation), will cease to exist when execution of the construct is completed. For example, an allocated ALLOCATABLE variable will be automatically deallocated, and a variable with a FINAL procedure will be finalised.

4 Additional mathematical intrinsic functions [5.3.1]

  • The elemental intrinsic functions ACOSH, ASINH and ATANH compute the inverse hyperbolic cosine, sine or tangent respectively. There is a single argument X, which may be of type Real or Complex; the result of the function has the same type and kind. When the argument is Complex, the imaginary part is expressed in radians and lies in the range 0≤im≤π for the ACOSH function, and −π/2≤im≤π/2 for the ASINH and ATANH functions.

    For example, ACOSH(1.543081), ASINH(1.175201) and ATANH(0.7615942) are all approximately equal to 1.0.

  • The elemental intrinsic function HYPOT computes the “Euclidean distance function” (square root of the sum of squares) of its arguments X and Y without overflow or underflow for very large or small X or Y (unless the result itself overflows or underflows). The arguments must be of type Real with the same kind, and the result is of type Real with that kind. Note that HYPOT(X,Y) is semantically and numerically equal to ABS(CMPLX(X,Y,KIND(X))).

    For example, HYPOT(3e30,4e30) is approximately equal to 5e30.

  • The array reduction intrinsic function NORM2(X,DIM) reduces Real arrays using the L2-norm operation. This operates exactly the same as SUM and PRODUCT, except for the operation involved. The L2 norm of an array is the square root of the sum of the squares of the elements. Note that unlike most of the other reduction functions, NORM2 does not have a MASK argument. The DIM argument is optional; an actual argument for DIM is not itself permitted to be an optional dummy argument.

    The calculation of the result value is done in such a way as to avoid intermediate overflow and underflow, except when the result itself is outside the maximum range. For example, NORM2([X,Y]) is approximately the same as HYPOT(X,Y).

5 Additional intrinsic functions for bit manipulation [mostly 5.3]

  • The elemental intrinsic functions BGE, BGT, BLE and BLT perform bitwise (i.e. unsigned) comparisons. They each have two arguments, I and J, which must be of type Integer but may be of different kind. The result is default Logical.

    For example, BGE(INT(Z'FF',INT8),128) is true, while INT(Z'FF',INT8)>=128 is false.

  • [5.3.1] The elemental intrinsic functions DSHIFTL and DSHIFTR perform double-width shifting. They each have three arguments, I, J and SHIFT which must be of type Integer, except that one of I or J may be a BOZ literal constant – it will be converted to the type and kind of the other I or J argument. I and J must have the same kind if they are both of type Integer. The result is of type Integer, with the same kind as I and J. The I and J arguments are effectively concatenated to form a single double-width value, which is shifted left or right by SHIFT positions; for DSHIFTL the result is the top half of the combined shift, and for DSHIFTR the result is the bottom half of the combined shift.

    For example, DSHIFTL(INT(B'11000101',1),B'11001001',2) has the value INT(B'00010111',1) (decimal value 23), whereas DSHIFTR(INT(B'11000101',1),B'11001001',2) has the value INT(B'01110010',1) (decimal value 114).

  • The array reduction intrinsic functions IALL, IANY and IPARITY reduce arrays using bitwise operations. These are exactly the same as SUM and PRODUCT, except that instead of reducing the array by the + or * operation, they reduce it by the IAND, IOR and IEOR intrinsic functions respectively. That it, each element of the result is the bitwise-and, bitwise-or, or bitwise-exclusive-or of the reduced elements. If the number of reduced elements is zero, the result is zero for IANY and IPARITY, and NOT(zero) for IALL.
  • The elemental intrinsic functions LEADZ and TRAILZ return the number of leading (most significant) and trailing (least significant) zero bits in the argument I, which must be of type Integer (of any kind). The result is default Integer.
  • The elemental intrinsic functions MASKL and MASKR generate simple left-justified and right-justified bitmasks. The value of MASKL(I,KIND) is an integer with the specified kind that has its leftmost I bits set to one and the rest set to zero; I must be non-negative and less than or equal to the bitsize of the result. If KIND is omitted, the result is default integer. The value of MASKR is similar, but has its rightmost I bits set to one instead.
  • [5.3.1] The elemental intrinsic function MERGE_BITS(I,J,MASK) merges the bits from Integer values I and J, taking the bit from I when the corresponding bit in MASK is 1, and taking the bit from J when it is zero. All arguments must be BOZ literal constants or of type Integer, and all the Integer arguments must have the same kind; at least one of I and J must be of type Integer, and the result has the same type and kind.

    Note that MERGE_BITS(I,J,MASK) is identical to IOR(IAND(I,MASK),IAND(J,NOT(MASK))).

    For example, MERGE_BITS(INT(B'00110011',1),B'11110000',B'10101010') is equal to INT(B'01110010') (decimal value 114).

  • The array reduction intrinsic function PARITY reduces Logical arrays. It is exactly the same as ALL and ANY, except that instead of reducing the array by the .AND. or .OR. operation, it reduces it by the .NEQV. operation. That is, each element of the result is .TRUE. if an odd number of reduced elements is .TRUE..
  • The elemental intrinsic function POPCNT(I) returns the number of bits in the Integer argument I that are set to 1. The elemental intrinsic function POPPAR(I) returns zero if the number of bits in I that are set to 1 are even, and one if it is odd. The result is default Integer.

6 Other new intrinsic procedures [5.3.1]

  • The intrinsic subroutine EXECUTE_COMMAND_LINE passes a command line to the operating system's command processor for execution. It has five arguments, in order these are:
    CHARACTER(*),INTENT(IN) :: COMMAND — the command to be executed;
    LOGICAL,INTENT(IN),OPTIONAL :: WAIT — whether to wait for command completion (default true);
    INTEGER,INTENT(INOUT),OPTIONAL :: EXITSTAT — the result value of the command;
    INTEGER,INTENT(OUT),OPTIONAL :: CMDSTAT — see below;
    CHARACTER(*),INTENT(INOUT),OPTIONAL :: CMDMSG — the error message if CMDSTAT is non-zero.

    CMDSTAT values are zero for success, −1 if command line execution is not supported, −2 if WAIT is present and false but asynchronous execution is not supported, and a positive value to indicate some other error. If CMDSTAT is not present but would have been set non-zero, the program will be terminated. Note that Release 5.3.1 supports command line execution on all systems, and does not support asynchronous execution on any system.

    For example, CALL EXECUTE_COMMAND_LINE('echo Hello') will probably display ‘Hello’ in the console window.

  • The intrinsic function STORAGE_SIZE(A,KIND) returns the size in bits of a scalar object with the same dynamic type and type parameters as A, when it is stored as an array element (i.e. including any padding). The KIND argument is optional; the result is type Integer with kind KIND if it is present, and default kind otherwise.

    If A is allocatable or a pointer, it does not have to be allocated unless it has a deferred type parameter (e.g. CHARACTER(:)) or is CLASS(*). If it is a polymorphic pointer, it must not have an undefined status.

    For example, STORAGE_SIZE(13_1) is equal to 8 (bits).

7 Changes to existing intrinsic procedures [5.3.1]

  • The intrinsic functions ACOS, ASIN, ATAN, COSH, SINH, TAN and TANH now accept arguments of type Complex. Note that the hyperbolic and non-hyperbolic versions of these functions and the new ACOSH, ASINH and ATANH functions are all related by simple algebraic identities, for example the new COSH(X) is identical to the old COS((0,1)*X) and the new SINH(X) is identical to the old (0,-1)*SIN((0,1)*X).
  • The intrinsic function ATAN now has an extra form ATAN(Y,X), with exactly the same semantics as ATAN2(Y,X).
  • The intrinsic function SELECTED_REAL_KIND now has a third argument RADIX; this specifies the desired radix of the Real kind requested. Note that the function IEEE_SELECTED_REAL_KIND in the intrinsic module IEEE_ARITHMETIC also has this new third argument, and will allow requesting IEEE decimal floating-point kinds if they become available in the future.

8 ISO_FORTRAN_ENV additions [5.3]

The intrinsic module ISO_FORTRAN_ENV contains additional named constants as follows.

  • The additional scalar integer constants INT8, INT16, INT32, INT64, REAL32, REAL64 and REAL128 supply the kind type parameter values for integer and real kinds with the indicated bit sizes.
  • The additional named array constants CHARACTER_KINDS, INTEGER_KINDS, LOGICAL_KINDS and REAL_KINDS list the available kind type parameter values for each type (in no particular order).

9 Miscellaneous and convenience features [mostly 5.3]

  • In a structure constructor, the value for an allocatable component may be omitted: this has the same effect as specifying NULL().
  • [5.3.1] Assignment to a polymorphic allocatable variable is permitted. If the variable has different dynamic type or type parameters, or if an array, a different shape, it is first deallocated. If it is unallocated (or is deallocated by step 1), it is then allocated to have the correct type and shape. It is then assigned the value of the expression. Note that the operaton of this feature is similar to the way that ALLOCATE(variable,SOURCE=expr) works.
  • In a STOP statement, the stop-code may be any scalar constant expression of type integer or default character. (In the NAG Fortran Compiler this also applies to the PAUSE statement, but that statement is no longer standard Fortran.)
  • ENTRY statements are regarded as obsolescent.
  • An empty internal subprogram part, module subprogram part or type-bound procedure part is now permitted following a CONTAINS statement. In the case of the type-bound procedure part, an ineffectual PRIVATE statement may appear following the unnecessary CONTAINS statement.
  • [5.3.1] The FUNCTION and SUBROUTINE keywords on the END statement for an internal or module subprogram is now optional (when the subprogram name does not appear). Previously these keywords were only optional for external subprograms.
  • A type-bound procedure declaration statement may now declare multiple type-bound procedures. For example, instead of
          PROCEDURE,NOPASS :: a
          PROCEDURE,NOPASS :: b=>x
          PROCEDURE,NOPASS :: c
    
    the single statement
          PROCEDURE,NOPASS :: a, b=>x, c
    
    will suffice.
  • The NEWUNIT= specifier has been added to the OPEN statement; this allocates a new unit number that cannot clash with any other logical unit (the value will be a special negative value). For example,
          INTEGER unit
          OPEN(FILE='output.log',FORM='FORMATTED',NEWUNIT=unit)
          WRITE(unit,*) 'Logfile opened.'
    
    The NEWUNIT= specifier can only be used if either the FILE= specifier is also used, or if the STATUS= specifier is used with the value 'SCRATCH'.
  • Fortran 2008 extends the rules that are used for generic resolution and for checking that procedures in a generic are unambiguous. The new rules are that
    • a dummy procedure is distinguishable from a dummy variable;
    • an ALLOCATABLE dummy variable is distinguishable from a POINTER dummy variable that does not have INTENT(IN).

10 References

The Fortran 2008 standard, IS 1539-1:2010(E), is available from ISO as well as from many national standards bodies. A number of books describing the new standard are available; the recommended reference book is “Modern Fortran Explained” by Metcalf, Reid & Cohen, Oxford University Press, 2011 (ISBN 978-0-19-960141-7).