This is the mail archive of the fortran@gcc.gnu.org mailing list for the GNU Fortran project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Best way to simulate C-assert using gfortran


Am 04.02.2013 09:50, schrieb Arjen Markus:
     pure subroutine assertion(cond)
         implicit none
         logical, intent(in) :: cond
         real, volatile :: r
         if ( noassert ) return   !<===
         r = 1.0
         if (.not. cond) r = r / 0.0
     end subroutine assertion

The parameter noassert should - in principle - enable the compiler to delete
the actual call to the subroutine. (I have been unable to verify this from the
call tree, as the option -fdump-fortran-optimized did not produce any output.)

Try compiling with "-O1". "if (.true.)" should already at -O0 generate unconditional code (although, the - unreachable - code after the "return" is generated).


Instead of "r = r/0.0" consider "call abort()". "abort()" is a (common) vendor extension. The call to abort has two advantages: First, it really force-stops the code while "1.0 / 0.0" requires trapping.

Secondly, "abort" is marked as noreturn. If you have code like:
if (cond) then
call abort ()
else ! or "end if"
...
the compiler assumes that "cond" evaluates as false. This branch prediction can speed up the code. The downside is that if the compiler predicted it wrongly, some cycles are wasted to recover from the wrong branch. Well, as "abort()" exits the code, the wasted cycles shouldn't matter. (Hence, this kind of optimization is made if one branch contains a noreturn procedure call.)


The downside of "abort" is that it is not part of the Fortran standard, i.e. not all compilers support it.

Tobias


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]