- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Link Copied
8 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
I think there is no FORTRAN routine to do swap of variables, You must program it yourself.
Jakub
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - jimdempseyatthecove
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
I think there is no FORTRAN routine to do swap of variables, You must program it yourself.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - jparsly@charter.net
read*4 a,b
a = 100000000.
b = 1.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 movs 2 integer operations
[cpp]; a = ieor(a,b) mov rax, qword ptr ; assuming a not in register already mov rbx, qword ptr ; assuming b not in register already xor rbx, rax ; b = ieor(b,a) mov qword ptr , rbx ; a = ieor(a,b) xor rax,rbx mov qword ptr , rax [/cpp]
Too bad there isn't an intrinsic for swap
[cpp]; swap(a,b) mov rax, qword ptr ; assuming a not in register already xchg qword ptr , rax mov qword ptr , rax [/cpp]Jim Dempsey

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page