Sign in to follow this  
tapped

Vec4D: SSE-ASM, SSE-INTRINSICS, NORMAL

Recommended Posts

tapped    412

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 :)
 

Share this post


Link to post
Share on other sites
cr88192    1570

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

Share this post


Link to post
Share on other sites
tapped    412
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.

Share this post


Link to post
Share on other sites
Hodgman    51234
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...

Share this post


Link to post
Share on other sites
Aressera    2919

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

Share this post


Link to post
Share on other sites
darthdeus    165

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?

Share this post


Link to post
Share on other sites
frob    44908

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.

Share this post


Link to post
Share on other sites
Juliean    7068

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.

Share this post


Link to post
Share on other sites
Geri    367

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. 

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