Рефакторинг и оптимизация вычислений / Refactoring and optimization of computations

This commit is contained in:
Andrey Pokidov 2024-11-26 02:25:04 +07:00
parent 03e390c1d0
commit 2655e43cb4
15 changed files with 810 additions and 829 deletions

View file

@ -52,7 +52,7 @@ BgFP32Versor * make_random_versors(const unsigned int amount)
void print_versor(const BgFP32Versor* versor)
{
printf("(%f, %f, %f, %f)\n", versor->s0, versor->x1, versor->x2, versor->x3);
printf("Versor (%f, %f, %f, %f); Delta = %e\n", versor->s0, versor->x1, versor->x2, versor->x3, bg_fp32_versor_get_modulus(versor) - 1.0f);
}
void print_vector(const BgFP32Vector3* vector)
@ -125,11 +125,11 @@ int main()
const unsigned int amount = 1000000;
#ifdef _WIN64
ULONGLONG now;
ULONGLONG now, start, end;
now = GetTickCount64();
srand((unsigned int)(now & 0xfffffff));
#else
struct timespec now;
struct timespec now, start, end;
clock_gettime(0, &now);
srand((unsigned int)(now.tv_nsec & 0xfffffff));
#endif // _WIN64
@ -159,10 +159,14 @@ int main()
}
#ifdef _WIN64
ULONGLONG start, end;
end = GetTickCount64();
printf("Setup time: %lld\n", end - now);
start = GetTickCount64();
#else
struct timespec start, end;
clock_gettime(CLOCK_REALTIME, &end);
printf("Time: %lf\n", (end.tv_sec - now.tv_sec) * 1000.0 + (end.tv_nsec - now.tv_nsec) * 0.000001);
clock_gettime(CLOCK_REALTIME, &start);
#endif // _WIN64
for (int j = 0; j < 1000; j++) {

View file

@ -1 +1,105 @@
#include "matrix2x2.h"
// ================= Inversion ================== //
int bg_fp32_matrix2x2_invert(BgFP32Matrix2x2* matrix)
{
const float determinant = bg_fp32_matrix2x2_get_determinant(matrix);
if (-BG_FP32_EPSYLON <= determinant && determinant <= BG_FP32_EPSYLON) {
return 0;
}
const float r1c1 = matrix->r2c2;
const float r1c2 = -matrix->r1c2;
const float r2c1 = -matrix->r2c1;
const float r2c2 = matrix->r1c1;
const float multiplier = 1.0f / determinant;
matrix->r1c1 = r1c1 * multiplier;
matrix->r1c2 = r1c2 * multiplier;
matrix->r2c1 = r2c1 * multiplier;
matrix->r2c2 = r2c2 * multiplier;
return 1;
}
int bg_fp64_matrix2x2_invert(BgFP64Matrix2x2* matrix)
{
const double determinant = bg_fp64_matrix2x2_get_determinant(matrix);
if (-BG_FP64_EPSYLON <= determinant && determinant <= BG_FP64_EPSYLON) {
return 0;
}
const double r1c1 = matrix->r2c2;
const double r1c2 = -matrix->r1c2;
const double r2c1 = -matrix->r2c1;
const double r2c2 = matrix->r1c1;
const double multiplier = 1.0 / determinant;
matrix->r1c1 = r1c1 * multiplier;
matrix->r1c2 = r1c2 * multiplier;
matrix->r2c1 = r2c1 * multiplier;
matrix->r2c2 = r2c2 * multiplier;
return 1;
}
// ================ Set Inverted ================ //
int bg_fp32_matrix2x2_set_inverted(const BgFP32Matrix2x2* from, BgFP32Matrix2x2* to)
{
const float determinant = bg_fp32_matrix2x2_get_determinant(from);
if (-BG_FP32_EPSYLON <= determinant && determinant <= BG_FP32_EPSYLON) {
return 0;
}
const float r1c1 = from->r2c2;
const float r1c2 = -from->r1c2;
const float r2c1 = -from->r2c1;
const float r2c2 = from->r1c1;
const float multiplier = 1.0f / determinant;
to->r1c1 = r1c1 * multiplier;
to->r1c2 = r1c2 * multiplier;
to->r2c1 = r2c1 * multiplier;
to->r2c2 = r2c2 * multiplier;
return 1;
}
int bg_fp64_matrix2x2_set_inverted(const BgFP64Matrix2x2* from, BgFP64Matrix2x2* to)
{
const double determinant = bg_fp64_matrix2x2_get_determinant(from);
if (-BG_FP64_EPSYLON <= determinant && determinant <= BG_FP64_EPSYLON) {
return 0;
}
const double r1c1 = from->r2c2;
const double r1c2 = -from->r1c2;
const double r2c1 = -from->r2c1;
const double r2c2 = from->r1c1;
const double multiplier = 1.0 / determinant;
to->r1c1 = r1c1 * multiplier;
to->r1c2 = r1c2 * multiplier;
to->r2c1 = r2c1 * multiplier;
to->r2c2 = r2c2 * multiplier;
return 1;
}

View file

Internal server error - Personal Git Server: Beyond coding. We Forge.

500

Internal server error

Forgejo version: 11.0.1+gitea-1.22.0

@ -171,51 +171,9 @@ static inline void bg_fp64_matrix2x2_transpose(BgFP64Matrix2x2* matrix)
// ================= Inversion ================== //
static inline int bg_fp32_matrix2x2_invert(BgFP32Matrix2x2* matrix)
{
const float determinant = bg_fp32_matrix2x2_get_determinant(matrix);
int bg_fp32_matrix2x2_invert(BgFP32Matrix2x2* matrix);
if (-BG_FP32_EPSYLON <= determinant && determinant <= BG_FP32_EPSYLON) {
return 0;
}
const float r1c1 = matrix->r2c2;
const float r1c2 = -matrix->r1c2;
const float r2c1 = -matrix->r2c1;
const float r2c2 = matrix->r1c1;
matrix->r1c1 = r1c1 / determinant;
matrix->r1c2 = r1c2 / determinant;
matrix->r2c1 = r2c1 / determinant;
matrix->r2c2 = r2c2 / determinant;
return 1;
}
static inline int bg_fp64_matrix2x2_invert(BgFP64Matrix2x2* matrix)
{
const double determinant = bg_fp64_matrix2x2_get_determinant(matrix);
if (-BG_FP64_EPSYLON <= determinant && determinant <= BG_FP64_EPSYLON) {
return 0;
}
const double r1c1 = matrix->r2c2;
const double r1c2 = -matrix->r1c2;
const double r2c1 = -matrix->r2c1;
const double r2c2 = matrix->r1c1;
matrix->r1c1 = r1c1 / determinant;
matrix->r1c2 = r1c2 / determinant;
matrix->r2c1 = r2c1 / determinant;
matrix->r2c2 = r2c2 / determinant;
return 1;
}
int bg_fp64_matrix2x2_invert(BgFP64Matrix2x2* matrix);
// =============== Set Transposed =============== //
@ -243,51 +201,9 @@ static inline void bg_fp64_matrix2x2_set_transposed(const BgFP64Matrix2x2* from,
// ================ Set Inverted ================ //
static inline int bg_fp32_matrix2x2_set_inverted(const BgFP32Matrix2x2* from, BgFP32Matrix2x2* to)
{
const float determinant = bg_fp32_matrix2x2_get_determinant(from);
int bg_fp32_matrix2x2_set_inverted(const BgFP32Matrix2x2* from, BgFP32Matrix2x2* to);
if (-BG_FP32_EPSYLON <= determinant && determinant <= BG_FP32_EPSYLON) {
return 0;
}
const float r1c1 = from->r2c2;
const float r1c2 = -from->r1c2;
const float r2c1 = -from->r2c1;
const float r2c2 = from->r1c1;
to->r1c1 = r1c1 / determinant;
to->r1c2 = r1c2 / determinant;
to->r2c1 = r2c1 / determinant;
to->r2c2 = r2c2 / determinant;
return 1;
}