Skip to content

Instantly share code, notes, and snippets.

@leppie
Created September 4, 2013 16:13
Show Gist options
  • Save leppie/6439246 to your computer and use it in GitHub Desktop.
Save leppie/6439246 to your computer and use it in GitHub Desktop.
JIT assembly
fxdiv0
int d = 0;
00000000 push ebp
00000001 mov ebp,esp
00000003 push edi
00000004 push esi
00000005 mov esi,ecx
00000007 mov ecx,edx
00000009 xor edx,edx
if (x1 != 0)
0000000b test esi,esi
0000000d je 00000034
{
if (x1 > 0)
0000000f test esi,esi
00000011 jle 0000001C
{
d = x1 / x2;
00000013 mov eax,esi
00000015 cdq
00000016 idiv eax,ecx
00000018 mov edx,eax
0000001a jmp 00000034
}
else if (x2 > 0)
0000001c test ecx,ecx
0000001e jle 0000002B
{
d = (x1 + 1) / x2 - 1;
00000020 lea eax,[esi+1]
00000023 cdq
00000024 idiv eax,ecx
00000026 dec eax
00000027 mov edx,eax
00000029 jmp 00000034
}
else
{
d = (x1 + 1) / x2 + 1;
0000002b lea eax,[esi+1]
0000002e cdq
0000002f idiv eax,ecx
00000031 inc eax
00000032 mov edx,eax
}
}
int m = -(unchecked(d * x2 - x1));
00000034 mov edi,ecx
00000036 imul edi,edx
00000039 sub edi,esi
0000003b neg edi
int halfx2 = x2 / 2;
0000003d mov eax,ecx
0000003f sar eax,1
00000041 jns 00000046
00000043 adc eax,0
// Math.Abs is too slow
int abshalfx2 = halfx2 >= 0 ? halfx2 : -halfx2;
00000046 test eax,eax
00000048 jge 00000050
0000004a neg eax
0000004c mov esi,eax
0000004e jmp 00000052
00000050 mov esi,eax
if (m < (abshalfx2 + (x2 & 1)))
00000052 mov eax,ecx
00000054 and eax,1
00000057 add esi,eax
00000059 cmp esi,edi
0000005b jle 00000063
{
return d;
0000005d mov eax,edx
0000005f pop esi
00000060 pop edi
00000061 pop ebp
00000062 ret
}
if (x2 > 0)
00000063 test ecx,ecx
00000065 jle 0000006E
{
return d + 1;
00000067 inc edx
00000068 mov eax,edx
0000006a pop esi
0000006b pop edi
0000006c pop ebp
0000006d ret
}
return d - 1;
0000006e dec edx
0000006f mov eax,edx
00000071 pop esi
00000072 pop edi
00000073 pop ebp
00000074 ret
fxdiv
if (x1 == 0)
00000000 push ebp
00000001 mov ebp,esp
00000003 xchg ecx,edx
00000005 test edx,edx
00000007 jne 0000000D
{
return 0;
00000009 xor eax,eax
0000000b pop ebp
0000000c ret
}
if (x1 > 0)
0000000d test edx,edx
0000000f jle 00000018
{
return x1 / x2;
00000011 mov eax,edx
00000013 cdq
00000014 idiv eax,ecx
00000016 pop ebp
00000017 ret
}
if (x2 > 0)
00000018 test ecx,ecx
0000001a jle 00000025
{
return (x1 + 1) / x2 - 1;
0000001c inc edx
0000001d mov eax,edx
0000001f cdq
00000020 idiv eax,ecx
00000022 dec eax
00000023 pop ebp
00000024 ret
}
return (x1 + 1) / x2 + 1;
00000025 inc edx
00000026 mov eax,edx
00000028 cdq
00000029 idiv eax,ecx
0000002b inc eax
0000002c pop ebp
0000002d ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment