Skip to content

Instantly share code, notes, and snippets.

@MurageKibicho
Created August 14, 2025 09:58
Show Gist options
  • Select an option

  • Save MurageKibicho/c1142b842d5233929d84fe0c8fbc6199 to your computer and use it in GitHub Desktop.

Select an option

Save MurageKibicho/c1142b842d5233929d84fe0c8fbc6199 to your computer and use it in GitHub Desktop.
Matrix Multiplication in Pmat32
void TestMatmul()
{
int integerBits = 8;
int fractionBits= 20;
assert(integerBits + fractionBits < 32);assert(integerBits > -1);assert(fractionBits > -1);
/*Create mandatory constants*/
PMat32 ONE_CONSTANT = (1 << fractionBits);
PMat32 MAX_CONSTANT = (1 << (integerBits + fractionBits));
//A * B = C
int rowA = 3;
int colA = 4;
int rowB = colA;
int colB = 4;
float *A = malloc(rowA * colA * sizeof(float));
float *B = malloc(rowB * colB * sizeof(float));
float *C = malloc(rowA * colB * sizeof(float));
float *R = malloc(rowA * colB * sizeof(float));
PMat32 *D = malloc(rowA * colA * sizeof(PMat32));
PMat32 *E = malloc(rowB * colB * sizeof(PMat32));
PMat32 *F = malloc(rowA * colB * sizeof(PMat32));
//Generate random data
FloatRandomMatrix(rowA, colA, -5.0f, 5.0f, A);
FloatRandomMatrix(rowB, colB, -5.0f, 5.0f, B);
//Convert float matrix to Pmat32 matrix
ConvertFloatArrayToPMAT(rowA * colA , A, D, ONE_CONSTANT, MAX_CONSTANT);
ConvertFloatArrayToPMAT(rowB * rowB , B, E, ONE_CONSTANT, MAX_CONSTANT);
//Multiply matrices
FloatMultiply(rowA, colA, colB, A, B, C);
PMatMultiply(rowA, colA, colB, D, E, F, fractionBits, MAX_CONSTANT);
ConvertPMATArrayToFloat(rowA * colB , R, F, ONE_CONSTANT, MAX_CONSTANT);
//Print Results
FloatMatrixPrint(rowA, colA, A);
FloatMatrixPrint(rowB, colB, B);
FloatMatrixPrint(rowA, colB, C);
FloatMatrixPrint(rowA, colB, R);
//Free memory
free(A);free(B);free(C);free(R);
free(D);free(E);free(F);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment