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

Swapping the value of two variables

haminin
Beginner
3,257 Views

Hi there

Swapping the value of two variables (a and b) without a temporary variable, can be done as: a = a + b; b = a - b; a = a - b.

But could someone let me know if there is a direct command forthis purpose in FORTRAN.

Many thanks

Hamid

0 Kudos
8 Replies
TimP
Honored Contributor III
3,257 Views
Quoting - haminin

Swapping the value of two variables (a and b) without a temporary variable, can be done as: a = a + b; b = a - b; a = a - b.

But could someone let me know if there is a direct command forthis purpose in FORTRAN.

No, any optimizing compiler in any language can do a good job with it.
0 Kudos
ZlamalJakub
New Contributor III
3,257 Views
Why to do swaping in this manner. I suppose compiler creates code which change variables in processor registers (and then puts values to the memory) and does not create intermediate variable in memory, so swap using intermediate variable will be faster than mathematic operatins as You suggested.

I think there is no FORTRAN routine to do swap of variables, You must program it yourself.

Jakub
0 Kudos
jimdempseyatthecove
Honored Contributor III
3,257 Views

try using ieor (xor) intrinsic function

[cpp]a = ieor(a,b)
b = ieor(b,a)
a = ieor(a,b)
[/cpp]

This may eliminate a neg instruction or two
Also experiment with swapping the order of the a,b or b,a in the function arguments.

Look at the dissassembly code.

Jim Dempsey
0 Kudos
jimdempseyatthecove
Honored Contributor III
3,257 Views

try using ieor (xor) intrinsic function

[cpp]a = ieor(a,b)
b = ieor(b,a)
a = ieor(a,b)
[/cpp]

This may eliminate a neg instruction or two
Also experiment with swapping the order of the a,b or b,a in the function arguments.

Look at the dissassembly code.

Jim Dempsey

Also, an inline subroutine that declares a temp may optimize the temp out of the code.
You can declare the subroutine as PURE as long as a and b are declared with INTENT(INOUT).

Jim Dempsey
0 Kudos
jparsly1
New Contributor I
3,257 Views
Go ahead and create a temporary variable. It's a lot clearer what you are doing, and a good compiler is likely
to optimize the temp variable out of existence anyhow and just do the swap in registers.

Also, if 'a' and 'b' are floating point variables, the math method can alter the value of the variables through truncation error.

Consider this carefully constructed case:

read*4 a,b
a = 100000000.
b = 1.
write(*,*) a,b
a = a + b
b = a - b
a = a - b
write(*,*) a,b
stop
end

Depending on your compiler, or just using different options on the same compiler, you can get
different results.The first two compilers I just tried both give a = 0, b = 1.e8 as the final result.


Quoting - haminin

Hi there

Swapping the value of two variables (a and b) without a temporary variable, can be done as: a = a + b; b = a - b; a = a - b.

But could someone let me know if there is a direct command forthis purpose in FORTRAN.

Many thanks

Hamid


0 Kudos
TimP
Honored Contributor III
3,257 Views
Quoting - Quba
Why to do swaping in this manner. I suppose compiler creates code which change variables in processor registers (and then puts values to the memory) and does not create intermediate variable in memory, so swap using intermediate variable will be faster than mathematic operatins as You suggested.

I think there is no FORTRAN routine to do swap of variables, You must program it yourself.

Yes, on the face of it, inserting the arithmetic or xor operations involves the latency of 3 integer operations rather than 2 moves, plus another move which can be done in parallel on any pipeline CPU, saving at best 1 register use. It's been over 20 years since I had access to a machine with only 2 integer registers.
0 Kudos
TimP
Honored Contributor III
3,257 Views

read*4 a,b
a = 100000000.
b = 1.

"carefully" not using integer operations?
As Jim pointed out, integer operations have to be used, not floating point. If you were using x87 registers, the move method undoubtedly would be superior. Even floating point moves might raise a question on some machines about whether subnormals are preserved.
0 Kudos
jimdempseyatthecove
Honored Contributor III
3,257 Views
0 Kudos
Reply