• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
tapped

Vec4D: SSE-ASM, SSE-INTRINSICS, NORMAL

11 posts in this topic

I have played around with SSE. I am using VS2012, and I wanted to know what was the fastest way to calculate the length of a vector.
The code looks very bad.
 

float Magnitude() const
{
#if SSE && SSE_ASM
	float result;
	//Optimized magnitude calculation with SSE and Assembly
	__asm
	{
	MOV EAX, this								//Move [this] to EAX.
	MOVAPS XMM2, [EAX]							//Copy data EAX to XMM2 register
	MULPS  XMM2, XMM2                            //Square the XMM2 register.
	MOVAPS XMM1, XMM2                            //Make a copy
	SHUFPS XMM2, XMM1, _MM_SHUFFLE(1, 0, 3, 2)   //Shuffle so that we can add together the elements.
	ADDPS  XMM2, XMM1			                //Add the elements.
	MOVAPS XMM1, XMM2			                //Make a copy
	SHUFPS XMM1, XMM1, _MM_SHUFFLE(0, 1, 0, 1)   //Second addition of elements using shuffle
	ADDPS  XMM2, XMM1
	SQRTPS XMM2, XMM2			                //Get the square root
	MOVSS [result], XMM2                         //Store the result in the float.
	}
	return result;
#elif SSE
	__m128 tmp = _mm_mul_ps(components, components); 
	tmp = _mm_add_ps(_mm_shuffle_ps(tmp, tmp, _MM_SHUFFLE(1, 0, 3, 2)), tmp);
	tmp = _mm_sqrt_ps(_mm_add_ps(tmp, _mm_shuffle_ps(tmp, tmp, _MM_SHUFFLE(0, 1, 0, 1))));
		
	float result;
	_mm_store_ss(&result, tmp);
		
	return result;
#endif

#if !SSE && !SSE_ASM
        return sqrtf(__x * __x + __y * __y + __z * __z + __w * __w);
#endif
}

 


Guess what, the normal method was as fast as the SSE-Intrinsics, while my assembly code was acctually slower.
So yeah, you can't beat the compiler :)
 

0

Share this post


Link to post
Share on other sites

maybe or maybe not relevant in this case, but AFAIK, inline ASM forces the compiler to spill any registers, causing extra register loads/stores around the ASM.

this can in some cases result in inline ASM being slower than its plain C equivalent.

 

more-so, things like SSE intrinsics are thin wrappers over the raw machine instructions anyways (when used, the compiler spits out the instructions in question), so using inlime ASM in this case wont really gain much. (likewise, types like "__m128" are also handled by specially by the compiler. ...).

 

 

otherwise:

if SSE is enabled, the compiler may sometimes try to "vectorize" code (potentially, *1), or at least it will use SSE operations where appropriate (previously observed);

sqrtps is wasteful in this case, vs sqrtss (it depends on the CPU, but not all of them can do 4 sqrt operations in parallel, and in this case, only the scalar square-root is needed);

...

 

