Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29245 Discussions

Difference between "EQUIVALENCE" and "TRANSFER"

david_______
Beginner
1,056 Views
Hi!

I posted a question a few days ago concerning bit inquiry functions to replicate ones I found in an old Univac Fortran code I am trying to rewrite in FORTRAN90. I received a lot of great information from members here. Thank you very much.

Once again, in a nutshell, my goal is to load a floating point variable number (real) into a 32-bit word (that is the easy part!) and then I need to pull out various bits for some further calculations. Unlike the old UNIVAC code, FORTRAN90 bit inquiry instrinsics force me to use INTEGER variables, not REAL variable inputs. In order to work with INTEGER values, one of the responses suggested that I use an EQUIVALENCE statement and another responder suggested I use the TRANSFER statement. In looking at the documentation, both statements seem to do the same thing? Is one more preferable than the other, ie, faster?

Thank you again for all of your help.

Sincerely,

David

0 Kudos
2 Replies
TimP
Honored Contributor III
1,056 Views
It's certainly likely that equivalence and transfer may work alike in your usage:

integer i
real r,x
equivalence(r,i)

Then
r = x
and
i = transfer(x,i)

accomplish the same thing. Even though I'm not accustomed to transfer(), I think transfer() is much clearer to humans, as it keeps the functionality in one place, and avoids introduction of an otherwise meaningless new variable name.
0 Kudos
Steven_L_Intel1
Employee
1,056 Views
The difference is that the result of TRANSFER is an expression which needs to be used or assigned. EQUIVALENCE sets up "storage association" between two variables and this has wide effects on the generated code (in preventing optimizations.)

As Tim suggests, use of TRANSFER makes it much more obvious what is going on. Code that assigns to r and then reads from i doesn't suggest that they are the same storage - you'd have to go back and look for an EQUIVALENCE to see if they might be.

Another advantage of TRANSFER is that you can assign a run-time shape to the result which you can't with EQUIVALENCE.
0 Kudos
Reply