What is the best way to get the distance between 2 points with DirectXMath

Using the new XMVECTOR and XMFLOAT3 classes, what's the best way to get the distance between two points? I could not find a function that does this in the XMVector * family of functions, so I came up with the following:

float distance(const XMFLOAT3& v1,const XMFLOAT3& v2)
{
    XMVECTOR vector1 = XMLoadFloat3(&v1);
    XMVECTOR vector2 = XMLoadFloat3(&v2);
    XMVECTOR vectorSub = XMVectorSubtract(vector1,vector2);
    XMVECTOR length = XMVector3Length(vectorSub);

    float distance = 0.0f;
    XMStoreFloat(&distance,length);
    return distance;
}

Would it be faster than the regular Vector3 class with 3 floats for x, y, z, and then using sqrt, because it uses built-in optimizations? Namely:

float Distance(const Vector3& point1,const Vector3& point2)
{
    float distance = sqrt( (point1.x - point2.x) * (point1.x - point2.x) +
                            (point1.y - point2.y) * (point1.y - point2.y) +
                            (point1.z - point2.z) * (point1.z - point2.z) );
    return distance;
}
+3
source share
2 answers

There is only one way to get the distance between points. And so you described.vec3 diff = b - a; float distance = sqrtf(dot(diff, diff));

Vector3 x, y, z, sqrt, ?

, , , . , " " .

, , . , "" ( ) , , "XMVECTOR". . , 6 , 2 , 1 sqrtf 3 . "XMVECTOR" 3 , 2 , 3 1 sqrtf.

AQTime 7 Standard ( ) gprof ( gcc/g++).

+5

D3DXVec3Length(&(Point1-Point2)) .

+3

All Articles