Skip to content

Commit

Permalink
Continue work on math and vector stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
iWas-Coder committed Nov 5, 2024
1 parent 444cb63 commit 836cff2
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 3 deletions.
20 changes: 20 additions & 0 deletions carbon.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,35 @@ typedef union {
};
} CBN_Vec2;

typedef union {
f32 items[3];
struct {
union { f32 x, r, s, u; };
union { f32 y, g, t, v; };
union { f32 z, b, p, w; };
};
} CBN_Vec3;

CARBON_API f32 carbon_math_abs(f32 x);
CARBON_API f32 carbon_math_exp(f32 x);
CARBON_API f32 carbon_math_sigmoid(f32 x);
CARBON_API f32 carbon_math_tanh(f32 x);
CARBON_API f32 carbon_math_smoothstep(f32 a, f32 b, f32 t);
CARBON_API CBN_Vec2 carbon_math_vec2_add(CBN_Vec2 u, CBN_Vec2 v);
CARBON_API CBN_Vec3 carbon_math_vec3_add(CBN_Vec3 u, CBN_Vec3 v);
CARBON_API CBN_Vec2 carbon_math_vec2_sub(CBN_Vec2 u, CBN_Vec2 v);
CARBON_API CBN_Vec3 carbon_math_vec3_sub(CBN_Vec3 u, CBN_Vec3 v);
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_Vec2 carbon_math_vec2_rotate(CBN_Vec2 v, f32 angle);

#ifdef __cplusplus
CBN_Vec2 operator+(const CBN_Vec2 &u, const CBN_Vec2 &v);
CBN_Vec3 operator+(const CBN_Vec3 &u, const CBN_Vec3 &v);
CBN_Vec2 operator-(const CBN_Vec2 &u, const CBN_Vec2 &v);
CBN_Vec3 operator-(const CBN_Vec3 &u, const CBN_Vec3 &v);
f32 operator*(const CBN_Vec2 &u, const CBN_Vec2 &v);
f32 operator*(const CBN_Vec3 &u, const CBN_Vec3 &v);
#endif

