mirror of
https://github.com/guezoloic/t3dsr.git
synced 2026-01-25 06:34:23 +00:00
feat: add MAT_DIM to most mat4f_t functions
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
Mat4f_t mat4f_from_array(const float arr[16])
|
||||
{
|
||||
Mat4f_t mat;
|
||||
for(int i = 0; i<MAT_SIZE; i+=4) {
|
||||
for(int i = 0; i<MAT_SIZE; i+=MAT_DIM) {
|
||||
#if defined (SIMD_X86)
|
||||
__m128 line = _mm_load_ps(&arr[i]);
|
||||
_mm_store_ps(&mat.m[i], line);
|
||||
@@ -11,7 +11,7 @@ Mat4f_t mat4f_from_array(const float arr[16])
|
||||
float32x4_t line = vld1q_f32(&arr[i]);
|
||||
vst1q_f32(&mat.m[i], line);
|
||||
#else
|
||||
for(int j = 0; j<4; j++) {
|
||||
for(int j = 0; j<MAT_DIM; j++) {
|
||||
mat.m[i+j] = arr[i+j];
|
||||
}
|
||||
#endif
|
||||
@@ -22,7 +22,7 @@ Mat4f_t mat4f_from_array(const float arr[16])
|
||||
Mat4f_t mat4f_scalar(float f)
|
||||
{
|
||||
Mat4f_t mat;
|
||||
for(int i = 0; i<MAT_SIZE; i+=4) {
|
||||
for(int i = 0; i<MAT_SIZE; i+=MAT_DIM) {
|
||||
#if defined (SIMD_X86)
|
||||
__m128 line_scalar = _mm_set1_ps(f);
|
||||
_mm_store_ps(&mat.m[i], line_scalar);
|
||||
@@ -32,7 +32,7 @@ Mat4f_t mat4f_scalar(float f)
|
||||
vst1q_f32(&mat.m[i], line_scalar);
|
||||
|
||||
#else
|
||||
for(int j = 0; j<4; j++) {
|
||||
for(int j = 0; j<MAT_DIM; j++) {
|
||||
mat.m[i+j] = f;
|
||||
}
|
||||
#endif
|
||||
@@ -45,7 +45,7 @@ Mat4f_t mat4f_zero()
|
||||
#if defined (SIMD_X86)
|
||||
Mat4f_t mat;
|
||||
|
||||
for(int i = 0; i<MAT_SIZE; i+=4) {
|
||||
for(int i = 0; i<MAT_SIZE; i+=MAT_DIM) {
|
||||
__m128 line_zero = _mm_setzero_ps();
|
||||
_mm_store_ps(&mat.m[i], line_zero);
|
||||
}
|
||||
@@ -68,7 +68,7 @@ Mat4f_t mat4f_identity()
|
||||
|
||||
Mat4f_t* mat4f_add_r(Mat4f_t *out, const Mat4f_t *m2)
|
||||
{
|
||||
for(int i = 0; i<MAT_SIZE; i+=4) {
|
||||
for(int i = 0; i<MAT_SIZE; i+=MAT_DIM) {
|
||||
#if defined (SIMD_X86)
|
||||
__m128 ma = _mm_load_ps(&out->m[i]);
|
||||
__m128 mb = _mm_load_ps(&m2->m[i]);
|
||||
@@ -102,7 +102,7 @@ Mat4f_t mat4_add(const Mat4f_t* m1, const Mat4f_t* m2)
|
||||
|
||||
Mat4f_t* mat4f_sub_r(Mat4f_t *out, const Mat4f_t *m2)
|
||||
{
|
||||
for(int i = 0; i<MAT_SIZE; i+=4) {
|
||||
for(int i = 0; i<MAT_SIZE; i+=MAT_DIM) {
|
||||
#if defined (SIMD_X86)
|
||||
__m128 ma = _mm_load_ps(&out->m[i]);
|
||||
__m128 mb = _mm_load_ps(&m2->m[i]);
|
||||
@@ -136,7 +136,7 @@ Mat4f_t mat4_sub(const Mat4f_t* m1, const Mat4f_t* m2)
|
||||
|
||||
Mat4f_t* mat4f_scale_r(Mat4f_t *out, float scalar)
|
||||
{
|
||||
for(int i = 0; i<MAT_SIZE; i+=4) {
|
||||
for(int i = 0; i<MAT_SIZE; i+=MAT_DIM) {
|
||||
#if defined (SIMD_X86)
|
||||
__m128 ma = _mm_load_ps(&out->m[i]);
|
||||
__m128 mb = _mm_set1_ps(scalar);
|
||||
@@ -149,7 +149,7 @@ Mat4f_t* mat4f_scale_r(Mat4f_t *out, float scalar)
|
||||
float32x4_t mres = vmulq_f32(ma, mb);
|
||||
vst1q_f32(&out->m[i], mres);
|
||||
#else
|
||||
for(int j = 0; j<4; j++) {
|
||||
for(int j = 0; j<MAT_DIM; j++) {
|
||||
out->m[i+j] *= scalar;
|
||||
}
|
||||
#endif
|
||||
@@ -168,16 +168,16 @@ Mat4f_t* mat4f_mul_r(Mat4f_t* out, const Mat4f_t* m2)
|
||||
{
|
||||
Mat4f_t clone = mat4f_clone(out);
|
||||
|
||||
for (int row = 0; row<4; row++) {
|
||||
for (int row = 0; row<MAT_DIM; row++) {
|
||||
#if defined (SIMD_X86)
|
||||
__m128 mrow = _mm_load_ps(&clone.m[row * 4]);
|
||||
__m128 mrow = _mm_load_ps(&clone.m[row * MAT_DIM]);
|
||||
|
||||
for (int col = 0; col<4; col++) {
|
||||
for (int col = 0; col<MAT_DIM; col++) {
|
||||
__m128 mcol = _mm_set_ps(
|
||||
m2->m[3 * 4 + col],
|
||||
m2->m[2 * 4 + col],
|
||||
m2->m[1 * 4 + col],
|
||||
m2->m[0 * 4 + col]
|
||||
m2->m[3 * MAT_DIM + col],
|
||||
m2->m[2 * MAT_DIM + col],
|
||||
m2->m[1 * MAT_DIM + col],
|
||||
m2->m[0 * MAT_DIM + col]
|
||||
);
|
||||
__m128 mmul = _mm_mul_ps(mrow, mcol);
|
||||
|
||||
@@ -188,17 +188,17 @@ Mat4f_t* mat4f_mul_r(Mat4f_t* out, const Mat4f_t* m2)
|
||||
sum = _mm_add_ss(sum, shuf); // [x+y+z+w, y+y, z+w, w+w]
|
||||
float mres = _mm_cvtss_f32(sum);
|
||||
|
||||
out->m[row * 4 + col] = mres;
|
||||
out->m[row * MAT_DIM + col] = mres;
|
||||
}
|
||||
#elif defined (SIMD_ARCH)
|
||||
float32x4_t mrow = vld1q_f32(&clone.m[row*4]);
|
||||
float32x4_t mrow = vld1q_f32(&clone.m[row*MAT_DIM]);
|
||||
|
||||
for (int col = 0; col<4; col++) {
|
||||
for (int col = 0; col<MAT_DIM; col++) {
|
||||
float32x4_t mcol = {
|
||||
m2->m[0 * 4 + col],
|
||||
m2->m[1 * 4 + col],
|
||||
m2->m[2 * 4 + col],
|
||||
m2->m[3 * 4 + col]
|
||||
m2->m[0 * MAT_DIM + col],
|
||||
m2->m[1 * MAT_DIM + col],
|
||||
m2->m[2 * MAT_DIM + col],
|
||||
m2->m[3 * MAT_DIM + col]
|
||||
};
|
||||
|
||||
float32x4_t mmul = vmulq_f32(mrow, mcol);
|
||||
@@ -206,15 +206,15 @@ Mat4f_t* mat4f_mul_r(Mat4f_t* out, const Mat4f_t* m2)
|
||||
float32x2_t final_sum = vpadd_f32(sum_pair, sum_pair);
|
||||
|
||||
float mres = vget_lane_f32(final_sum, 0);
|
||||
out->m[row * 4 + col] = mres;
|
||||
out->m[row * MAT_DIM + col] = mres;
|
||||
}
|
||||
#else
|
||||
for (int col = 0; col < 4; col++) {
|
||||
for (int col = 0; col < MAT_DIM; col++) {
|
||||
float sum = 0.0f;
|
||||
for (int k = 0; k < 4; k++) {
|
||||
sum += clone.m[row * 4 + k] * m2->m[k * 4 + col];
|
||||
for (int k = 0; k < MAT_DIM; k++) {
|
||||
sum += clone.m[row * MAT_DIM + k] * m2->m[k * MAT_DIM + col];
|
||||
}
|
||||
out->m[row * 4 + col] = sum;
|
||||
out->m[row * MAT_DIM + col] = sum;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -226,3 +226,8 @@ Mat4f_t mat4_mul(const Mat4f_t* m1, const Mat4f_t* m2)
|
||||
mat4f_mul_r(&mout, m2);
|
||||
return mout;
|
||||
}
|
||||
|
||||
Mat4f_t* mat4_tpo_r(Mat4f_t *__restrict m)
|
||||
{
|
||||
return m;
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "mconfig.h"
|
||||
|
||||
#define MAT_SIZE 16
|
||||
#define MAT_DIM 4
|
||||
|
||||
typedef struct
|
||||
{
|
||||
@@ -33,7 +34,8 @@ Mat4f_t* mat4f_scale_r(Mat4f_t *out, float scalar);
|
||||
Mat4f_t mat4f_mul(const Mat4f_t* m1, const Mat4f_t* m2);
|
||||
Mat4f_t* mat4f_mul_r(Mat4f_t* out, const Mat4f_t* m2);
|
||||
|
||||
// Mat4_t mat4_tpo(const Mat4_t* m);
|
||||
Mat4f_t mat4_tpo(const Mat4f_t *__restrict m);
|
||||
Mat4f_t* mat4_tpo_r(Mat4f_t *__restrict m);
|
||||
|
||||
// float mat4_det(const Mat4_t* m);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user