Skip to main content
gamedev.net
🔒 Locked

inline asm prob

Started by m4gnus 8 replies 1.1k views
Original Post
m4gnus
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). 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:

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;
}

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
"There are 10 types of people in the world... those who understand binary and those who don't."
roel
roel
If the components of your vector are floats, then it doesn't work indeed, since you read them as ints and multiply them as ints.
m4gnus
m4gnus
yep my components are floats...so how can i work with floats?

regards,
m4gnus
"There are 10 types of people in the world... those who understand binary and those who don't."
dave
dave
Inside your function im guessing you could create separate variables for each of the components of the vectors, then you won't be shifting floats into registers. It might help but it is just a guess.

ace
m4gnus
m4gnus
i already tested that...that didn't work..
is it possible to work with floats in these registers at all?

regards,
m4gnus
"There are 10 types of people in the world... those who understand binary and those who don't."
dave
dave
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:

		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
Enigma
Enigma
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

m4gnus
m4gnus
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]
"There are 10 types of people in the world... those who understand binary and those who don't."
arm
arm
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.
zoggo
zoggo
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 ;) )

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.