feat: add c++ file and remove mconfig file

This commit is contained in:
2025-08-11 23:38:45 +02:00
parent bc9c9b2a94
commit 78a6edf84b
11 changed files with 120 additions and 88 deletions

View File

@@ -1,12 +1,20 @@
cmake_minimum_required(VERSION 3.10) cmake_minimum_required(VERSION 3.10)
project(t3dsr C) project(t3dsr C CXX)
set(CMAKE_C_STANDARD 17) set(CMAKE_C_STANDARD 17)
set(CMAKE_CXX_STANDARD 17)
file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS src/*.c) # ==== sources ====
file(GLOB_RECURSE HEADERS CONFIGURE_DEPENDS src/*.h) 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) if(NOT MSVC)
target_link_libraries(main m) target_link_libraries(main m)
endif() endif()
@@ -14,13 +22,11 @@ endif()
# ==== tests ==== # ==== tests ====
enable_testing() enable_testing()
file(GLOB_RECURSE TEST_SOURCES CONFIGURE_DEPENDS tests/*.c) file(GLOB_RECURSE TEST_SOURCES CONFIGURE_DEPENDS tests/*.c)
list(FILTER SOURCES EXCLUDE REGEX ".*src/main\\.c$")
foreach(test_src ${TEST_SOURCES}) foreach(test_src ${TEST_SOURCES})
get_filename_component(test_name ${test_src} NAME_WE) 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) if(NOT MSVC)
target_link_libraries(${test_name} m) target_link_libraries(${test_name} m)
endif() endif()

View File

@@ -1,36 +0,0 @@
#include <stdio.h>
#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;
}

13
src/main.cpp Normal file
View File

@@ -0,0 +1,13 @@
#include <iostream>
extern "C"
{
#include "math/mat4.h"
#include "math/vec3.h"
#include "math/vec4.h"
}
int main()
{
return 0;
}

View File

@@ -300,4 +300,22 @@ Mat4f_t mat4f_tpo(const Mat4f_t *restrict m)
Mat4f_t res = mat4f_clone(m); Mat4f_t res = mat4f_clone(m);
mat4f_tpo_r(&res); mat4f_tpo_r(&res);
return res; return res;
} }
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;
}

View File

@@ -1,7 +1,22 @@
#ifndef MATRIX4_H #ifndef MATRIX4_H
#define MATRIX4_H #define MATRIX4_H
#include "mconfig.h" #if defined(__x86_64__) || defined(__amd64__) || defined(_M_X64)
#define SIMD_X86
#include <xmmintrin.h>
#elif defined(__aarch64__) || defined(__arm64__) || defined(_M_ARM64)
#define SIMD_ARCH
#include <arm_neon.h>
#else
#endif
#ifdef _MSC_VER
#define ALIGN16 __declspec(align(16))
#else
#define ALIGN16 __attribute__((aligned(16)))
#endif
#define MAT_SIZE 16 #define MAT_SIZE 16
#define MAT_DIM 4 #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(const Mat4f_t *__restrict m);
Mat4f_t* mat4f_tpo_r(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 #endif // MATRIX4_H

View File

@@ -1,32 +0,0 @@
// Common math library
#ifndef MCONFIG_H
#define MCONFIG_H
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <float.h>
#if defined(__x86_64__) || defined(__amd64__) || defined(_M_X64)
#define SIMD_X86
#define SIMD_ENABLE
#include <xmmintrin.h>
#elif defined(__aarch64__) || defined(__arm64__) || defined(_M_ARM64)
#define SIMD_ARCH
#define SIMD_ENABLE
#include <arm_neon.h>
#else
#define SIMD_NONE
#endif
#ifdef _MSC_VER
#define ALIGN16 __declspec(align(16))
#else
#define ALIGN16 __attribute__((aligned(16)))
#endif
#endif // MCONFIG_H

View File

@@ -1,7 +1,5 @@
#include "vec3.h" #include "vec3.h"
#define VEC_SIZE 4
Vec3f_t vec3f(float x, float y, float z) Vec3f_t vec3f(float x, float y, float z)
{ {
Vec3f_t vec = {.x = x, .y = y, .z = z}; Vec3f_t vec = {.x = x, .y = y, .z = z};

View File

@@ -8,11 +8,33 @@
#ifndef vec3_h #ifndef vec3_h
#define vec3_h #define vec3_h
#include "mconfig.h" #include <math.h>
#include <float.h>
typedef union #if defined(__x86_64__) || defined(__amd64__) || defined(_M_X64)
{ #define SIMD_X86
struct {float x, y, z; }; #define SIMD_ENABLE
#include <xmmintrin.h>
#elif defined(__aarch64__) || defined(__arm64__) || defined(_M_ARM64)
#define SIMD_ARCH
#define SIMD_ENABLE
#include <arm_neon.h>
#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]; float data[4];
} ALIGN16 Vec3f_t; } 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); float vec3f_dist(Vec3f_t a, Vec3f_t b);
#undef VEC_SIZE
#endif /* vec3_h */ #endif /* vec3_h */

View File

@@ -1,10 +1,33 @@
#ifndef VECTOR4_H #ifndef VECTOR4_H
#define VECTOR4_H #define VECTOR4_H
#include "mconfig.h" #include <math.h>
#include <float.h>
#if defined(__x86_64__) || defined(__amd64__) || defined(_M_X64)
#define SIMD_X86
#define SIMD_ENABLE
#include <xmmintrin.h>
#elif defined(__aarch64__) || defined(__arm64__) || defined(_M_ARM64)
#define SIMD_ARCH
#define SIMD_ENABLE
#include <arm_neon.h>
#else
#define SIMD_NONE
#endif
#ifdef _MSC_VER
#define ALIGN16 __declspec(align(16))
#else
#define ALIGN16 __attribute__((aligned(16)))
#endif
#define VEC_SIZE 4 #define VEC_SIZE 4
// must be aligned by 16 Bytes (less instruction executed for SSE) // must be aligned by 16 Bytes (less instruction executed for SSE)
typedef union 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); float vec4f_dist(Vec4f_t a, Vec4f_t b);
#undef VEC_SIZE
#endif // VECTOR4_H #endif // VECTOR4_H

View File

@@ -1,4 +1,4 @@
#include "../src/math/mconfig.h" #include "../src/math/mat4.h"
#include <stdio.h> #include <stdio.h>
void config() { void config() {

View File

@@ -7,7 +7,7 @@
#include <assert.h> #include <assert.h>
#include <stdio.h> #include <stdio.h>
#include "../src/math/mconfig.h"
#include "../src/math/vec3.h" #include "../src/math/vec3.h"
#include "../src/math/vec4.h" #include "../src/math/vec4.h"