Site
News
Files Visual Basic
Strings
Math
General
Properties
Memory
Methods Search
Testing Inline ASM-VB
Strings
Math
General
Memory Search
Using inline ASM
Submit!
News
Files Visual Basic
Strings
Math
General
Properties
Memory
Methods Search
Testing Inline ASM-VB
Strings
Math
General
Memory Search
Using inline ASM
Submit!
asmDotProduct vs. vbDotProductThe DotProduct is often used in games where vector math is involved :).
Unfortunatly, my version cannot beat the VB one, but I'll put it online anyway for example purposes.
Simply call it like the VB version, and use an empty single var at the last argument, which will contain the result :)
The VB version:
My ASM version:
VB's own asm generated:
User contributed notes:
Unfortunatly, my version cannot beat the VB one, but I'll put it online anyway for example purposes.
Simply call it like the VB version, and use an empty single var at the last argument, which will contain the result :)
The VB version:
Public Function vbDotProduct(ByRef x1 As Single, ByRef y1 As Single, ByRef x2 As Single, ByRef y2 As Single) As Single vbDotProduct = (x1 * x2) + (y1 * y2) End Function |
My ASM version:
Public Function asmDotProduct(ByRef x1 As Single, ByRef y1 As Single, ByRef x2 As Single, ByRef y2 As Single, ByRef result As Single) As Single '//VB's own ASM version (faster), should be like this, but it doesn't work, unfortunatly? : '#ASM_START ' ; BIG LESSON: Using the POPPING correctly is extremely important, to restore the ST stack ' ; so use ' push ebp ' mov ebp, esp ' mov eax, DWORD PTR [ebp+8] ;//Vector1.x ' fld dword ptr [eax] ;Load into fpu stack ' mov eax, DWORD PTR [ebp+16] ;'//Vector2.x ' fld dword ptr [eax] ;'Load into fpu stack ' ; Multiply both (Vector1.x * Vector2.x) ' fmul st(1), st ; Multiply st1 with st0.. ' mov eax, DWORD PTR [ebp+12] ;//Vector1.y ' fld dword ptr [eax] ;Load into fpu stack ' mov eax, DWORD PTR [ebp+20] ;'//Vector2.y ' fld dword ptr [eax] ;'Load into fpu stack ' ; Multiply both (Vector1.y * Vector2.y) ' fmulp st(1), st ; Multiply st1 with st0.. pop "remove" st0.. st1 -> st0 ' ; Now add both multiplies together: ' ; The first vector1 multiply has been shifted to item 3 by now on the stack ' faddp st(2), st ;Add(st2) and st0, store in st2, pop st(0) so st(1) -st ' ; return! ' mov eax, DWORD PTR [ebp+24] ;//Vector1.x and function return ' fstp dword ptr [eax] ' mov esp, ebp ;MOV/POP is much faster ' pop ebp ;on 486 and Pentium than Leave ' ret 20 '#ASM_END End Function |
VB's own asm generated:
'Public Function asmDotProduct(ByRef x1 As Single, ByRef y1 As Single, ByRef x2 As Single, ByRef y2 As Single, ByRef result As Single) As Single ''//VB's own ASM version (faster), should be like this, but it doesn't work, unfortunatly? : ' ' ' '#ASM_START '' ; BIG LESSON: Using the POPPING correctly is extremely important, to restore the ST stack '' ; so use '' push ebp '' mov ebp, esp ' '' mov eax, DWORD PTR [ebp+12] '' mov ecx, DWORD PTR [ebp+20] '' mov edx, DWORD PTR [ebp+8] '' fld DWORD PTR [eax] '' fmul DWORD PTR [ecx] '' mov eax, DWORD PTR [ebp+16] '' fld DWORD PTR [edx] '' fmul DWORD PTR [eax] '' faddp ST(1), ST(0) '' mov eax, [ebp+24] '' fstp DWORD PTR [eax] ' '' mov esp, ebp ;MOV/POP is much faster '' pop ebp ;on 486 and Pentium than Leave '' ret 20 ''#ASM_END ' End Function |
VBDotProduct % faster than ASMDotProduct | VBDotProduct (sec) | ASMDotProduct (sec) |
-22.7% | 0.00821 | 0.010624 |
-3.3% | 0.010571 | 0.010935 |
-1.2% | 0.007962 | 0.008062 |
-17.4% | 0.007987 | 0.009669 |
10.5% | 0.00874 | 0.007908 |
VBDotProduct % faster than ASMDotProduct | VBDotProduct (sec) | ASMDotProduct (sec) |
0.1% | 0.019466 | 0.019445 |
-43.6% | 0.015944 | 0.028293 |
-30.2% | 0.015797 | 0.022633 |
-2.7% | 0.022307 | 0.022926 |
-18.3% | 0.015962 | 0.019541 |
User contributed notes:
Author: Tom (hurendo_kun at hotmail dot com) | Date: 16:06 08/06/2005 |
I bet you could make a normalized version that out-performs VB. |