Skip to content

Instantly share code, notes, and snippets.

@MurageKibicho
Created July 23, 2025 13:07
Show Gist options
  • Save MurageKibicho/f4b9561c483f45b9e8beafce970f0ec1 to your computer and use it in GitHub Desktop.
Save MurageKibicho/f4b9561c483f45b9e8beafce970f0ec1 to your computer and use it in GitHub Desktop.
Starter code for PMat32 Implementation Guide
//Full guide : https://leetarxiv.substack.com/p/positive-only-binary-pmat32
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdint.h>
#include <stdbool.h>
#include <assert.h>
#define INDEX(x, y, cols) ((x) * (cols) + (y))
//Run : clear && gcc PMat.c -lm -o m.o && ./m.o
float RandomFloat(float min, float max)
{
return min + ((float)rand() / RAND_MAX) * (max - min);
}
void FloatRandomMatrix(int rows, int cols, float min, float max, float *matrix)
{
for(int i = 0; i < rows * cols; i++)
{
matrix[i] = RandomFloat(min, max);
}
}
void FloatMultiply(int m, int n, int p, float *A, float *B, float *C)
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < p; j++)
{
float sum = 0.0f;
for(int k = 0; k < n; k++)
{
sum += A[i * n + k] * B[k * p + j];
}
C[i * p + j] = sum;
}
}
}
void FloatMatrixPrint(int rows, int cols, float *matrix)
{
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
printf("%6.2f ", matrix[i * cols + j]);
}
printf("\n");
}
printf("\n");
}
void TestFloatMatrix()
{
//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));
//Generate random data
FloatRandomMatrix(rowA, colA, -5.0f, 5.0f, A);
FloatRandomMatrix(rowB, colB, -5.0f, 5.0f, B);
//Multiply matrices
FloatMultiply(rowA, colA, colB, A, B, C);
//Print Results
FloatMatrixPrint(rowA, colA, A);
FloatMatrixPrint(rowB, colB, B);
FloatMatrixPrint(rowA, colB, C);
//Free memory
free(A);free(B);free(C);
}
int main()
{
TestFloatMatrix();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment