refactor, libc made optional
This commit is contained in:
+21
-23
@@ -6,15 +6,10 @@
|
||||
#include "fix.h"
|
||||
#include "Timer/timer.h"
|
||||
#include "def.h"
|
||||
#include "std.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifndef COM_DEF_NOSTD
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <tgmath.h>
|
||||
#endif
|
||||
|
||||
// 4-bit LUT (16 entries) for the normalized range [0.5, 2.0)
|
||||
// It stores the initial guess scaled to Q16.16.
|
||||
static const uint32_t com_fix_sqrt_lut[16] = {
|
||||
@@ -40,7 +35,7 @@ const uint16_t com_fix_sin_lut[128] = {
|
||||
|
||||
com_fix_t com_fix_sqrt(com_fix_t a) {
|
||||
// 1. Handle sign and edge cases
|
||||
assert(a >= 0);
|
||||
com_std_assert(a >= 0);
|
||||
if (a == 0)
|
||||
return 0;
|
||||
|
||||
@@ -86,7 +81,7 @@ com_fix_t com_fix_sqrt(com_fix_t a) {
|
||||
|
||||
void com_fix_print(com_fix_t a) {
|
||||
if (a < 0) {
|
||||
printf("-");
|
||||
com_std_printf("-");
|
||||
a = -a;
|
||||
}
|
||||
int32_t int_part = a >> 16;
|
||||
@@ -95,7 +90,7 @@ void com_fix_print(com_fix_t a) {
|
||||
// Convert fractional 16-bit part to a decimal value (up to 4 decimal places)
|
||||
uint32_t decimal_val = (frac_part * 10000) >> 16;
|
||||
|
||||
printf("%d.%04u\n", int_part, decimal_val);
|
||||
com_std_printf("%d.%04u\n", int_part, decimal_val);
|
||||
}
|
||||
|
||||
void com_fix_run_bench(void) {
|
||||
@@ -133,7 +128,6 @@ void com_fix_run_bench(void) {
|
||||
}
|
||||
|
||||
void com_fix_run_tests(void) {
|
||||
#ifndef COM_DEF_NOSTD
|
||||
{
|
||||
double max_sqrt_deviation = 0.0f;
|
||||
com_fix_t sqrt_accumulator = 0;
|
||||
@@ -141,12 +135,13 @@ void com_fix_run_tests(void) {
|
||||
for (int i = 0; i < COM_FIX_FRACUNIT << 1; ++i) {
|
||||
com_fix_t sqrt = com_fix_sqrt(sqrt_accumulator);
|
||||
double const deviation =
|
||||
sqrt(com_fix_as_float(sqrt_accumulator)) - com_fix_as_float(sqrt);
|
||||
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;
|
||||
}
|
||||
printf("max sqrt deviation: %f\n", max_sqrt_deviation);
|
||||
com_std_printf("max sqrt deviation: %f\n", max_sqrt_deviation);
|
||||
}
|
||||
|
||||
{
|
||||
@@ -156,12 +151,13 @@ void com_fix_run_tests(void) {
|
||||
for (int i = 0; i < 1024 * 32; ++i) {
|
||||
com_fix_t sin = com_fix_sin(sin_accumulator);
|
||||
double const deviation =
|
||||
fabs(sin(com_fix_as_float(sin_accumulator)) - com_fix_as_float(sin));
|
||||
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;
|
||||
}
|
||||
printf("max sin deviation: %f\n", max_sin_deviation);
|
||||
com_std_printf("max sin deviation: %f\n", max_sin_deviation);
|
||||
}
|
||||
|
||||
{
|
||||
@@ -171,12 +167,13 @@ void com_fix_run_tests(void) {
|
||||
for (int i = 0; i < 1024 * 32; ++i) {
|
||||
com_fix_t cos = com_fix_cos(cos_accumulator);
|
||||
double const deviation =
|
||||
fabs(cos(com_fix_as_float(cos_accumulator)) - com_fix_as_float(cos));
|
||||
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;
|
||||
}
|
||||
printf("max cos deviation: %f\n", max_cos_deviation);
|
||||
com_std_printf("max cos deviation: %f\n", max_cos_deviation);
|
||||
}
|
||||
|
||||
{
|
||||
@@ -187,18 +184,19 @@ void com_fix_run_tests(void) {
|
||||
for (int i = 0; i < 1024 * 32; ++i) {
|
||||
com_fix_t sin, cos;
|
||||
com_fix_sincos(sincos_accumulator, &sin, &cos);
|
||||
double const sin_deviation = fabs(
|
||||
sin(com_fix_as_float(sincos_accumulator)) - com_fix_as_float(sin));
|
||||
double const cos_deviation = fabs(
|
||||
cos(com_fix_as_float(sincos_accumulator)) - com_fix_as_float(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;
|
||||
}
|
||||
printf("max sincos deviations: %f %f\n", max_sin_deviation,
|
||||
max_cos_deviation);
|
||||
com_std_printf("max sincos deviations: %f %f\n", max_sin_deviation,
|
||||
max_cos_deviation);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user