Skip to content

Commit

Permalink
Implemented RNG and Matrix/Row operations
Browse files Browse the repository at this point in the history
  • Loading branch information
iWas-Coder committed Nov 7, 2024
1 parent 6b94e36 commit 61f105b
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 1 deletion.
32 changes: 32 additions & 0 deletions carbon.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,18 @@ CARBON_API void carbon_assert_abort(const char *expr, const char *file, u32 line
#define CARBON_1_SQRT2 (1 / CARBON_SQRT2)
#define CARBON_SQRT3 1.73205080756887729352
#define CARBON_1_SQRT3 (1 / CARBON_SQRT3)
#define CARBON_I32_MAX 0x7fffffff
#define CARBON_RAND_MAX CARBON_I32_MAX
#define CARBON_PCG_RAND_MAGIC 6364136223846793005ULL
#define CARBON_MIN(x, y) (x < y ? x : y)
#define CARBON_MAX(x, y) (x > y ? x : y)
#define CARBON_CLAMP(x, min, max) ((x <= min) ? min : (x >= max) ? max : x)
#define CARBON_SIGN(x) (!x ? 0 : x < 0 ? -1 : 1)
#define CARBON_STEP(edge, x) (x < edge ? 0 : 1)
#define CARBON_SWAP(T, x, y) do { T z = x; x = y; y = z; } while (0)
#define CARBON_LERP(a, b, t) (a + (b - a) * t)
#define CARBON_MAT_AT(m, i, j) (m).items[(i) * (m).cols + (j)]
#define CARBON_ROW_AT(i, j) (i).items[(j)]

typedef union {
f32 items[2];
Expand All @@ -236,6 +241,20 @@ typedef union {
};
} CBN_Vec3;

typedef struct {
f32 *items;
usz rows;
usz cols;
} CBN_Matrix;

typedef struct {
f32 *items;
usz cols;
} CBN_Row;

CARBON_API void carbon_math_srand(u64 seed);
CARBON_API int carbon_math_rand(void);
CARBON_API f32 carbon_math_randf(void);
CARBON_API f32 carbon_math_abs(f32 x);
CARBON_API f32 carbon_math_sqrt(f32 x);
CARBON_API f32 carbon_math_exp(f32 x);
Expand All @@ -250,6 +269,19 @@ CARBON_API f32 carbon_math_vec2_dot(CBN_Vec2 u, CBN_Vec2 v);
CARBON_API f32 carbon_math_vec3_dot(CBN_Vec3 u, CBN_Vec3 v);
CARBON_API CBN_Vec3 carbon_math_vec3_cross(CBN_Vec3 u, CBN_Vec3 v);
CARBON_API CBN_Vec2 carbon_math_vec2_rotate(CBN_Vec2 v, f32 angle);
CARBON_API CBN_Matrix carbon_math_mat_create(usz rows, usz cols);
CARBON_API void carbon_math_mat_fill(CBN_Matrix m, f32 x);
CARBON_API void carbon_math_mat_rand(CBN_Matrix m, f32 min, f32 max);
CARBON_API CBN_Row carbon_math_mat_row(CBN_Matrix m, usz row);
CARBON_API void carbon_math_mat_copy(CBN_Matrix dst, CBN_Matrix src);
CARBON_API void carbon_math_mat_add(CBN_Matrix dst, CBN_Matrix m);
CARBON_API void carbon_math_mat_dot(CBN_Matrix dst, CBN_Matrix a, CBN_Matrix b);
CARBON_API CBN_Row carbon_math_row_create(usz cols);
CARBON_API CBN_Matrix carbon_math_row_to_mat(CBN_Row r);
CARBON_API CBN_Row carbon_math_row_slice(CBN_Row r, usz i, usz cols);
CARBON_API void carbon_math_row_fill(CBN_Row r, f32 x);
CARBON_API void carbon_math_row_rand(CBN_Row r, f32 min, f32 max);
CARBON_API void carbon_math_row_copy(CBN_Row dst, CBN_Row src);

#ifdef __cplusplus
CBN_Vec2 operator+(const CBN_Vec2 &u, const CBN_Vec2 &v);
Expand Down
121 changes: 120 additions & 1 deletion src/carbon_math.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,24 @@ CARBON_API f32 expf(f32);
CARBON_API f32 sinf(f32);
CARBON_API f32 cosf(f32);

static u64 cbn_math_rand_seed;

void carbon_math_srand(u64 seed) {
cbn_math_rand_seed = seed - 1;
}

int carbon_math_rand(void) {
cbn_math_rand_seed = CARBON_PCG_RAND_MAGIC * cbn_math_rand_seed + 1;
return cbn_math_rand_seed >> 33;
}

f32 carbon_math_randf(void) {
return (f32) carbon_math_rand() / (f32) CARBON_RAND_MAX;
}

f32 carbon_math_abs(f32 x) {
union { f32 f; u32 i; } u = {x};
u.i &= 0x7fffffff;
u.i &= CARBON_I32_MAX;
return u.f;
}

Expand Down Expand Up @@ -132,3 +147,107 @@ CBN_Vec2 carbon_math_vec2_rotate(CBN_Vec2 v, f32 angle) {
.y = (v.x * s) + (v.y * c)
};
}

