Is this C++ program correctly implemented in SSE? I have to implement a simple program in SSE, and I don't know if I have done it in the right way (it's my first program in SSE).

This is the C++ program:

double distance (int x1, int x2, int y1, int y2)
{
    double x = abs(x2 - x1);
    double y = abs(y2 - y1);

    double dist = sqrt(x*x + y*y);

    return dist;
}


And this is the program in SSE instructions:

__m128d vx1 _mm_load_pd(x1); // load x1 and x2
__m128d vx2 = _mm_load_pd(x2);
__m128d vdiff = _mm_sub_pd(vx2, vx1); // x2 - x1
__m128d vnegdiff = mm_sub_pd(_mm_set1_pd(0.0), vdiff); // 0.0 - diff
__m128d vabsX = _mm_max_pd(vdiff, vnegdiff);        //  abs = max(diff, - diff)

__m128d vy1 = _mm_load_pd(y1); 
__m128d vy2 = _mm_load_pd(y2);
__m128d vdiff2 = _mm_sub_pd(vy2, vy1); 
__m128d vnegdiff2 = mm_sub_pd(_mm_set1_pd(0.0), vdiff2); 
__m128d vabsY = _mm_max_pd(vdiff2, vnegdiff2);

__m128d absX = _mm_mul_pd(vabsX,vabsX); //x*x
__m128d absY = _mm_mul_pd(vabsY,vabsY); //y*y

__m128d suma = _mm_add_pd(absX, absY); // x^2 + y^2

__m128d square = _mm_sqrt_pd(suma); // square root x^2 + y^2

return square;
Your SSE code looks like a reasonably accurate translation of your original C, but that C code doesn't immediately jump out as the best way to do things. In particular, I don't see where you gain anything by taking the absolute value before you do your multiplication. Given that your're squaring the value immediately afterwards anyway, it would appear simpler to just leave it negative if that's what it happens to be. Lacking a specific reason to do otherwise, I'd start with something simpler, like this:

double x = x1 - x2;
double y = y1 - y2;

double dist = sqrt(x * x + y * y);


Then I'd convert that to SSE instructions.

Especially if you're concerned with speed, you might want to consider using an alternative to the Pythagorean theorem to compute the hypotenuse though. I posted some C for one possibility in a previous answer.