Computed gotos on Reddit

Dmitry Olshansky dmitry.olsh at gmail.com
Thu Jul 26 08:55:51 PDT 2012


On 26-Jul-12 10:24, Walter Bright wrote:
> On 7/25/2012 10:19 AM, Walter Bright wrote:
>> Is it possible you could code it up and test it using inline asm?
>
> I dummied up some code to do it:
>
> int test(int i)
> {
>      switch (i)
>      {
>          case 3: i += 3; break;
>          case 4: i += 4; break;
>          case 5: i += 5; break;
>          case 6: i += 6; break;
>          case 7: i += 7; break;
>          case 8: i += 8; break;
>          default: i += 100; break;
>      }
>      return i;
> }
>

Do the above in loop. And more cases of course. Something around 40 
should do. I'm still figuring out why my inline asm version segfaults :)

>                  enter   4,0
>                  push    EBX
>                  mov     -4[EBP],EAX
>                  mov     EBX,EAX
>                  sub     EBX,3
>                  cmp     EBX,5
>                  ja      L5D
>                  lea     ECX,_D3foo4testFiZi[01Bh][EBX*4][EBX]
>                  jmp     ECX
>                  jmp     near ptr L39
>                  jmp     near ptr L3F
>                  jmp     near ptr L45
>                  jmp     near ptr L4B
>                  jmp     near ptr L51
>                  jmp     near ptr L57
> L39:            add     dword ptr -4[EBP],3
>                  jmp short       L61
> L3F:            add     dword ptr -4[EBP],4
>                  jmp short       L61
> L45:            add     dword ptr -4[EBP],5
>                  jmp short       L61
> L4B:            add     dword ptr -4[EBP],6
>                  jmp short       L61
> L51:            add     dword ptr -4[EBP],7
>                  jmp short       L61
> L57:            add     dword ptr -4[EBP],8
>                  jmp short       L61
> L5D:            add     dword ptr -4[EBP],064h
> L61:            mov     EAX,-4[EBP]
>                  pop     EBX
>                  leave
>                  ret
>
> ===============================================
> Sadly, it's significantly slower than:
>
>                  enter   4,0
>                  push    EBX
>                  mov     -4[EBP],EAX
>                  mov     EBX,EAX
>                  sub     EBX,3
>                  cmp     EBX,5
>                  ja      L3D
>                  jmp     dword ptr FLAT:_DATA[00h][EBX*4]
>                  add     dword ptr -4[EBP],3
>                  jmp short       L41
>                  add     dword ptr -4[EBP],4
>                  jmp short       L41
>                  add     dword ptr -4[EBP],5
>                  jmp short       L41
>                  add     dword ptr -4[EBP],6
>                  jmp short       L41
>                  add     dword ptr -4[EBP],7
>                  jmp short       L41
>                  add     dword ptr -4[EBP],8
>                  jmp short       L41
> L3D:            add     dword ptr -4[EBP],064h
> L41:            mov     EAX,-4[EBP]
>                  pop     EBX
>                  leave
>                  ret
>
> Maybe the branch prediction logic doesn't work for JMP ECX.


-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list