Optimized matrix multiplication in C

后端 未结 13 2433
一整个雨季
一整个雨季 2020-11-30 01:44

I\'m trying to compare different methods for matrix multiplication. The first one is normal method:

do
{
    for (j = 0; j < i; j++)
    {
        for (k          


        
13条回答
  •  隐瞒了意图╮
    2020-11-30 02:45

    Very old question, but heres my current implementation for my opengl projects:

    typedef float matN[N][N];
    
    inline void matN_mul(matN dest, matN src1, matN src2)
    {
        unsigned int i;
        for(i = 0; i < N^2; i++)
        {
            unsigned int row = (int) i / 4, col = i % 4;
            dest[row][col] = src1[row][0] * src2[0][col] +
                             src1[row][1] * src2[1][col] +
                             ....
                             src[row][N-1] * src3[N-1][col];
        }
    }
    

    Where N is replaced with the size of the matrix. So if you are multiplying 4x4 matrices, then you use:

    typedef float mat4[4][4];    
    
    inline void mat4_mul(mat4 dest, mat4 src1, mat4 src2)
    {
        unsigned int i;
        for(i = 0; i < 16; i++)
        {
            unsigned int row = (int) i / 4, col = i % 4;
            dest[row][col] = src1[row][0] * src2[0][col] +
                             src1[row][1] * src2[1][col] +
                             src1[row][2] * src2[2][col] +
                             src1[row][3] * src2[3][col];
        }
    }
    

    This function mainly minimizes loops but the modulus might be taxing... On my computer this function performed roughly 50% faster than a triple for loop multiplication function.

    Cons:

    • Lots of code needed (ex. different functions for mat3 x mat3, mat5 x mat5...)

    • Tweaks needed for irregular multiplication (ex. mat3 x mat4).....

提交回复
热议问题