FriendUP HQ

Please visit the main source of project information on the FriendUP website:

Optimization vs readability

Welcome to the Friend Developers Forum! Forums Bug Forum Optimization vs readability

This topic contains 1 reply, has 2 voices, and was last updated by  Hogne Titlestad 9 months, 2 weeks ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #948

    Grim
    Participant

    Again, not exactly a bug, but probably related to avoiding bugs in the first place. Optimization and readability are both two important factors that define good code quality. Sometimes it’s sensible to write more optimized code while sacrificing some of the readability. But sacrificing readability for no gain just reduces the code quality. One such example is using left shift to express multiplication. Then the thing you want to express is “double the size”, the most clear way to put it is size*2. But you are aware that (most) processors will be able to do this calculation quicker by using left shifts. So you optimize it as size<<1, or SHIFT_LEFT(size,1) in the latest version of FriendUP. Not exactly obscure code, still clear enough, but at least less clear and definitely more code. This is absolutely pointless since every compiler will know how to optimize this code and might choose addition instead of left shift, or even special opcodes. It’s typically the case that the compiler is very good at performing low level optimizations like this, and that it’s better to write clear and readable code (and put the optimization efforts to a higher level by choosing better algorithms and such). To prove the point I compiled some examples for different processors (I even tries different compilers for the same processor but didn’t find any that generated different results). This includes 64-bit (AMD64 and Arm64), 32-bit (Arm32 and IA32), 16-bin (M68K) and 8-bit (AVR) processors. From the resulting assembly code we can draw the following conclusions:

    1. 1. The generated code for multiplication and left shifts are always identical, without execption.
    2. 2. Left shifts are not always used by the compiler, there are sometimes even better options (see AMD64).

    C code

    int a(int x){
        return x<<1;
    }
    int b(int x){
        return x*2;
    }
    int c(int x){
        return x<<2;
    }
    int d(int x){
        return x*4;
    }
    int e(int x){
        return x<<9;
    }
    int f(int x){
        return x*512;
    }
    

    IA32

    a:mov eax, dword ptr [esp + 4]
      add eax, eax
      ret
    b:mov eax, dword ptr [esp + 4]
      add eax, eax
      ret
    c:mov eax, dword ptr [esp + 4]
      shl eax, 2
      ret
    d:mov eax, dword ptr [esp + 4]
      shl eax, 2
      ret
    e:mov eax, dword ptr [esp + 4]
      shl eax, 9
      ret
    f:mov eax, dword ptr [esp + 4]
      shl eax, 9
      ret
    

    AMD64

    a:lea eax, [rdi + rdi]
      ret
    b:lea eax, [rdi + rdi]
      ret
    c:lea eax, [4*rdi]
      ret
    d:lea eax, [4*rdi]
      ret
    e:shl edi, 9
      mov eax, edi
      ret
    f:shl edi, 9
      mov eax, edi
      ret
    

    Arm32

    a:lsl r0, r0, #1
      bx lr
    b:lsl r0, r0, #1
      bx lr
    c:lsl r0, r0, #2
      bx lr
    d:lsl r0, r0, #2
      bx lr
    e:lsl r0, r0, #9
      bx lr
    f:lsl r0, r0, #9
      bx lr
    

    Arm64

    a:lsl w0, w0, 1
      ret
    b:lsl w0, w0, 1
      ret
    c:lsl w0, w0, 2
      ret
    d:lsl w0, w0, 2
      ret
    e:lsl w0, w0, 9
      ret
    f:lsl w0, w0, 9
      ret
    

    M68K

    a:linkw fp,#0
      movew (fp,8),%d0
      lslw #1,d0
      unlk fp
      rts
    b:linkw fp,#0
      movew (fp,8),d0
      ls.w #1,d0
      unlk fp
      rts
    c:linkw fp,#0
      movew (fp,8),d0
      lslw #2,d0
      unlk fp
      rts
    d:linkw fp,#0
      movew (fp,8),d0
      lslw #2,d0
      unlk fp
      rts
    e:link.w fp,#0
      movew (fp,8),d0
      moveq #9,d1
      ls.w d1,d0
      unlk fp
      rts
    f:link.w fp,#0
      movew (fp,8),d0
      moveq #9,d1
      lsl.w d1,d0
      unlk fp
      rts
    

    AVR

    a:lsl r24
      rol r25
      ret
    b:lsl r24
      rol r25
      ret
    c:lsl r24
      rol r25
      lsl r24
      rol r25
      ret
    d:lsl r24
      rol r25
      lsl r24
      rol r25
      ret
    e:mov r25,r24
      lsl r25
      ldi r24,lo8(0)
      ret
    f:mov r25,r24
      lsl r25
      ldi r24,lo8(0)
      ret
    
    • This topic was modified 9 months, 3 weeks ago by  Grim. Reason: Added pre around code
    #954

    Hogne Titlestad
    Keymaster

    Trying to contact you by e-mail. 🙂 Could we schedule an IRC / Telegram / Friend Chat session?

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

Comments are closed.