Raul Laasner
Raul Laasner

Reputation: 1585

Order of logical operations (potential ifort bug)

Before reporting this as a compiler bug at the Intel forums, I'd like to know if the following is standard conforming. My question is: is the order of logical operations always fixed in Fortran?

! main.f90
  interface
     subroutine f(x)
       logical, intent(in), optional :: x
     end subroutine f
  end interface
  call f(.false.)
  call f(.true.)
  call f()
end program

! f.f90
subroutine f(x)
  logical, intent(in), optional :: x
  print*, present(x) .and. x
end subroutine f

gfortran main.f90 f.f90 && ./a.out prints

F
T
F

ifort main.f90 f.f90 && ./a.out prints

 F
 T
forrtl: severe (174): SIGSEGV, segmentation fault occurred
Image              PC                Routine            Line        Source             
a.out              0000000000476D75  Unknown               Unknown  Unknown
a.out              0000000000474997  Unknown               Unknown  Unknown
a.out              0000000000444264  Unknown               Unknown  Unknown
a.out              0000000000444076  Unknown               Unknown  Unknown
a.out              0000000000425176  Unknown               Unknown  Unknown
a.out              00000000004027A0  Unknown               Unknown  Unknown
libpthread.so.0    00007F3560702E80  Unknown               Unknown  Unknown
a.out              0000000000402633  Unknown               Unknown  Unknown
a.out              000000000040260F  Unknown               Unknown  Unknown
a.out              00000000004025AE  Unknown               Unknown  Unknown
libc.so.6          00007F3560371710  Unknown               Unknown  Unknown
a.out              00000000004024A9  Unknown               Unknown  Unknown

I'm using GCC 5.3.0 and Ifort 16.0.2.

Upvotes: 3

Views: 288

Answers (1)

High Performance Mark
High Performance Mark

Reputation: 78316

No, the Fortran standards make no guarantee about the ordering in which logical expressions are evaluated. Like mathematical expressions compilers are free to reorder them into orders which would be equivalent in the perfect world of 'real logic' (or 'real mathematics') but which are not equivalent in the imperfect world of computers.

You might enforce an order of evaluation like this:

if (.not. present(x)) then
    print*, .false. 
else
    print*, x
end if

My copy of the draft standard includes the clause

7.1.5.4.2 Evaluation of logical intrinsic operations

Once the interpretation of a logical intrinsic operation is established, the processor may evaluate any other expression that is logically equivalent, provided that the integrity of parentheses in any expression is not violated.

My interpretation of the standard is that the program does not conform because it makes reference to an optional argument which is not present. Refer to section 12.5.2.12 3 (1) which rules against this. Because the order of evaluation of the operands of the .and. operator is undefined x may be referenced when not present.

Upvotes: 9

Related Questions