*1: basically, depending on compiler settings, the compiler may try to recognize cases where scalar code can instead be done using SIMD operations, and use these instead. (though, often, it may still make sense to use intrinsics manually, as while "clever", the compiler isn't necessarily actually "smart").

 

so, it is possible that in the case of the scalar default case, the compiler was still, in effect, using SIMD operations to calculate it (since SSE was already turned on presumably to be able to use the intrinsics).

 

 

or, at least, these are a few guesses...

1

Share this post


Link to post
Share on other sites
Yeah you are right about that the compiler use SSE, as long as the compiler /arch parameter is set to SSE.
SSE is default on in VS2012, and used in almost every float operation. I have looked at the dissambly when doing a single float comparision(like if(fTest < fRandom).
The compiler would use SSE in that situation. So my conclusion is that in new compilers SSE intrinstics are not needed. And the reason for slow SSE assembly, is basically the compilers fault, since the compiler don't optimize the registers that are used before or after the inline assembly part.
0

Share this post


Link to post
Share on other sites
If the Vec4 isn't in the cache before this function, then it doesnt matter what code is in there, as the memory accesses will be the bottleneck ;)

As above, writing ASM is really bad for the optimizer these days, because it doesn't understand ASM so becomes very defensive. Intrinsics are much preferred.

For really optimal SSE code, you'd have the function return a vec4, with the result in the 'x' component, to avoid the m128<->float conversions everywhere and allow all your different SSE'd math functions to be inlined together well. I've seen some engines use a special type for this case, like float_in_vec, etc...
2

Share this post


Link to post
Share on other sites

If you're alright with restricting yourself to SSE3+, you can use the _mm_hadd_ps intrinsic to save several steps in your code.

 

I find that SIMD is not very useful for your standard coordinate vectors (i.e. position), but are far more useful when you are dealing with many independent parallel operations (which vector magnitude is not, hence the horizontal add) and can manage to store your data aligned and in a SIMD-friendly format. For instance, you could write a much more efficient function to compute the magnitude of 4 different vectors at once, returning a 128 bit value containing the result of all 4, no shuffling required.

 

For this reason, my math library contains a specialized SIMDScalar<Type,N> template class that represents an N-wide SIMD value of the given type (so that you can support doubles, ints, or whatever else). I then have a SIMDVector3<Type,N> class which implements an N-wide standard 3D coordinate vector using a SIMDScalar for each of the x, y, and z components. You can then use a SIMDVector3 to perform parallel operations on many 3D vectors at once, stored in structure-of-arrays format. The advantage of doing it this way is that you avoid all of the packing/unpacking that would be necessary (and slow) if you were using an __m128 to make a 4-component coordinate vector class as you are trying to do.

Edited by Aressera
0

Share this post


Link to post
Share on other sites

You shouldn't use SSE to calculate the length of a vector, you should use it to calculate the length of 4 vectors at a time.

1

Share this post


Link to post
Share on other sites

You lose some performance by moving value from SSE register to normal register.

 

In what way? Obviously there'd be the extra instruction to move the value out of the register, but what other performance do you lose? It probably will create a dependency, but OTOH if you used the original value there'd be a dependency anyway. Maybe I'm missing something?

0

Share this post


Link to post
Share on other sites

You shouldn't use SSE to calculate the length of a vector, you should use it to calculate the length of 4 vectors at a time.

 

That is generally the correct approach for SIMD instructions.

 

The best performance improvements do not come from doing a single computation in one part, but doing a series of many operations as a block.  Better for the cache, better for the predictive engines, better for the OOO core, less overhead from function calls.

 

Here the better approach would be a function that takes four __m128 values, squares and sums them, then returns the square root of all four in an __m128. All of it done with intrinsics, no asm statements.   Maybe also have another function signature that takes four arrays.

 

If you're only computing a single vector's magnitude the SIMD approach is probably not worth the effort. The optimizer will probably recognize it and do something smart, it isn't worth attempting to hand-optimize.

0

Share this post


Link to post
Share on other sites

In what way? Obviously there'd be the extra instruction to move the value out of the register, but what other performance do you lose? It probably will create a dependency, but OTOH if you used the original value there'd be a dependency anyway. Maybe I'm missing something?

 

You will trigger a very, very costly Load-Hit-Store, which is especially expensive on consoles, but is also far from trivial on desktop CPUs. The same happens for conversion from int to float too, and to put it short in case of a TL;DR; from the article: You mess up your CPUs pipelining by doing so, which is way worse than an additional move-instruction.

1

Share this post


Link to post
Share on other sites

i dont suggest you to use SSE manually. i experimented it with a bit, i was able to do faster code after a bit of trying than the original on my computer. HOWEVER, after i tryed it on a different cpu, the sse code was far slower on different cpu. so its not really worth - you should do algorithmical optimizations instead. 

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0