Created
August 14, 2025 09:58
-
-
Save MurageKibicho/c1142b842d5233929d84fe0c8fbc6199 to your computer and use it in GitHub Desktop.
Matrix Multiplication in Pmat32
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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