/*
Expand Down
54 changes: 51 additions & 3 deletions src/carbon_math.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
#include <carbon.h>
#endif // CARBON_IMPLEMENTATION

// TODO: replace with own implementation
CARBON_API f32 expf(f32);
CARBON_API f32 sinf(f32);
CARBON_API f32 cosf(f32);

f32 carbon_math_abs(f32 x) {
union { f32 f; u32 i; } u = {x};
u.i &= 0x7fffffff;
Expand Down Expand Up @@ -47,12 +52,15 @@ f32 carbon_math_exp(f32 x) {
}

f32 carbon_math_sigmoid(f32 x) {
return 1 / (1 + carbon_math_exp(-x));
// return 1 / (1 + carbon_math_exp(-x));
return 1 / (1 + expf(-x));
}

f32 carbon_math_tanh(f32 x) {
f32 ex = carbon_math_exp(x);
f32 enx = carbon_math_exp(-x);
// f32 ex = carbon_math_exp(x);
f32 ex = expf(x);
// f32 enx = carbon_math_exp(-x);
f32 enx = expf(-x);
return (ex - enx) / (ex + enx);
}

Expand All @@ -67,3 +75,43 @@ CBN_Vec2 carbon_math_vec2_add(CBN_Vec2 u, CBN_Vec2 v) {
.y = u.y + v.y
};
}

CBN_Vec3 carbon_math_vec3_add(CBN_Vec3 u, CBN_Vec3 v) {
return (CBN_Vec3) {
.x = u.x + v.x,
.y = u.y + v.y,
.z = u.z + v.z
};
}

CBN_Vec2 carbon_math_vec2_sub(CBN_Vec2 u, CBN_Vec2 v) {
return (CBN_Vec2) {
.x = u.x - v.x,
.y = u.y - v.y
};
}

CBN_Vec3 carbon_math_vec3_sub(CBN_Vec3 u, CBN_Vec3 v) {
return (CBN_Vec3) {
.x = u.x - v.x,
.y = u.y - v.y,
.z = u.z - v.z
};
}

f32 carbon_math_vec2_dot(CBN_Vec2 u, CBN_Vec2 v) {
return (u.x * v.x) + (u.y * v.y);
}

f32 carbon_math_vec3_dot(CBN_Vec3 u, CBN_Vec3 v) {
return (u.x * v.x) + (u.y * v.y) + (u.z * v.z);
}

CBN_Vec2 carbon_math_vec2_rotate(CBN_Vec2 v, f32 angle) {
f32 rads = angle * (CARBON_PI / 180);
f32 c = cosf(rads), s = sinf(rads);
return (CBN_Vec2) {
.x = (v.x * c) - (v.y * s),
.y = (v.x * s) + (v.y * c)
};
}
20 changes: 20 additions & 0 deletions src/carbon_math_ops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,24 @@ CBN_Vec2 operator+(const CBN_Vec2 &u, const CBN_Vec2 &v) {
return carbon_math_vec2_add(u, v);
}

CBN_Vec3 operator+(const CBN_Vec3 &u, const CBN_Vec3 &v) {
return carbon_math_vec3_add(u, v);
}

CBN_Vec2 operator-(const CBN_Vec2 &u, const CBN_Vec2 &v) {
return carbon_math_vec2_sub(u, v);
}

CBN_Vec3 operator-(const CBN_Vec3 &u, const CBN_Vec3 &v) {
return carbon_math_vec3_sub(u, v);
}

f32 operator*(const CBN_Vec2 &u, const CBN_Vec2 &v) {
return carbon_math_vec2_dot(u, v);
}

f32 operator*(const CBN_Vec3 &u, const CBN_Vec3 &v) {
return carbon_math_vec3_dot(u, v);
}

#endif // __cplusplus
43 changes: 43 additions & 0 deletions test/carbon_math_ops_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,46 @@ CARBON_TEST(carbon_math_ops, vec2_add) {
carbon_should_be_f(6, r.y);
return CARBON_OK;
}

CARBON_TEST(carbon_math_ops, vec3_add) {
CBN_Vec3 u = {{1, 2, 3}};
CBN_Vec3 v = {{4, 5, 6}};
CBN_Vec3 r = u + v;
carbon_should_be_f(5, r.x);
carbon_should_be_f(7, r.y);
carbon_should_be_f(9, r.z);
return CARBON_OK;
}

CARBON_TEST(carbon_math_ops, vec2_sub) {
CBN_Vec2 u = {{1, 2}};
CBN_Vec2 v = {{3, 4}};
CBN_Vec2 r = u - v;
carbon_should_be_f(-2, r.x);
carbon_should_be_f(-2, r.y);
return CARBON_OK;
}

CARBON_TEST(carbon_math_ops, vec3_sub) {
CBN_Vec3 u = {{1, 2, 3}};
CBN_Vec3 v = {{4, 5, 6}};
CBN_Vec3 r = u - v;
carbon_should_be_f(-3, r.x);
carbon_should_be_f(-3, r.y);
carbon_should_be_f(-3, r.z);
return CARBON_OK;
}

CARBON_TEST(carbon_math_ops, vec2_dot) {
CBN_Vec2 u = {{1, 2}};
CBN_Vec2 v = {{3, 4}};
carbon_should_be_f(11, u * v);
return CARBON_OK;
}

CARBON_TEST(carbon_math_ops, vec3_dot) {
CBN_Vec3 u = {{1, 2, 3}};
CBN_Vec3 v = {{4, 5, 6}};
carbon_should_be_f(32, u * v);
return CARBON_OK;
}
79 changes: 79 additions & 0 deletions test/carbon_math_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,42 @@

#include <carbon.h>

CARBON_TEST(carbon_math, abs) {
carbon_should_be(77, carbon_math_abs(-77));
carbon_should_be(77, carbon_math_abs(77));
carbon_should_be_f(6.9, carbon_math_abs(-6.9));
carbon_should_be_f(6.9, carbon_math_abs(6.9));
return CARBON_OK;
}

CARBON_TEST(carbon_math, exp) {
// CARBON_INFO("e^0 :: %.2f", carbon_math_exp(0));
// CARBON_INFO("e^1 :: %.2f", carbon_math_exp(1));
// CARBON_INFO("e^2 :: %.2f", carbon_math_exp(2));
return CARBON_KO;
}

CARBON_TEST(carbon_math, sigmoid) {
carbon_should_be_f(0, carbon_math_sigmoid(-7));
carbon_should_be_f(0.006692, carbon_math_sigmoid(-5));
carbon_should_be_f(0.047425, carbon_math_sigmoid(-3));
carbon_should_be_f(0.268941, carbon_math_sigmoid(-1));
carbon_should_be_f(0.5, carbon_math_sigmoid(0));
carbon_should_be_f(0.731058, carbon_math_sigmoid(1));
carbon_should_be_f(0.952574, carbon_math_sigmoid(3));
carbon_should_be_f(0.993307, carbon_math_sigmoid(5));
carbon_should_be_f(1, carbon_math_sigmoid(7));
return CARBON_OK;
}

CARBON_TEST(carbon_math, tanh) {
return CARBON_KO;
}

CARBON_TEST(carbon_math, smoothstep) {
return CARBON_KO;
}

CARBON_TEST(carbon_math, vec2_add) {
CBN_Vec2 u = {{1, 2}};
CBN_Vec2 v = {{3, 4}};
Expand All @@ -11,3 +47,46 @@ CARBON_TEST(carbon_math, vec2_add) {
carbon_should_be_f(6, r.y);
return CARBON_OK;
}

CARBON_TEST(carbon_math, vec3_add) {
CBN_Vec3 u = {{1, 2, 3}};
CBN_Vec3 v = {{4, 5, 6}};
CBN_Vec3 r = carbon_math_vec3_add(u, v);
carbon_should_be_f(5, r.x);
carbon_should_be_f(7, r.y);
carbon_should_be_f(9, r.z);
return CARBON_OK;
}

CARBON_TEST(carbon_math, vec2_sub) {
CBN_Vec2 u = {{1, 2}};
CBN_Vec2 v = {{3, 4}};
CBN_Vec2 r = carbon_math_vec2_sub(u, v);
carbon_should_be_f(-2, r.x);
carbon_should_be_f(-2, r.y);
return CARBON_OK;
}

CARBON_TEST(carbon_math, vec3_sub) {
CBN_Vec3 u = {{1, 2, 3}};
CBN_Vec3 v = {{4, 5, 6}};
CBN_Vec3 r = carbon_math_vec3_sub(u, v);
carbon_should_be_f(-3, r.x);
carbon_should_be_f(-3, r.y);
carbon_should_be_f(-3, r.z);
return CARBON_OK;
}

CARBON_TEST(carbon_math, vec2_dot) {
CBN_Vec2 u = {{1, 2}};
CBN_Vec2 v = {{3, 4}};
carbon_should_be_f(11, carbon_math_vec2_dot(u, v));
return CARBON_OK;
}

CARBON_TEST(carbon_math, vec3_dot) {
CBN_Vec3 u = {{1, 2, 3}};
CBN_Vec3 v = {{4, 5, 6}};
carbon_should_be_f(32, carbon_math_vec3_dot(u, v));
return CARBON_OK;
}

0 comments on commit 836cff2

Please sign in to comment.