Рефакторинг и оптимизация вычислений / Refactoring and optimization of computations
This commit is contained in:
parent
03e390c1d0
commit
2655e43cb4
15 changed files with 810 additions and 829 deletions
|
|
@ -52,7 +52,7 @@ BgFP32Versor * make_random_versors(const unsigned int amount)
|
||||||
|
|
||||||
void print_versor(const BgFP32Versor* versor)
|
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)
|
void print_vector(const BgFP32Vector3* vector)
|
||||||
|
|
@ -125,11 +125,11 @@ int main()
|
||||||
const unsigned int amount = 1000000;
|
const unsigned int amount = 1000000;
|
||||||
|
|
||||||
#ifdef _WIN64
|
#ifdef _WIN64
|
||||||
ULONGLONG now;
|
ULONGLONG now, start, end;
|
||||||
now = GetTickCount64();
|
now = GetTickCount64();
|
||||||
srand((unsigned int)(now & 0xfffffff));
|
srand((unsigned int)(now & 0xfffffff));
|
||||||
#else
|
#else
|
||||||
struct timespec now;
|
struct timespec now, start, end;
|
||||||
clock_gettime(0, &now);
|
clock_gettime(0, &now);
|
||||||
srand((unsigned int)(now.tv_nsec & 0xfffffff));
|
srand((unsigned int)(now.tv_nsec & 0xfffffff));
|
||||||
#endif // _WIN64
|
#endif // _WIN64
|
||||||
|
|
@ -159,10 +159,14 @@ int main()
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _WIN64
|
#ifdef _WIN64
|
||||||
ULONGLONG start, end;
|
end = GetTickCount64();
|
||||||
|
printf("Setup time: %lld\n", end - now);
|
||||||
|
|
||||||
start = GetTickCount64();
|
start = GetTickCount64();
|
||||||
#else
|
#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);
|
clock_gettime(CLOCK_REALTIME, &start);
|
||||||
#endif // _WIN64
|
#endif // _WIN64
|
||||||
for (int j = 0; j < 1000; j++) {
|
for (int j = 0; j < 1000; j++) {
|
||||||
|
|
|
||||||
|
|
@ -1 +1,105 @@
|
||||||
#include "matrix2x2.h"
|
#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;
|
||||||
|
|
||||||