feat: add mat4 init functions

This commit is contained in:
2025-07-04 06:41:15 +02:00
parent 8b14924da3
commit bdd4bd3e40
6 changed files with 96 additions and 38 deletions

View File

@@ -21,5 +21,8 @@ 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} ${SOURCES})
if(NOT MSVC)
target_link_libraries(${test_name} m)
endif()
add_test(NAME ${test_name} COMMAND ${test_name}) add_test(NAME ${test_name} COMMAND ${test_name})
endforeach() endforeach()

View File

@@ -1,7 +1,13 @@
#include <stdio.h> #include <stdio.h>
#include "math/vec4.h" #include "math/mat4.h"
int main(void) 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 mat2 = mat4f_scalar(1);
Mat4f_t mat3 = mat4f_zero();
Mat4f_t mat4 = mat4f_identity();
printf("%f", arr[1]);
return 0; return 0;
} }

View File

@@ -1,27 +1,69 @@
// #include "mat4.h" #include "mat4.h"
// #include "math.h"
// Mat4_t mat4(const float arr[16]) Mat4f_t mat4f_from_array(const float arr[16])
// { {
// Mat4_t mat; Mat4f_t mat;
// memcpy(mat.m, arr, 16*sizeof(float)); for(int i = 0; i<MAT_SIZE; i+=4) {
// return mat; #if defined (SIMD_X86)
// } __m128 line = _mm_load_ps(&arr[i]);
_mm_store_ps(&mat.m[i], line);
#elif defined (SIMD_ARCH)
float32x4_t line = vld1q_f32(&arr[i]);
vst1q_f32(&mat.m[i], line);
#else
for(int j = 0; j<4; j++) {
mat.m[i+j] = arr[i+j];
}
#endif
}
return mat;
}
// Mat4_t mat4_zro(void) Mat4f_t mat4f_scalar(float f)
// { {
// return (Mat4_t){0}; Mat4f_t mat;
// } for(int i = 0; i<MAT_SIZE; i+=4) {
#if defined (SIMD_X86)
__m128 line_scalar = _mm_set1_ps(f);
_mm_store_ps(&mat.m[i], line_scalar);
#elif defined (SIMD_ARCH)
float32x4_t line_scalar = vdupq_n_f32(f);
vst1q_f32(&mat.m[i], line_scalar);
#else
for(int j = 0; j<4; j++) {
mat.m[i+j] = f;
}
#endif
}
return mat;
}
Mat4f_t mat4f_zero()
{
#if defined (SIMD_X86)
Mat4f_t mat;
for(int i = 0; i<MAT_SIZE; i+=4) {
__m128 line_zero = _mm_setzero_ps();
_mm_store_ps(&mat.m[i], line_zero);
}
return mat;
#else
return mat4f_scalar(0.f);
#endif
}
Mat4f_t mat4f_identity()
{
Mat4f_t mat = mat4f_zero();
mat.m[0] = 1.f;
mat.m[5] = 1.f;
mat.m[10] = 1.f;
mat.m[15] = 1.f;
return mat;
}
// Mat4_t mat4_ity(void)
// {
// return (Mat4_t) {{
// 1, 0, 0, 0,
// 0, 1, 0, 0,
// 0, 0, 1, 0,
// 0, 0, 0, 1,
// }};
// }
// Mat4_t mat4_add(const Mat4_t* m1, const Mat4_t* m2) // Mat4_t mat4_add(const Mat4_t* m1, const Mat4_t* m2)
// { // {

View File

@@ -1,30 +1,37 @@
#ifndef MATRIX4_H #ifndef MATRIX4_H
#define MATRIX4_H #define MATRIX4_H
#include "mconfig.h"
#define MAT_SIZE 16
typedef struct typedef struct
{ {
float m[16]; float m[MAT_SIZE];
} Mat4_t; } ALIGN16 Mat4f_t;
Mat4_t mat4(const float arr[16]); Mat4f_t mat4f_from_array(const float arr[16]);
Mat4f_t mat4f_scalar(float f);
Mat4f_t mat4f_zero(void);
Mat4f_t mat4f_identity(void);
Mat4_t mat4_zro(void); // Mat4_t mat4_zro(void);
Mat4_t mat4_ity(void); // Mat4_t mat4_ity(void);
Mat4_t mat4_add(const Mat4_t* m1, const Mat4_t* m2); // Mat4_t mat4_add(const Mat4_t* m1, const Mat4_t* m2);
Mat4_t mat4_sub(const Mat4_t* m1, const Mat4_t* m2); // Mat4_t mat4_sub(const Mat4_t* m1, const Mat4_t* m2);
Mat4_t mat4_scl(const Mat4_t* m, float scalar); // Mat4_t mat4_scl(const Mat4_t* m, float scalar);
// row * col // // row * col
Mat4_t mat4_mul(const Mat4_t* m1, const Mat4_t* m2); // Mat4_t mat4_mul(const Mat4_t* m1, const Mat4_t* m2);
Mat4_t mat4_tpo(const Mat4_t* m); // Mat4_t mat4_tpo(const Mat4_t* m);
float mat4_det(const Mat4_t* m); // float mat4_det(const Mat4_t* m);
Mat4_t mat4_inv(const Mat4_t* m); // Mat4_t mat4_inv(const Mat4_t* m);
#endif // MATRIX4_H #endif // MATRIX4_H

View File

@@ -1,7 +1,5 @@
#include "vec4.h" #include "vec4.h"
#define VEC_SIZE 4
Vec4f_t vec4f(float x, float y, float z, float w) Vec4f_t vec4f(float x, float y, float z, float w)
{ {
return (Vec4f_t){.x = x, .y = y, .z = z, .w = w}; return (Vec4f_t){.x = x, .y = y, .z = z, .w = w};

View File

@@ -3,11 +3,13 @@
#include "mconfig.h" #include "mconfig.h"
#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
{ {
struct { float x, y, z, w; }; struct { float x, y, z, w; };
float data[4]; float data[VEC_SIZE];
} ALIGN16 Vec4f_t; } ALIGN16 Vec4f_t;
Vec4f_t vec4f_from_array(const float *__restrict val); Vec4f_t vec4f_from_array(const float *__restrict val);