[C++] SSE Intrinsics Help

Recommended Posts

I have a very math laden function that works fine in it's non-sse version but causes some very odd behavior when I try to switch to using SSE Intrinsics. Here is the non-sse function that works. It's purpose is to simulate a gravitational pull on a large array of objects.
void CalculateMath ()
{
xPos, yPos, xVel, yVel;
const float AccelFactor = -dT*GravityDirection*GravityMultiplier*100000.0f;

for (int i=0; i<Population; i++)
{
xPos = xpos[i]; //Copy array values into normal variables; mainly for readability.
yPos = ypos[i];
xVel = xvel[i];
yVel = yvel[i];

RadX = xPos-CenterX;// X & Y components of the distance to the center.

xPos += dT*xVel;
yPos += dT*yVel;

xpos[i] = xPos; //Put values back into array
ypos[i] = yPos;
xvel[i] = xVel;
yvel[i] = yVel;
}
}


Here is the "equvalent" sse code that I've come up with so far and doesn't work. I can't find the problem with it.
void CalculateMathSSE()
{
__m128 xPos, yPos, xVel, yVel,
AccelFactor = _mm_set_ps1( -dT*GravityDirection*GravityMultiplier*100000.0f ),

for (int i=0; i<Population; i+=4)
{
xPos = _mm_load_ps( &xpos[i] ); //Load 4 floats at a time from the array into a _m128 value

RadX = _mm_sub_ps( xPos, sseCenterX ); //Distance
RadY = _mm_sub_ps( yPos, sseCenterY );

xPos = _mm_add_ps( xPos, _mm_mul_ps( sse_dT, xVel ));
yPos = _mm_add_ps( yPos, _mm_mul_ps( sse_dT, yVel ));

_mm_store_ps( &xpos[i], xPos ); //Put values back into arrays
_mm_store_ps( &ypos[i], yPos );
_mm_store_ps( &xvel[i], xVel );
_mm_store_ps( &yvel[i], yVel );

}
}


What am I doing wrong? I removed some code that would simulate friction because it worked in both versions, so I think the problem lies within the "fancy math" part. All the ugly stuff like alignment is taken care of, and it runs without crashing, but it just doesn't behave like the original. [Edited by - KaerfSusej on September 19, 2008 12:16:03 PM]

Share on other sites
And this might be better off in the math and physics forum. I don't know.

Share on other sites
Whats actually wrong with it? The maths? Does it crash?

Share on other sites
hows the memory alignment for the arrays xpos, ypos ,xvel & yvel?

have you got your compiler configured for 16 byte alignment?

Share on other sites
All the ugly stuff like alignment is taken care of, and it runs without crashing, but it just doesn't behave like the original.

Share on other sites
How different is the output? "_mm_sqrt_ps" is quite inaccurate afaik.

Share on other sites
Quote:
 Original post by BlindSideHow different is the output? "_mm_sqrt_ps" is quite inaccurate afaik.

using 32 bit instead of 80 bit is inaccurate fullstop...

Share on other sites
This is just an idea, but I think you should rewrite it into simple assembly SSE code. Without this Intel __mm_addps.... stuff.
And this works correctly if you have n*4 elements in your population.

Share on other sites
What compiler are you using? I've heard of a few cases of Visual C++ producing incorrect intrinsic code. I might be worth taking a look at the disassembly.

Create an account

Register a new account

• Partner Spotlight

• Forum Statistics

• Total Topics
627657
• Total Posts
2978472

• 10
• 12
• 22
• 13
• 33