mirror of
https://github.com/guezoloic/t3dsr.git
synced 2026-01-25 12:34:24 +00:00
feat(mat4): add det function
This commit is contained in:
118
src/math/mat4.c
118
src/math/mat4.c
@@ -1,68 +1,92 @@
|
|||||||
#include "mat4.h"
|
#include "mat4.h"
|
||||||
|
|
||||||
|
Mat4f_t* mat4f_from_array_r(Mat4f_t *__restrict m, const float arr[16])
|
||||||
|
{
|
||||||
|
for(int i = 0; i<MAT_SIZE; i+=MAT_DIM) {
|
||||||
|
#if defined (SIMD_X86)
|
||||||
|
__m128 line = _mm_load_ps(&arr[i]);
|
||||||
|
_mm_store_ps(&m->m[i], line);
|
||||||
|
#elif defined (SIMD_ARCH)
|
||||||
|
float32x4_t line = vld1q_f32(&arr[i]);
|
||||||
|
vst1q_f32(&m->m[i], line);
|
||||||
|
#else
|
||||||
|
for(int j = 0; j<MAT_DIM; j++) {
|
||||||
|
m->m[i+j] = arr[i+j];
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
Mat4f_t mat4f_from_array(const float arr[16])
|
Mat4f_t mat4f_from_array(const float arr[16])
|
||||||
{
|
{
|
||||||
Mat4f_t mat;
|
Mat4f_t mat;
|
||||||
|
mat4f_from_array_r(&mat, arr);
|
||||||
|
return mat;
|
||||||
|
}
|
||||||
|
|
||||||
|
Mat4f_t* mat4f_scalar_r(Mat4f_t *__restrict m, float f)
|
||||||
|
{
|
||||||
for(int i = 0; i<MAT_SIZE; i+=MAT_DIM) {
|
for(int i = 0; i<MAT_SIZE; i+=MAT_DIM) {
|
||||||
#if defined (SIMD_X86)
|
#if defined (SIMD_X86)
|
||||||
__m128 line = _mm_load_ps(&arr[i]);
|
__m128 line_scalar = _mm_set1_ps(f);
|
||||||
_mm_store_ps(&mat.m[i], line);
|
_mm_store_ps(&m->m[i], line_scalar);
|
||||||
|
|
||||||
#elif defined (SIMD_ARCH)
|
#elif defined (SIMD_ARCH)
|
||||||
float32x4_t line = vld1q_f32(&arr[i]);
|
float32x4_t line_scalar = vdupq_n_f32(f);
|
||||||
vst1q_f32(&mat.m[i], line);
|
vst1q_f32(&m->m[i], line_scalar);
|
||||||
|
|
||||||
#else
|
#else
|
||||||
for(int j = 0; j<MAT_DIM; j++) {
|
for(int j = 0; j<MAT_DIM; j++) {
|
||||||
mat.m[i+j] = arr[i+j];
|
m->m[i+j] = f;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
return mat;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
Mat4f_t mat4f_scalar(float f)
|
Mat4f_t mat4f_scalar(float f)
|
||||||
{
|
{
|
||||||
Mat4f_t mat;
|
Mat4f_t mat;
|
||||||
for(int i = 0; i<MAT_SIZE; i+=MAT_DIM) {
|
mat4f_scalar_r(&mat, f);
|
||||||
#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<MAT_DIM; j++) {
|
|
||||||
mat.m[i+j] = f;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
return mat;
|
return mat;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Mat4f_t* mat4f_zero_r(Mat4f_t *__restrict m)
|
||||||
|
{
|
||||||
|
#if defined (SIMD_X86)
|
||||||
|
for(int i = 0; i<MAT_SIZE; i+=MAT_DIM) {
|
||||||
|
__m128 line_zero = _mm_setzero_ps();
|
||||||
|
_mm_store_ps(&m->m[i], line_zero);
|
||||||
|
}
|
||||||
|
|
||||||
|
return m;
|
||||||
|
#else
|
||||||
|
return mat4f_scalar_r(m, 0.f);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
Mat4f_t mat4f_zero()
|
Mat4f_t mat4f_zero()
|
||||||
{
|
{
|
||||||
#if defined (SIMD_X86)
|
|
||||||
Mat4f_t mat;
|
Mat4f_t mat;
|
||||||
|
mat4f_zero_r(&mat);
|
||||||
for(int i = 0; i<MAT_SIZE; i+=MAT_DIM) {
|
|
||||||
__m128 line_zero = _mm_setzero_ps();
|
|
||||||
_mm_store_ps(&mat.m[i], line_zero);
|
|
||||||
}
|
|
||||||
|
|
||||||
return mat;
|
return mat;
|
||||||
#else
|
}
|
||||||
return mat4f_scalar(0.f);
|
|
||||||
#endif
|
Mat4f_t* mat4f_identity_r(Mat4f_t *__restrict m)
|
||||||
|
{
|
||||||
|
mat4f_zero_r(m);
|
||||||
|
m->m[0] = 1.f;
|
||||||
|
m->m[5] = 1.f;
|
||||||
|
m->m[10] = 1.f;
|
||||||
|
m->m[15] = 1.f;
|
||||||
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
Mat4f_t mat4f_identity()
|
Mat4f_t mat4f_identity()
|
||||||
{
|
{
|
||||||
Mat4f_t mat = mat4f_zero();
|
Mat4f_t mat;
|
||||||
mat.m[0] = 1.f;
|
mat4f_identity_r(&mat);
|
||||||
mat.m[5] = 1.f;
|
|
||||||
mat.m[10] = 1.f;
|
|
||||||
mat.m[15] = 1.f;
|
|
||||||
return mat;
|
return mat;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,7 +158,7 @@ Mat4f_t mat4_sub(const Mat4f_t* m1, const Mat4f_t* m2)
|
|||||||
return mout;
|
return mout;
|
||||||
}
|
}
|
||||||
|
|
||||||
Mat4f_t* mat4f_scale_r(Mat4f_t *out, float scalar)
|
Mat4f_t* mat4f_scale_r(Mat4f_t *__restrict out, float scalar)
|
||||||
{
|
{
|
||||||
for(int i = 0; i<MAT_SIZE; i+=MAT_DIM) {
|
for(int i = 0; i<MAT_SIZE; i+=MAT_DIM) {
|
||||||
#if defined (SIMD_X86)
|
#if defined (SIMD_X86)
|
||||||
@@ -220,7 +244,8 @@ Mat4f_t* mat4f_mul_r(Mat4f_t* out, const Mat4f_t* m2)
|
|||||||
}
|
}
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
Mat4f_t mat4_mul(const Mat4f_t* m1, const Mat4f_t* m2)
|
|
||||||
|
Mat4f_t mat4f_mul(const Mat4f_t* m1, const Mat4f_t* m2)
|
||||||
{
|
{
|
||||||
Mat4f_t mout = mat4f_clone(m1);
|
Mat4f_t mout = mat4f_clone(m1);
|
||||||
mat4f_mul_r(&mout, m2);
|
mat4f_mul_r(&mout, m2);
|
||||||
@@ -304,13 +329,16 @@ Mat4f_t mat4f_tpo(const Mat4f_t *restrict m)
|
|||||||
|
|
||||||
float mat4f_det(const Mat4f_t* m)
|
float mat4f_det(const Mat4f_t* m)
|
||||||
{
|
{
|
||||||
float det;
|
const float* a = m->m;
|
||||||
return det;
|
|
||||||
}
|
|
||||||
|
|
||||||
Mat4f_t* mat4f_inv_r(Mat4f_t* __restrict m)
|
float det;
|
||||||
{
|
det =
|
||||||
return m;
|
a[0] * (a[5]*(a[10]*a[15] - a[11]*a[14]) - a[9]*(a[6]*a[15] - a[7]*a[14]) + a[13]*(a[6]*a[11] - a[7]*a[10])) -
|
||||||
|
a[4] * (a[1]*(a[10]*a[15] - a[11]*a[14]) - a[9]*(a[2]*a[15] - a[3]*a[14]) + a[13]*(a[2]*a[11] - a[3]*a[10])) +
|
||||||
|
a[8] * (a[1]*(a[6]*a[15] - a[7]*a[14]) - a[5]*(a[2]*a[15] - a[3]*a[14]) + a[13]*(a[2]*a[7] - a[3]*a[6])) -
|
||||||
|
a[12]* (a[1]*(a[6]*a[11] - a[7]*a[10]) - a[5]*(a[2]*a[11] - a[3]*a[10]) + a[9] *(a[2]*a[7] - a[3]*a[6]));
|
||||||
|
|
||||||
|
return det;
|
||||||
}
|
}
|
||||||
|
|
||||||
Mat4f_t mat4f_inv(const Mat4f_t* m)
|
Mat4f_t mat4f_inv(const Mat4f_t* m)
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
#ifndef MATRIX4_H
|
#ifndef MATRIX4_H
|
||||||
#define MATRIX4_H
|
#define MATRIX4_H
|
||||||
|
|
||||||
|
#include <math.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#if defined(__x86_64__) || defined(__amd64__) || defined(_M_X64)
|
#if defined(__x86_64__) || defined(__amd64__) || defined(_M_X64)
|
||||||
#define SIMD_X86
|
#define SIMD_X86
|
||||||
#include <xmmintrin.h>
|
#include <xmmintrin.h>
|
||||||
@@ -26,9 +29,16 @@ typedef struct
|
|||||||
float m[MAT_SIZE];
|
float m[MAT_SIZE];
|
||||||
} ALIGN16 Mat4f_t;
|
} ALIGN16 Mat4f_t;
|
||||||
|
|
||||||
|
Mat4f_t* mat4f_from_array_r(Mat4f_t *__restrict m, const float arr[16]);
|
||||||
Mat4f_t mat4f_from_array(const float arr[16]);
|
Mat4f_t mat4f_from_array(const float arr[16]);
|
||||||
|
|
||||||
|
Mat4f_t* mat4f_scalar_r(Mat4f_t *__restrict m, float f);
|
||||||
Mat4f_t mat4f_scalar(float f);
|
Mat4f_t mat4f_scalar(float f);
|
||||||
|
|
||||||
|
Mat4f_t* mat4f_zero_r(Mat4f_t *__restrict m);
|
||||||
Mat4f_t mat4f_zero(void);
|
Mat4f_t mat4f_zero(void);
|
||||||
|
|
||||||
|
Mat4f_t* mat4f_identity_r(Mat4f_t *__restrict m);
|
||||||
Mat4f_t mat4f_identity(void);
|
Mat4f_t mat4f_identity(void);
|
||||||
|
|
||||||
inline static Mat4f_t mat4f_clone(const Mat4f_t *__restrict out)
|
inline static Mat4f_t mat4f_clone(const Mat4f_t *__restrict out)
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
#include "../src/math/mat4.h"
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
void config() {
|
|
||||||
#if defined (SIMD_X86)
|
|
||||||
printf("SIMD enabled: X86\n");
|
|
||||||
#elif defined (SIMD_ARCH)
|
|
||||||
printf("SIMD enabled: ARCH\n");
|
|
||||||
#else
|
|
||||||
printf("SIMD disabled\n");
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
config();
|
|
||||||
}
|
|
||||||
@@ -9,7 +9,6 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "../src/math/vec3.h"
|
#include "../src/math/vec3.h"
|
||||||
#include "../src/math/vec4.h"
|
|
||||||
|
|
||||||
#define EPSILON 1e-6f
|
#define EPSILON 1e-6f
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user