• Create Account

Vec4D: SSE-ASM, SSE-INTRINSICS, NORMAL

11 replies to this topic

#1Tapped  Members

412
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  Members

7797
Like
1Likes
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  Members

1570
Like
1Likes
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

412
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

49417
Like
2Likes
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

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

#7MikeWillHugYou  Members

239
Like
1Likes
Like

Posted 29 May 2016 - 11:33 PM

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.

#8darthdeus  Members

164
Like
0Likes
Like

Posted 10 July 2016 - 06:41 PM

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?

#9frob  Moderators

41320
Like
0Likes
Like

Posted 11 July 2016 - 05:52 PM

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.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I occasionally write about assorted stuff.

#10Juliean  GDNet+

6212
Like
1Likes
Like

Posted 11 July 2016 - 06:33 PM

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.

#11Alessio1989  Members

4382
Like
0Likes
Like

Posted 14 July 2016 - 01:36 PM

Just to remeber: by default Visual Studio 2012 and successive versions enable SSE2 optimizations under x86-ia32 too. Not sure about previous versions..

Edited by Alessio1989, 14 July 2016 - 01:37 PM.

"Recursion is the first step towards madness." - "Skeggǫld, Skálmǫld, Skildir ro Klofnir!"
Direct3D 12 quick reference: https://github.com/alessiot89/D3D12QuickRef/

#12Geri  Members

355
Like
0Likes
Like

Posted 12 August 2016 - 06:59 AM

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.