• Create Account

Banner advertising on our site currently available from just \$5!

# Vec4D: SSE-ASM, SSE-INTRINSICS, NORMAL

5 replies to this topic

### #1Tapped  Members   -  Reputation: 384

Like
0Likes
Like

Posted 21 April 2013 - 07:35 AM

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.

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.
MOVAPS XMM1, XMM2			                //Make a copy
SHUFPS XMM1, XMM1, _MM_SHUFFLE(0, 1, 0, 1)   //Second addition of elements using shuffle
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

### #2Zaoshi Kaba  Crossbones+   -  Reputation: 5635

Like
0Likes
Like

Posted 21 April 2013 - 08:24 AM

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

If you're fine with using SSE4 there's instruction to calculate dot product right away: http://msdn.microsoft.com/en-us/library/bb514054(v=vs.100).aspx. This way you won't need shuffles and such.

### #3BGB  Crossbones+   -  Reputation: 1554

Like
0Likes
Like

Posted 21 April 2013 - 02:10 PM

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

### #4Tapped  Members   -  Reputation: 384

Like
0Likes
Like

Posted 21 April 2013 - 02:39 PM

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.

### #5Hodgman  Moderators   -  Reputation: 40013

Like
1Likes
Like

Posted 21 April 2013 - 06:22 PM

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

### #6Aressera  Members   -  Reputation: 1763

Like
0Likes
Like

Posted 21 April 2013 - 10:09 PM

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, 21 April 2013 - 10:13 PM.

PARTNERS