CBN_Matrix carbon_math_mat_create(usz rows, usz cols) {
CBN_Matrix m = {
.items = (f32 *) CARBON_MALLOC(rows * cols * sizeof(f32)),
.rows = rows,
.cols = cols
};
if (!m.items) {
CARBON_ERROR("failed to allocate memory (%zuB)", rows * cols * sizeof(f32));
memset(&m, 0, sizeof(CBN_Matrix));
}
return m;
}

void carbon_math_mat_fill(CBN_Matrix m, f32 x) {
for (usz i = 0; i < m.rows; ++i) {
for (usz j = 0; j < m.cols; ++j) {
CARBON_MAT_AT(m, i, j) = x;
}
}
}

void carbon_math_mat_rand(CBN_Matrix m, f32 min, f32 max) {
for (usz i = 0; i < m.rows; ++i) {
for (usz j = 0; j < m.cols; ++j) {
CARBON_MAT_AT(m, i, j) = carbon_math_randf() * (max - min) + min;
}
}
}

CBN_Row carbon_math_mat_row(CBN_Matrix m, usz row) {
return (CBN_Row) {
.items = &CARBON_MAT_AT(m, row, 0),
.cols = m.cols
};
}

void carbon_math_mat_copy(CBN_Matrix dst, CBN_Matrix src) {
CARBON_ASSERT(dst.rows == src.rows);
CARBON_ASSERT(dst.cols == src.cols);
for (usz i = 0; i < dst.rows; ++i) {
for (usz j = 0; j < dst.cols; ++j) {
CARBON_MAT_AT(dst, i, j) = CARBON_MAT_AT(src, i, j);
}
}
}

void carbon_math_mat_add(CBN_Matrix dst, CBN_Matrix m) {
CARBON_ASSERT(dst.rows == m.rows);
CARBON_ASSERT(dst.cols == m.cols);
for (usz i = 0; i < dst.rows; ++i) {
for (usz j = 0; j < dst.cols; ++j) {
CARBON_MAT_AT(dst, i, j) += CARBON_MAT_AT(m, i, j);
}
}
}

void carbon_math_mat_dot(CBN_Matrix dst, CBN_Matrix a, CBN_Matrix b) {
CARBON_ASSERT(a.cols == b.rows);
CARBON_ASSERT(dst.rows == a.rows);
CARBON_ASSERT(dst.cols == b.cols);
carbon_math_mat_fill(dst, 0);
usz n = a.cols;
for (usz i = 0; i < dst.rows; ++i) {
for (usz k = 0; k < n; ++k) {
for (usz j = 0; j < dst.cols; ++j) {
CARBON_MAT_AT(dst, i, j) += CARBON_MAT_AT(a, i, k) * CARBON_MAT_AT(b, k, j);
}
}
}
}

CBN_Row carbon_math_row_create(usz cols) {
return carbon_math_mat_row(carbon_math_mat_create(1, cols), 0);
}

CBN_Matrix carbon_math_row_to_mat(CBN_Row r) {
return (CBN_Matrix) {
.items = r.items,
.rows = 1,
.cols = r.cols
};
}

CBN_Row carbon_math_row_slice(CBN_Row r, usz i, usz cols) {
CARBON_ASSERT(i < r.cols);
CARBON_ASSERT(i + cols <= r.cols);
return (CBN_Row) {
.items = &CARBON_ROW_AT(r, i),
.cols = cols
};
}

void carbon_math_row_fill(CBN_Row r, f32 x) {
carbon_math_mat_fill(carbon_math_row_to_mat(r), x);
}

void carbon_math_row_rand(CBN_Row r, f32 min, f32 max) {
carbon_math_mat_rand(carbon_math_row_to_mat(r), min, max);
}

void carbon_math_row_copy(CBN_Row dst, CBN_Row src) {
carbon_math_mat_copy(carbon_math_row_to_mat(dst), carbon_math_row_to_mat(src));
}

0 comments on commit 61f105b

Please sign in to comment.