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

RSHIFT and SHIFTA produce very different assembly

Basicas__Mark
Beginner
473 Views

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:

SHIFTA:

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

gives:

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        

 

RSHIFT:

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

gives:

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 

GCC gives:

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

 

0 Kudos
3 Replies
andrew_4619
Honored Contributor II
473 Views

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.

0 Kudos
cryptogram
New Contributor I
473 Views

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.

 

 

 

0 Kudos
mecej4
Honored Contributor III
473 Views

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.

0 Kudos
Reply