fpt and WinFPT Reference Manual - Command-line Commands

| SimCon Home | Ref Manual Home |

ENFORCE ORDER OF EVALUATION IN LOGICAL EXPRESSIONS

Syntax:

[DO NOT] ENFORCE ORDER [OF] EVALUATION IN LOGICAL EXPRESSIONS
[DO NOT] ENFORCE ORDER [OF] EVALUATION OF LOGICAL EXPRESSIONS

Function:

If a logical expression contains two or more components separated by .AND. or .OR. operators, the Fortran standard explicitly states that the order in which the components are evaluated is undefined. Diferent compilers may evaluate the components in different orders or even in parallel.

This causes two problems:

  1. If the performance of the code under two different compilers is compared, for example by the fpt run-time trace mechanism, the runs under the two compilers may take different paths, creating spurious differences.
  2. If code is migrated from VMS, where the components of logical expressions are always evaluated from left to right, The run-time behaviour in the migrated code may change.

This command edits the code so that the order of evaluation is always the same, and is always from left to right as under VMS.

Where to Use this Command

Operating system command line Yes
Configuration file, config.fsp Yes
Specification (fsp) files, *.fsp Yes
Interactively, to FPT> prompt Yes
Interactive command files Yes
Embedded in the Fortran code Yes

Default

Logical expressions are not changed by default. (Note that defaults may be changed in the configuration file).

Example

The code, in a VMS program:

INTEGER a(10) REAL loga(10) : : IF ((i .GT. 0) .AND. (a(i) .GE. 0)) THEN loga(i) = log(REAL(a(i))) ENDIF

could crash, with an array reference out-of-bounds, if the components of the logical expression were evaluated from right to left. This command modifies the code:

LOGICAL*4 fpt_temp_l(1:1) INTEGER a(10) REAL loga(10) : : ! FPT IF ((i .GT. 0) .AND. (a(i) .GE. 0)) THEN !--------^--------------------------------------------------------------------- !!! FPT - 3837 .AND. or .OR. re-programmed to enforce order of evaluation !------------------------------------------------------------------------------ fpt_temp_l(1)=(i .GT. 0) IF (fpt_temp_l(1)) THEN fpt_temp_l(1)=(a(i) .GE. 0) ENDIF IF (fpt_temp_l(1)) THEN loga(i)=LOG(REAL(a(i))) ENDIF

In the code:

! Functions for file handling LOGICAL*4 f_directory_exists,f_can_open_file EXTERNAL f_directory_exists,f_can_open_file : : IF ((f_directory_exists(direct)) .AND. (f_can_open_file(fname))) THEN OPEN(iwlun,STATUS='UNKNOWN',FILE=fname) ENDIF

The check must be made that the requested directory exists before attempting to open the file. The code is rewritten:

LOGICAL*4 fpt_temp_l(1:1) ! Functions for file handling LOGICAL*4 f_directory_exists,f_can_open_file EXTERNAL f_directory_exists,f_can_open_file : : ! FPT IF ((f_directory_exists(direct)) .AND. (f_can_open_file(fname))) THEN !--------^--------------------------------------------------------------------- !!! FPT - 3837 .AND. or .OR. re-programmed to enforce order of evaluation !------------------------------------------------------------------------------ fpt_temp_l(1)=(f_directory_exists(direct)) IF (fpt_temp_l(1)) THEN fpt_temp_l(1)=(f_can_open_file(fname)) ENDIF IF (fpt_temp_l(1)) THEN OPEN(iwlun,STATUS='UNKNOWN',FILE=fname) ENDIF

Copyright ©1995 to 2024 Software Validation Ltd. All rights reserved.