Hello, if I understand correctly RSHIFT and SHIFTA should be functionally identical, with SHIFTA introduced in Fortran 2008; however, when I compiled a simple test function with https://gcc.godbolt.org/ on ifort 19.0.0 with -O3, they generate very different assemblies (gcc produces identical assembly). I'm not well versed in assembly and was wondering why the difference and which version is preferable as far as performance. Code and assembly below:
INTEGER function shift(x, y) USE, INTRINSIC :: ISO_FORTRAN_ENV implicit none INTEGER (INT32), intent(in) :: x, y shift = SHIFTR(x, SHIFTA(y, 5)) return end function shift
mov ecx, DWORD PTR [rsi] sar ecx, 5 mov eax, ecx cdq mov r8d, DWORD PTR [rdi] xor eax, eax shr r8d, cl xor ecx, edx sub ecx, edx cmp ecx, 32 cmovl eax, r8d ret
INTEGER function shift(x, y) USE, INTRINSIC :: ISO_FORTRAN_ENV implicit none INTEGER (INT32), intent(in) :: x, y shift = SHIFTR(x, RSHIFT(y, 5)) return end function shift
mov ecx, DWORD PTR [rsi] shr ecx, 5 mov eax, DWORD PTR [rdi] shr eax, cl add ecx, -32 sar ecx, 31 and eax, ecx ret
mov ecx, DWORD PTR [rsi] mov eax, DWORD PTR [rdi] mov edx, 0 sar ecx, 5 shr eax, cl cmp ecx, 32 cmovge eax, edx ret
Why worry? I don't really see that either way is going to have a measurable performance impact. You could perform several million of either operation in the time it takes to blink on a modern computer.
I used to argue that programmers should get the slowest possible computers, so that they would write more efficient programs, but I've pretty much given up on that. The computers are so much faster these days.
First computer I used regularly was in high school, a 60's vintage IBM 1620, in 1975. I once wrote a numerical integration with 10,000 intervals in Fortran 2, which took 45 minutes to complete. My father ran the same program at ORNL on the mighty IBM 360, and it took 15 seconds.
I've been running the same Fortran numerical reservoir model for several decades now. I can remember that in 1990, a 100 year case study would take about 6 hours. Now I run the same simulation and it takes under a minute.
More memory, faster/parallel processing, its all good.
Basicas, Mark wrote:
if I understand correctly RSHIFT and SHIFTA should be functionally identical
They are not functionally identical. The former (RSHIFT) causes zero bits to be shifted in from the end from where bits are being moved away. The latter (SHIFTA) causes the vacated bit positions to be filled with the leftmost bit. For example, SHIFTA(-1, 2) = -1, whereas RSHIFT(-1, 2) = 1073741823.