Skip to main content
gamedev.net
🔒 Locked

Optimizing with 3Dnow!

Started by blueshogun96 1 reply 600+ views
Original Post
blueshogun96
blueshogun96
Hey yall, I was wondering how would I go about optimizing my apps with things like MMX, 3Dnow!, and SSE? And another thing, how do I use ASM files in my visual studio projects?
DadleFish
DadleFish
Well, first of all you should find out whether or not the system you are running on supports these features. You can find it out by trying to run SSE/SSE2/MMX/3DNOW! operations and just catching exceptions, for example-

bool mmxSupported = true;try {    __asm     {        pxor mm0, mm0        emms    }catch(...){    mmxSupported = false;}


The same goes to all the rest, try running these ops for the other capabilities:

SSE:
xorps xmm0, xmm0
SSE2:
xorpd xmm0, xmm0
3DNOW:
pfrcp mm0, mm0
emms

As you can see, you can run your assembly instructions from within your C++ code just wrapping them with __asm { } blocks. It is also convenient since you can actually use your local variables. You can also use external assembly files and direct your visual studio to assemble them - but I'm lacking the information about this right now so I'll leave you to find it if you really need it.

As for actual programming, I know for one that Stefan Zerbst has some self-proclaimed optimized code in his book, 3D Game Engine Programming, so you might want to check it out. Remember that if you don't really know what you're doing, odds are the VS compiler would create a better code than yours - so always profile your functions to see if you've really gained any performance improvement.
Dubito, Cogito ergo sum.
Jan Wassenberg
Jan Wassenberg
Quote:
You can find it out by trying to run SSE/SSE2/MMX/3DNOW! operations and just catching exceptions

A safer+mayhaps nicer way is to check the CPUID feature bits: (license = ZLib)
// CPU caps (128 bits)// do not change the order!enum CpuCap{	// standard (ecx) - currently only defined by Intel	SSE3 = 0+0,		// Streaming SIMD Extensions 3	EST  = 0+7,		// Enhanced Speedstep Technology	// standard (edx)	TSC  = 32+4,	// TimeStamp Counter	CMOV = 32+15,	// Conditional MOVe	MMX  = 32+23,	// MultiMedia eXtensions	SSE  = 32+25,	// Streaming SIMD Extensions	SSE2 = 32+26,	// Streaming SIMD Extensions 2	HT   = 32+28,	// HyperThreading	// extended (ecx)	// extended (edx) - currently only defined by AMD	AMD_MP        = 96+19,	// MultiProcessing capable; reserved on AMD64	AMD_3DNOW_PRO = 96+30,	AMD_3DNOW     = 96+31};bool ia32_cap(CpuCap cap){	// treated as 128 bit field; order: std ecx, std edx, ext ecx, ext edx	// keep in sync with enum CpuCap!	static u32 caps[4];	ONCE(		u32 regs[4];		if(ia32_cpuid(1, regs))		{			caps[0] = regs[ECX];			caps[1] = regs[EDX];		}		if(ia32_cpuid(0x80000001, regs))		{			caps[2] = regs[ECX];			caps[3] = regs[EDX];		}	);	const uint tbl_idx = cap >> 5;	const uint bit_idx = cap & 0x1f;	if(tbl_idx > 3)	{		debug_warn("cap invalid");		return false;	}	return (caps[tbl_idx] & BIT(bit_idx)) != 0;}	// usage example (at init-time):	u32 (*ConvertRGBColorTo4ub)(const RGBColor& src);	if(ia32_cap(SSE))		ConvertRGBColorTo4ub = sse_ConvertRGBColorTo4ub;	else		debug_printf("No SSE available. Slow fallback routines will be used.\n");	// after that, just call through the function pointer.



Quote:
how do I use ASM files in my visual studio projects?

Just add the asm file to your project and set up a custom build step:
Command Line: nasm -f win32 -o "$(IntDir)\$(InputName).asm.obj" "$(InputPath)"
Outputs: $(IntDir)\$(InputName).asm.obj
(we use premake to generate the workspace and have patched it to take care of this as well. note: .asm.obj is because we have asm and C++ files of the same name, and they must not compile/assemble to the same object file)
You will need NASM; adjust the path to it as appropriate.

HTH+HAND.


// edit: note to mods/poor maintainers of the ASP beast: posting and editing fails (no data returned to browser) when the "Command Line.. " and "Outputs" text lines are included in 'code' or 'pre' tags.
E8 17 00 42 CE DC D2 DC E4 EA C4 40 CA DA C2 D8 CC 40 CA D0 E8 40E0 CA CA 96 5B B0 16 50 D7 D4 02 B2 02 86 E2 CD 21 58 48 79 F2 C3

Topic Locked

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

Sign in to reply to this topic.