c++ – I don’t know how to use the VectorMatrixMultiply SIMD version of Unreal Engine 5

I am a student studying C++ and Unreal Engine in Korea.

I would like to try using the SIMD version of VectorMatrixMultiply in Unreal Engine 5, but the results are not correct. I am seeking help with the correct usage

The implementation of VectorMatrixMultiply in Unreal Engine that I am using is as follows.

void VectorMatrixMultiply(FMatrix44f* Result, const FMatrix44f* Matrix1, const FMatrix44f* Matrix2)
{
    const VectorRegister4Float* A = (const VectorRegister4Float*)Matrix1;
    const VectorRegister4Float* B = (const VectorRegister4Float*)Matrix2;
    VectorRegister4Float* R = (VectorRegister4Float*)Result;
    VectorRegister4Float Temp, R0, R1, R2;

    // First row of result (Matrix1[0] * Matrix2).
    Temp = VectorMultiply(VectorReplicate(A[0], 0), B[0]);
    Temp = VectorMultiplyAdd(VectorReplicate(A[0], 1), B[1], Temp);
    Temp = VectorMultiplyAdd(VectorReplicate(A[0], 2), B[2], Temp);
    R0 = VectorMultiplyAdd(VectorReplicate(A[0], 3), B[3], Temp);

    // Second row of result (Matrix1[1] * Matrix2).
    Temp = VectorMultiply(VectorReplicate(A[1], 0), B[0]);
    Temp = VectorMultiplyAdd(VectorReplicate(A[1], 1), B[1], Temp);
    Temp = VectorMultiplyAdd(VectorReplicate(A[1], 2), B[2], Temp);
    R1 = VectorMultiplyAdd(VectorReplicate(A[1], 3), B[3], Temp);

    // Third row of result (Matrix1[2] * Matrix2).
    Temp = VectorMultiply(VectorReplicate(A[2], 0), B[0]);
    Temp = VectorMultiplyAdd(VectorReplicate(A[2], 1), B[1], Temp);
    Temp = VectorMultiplyAdd(VectorReplicate(A[2], 2), B[2], Temp);
    R2 = VectorMultiplyAdd(VectorReplicate(A[2], 3), B[3], Temp);

    // Fourth row of result (Matrix1[3] * Matrix2).
    Temp = VectorMultiply(VectorReplicate(A[3], 0), B[0]);`enter code here`
    Temp = VectorMultiplyAdd(VectorReplicate(A[3], 1), B[1], Temp);
    Temp = VectorMultiplyAdd(VectorReplicate(A[3], 2), B[2], Temp);
    Temp = VectorMultiplyAdd(VectorReplicate(A[3], 3), B[3], Temp);

    // Store result. Must not be done during steps above in case source and destination are the same.
    R[0] = R0;
    R[1] = R1;
    R[2] = R2;
    R[3] = Temp;
}

First, it seems that the result is not correct whether I use the second matrix as it is or transpose it. My intuition is that B[] represents the row vectors of the second matrix, but I’m unsure if that’s correct.

How should I use the two matrices to get the correct multiplication result?

Read more here: Source link