float dotASM(const ngVector3& a,const ngVector3& b)
{
float res;
_asm
{
mov eax,[a.x]
mov ebx,[a.y]
mov ecx,[a.z]
imul eax,[b.x]
imul ebx,[b.y]
imul ecx,[b.z]
add eax,ebx
add eax,ecx
mov res,eax
}
return res;
}
🔒 Locked
inline asm prob
Advertisement
Original Post
Hi Recently i did some tests with inline assembler and discovered that inline assembler can really be much faster then C++(especially if calculations are repeated very often). So i wanted to test how much faster a dotproduct of 2 vectors in asm is. It compiles fine but i can't get the .x,.y, and .z values of the input vectors properly...i'm quite new to inline asm but i don't know hat's wrong with this: i only pass normalized vectors that follow the world axis(like (0.0f, 1.0f, 0.0f) )but sometimes some CPU registers get values like 345656436. The result of the dotproduct of 2 vectors a(0.0f, 0.0f, 1.0f) and b(0.0f, 0.0f, -1.0f) is -4.5811971e+011... why doesn't this work? regards, m4gnus
It took me a while to get this and understand what is going on but here is the code for your ASM dot product function:
Let me know if it doesn't work!
ace
float dotASM3(ngVector3 a,ngVector3 b) { float resX, resY, resZ; _asm { fld dword ptr [a.x] fmul dword ptr [b.x] fstp dword ptr [resX] fld dword ptr [a.y] fmul dword ptr [b.y] fstp dword ptr [resY] fld dword ptr [a.z] fmul dword ptr [b.z] fstp dword ptr [resZ] fld dword ptr [resX] fadd dword ptr [resY] fstp dword ptr [resX] fld dword ptr [resX] fadd dword ptr [resZ] fstp dword ptr [resX] } return resX; }Let me know if it doesn't work!
ace
Quote:
Original post by m4gnus
Hi
Recently i did some tests with inline assembler and discovered that inline assembler can really be much faster then C++(especially if calculations are repeated very often).
What sort of tests? Unless you're using fairly advanced assembly (pre-fetching, SSE etc.) you won't beat a modern compiler. Certainly not using the kind of assembly you posted since you'll likely actually reduce the number of optimisations the compiler can perform.
Enigma
i tested a simple calculations liek a*b+j and repeated it 10000times with C++ and asm... the oftner i repeated the loops the faster got asm(in percentage) with 1000000 iterations the asm code needed about 0.002% of the time the C++ code needed...but probably i did something wrong...i compiled it with Release and used the performance counter...
@ace_lovegrove:
I will test this evening..
regards,
m4gnus
[Edited by - m4gnus on September 18, 2005 11:33:25 AM]
@ace_lovegrove:
I will test this evening..
regards,
m4gnus
[Edited by - m4gnus on September 18, 2005 11:33:25 AM]
Hmm,
If a simple calculation like x*y+z comes up 500 times faster then inevitably something went wrong with your tests. The compiler should beat assembler hands-down on something like this because of all the work it would have to do working around your asm code. It should be no slower at the very least.
arm.
If a simple calculation like x*y+z comes up 500 times faster then inevitably something went wrong with your tests. The compiler should beat assembler hands-down on something like this because of all the work it would have to do working around your asm code. It should be no slower at the very least.
arm.
Two thoughts:
1. I hope the compiler would create better machine code than the code ace_lovegrove presented. You could do the whole calculation on the floating point stack and avoid any writes to memory at all. (floats usually return on the top of the FPU stack so no write there either)
2. Optimization works because you know something the compiler didn't. This is the case with all optimizations, not just rolling your own asm. In this case you don't know anymore about the algorithm than the compiler does because it only multiplies and adds 6 floats. Not only that, you have probably scuppered any chances of the function being inlined or replaced by SIMD code. (Really good vectorizing compilers only ;) )
1. I hope the compiler would create better machine code than the code ace_lovegrove presented. You could do the whole calculation on the floating point stack and avoid any writes to memory at all. (floats usually return on the top of the FPU stack so no write there either)
2. Optimization works because you know something the compiler didn't. This is the case with all optimizations, not just rolling your own asm. In this case you don't know anymore about the algorithm than the compiler does because it only multiplies and adds 6 floats. Not only that, you have probably scuppered any chances of the function being inlined or replaced by SIMD code. (Really good vectorizing compilers only ;) )
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement