some progress

This commit is contained in:
veclavtlica
2026-10-07 11:21:21 +03:00
parent 14062e9d00
commit 8d502173a2
26 changed files with 705 additions and 212 deletions
+76 -84
View File
@@ -51,12 +51,7 @@ com_fix_t com_fix_sqrt(com_fix_t a) {
if (shift & 1)
shift -= 1;
uint32_t normalized_a;
if (shift > 0) {
normalized_a = a >> shift;
} else {
normalized_a = a << (-shift);
}
uint32_t normalized_a = shift > 0 ? a >> shift : a << (-shift);
// 3. LUT Lookup using 4 MSBs of the normalized value
// Extracted index corresponds to the interval [0.5, 2.0)
@@ -72,11 +67,8 @@ com_fix_t com_fix_sqrt(com_fix_t a) {
// 5. Denormalize back to the target scale: result = x * 2^(shift / 2)
int32_t final_shift = shift / 2;
if (final_shift > 0) {
return (int32_t)(x << final_shift);
} else {
return (int32_t)(x >> (-final_shift));
}
return final_shift > 0 ? (int32_t)(x << final_shift)
: (int32_t)(x >> (-final_shift));
}
void com_fix_print(com_fix_t a) {
@@ -97,21 +89,21 @@ void com_fix_run_bench(void) {
uint64_t start = com_timer_count_ns();
for (int i = 5000000; i--;) {
com_fix_t sqrt = com_fix_sqrt(i);
COM_DEF_PROFILE_SINK(sqrt);
com_def_profile_sink(sqrt);
}
com_timer_profile(start, "fixed: sqrt");
start = com_timer_count_ns();
for (int i = 50000000; i--;) {
com_fix_t sin = com_fix_sin(i);
COM_DEF_PROFILE_SINK(sin);
com_def_profile_sink(sin);
}
com_timer_profile(start, "fixed: sin");
start = com_timer_count_ns();
for (int i = 50000000; i--;) {
com_fix_t cos = com_fix_cos(i);
COM_DEF_PROFILE_SINK(cos);
com_def_profile_sink(cos);
}
com_timer_profile(start, "fixed: cos");
@@ -121,82 +113,82 @@ void com_fix_run_bench(void) {
com_fix_sincos(i, &sin, &cos);
com_fix_t sinv = sin;
com_fix_t cosv = cos;
COM_DEF_PROFILE_SINK(sinv);
COM_DEF_PROFILE_SINK(cosv);
com_def_profile_sink(sinv);
com_def_profile_sink(cosv);
}
com_timer_profile(start, "fixed: sincos");
}
void com_fix_run_tests(void) {
// {
// double max_sqrt_deviation = 0.0f;
// com_fix_t sqrt_accumulator = 0;
// com_fix_t sqrt_test_step = COM_FIX_FRACUNIT >> 2; /* Quarter step */
// for (int i = 0; i < COM_FIX_FRACUNIT << 1; ++i) {
// com_fix_t sqrt = com_fix_sqrt(sqrt_accumulator);
// double const deviation =
// com_std_sqrt(com_fix_as_float(sqrt_accumulator)) -
// com_fix_as_float(sqrt);
// if (deviation > max_sqrt_deviation)
// max_sqrt_deviation = deviation;
// sqrt_accumulator += sqrt_test_step;
// }
// com_std_printf("max sqrt deviation: %f\n", max_sqrt_deviation);
// }
{
double max_sqrt_deviation = 0.0f;
com_fix_t sqrt_accumulator = 0;
com_fix_t sqrt_test_step = COM_FIX_FRACUNIT >> 2; /* Quarter step */
for (int i = 0; i < COM_FIX_FRACUNIT << 1; ++i) {
com_fix_t sqrt = com_fix_sqrt(sqrt_accumulator);
double const deviation =
com_std_sqrt(com_fix_as_float(sqrt_accumulator)) -
com_fix_as_float(sqrt);
if (deviation > max_sqrt_deviation)
max_sqrt_deviation = deviation;
sqrt_accumulator += sqrt_test_step;
}
com_std_printf("max sqrt deviation: %f\n", max_sqrt_deviation);
}
// {
// double max_sin_deviation = 0.0f;
// com_fix_t sin_accumulator = -COM_FIX_PI * 4;
// com_fix_t sin_test_step = (COM_FIX_PI << 1) >> 10;
// for (int i = 0; i < 1024 * 32; ++i) {
// com_fix_t sin = com_fix_sin(sin_accumulator);
// double const deviation =
// com_std_fabs(com_std_sin(com_fix_as_float(sin_accumulator)) -
// com_fix_as_float(sin));
// if (deviation > max_sin_deviation)
// max_sin_deviation = deviation;
// sin_accumulator += sin_test_step;
// }
// com_std_printf("max sin deviation: %f\n", max_sin_deviation);
// }
{
double max_sin_deviation = 0.0f;
com_fix_t sin_accumulator = -COM_FIX_PI * 4;
com_fix_t sin_test_step = (COM_FIX_PI << 1) >> 10;
for (int i = 0; i < 1024 * 32; ++i) {
com_fix_t sin = com_fix_sin(sin_accumulator);
double const deviation =
com_std_fabs(com_std_sin(com_fix_as_float(sin_accumulator)) -
com_fix_as_float(sin));
if (deviation > max_sin_deviation)
max_sin_deviation = deviation;
sin_accumulator += sin_test_step;
}
com_std_printf("max sin deviation: %f\n", max_sin_deviation);
}
// {
// double max_cos_deviation = 0.0f;
// com_fix_t cos_accumulator = -COM_FIX_PI * 4;
// com_fix_t cos_test_step = (COM_FIX_PI << 1) >> 10;
// for (int i = 0; i < 1024 * 32; ++i) {
// com_fix_t cos = com_fix_cos(cos_accumulator);
// double const deviation =
// com_std_fabs(com_std_cos(com_fix_as_float(cos_accumulator)) -
// com_fix_as_float(cos));
// if (deviation > max_cos_deviation)
// max_cos_deviation = deviation;
// cos_accumulator += cos_test_step;
// }
// com_std_printf("max cos deviation: %f\n", max_cos_deviation);
// }
{
double max_cos_deviation = 0.0f;
com_fix_t cos_accumulator = -COM_FIX_PI * 4;
com_fix_t cos_test_step = (COM_FIX_PI << 1) >> 10;
for (int i = 0; i < 1024 * 32; ++i) {
com_fix_t cos = com_fix_cos(cos_accumulator);
double const deviation =
com_std_fabs(com_std_cos(com_fix_as_float(cos_accumulator)) -
com_fix_as_float(cos));
if (deviation > max_cos_deviation)
max_cos_deviation = deviation;
cos_accumulator += cos_test_step;
}
com_std_printf("max cos deviation: %f\n", max_cos_deviation);
}
// {
// double max_sin_deviation = 0.0f;
// double max_cos_deviation = 0.0f;
// com_fix_t sincos_accumulator = -COM_FIX_PI * 4;
// com_fix_t sincos_test_step = (COM_FIX_PI << 1) >> 10;
// for (int i = 0; i < 1024 * 32; ++i) {
// com_fix_t sin, cos;
// com_fix_sincos(sincos_accumulator, &sin, &cos);
// double const sin_deviation =
// com_std_fabs(com_std_sin(com_fix_as_float(sincos_accumulator)) -
// com_fix_as_float(sin));
// double const cos_deviation =
// com_std_fabs(com_std_cos(com_fix_as_float(sincos_accumulator)) -
// com_fix_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;
// }
// com_std_printf("max sincos deviations: %f %f\n", max_sin_deviation,
// max_cos_deviation);
// }
{
double max_sin_deviation = 0.0f;
double max_cos_deviation = 0.0f;
com_fix_t sincos_accumulator = -COM_FIX_PI * 4;
com_fix_t sincos_test_step = (COM_FIX_PI << 1) >> 10;
for (int i = 0; i < 1024 * 32; ++i) {
com_fix_t sin, cos;
com_fix_sincos(sincos_accumulator, &sin, &cos);
double const sin_deviation =
com_std_fabs(com_std_sin(com_fix_as_float(sincos_accumulator)) -
com_fix_as_float(sin));
double const cos_deviation =
com_std_fabs(com_std_cos(com_fix_as_float(sincos_accumulator)) -
com_fix_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;
}
com_std_printf("max sincos deviations: %f %f\n", max_sin_deviation,
max_cos_deviation);
}
}