2 Achegas c24891804f ... e5496df6e9

Autor SHA1 Mensaxe Data
  Kajetan Johannes Hammerle e5496df6e9 View and tests hai 1 mes
  Kajetan Johannes Hammerle d6e7630e19 SpinLock and tests hai 1 mes
Modificáronse 9 ficheiros con 241 adicións e 16 borrados
  1. 6 5
      CMakeLists.txt
  2. 14 0
      include/core/SpinLock.h
  3. 21 0
      include/core/View.h
  4. 22 0
      src/SpinLock.c
  5. 48 0
      src/View.c
  6. 5 5
      test/Main.c
  7. 6 6
      test/Tests.h
  8. 73 0
      test/modules/SpinLockTests.c
  9. 46 0
      test/modules/ViewTests.c

+ 6 - 5
CMakeLists.txt

@@ -10,13 +10,13 @@ set(SRC
     "src/Plane.c"
     "src/Quaternion.c"
     "src/Random.c"
+    "src/SpinLock.c"
     "src/Utility.c"
     "src/Vector.c"
+    "src/View.c"
     #"src/BitArray.cpp"
     #"src/Box.cpp"
     #"src/Frustum.cpp"
-    #"src/SpinLock.cpp"
-    #"src/View.cpp"
 )
 
 set(SRC_TESTS
@@ -27,8 +27,10 @@ set(SRC_TESTS
     "test/modules/PlaneTests.c"
     "test/modules/QuaternionTests.c"
     "test/modules/RandomTests.c"
+    "test/modules/SpinLockTests.c"
     "test/modules/UtilityTests.c"
     "test/modules/VectorTests.c"
+    "test/modules/ViewTests.c"
     #"test/modules/BitArrayTests.cpp"
     #"test/modules/BoxTests.cpp"
     #"test/modules/ComponentsTests.cpp"
@@ -39,8 +41,6 @@ set(SRC_TESTS
     #"test/modules/MatrixStackTests.cpp"
     #"test/modules/RingBufferTests.cpp"
     #"test/modules/StackTests.cpp"
-    #"test/modules/ThreadTests.cpp"
-    #"test/modules/ViewTests.cpp"
 )
 
 set(SRC_PERFORMANCE
@@ -103,8 +103,10 @@ target_sources(core PUBLIC
         ./include/core/Plane.h
         ./include/core/Quaternion.h
         ./include/core/Random.h
+        ./include/core/SpinLock.h
         ./include/core/Types.h
         ./include/core/Utility.h
+        ./include/core/View.h
 #        ./include/core/BitArray.hpp
 #        ./include/core/Box.hpp
 #        ./include/core/Components.hpp
@@ -117,7 +119,6 @@ target_sources(core PUBLIC
 #        ./include/core/RingBuffer.hpp
 #        ./include/core/Stack.hpp
 #        ./include/core/Vector.hpp
-#        ./include/core/View.hpp
 )
 install(TARGETS core FILE_SET HEADERS)
 

+ 14 - 0
include/core/SpinLock.h

@@ -0,0 +1,14 @@
+#ifndef CORE_SPIN_LOCK_H
+#define CORE_SPIN_LOCK_H
+
+#include <stdatomic.h>
+
+typedef struct {
+    atomic_bool lock;
+} CoreSpinLock;
+
+void coreInitSpinLock(CoreSpinLock* l);
+void coreLockSpinLock(CoreSpinLock* l);
+void coreUnlockSpinLock(CoreSpinLock* l);
+
+#endif

+ 21 - 0
include/core/View.h

@@ -0,0 +1,21 @@
+#ifndef CORE_VIEW_H
+#define CORE_VIEW_H
+
+#include "core/Matrix.h"
+
+typedef struct {
+    CoreMatrix view;
+    CoreVector3 back;
+    CoreVector3 down;
+    CoreVector3 front;
+    CoreVector3 left;
+    CoreVector3 right;
+    CoreVector3 up;
+} CoreView;
+
+#define CORE_VIEW ((CoreView){0})
+void coreUpdateDirections(CoreView* v, float lengthAngle, float widthAngle);
+void coreUpdateDirectionsQ(CoreView* v, const CoreQuaternion* q);
+CoreMatrix* coreUpdateMatrix(CoreView* v, const CoreVector3* pos);
+
+#endif

+ 22 - 0
src/SpinLock.c

@@ -0,0 +1,22 @@
+#include "core/SpinLock.h"
+
+#include <threads.h>
+
+void coreInitSpinLock(CoreSpinLock* l) {
+    atomic_init(&l->lock, false);
+}
+
+void coreLockSpinLock(CoreSpinLock* l) {
+    while(true) {
+        bool expected = false;
+        if(atomic_compare_exchange_weak(&l->lock, &expected, true)) {
+            break;
+        }
+        struct timespec s = {0};
+        thrd_sleep(&s, nullptr);
+    }
+}
+
+void coreUnlockSpinLock(CoreSpinLock* l) {
+    atomic_store(&l->lock, false);
+}

+ 48 - 0
src/View.c

@@ -0,0 +1,48 @@
+#include "core/View.h"
+
+#define CV3(a, b, c) (&(CoreVector3){{a, b, c}})
+
+void coreUpdateDirections(CoreView* v, float lengthAngle, float widthAngle) {
+    coreAngles(&v->front, lengthAngle, widthAngle);
+
+    coreCross(&v->right, &v->front, CV3(0.0f, 1.0f, 0.0f));
+    coreNormalizeV3(&v->right);
+
+    coreCross(&v->up, &v->right, &v->front);
+    coreNormalizeV3(&v->up);
+
+    coreInvertV3(&v->left, &v->right);
+    coreInvertV3(&v->back, &v->front);
+    coreInvertV3(&v->down, &v->up);
+}
+
+void coreUpdateDirectionsQ(CoreView* v, const CoreQuaternion* q) {
+    coreMulQV3(&v->up, q, CV3(0.0f, 1.0f, 0.0f));
+
+    coreMulQV3(&v->back, q, CV3(-1.0f, 0.0f, 0.0f));
+
+    coreCross(&v->right, &v->up, &v->back);
+    coreNormalizeV3(&v->right);
+
+    coreInvertV3(&v->left, &v->right);
+    coreInvertV3(&v->front, &v->back);
+    coreInvertV3(&v->down, &v->up);
+}
+
+CoreMatrix* coreUpdateMatrix(CoreView* v, const CoreVector3* pos) {
+    v->view.data[0] =
+        (CoreVector4){{v->right.data[0], v->right.data[1], v->right.data[1],
+                       -coreDotV3(&v->right, pos)}};
+    v->view.data[1] = (CoreVector4){
+        {v->up.data[0], v->up.data[1], v->up.data[1], -coreDotV3(&v->up, pos)}};
+    v->view.data[2] =
+        (CoreVector4){{v->back.data[0], v->back.data[1], v->back.data[1],
+                       -coreDotV3(&v->back, pos)}};
+    v->view.data[3] = (CoreVector4){{0.0f, 0.0f, 0.0f, 1.0f}};
+
+    //  view.set(0, Vector4(right[0], right[1], right[2], right.dot(-pos)));
+    //  view.set(1, Vector4(up[0], up[1], up[2], up.dot(-pos)));
+    //  view.set(2, Vector4(back[0], back[1], back[2], back.dot(-pos)));
+    //  view.set(3, Vector4(0.0f, 0.0f, 0.0f, 1.0f));
+    return &v->view;
+}

+ 5 - 5
test/Main.c

@@ -33,23 +33,23 @@ int main(int argAmount, const char** args) {
 
     // coreTestBitArray();
     // coreTestBox();
-    coreTestBuffer(light);
     // coreTestComponents();
     // coreTestFrustum();
     // coreTestHashMap(light);
     // coreTestLinkedList(light);
     // coreTestList(light);
     // coreTestMatrixStack(light);
+    // coreTestRingBuffer();
+    // coreTestStack(light);
+    coreTestBuffer(light);
     coreTestMatrix();
     coreTestPlane();
     coreTestQuaternion();
     coreTestRandom(light);
-    // coreTestRingBuffer();
-    // coreTestStack(light);
-    // coreTestThread();
+    coreTestSpinLock();
     coreTestUtility(light);
     coreTestVector();
-    // coreTestView();
+    coreTestView();
 
     coreLogLevel = CORE_LOG_WARNING;
     CORE_LOG_DEBUG("You won't see this!");

+ 6 - 6
test/Tests.h

@@ -3,6 +3,8 @@
 
 #include "Test.h"
 
+[[noreturn]] void coreTestInvalidAllocate(void);
+[[noreturn]] void coreTestInvalidReallocate(void);
 void coreTestBitArray(void);
 void coreTestBox(void);
 void coreTestBuffer(bool light);
@@ -12,19 +14,17 @@ void coreTestFrustum(void);
 void coreTestHashMap(bool light);
 void coreTestLinkedList(bool light);
 void coreTestList(bool light);
-void coreTestMatrixStack(bool light);
 void coreTestMatrix(void);
+void coreTestMatrixStack(bool light);
 void coreTestPlane(void);
+void coreTestPostCanary(void);
+void coreTestPreCanary(void);
 void coreTestQuaternion(void);
 void coreTestRandom(bool light);
 void coreTestRingBuffer(void);
+void coreTestSpinLock(void);
 void coreTestStack(bool light);
-void coreTestThread(void);
 void coreTestUtility(bool light);
-[[noreturn]] void coreTestInvalidAllocate(void);
-[[noreturn]] void coreTestInvalidReallocate(void);
-void coreTestPreCanary(void);
-void coreTestPostCanary(void);
 void coreTestVector(void);
 void coreTestView(void);
 

+ 73 - 0
test/modules/SpinLockTests.c

@@ -0,0 +1,73 @@
+#include <stdio.h>
+#include <threads.h>
+
+#include "../Tests.h"
+#include "core/SpinLock.h"
+#include "core/Utility.h"
+
+typedef struct {
+    mtx_t m;
+    int counter;
+} MutexCounter;
+
+static int incrementMutexCounter(void* p) {
+    MutexCounter* mcp = (MutexCounter*)p;
+    for(int i = 0; i < 10000; i++) {
+        mtx_lock(&mcp->m);
+        mcp->counter++;
+        mtx_unlock(&mcp->m);
+    }
+    return 0;
+}
+
+static void testMutex() {
+    i64 n = -coreNanos();
+
+    MutexCounter mc = {0};
+    mtx_init(&mc.m, mtx_plain);
+    thrd_t t[2];
+    thrd_create(t + 0, incrementMutexCounter, &mc);
+    thrd_create(t + 1, incrementMutexCounter, &mc);
+    thrd_join(t[0], nullptr);
+    thrd_join(t[1], nullptr);
+    CORE_TEST_INT(20000, mc.counter);
+
+    n += coreNanos();
+    printf("%ldns Mutex\n", n);
+}
+
+typedef struct {
+    CoreSpinLock s;
+    int counter;
+} SpinLockCounter;
+
+static int incrementSpinLockCounter(void* p) {
+    SpinLockCounter* mcp = (SpinLockCounter*)p;
+    for(int i = 0; i < 10000; i++) {
+        coreLockSpinLock(&mcp->s);
+        mcp->counter++;
+        coreUnlockSpinLock(&mcp->s);
+    }
+    return 0;
+}
+
+static void testSpinLock() {
+    i64 n = -coreNanos();
+
+    SpinLockCounter sc = {0};
+    coreInitSpinLock(&sc.s);
+    thrd_t t[2];
+    thrd_create(t + 0, incrementSpinLockCounter, &sc);
+    thrd_create(t + 1, incrementSpinLockCounter, &sc);
+    thrd_join(t[0], nullptr);
+    thrd_join(t[1], nullptr);
+    CORE_TEST_INT(20000, sc.counter);
+
+    n += coreNanos();
+    printf("%ldns SpinLock\n", n);
+}
+
+void coreTestSpinLock() {
+    testMutex();
+    testSpinLock();
+}

+ 46 - 0
test/modules/ViewTests.c

@@ -0,0 +1,46 @@
+#include "../Tests.h"
+#include "core/View.h"
+
+#define CV3(a, b, c) (&(CoreVector3){{a, b, c}})
+
+static void testFromAngles() {
+    CoreView v = {0};
+    coreUpdateDirections(&v, 0.0f, 0.0f);
+    CORE_TEST_V3(CV3(0.0f, 1.0f, 0.0f), &v.up);
+    CORE_TEST_V3(CV3(0.0f, -1.0f, 0.0f), &v.down);
+    CORE_TEST_V3(CV3(0.0f, 0.0f, -1.0f), &v.left);
+    CORE_TEST_V3(CV3(0.0f, 0.0f, 1.0f), &v.right);
+    CORE_TEST_V3(CV3(1.0f, 0.0f, 0.0f), &v.front);
+    CORE_TEST_V3(CV3(-1.0f, 0.0f, 0.0f), &v.back);
+}
+
+static void testFromQuaternion() {
+    CoreView v = {0};
+    coreUpdateDirectionsQ(&v, &CORE_UNIT_QUATERNION);
+    CORE_TEST_V3(CV3(0.0f, 1.0f, 0.0f), &v.up);
+    CORE_TEST_V3(CV3(0.0f, -1.0f, 0.0f), &v.down);
+    CORE_TEST_V3(CV3(0.0f, 0.0f, -1.0f), &v.left);
+    CORE_TEST_V3(CV3(0.0f, 0.0f, 1.0f), &v.right);
+    CORE_TEST_V3(CV3(1.0f, 0.0f, 0.0f), &v.front);
+    CORE_TEST_V3(CV3(-1.0f, 0.0f, 0.0f), &v.back);
+}
+
+static void testUpdateMatrix() {
+    CoreView v = {0};
+    CoreMatrix* m = coreUpdateMatrix(&v, CV3(1.0f, 2.0f, 3.0f));
+
+    char buffer[128];
+    coreToStringMatrix(m, buffer, sizeof(buffer));
+
+    CORE_TEST_STRING("[[0.000, 0.000, 0.000, -0.000], "
+                     "[0.000, 0.000, 0.000, -0.000], "
+                     "[0.000, 0.000, 0.000, -0.000], "
+                     "[0.000, 0.000, 0.000, 1.000]]",
+                     buffer);
+}
+
+void coreTestView() {
+    testFromAngles();
+    testFromQuaternion();
+    testUpdateMatrix();
+}