From 78a6edf84b9adf88de7114d0d5d7b67c2b9d11ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20GUEZO?= Date: Mon, 11 Aug 2025 23:38:45 +0200 Subject: [PATCH] feat: add c++ file and remove mconfig file --- CMakeLists.txt | 20 +++++++++++++------- src/main.c | 36 ------------------------------------ src/main.cpp | 13 +++++++++++++ src/math/mat4.c | 20 +++++++++++++++++++- src/math/mat4.h | 22 +++++++++++++++++++--- src/math/mconfig.h | 32 -------------------------------- src/math/vec3.c | 2 -- src/math/vec3.h | 32 ++++++++++++++++++++++++++++---- src/math/vec4.h | 27 ++++++++++++++++++++++++++- tests/tests_config.c | 2 +- tests/tests_vec3f.c | 2 +- 11 files changed, 120 insertions(+), 88 deletions(-) delete mode 100644 src/main.c create mode 100644 src/main.cpp delete mode 100644 src/math/mconfig.h diff --git a/CMakeLists.txt b/CMakeLists.txt index c8f847f..f43428c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,12 +1,20 @@ cmake_minimum_required(VERSION 3.10) -project(t3dsr C) +project(t3dsr C CXX) set(CMAKE_C_STANDARD 17) +set(CMAKE_CXX_STANDARD 17) -file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS src/*.c) -file(GLOB_RECURSE HEADERS CONFIGURE_DEPENDS src/*.h) +# ==== sources ==== +file(GLOB_RECURSE C_SOURCES CONFIGURE_DEPENDS src/*.c) +file(GLOB_RECURSE CPP_SOURCES CONFIGURE_DEPENDS src/*.cpp) +file(GLOB_RECURSE HEADERS CONFIGURE_DEPENDS src/*.h src/*.hpp) -add_executable(main ${SOURCES} ${HEADERS}) +file(GLOB MAIN_SRC CONFIGURE_DEPENDS src/main.*) + +set(ALL_SOURCES ${C_SOURCES} ${CPP_SOURCES}) +list(REMOVE_ITEM ALL_SOURCES ${MAIN_SRC}) + +add_executable(main ${MAIN_SRC} ${ALL_SOURCES} ${HEADERS}) if(NOT MSVC) target_link_libraries(main m) endif() @@ -14,13 +22,11 @@ endif() # ==== tests ==== enable_testing() - file(GLOB_RECURSE TEST_SOURCES CONFIGURE_DEPENDS tests/*.c) -list(FILTER SOURCES EXCLUDE REGEX ".*src/main\\.c$") foreach(test_src ${TEST_SOURCES}) get_filename_component(test_name ${test_src} NAME_WE) - add_executable(${test_name} ${test_src} ${SOURCES}) + add_executable(${test_name} ${test_src} ${ALL_SOURCES}) if(NOT MSVC) target_link_libraries(${test_name} m) endif() diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 815c47f..0000000 --- a/src/main.c +++ /dev/null @@ -1,36 +0,0 @@ -#include -#include "math/mat4.h" - -int main(void) -{ - float arr[16] = { - 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12, - 13, 14, 15, 16 - }; - - Mat4f_t mat1 = mat4f_from_array(arr); - Mat4f_t* mat_tps = mat4f_tpo_r(&mat1); - - printf("%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n", - mat1.m[0], - mat1.m[1], - mat1.m[2], - mat1.m[3], - mat1.m[4], - mat1.m[5], - mat1.m[6], - mat1.m[7], - mat1.m[8], - mat1.m[9], - mat1.m[10], - mat1.m[11], - mat1.m[12], - mat1.m[13], - mat1.m[14], - mat1.m[15] - - ); - return 0; -} diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..6fe6c07 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,13 @@ +#include + +extern "C" +{ + #include "math/mat4.h" + #include "math/vec3.h" + #include "math/vec4.h" +} + +int main() +{ + return 0; +} \ No newline at end of file diff --git a/src/math/mat4.c b/src/math/mat4.c index e5e28fc..55c32ce 100644 --- a/src/math/mat4.c +++ b/src/math/mat4.c @@ -300,4 +300,22 @@ Mat4f_t mat4f_tpo(const Mat4f_t *restrict m) Mat4f_t res = mat4f_clone(m); mat4f_tpo_r(&res); return res; -} \ No newline at end of file +} + +float mat4f_det(const Mat4f_t* m) +{ + float det; + return det; +} + +Mat4f_t* mat4f_inv_r(Mat4f_t* __restrict m) +{ + return m; +} + +Mat4f_t mat4f_inv(const Mat4f_t* m) +{ + Mat4f_t mout = mat4f_clone(m); + mat4f_inv_r(&mout); + return mout; +} diff --git a/src/math/mat4.h b/src/math/mat4.h index 11eace5..21da714 100644 --- a/src/math/mat4.h +++ b/src/math/mat4.h @@ -1,7 +1,22 @@ #ifndef MATRIX4_H #define MATRIX4_H -#include "mconfig.h" +#if defined(__x86_64__) || defined(__amd64__) || defined(_M_X64) + #define SIMD_X86 + #include + +#elif defined(__aarch64__) || defined(__arm64__) || defined(_M_ARM64) + #define SIMD_ARCH + #include + +#else +#endif + +#ifdef _MSC_VER + #define ALIGN16 __declspec(align(16)) +#else + #define ALIGN16 __attribute__((aligned(16))) +#endif #define MAT_SIZE 16 #define MAT_DIM 4 @@ -37,8 +52,9 @@ Mat4f_t* mat4f_mul_r(Mat4f_t* out, const Mat4f_t* m2); Mat4f_t mat4f_tpo(const Mat4f_t *__restrict m); Mat4f_t* mat4f_tpo_r(Mat4f_t *__restrict m); -// float mat4_det(const Mat4_t* m); +float mat4f_det(const Mat4f_t* m); -// Mat4_t mat4_inv(const Mat4_t* m); +Mat4f_t mat4f_inv(const Mat4f_t* m); +Mat4f_t* mat4f_inv_r(Mat4f_t* __restrict m); #endif // MATRIX4_H diff --git a/src/math/mconfig.h b/src/math/mconfig.h deleted file mode 100644 index 153a283..0000000 --- a/src/math/mconfig.h +++ /dev/null @@ -1,32 +0,0 @@ -// Common math library - -#ifndef MCONFIG_H -#define MCONFIG_H - -#include -#include -#include -#include - -#if defined(__x86_64__) || defined(__amd64__) || defined(_M_X64) - #define SIMD_X86 - #define SIMD_ENABLE - #include - -#elif defined(__aarch64__) || defined(__arm64__) || defined(_M_ARM64) - #define SIMD_ARCH - #define SIMD_ENABLE - #include - -#else - #define SIMD_NONE -#endif - - -#ifdef _MSC_VER - #define ALIGN16 __declspec(align(16)) -#else - #define ALIGN16 __attribute__((aligned(16))) -#endif - -#endif // MCONFIG_H diff --git a/src/math/vec3.c b/src/math/vec3.c index 48ce8b5..f6e2bfd 100644 --- a/src/math/vec3.c +++ b/src/math/vec3.c @@ -1,7 +1,5 @@ #include "vec3.h" -#define VEC_SIZE 4 - Vec3f_t vec3f(float x, float y, float z) { Vec3f_t vec = {.x = x, .y = y, .z = z}; diff --git a/src/math/vec3.h b/src/math/vec3.h index be57591..d578a02 100644 --- a/src/math/vec3.h +++ b/src/math/vec3.h @@ -8,11 +8,33 @@ #ifndef vec3_h #define vec3_h -#include "mconfig.h" +#include +#include -typedef union -{ - struct {float x, y, z; }; +#if defined(__x86_64__) || defined(__amd64__) || defined(_M_X64) + #define SIMD_X86 + #define SIMD_ENABLE + #include + +#elif defined(__aarch64__) || defined(__arm64__) || defined(_M_ARM64) + #define SIMD_ARCH + #define SIMD_ENABLE + #include + +#else + #define SIMD_NONE +#endif + +#ifdef _MSC_VER + #define ALIGN16 __declspec(align(16)) +#else + #define ALIGN16 __attribute__((aligned(16))) +#endif + +#define VEC_SIZE 3 + +typedef union { + struct { float x, y, z; }; float data[4]; } ALIGN16 Vec3f_t; @@ -58,4 +80,6 @@ Vec3f_t vec3f_refl(Vec3f_t v, Vec3f_t normal); float vec3f_dist(Vec3f_t a, Vec3f_t b); +#undef VEC_SIZE + #endif /* vec3_h */ diff --git a/src/math/vec4.h b/src/math/vec4.h index 3e415ca..68cc89f 100644 --- a/src/math/vec4.h +++ b/src/math/vec4.h @@ -1,10 +1,33 @@ #ifndef VECTOR4_H #define VECTOR4_H -#include "mconfig.h" +#include +#include + +#if defined(__x86_64__) || defined(__amd64__) || defined(_M_X64) + #define SIMD_X86 + #define SIMD_ENABLE + #include + +#elif defined(__aarch64__) || defined(__arm64__) || defined(_M_ARM64) + #define SIMD_ARCH + #define SIMD_ENABLE + #include + +#else + #define SIMD_NONE +#endif + +#ifdef _MSC_VER + #define ALIGN16 __declspec(align(16)) +#else + #define ALIGN16 __attribute__((aligned(16))) +#endif #define VEC_SIZE 4 + + // must be aligned by 16 Bytes (less instruction executed for SSE) typedef union { @@ -54,4 +77,6 @@ Vec4f_t vec4f_refl(Vec4f_t v, Vec4f_t normal); float vec4f_dist(Vec4f_t a, Vec4f_t b); +#undef VEC_SIZE + #endif // VECTOR4_H diff --git a/tests/tests_config.c b/tests/tests_config.c index ab10c51..2f9ab8c 100644 --- a/tests/tests_config.c +++ b/tests/tests_config.c @@ -1,4 +1,4 @@ -#include "../src/math/mconfig.h" +#include "../src/math/mat4.h" #include void config() { diff --git a/tests/tests_vec3f.c b/tests/tests_vec3f.c index 10c444c..1ef9a54 100644 --- a/tests/tests_vec3f.c +++ b/tests/tests_vec3f.c @@ -7,7 +7,7 @@ #include #include -#include "../src/math/mconfig.h" + #include "../src/math/vec3.h" #include "../src/math/vec4.h"