benches, optimizations for sin and cos

This commit is contained in:
veclavtlica
2026-09-13 17:59:20 +03:00
parent 6d7fe7392a
commit 739237575a
8 changed files with 218 additions and 65 deletions
+98 -32
View File
@@ -4,6 +4,8 @@
*/
#include "fixed.h"
#include "Timer/timer.h"
#include "def.h"
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
@@ -16,6 +18,7 @@ static const uint32_t com_fixed_sqrt_lut[16] = {
46340, 49547, 52521, 55314, 57954, 60464, 62862, 65161,
67373, 69508, 71572, 73572, 75514, 77402, 79240, 81033};
/* TODO: Test performance of uint_fast16_t here. */
/* Used for other quadrants as well as cosine eval, all from the same table. */
/* Only fractional part is present, reducing the cache footprint. */
const uint16_t com_fixed_sin_lut[128] = {
@@ -92,39 +95,102 @@ void com_fixed_print(com_fixed_t a) {
printf("%d.%04u\n", int_part, decimal_val);
}
void com_fixed_run_tests(void) {
com_fixed_print(com_fixed_sqrt(COM_FIXED_FRACUNIT * 2));
com_fixed_print(com_fixed_sqrt(COM_FIXED_FRACUNIT * 3));
com_fixed_print(com_fixed_sqrt(COM_FIXED_FRACUNIT * 4));
com_fixed_print(com_fixed_sqrt(COM_FIXED_FRACUNIT * 200));
com_fixed_print(com_fixed_sqrt(COM_FIXED_FRACUNIT * 1000));
com_fixed_print(COM_FIXED_PI);
com_fixed_print(com_fixed_div(COM_FIXED_PI + 234, COM_FIXED_PI2) << 2);
double max_sin_deviation = 0.0f;
com_fixed_t sin_accumulator = -COM_FIXED_PI * 4;
com_fixed_t sin_test_step = (COM_FIXED_PI << 1) >> 10;
for (int i = 0; i < 1024 * 32; ++i) {
com_fixed_t sin = com_fixed_sin(sin_accumulator);
double deviation = fabs(sin(com_fixed_as_float(sin_accumulator)) -
com_fixed_as_float(sin));
if (deviation > max_sin_deviation)
max_sin_deviation = deviation;
sin_accumulator += sin_test_step;
void com_fixed_run_bench(void) {
uint64_t start = com_timer_count_ns();
for (int i = 5000000; i--;) {
COM_DEF_PROFILE_VAR com_fixed_t sqrt = com_fixed_sqrt(i);
}
printf("max sin deviation: %f\n", max_sin_deviation);
com_timer_profile(start, "fixed: sqrt");
double max_cos_deviation = 0.0f;
com_fixed_t cos_accumulator = -COM_FIXED_PI * 4;
com_fixed_t cos_test_step = (COM_FIXED_PI << 1) >> 10;
for (int i = 0; i < 1024 * 32; ++i) {
com_fixed_t cos = com_fixed_cos(cos_accumulator);
double deviation = fabs(cos(com_fixed_as_float(cos_accumulator)) -
com_fixed_as_float(cos));
if (deviation > max_cos_deviation)
max_cos_deviation = deviation;
cos_accumulator += cos_test_step;
start = com_timer_count_ns();
for (int i = 50000000; i--;) {
COM_DEF_PROFILE_VAR com_fixed_t sin = com_fixed_sin(i);
}
com_timer_profile(start, "fixed: sin");
start = com_timer_count_ns();
for (int i = 50000000; i--;) {
COM_DEF_PROFILE_VAR com_fixed_t cos = com_fixed_cos(i);
}
com_timer_profile(start, "fixed: cos");
start = com_timer_count_ns();
for (int i = 50000000; i--;) {
com_fixed_t sin, cos;
com_fixed_sincos(i, &sin, &cos);
COM_DEF_PROFILE_VAR com_fixed_t sinv = sin;
COM_DEF_PROFILE_VAR com_fixed_t cosv = cos;
}
com_timer_profile(start, "fixed: sincos");
}
void com_fixed_run_tests(void) {
{
double max_sqrt_deviation = 0.0f;
com_fixed_t sqrt_accumulator = 0;
com_fixed_t sqrt_test_step = COM_FIXED_FRACUNIT >> 2; /* Quarter step */
for (int i = 0; i < COM_FIXED_FRACUNIT << 1; ++i) {
com_fixed_t sqrt = com_fixed_sqrt(sqrt_accumulator);
double const deviation =
sqrt(com_fixed_as_float(sqrt_accumulator)) - com_fixed_as_float(sqrt);
if (deviation > max_sqrt_deviation)
max_sqrt_deviation = deviation;
sqrt_accumulator += sqrt_test_step;
}
printf("max sqrt deviation: %f\n", max_sqrt_deviation);
}
{
double max_sin_deviation = 0.0f;
com_fixed_t sin_accumulator = -COM_FIXED_PI * 4;
com_fixed_t sin_test_step = (COM_FIXED_PI << 1) >> 10;
for (int i = 0; i < 1024 * 32; ++i) {
com_fixed_t sin = com_fixed_sin(sin_accumulator);
double const deviation = fabs(sin(com_fixed_as_float(sin_accumulator)) -
com_fixed_as_float(sin));
if (deviation > max_sin_deviation)
max_sin_deviation = deviation;
sin_accumulator += sin_test_step;
}
printf("max sin deviation: %f\n", max_sin_deviation);
}
{
double max_cos_deviation = 0.0f;
com_fixed_t cos_accumulator = -COM_FIXED_PI * 4;
com_fixed_t cos_test_step = (COM_FIXED_PI << 1) >> 10;
for (int i = 0; i < 1024 * 32; ++i) {
com_fixed_t cos = com_fixed_cos(cos_accumulator);
double const deviation = fabs(cos(com_fixed_as_float(cos_accumulator)) -
com_fixed_as_float(cos));
if (deviation > max_cos_deviation)
max_cos_deviation = deviation;
cos_accumulator += cos_test_step;
}
printf("max cos deviation: %f\n", max_cos_deviation);
}
{
double max_sin_deviation = 0.0f;
double max_cos_deviation = 0.0f;
com_fixed_t sincos_accumulator = -COM_FIXED_PI * 4;
com_fixed_t sincos_test_step = (COM_FIXED_PI << 1) >> 10;
for (int i = 0; i < 1024 * 32; ++i) {
com_fixed_t sin, cos;
com_fixed_sincos(sincos_accumulator, &sin, &cos);
double const sin_deviation =
fabs(sin(com_fixed_as_float(sincos_accumulator)) -
com_fixed_as_float(sin));
double const cos_deviation =
fabs(cos(com_fixed_as_float(sincos_accumulator)) -
com_fixed_as_float(cos));
if (sin_deviation > max_sin_deviation)
max_sin_deviation = sin_deviation;
if (cos_deviation > max_cos_deviation)
max_cos_deviation = cos_deviation;
sincos_accumulator += sincos_test_step;
}
printf("max sincos deviations: %f %f\n", max_sin_deviation,
max_cos_deviation);
}
printf("max cos deviation: %f\n", max_cos_deviation);
}