Is there another function in Fortran like `merge` that does not evaluate its arguments?

In early versions of the Cray CFT compiler, one of the requirements for automatic vectorization of a loop was that it fit in a single basic block. IF statements in the interior of a loop mess this up as they split the loop into multiple basic blocks. The CVMGx intrinsics were introduced as a work-around to allowing some conditional code without breaking vectorization.

In the case of CVMGT, all three expressions in the actual arguments were always evaluated. Results of the first two arguments were kept in two vector registers. The logical expression was used to create a vector mask in the mask register. Then there was a conditional assignment instruction which would select an element from one or the other vector register based on the corresponding bit in the VM register, and place the result in a third vector register.

F90 introduced the MERGE intrinsic which should have the same semantics as CVMGT. I like using MERGE to shorten my code. However it doesn’t work when using it for the optional arguments use case. When one of the arguments in the MERGE is the optional argument, it tries to dereference the optional argument, and if not present, abends.

The history of this actually goes way back before Cray. Check out John McCarthy’s paper on the history of LISP. He initially attempted to use Fortran and found that a three argument function he called XIF useful… Page 5 of the following:

http://jmc.stanford.edu/articles/lisp/lisp.pdf

2 Likes