From e47a33d9789850e0a45b5beafa1cc30f043bc15b Mon Sep 17 00:00:00 2001 From: Jochen Kiemes Date: Sat, 21 Sep 2024 00:26:34 +0200 Subject: [PATCH] autogenerate pmfl constants --- .gitignore | 2 ++ CHANGELOG.md | 1 + extras/gen_pmf_const/Makefile | 17 +++++++++ extras/gen_pmf_const/README.md | 4 +++ extras/gen_pmf_const/main.cpp | 63 +++++++++++++++++++++++++++++++++ extras/tests/pc_based/test_03.h | 2 +- src/PoorManFloat.h | 1 + src/PoorManFloatConst.h | 5 +-- 8 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 extras/gen_pmf_const/Makefile create mode 100644 extras/gen_pmf_const/README.md create mode 100644 extras/gen_pmf_const/main.cpp diff --git a/.gitignore b/.gitignore index 782b9324..07f55113 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,5 @@ extras/tests/simavr_based/test_seq*/Makefile extras/tests/esp32_hw_based/*.log library.properties examples +extras/gen_pmf_const/PoorManFloat.cpp +extras/gen_pmf_const/main diff --git a/CHANGELOG.md b/CHANGELOG.md index 16032094..4a9c1a9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ TODO: pre-0.31.2: - Move constants out of PoorManFloat.h +- Fix pmfl constant used by SAM Due aka 21MHz 0.31.1: - Fix for issue #280: stopMove() is interrupted if followed by update of speed/acceleration diff --git a/extras/gen_pmf_const/Makefile b/extras/gen_pmf_const/Makefile new file mode 100644 index 00000000..9a37a600 --- /dev/null +++ b/extras/gen_pmf_const/Makefile @@ -0,0 +1,17 @@ +CPPFLAGS=-I../../src + +../../src/PoorManFloatConst.h: main + ./main >../../src/PoorManFloatConst.h + +main: main.o PoorManFloat.o + +main.o: main.cpp + +PoorManFloat.o: PoorManFloat.cpp + +PoorManFloat.cpp: ../../src/PoorManFloat.cpp + cp ../../src/PoorManFloat.cpp PoorManFloat.cpp + +clean: + rm -f PoorManFloat.cpp + rm -f main main.o PoorManFloat.o diff --git a/extras/gen_pmf_const/README.md b/extras/gen_pmf_const/README.md new file mode 100644 index 00000000..234f74e8 --- /dev/null +++ b/extras/gen_pmf_const/README.md @@ -0,0 +1,4 @@ +# generate pmf constants + +Those constants are stored in PoorManFloatConst.h and are autogenerated +by invoking make. This builds main from main.cpp. diff --git a/extras/gen_pmf_const/main.cpp b/extras/gen_pmf_const/main.cpp new file mode 100644 index 00000000..c452da4c --- /dev/null +++ b/extras/gen_pmf_const/main.cpp @@ -0,0 +1,63 @@ +#include + +#include + +void out(const char *name, pmf_logarithmic value) { + printf("#define %s ((pmf_logarithmic)0x%04x)\n", name, value); +} + +int main() { + puts("// Autogenerated by extras/gen_pmf_const/main"); + puts("// DO NOT EDIT"); + puts("#ifndef POORMANFLOATCONST_H"); + puts("#define POORMANFLOATCONST_H"); + puts(""); + puts("#include "); + puts(""); + + pmf_logarithmic x; + + x = pmfl_from((uint8_t)1); + out("PMF_CONST_1", x); + + x = pmfl_shr(pmfl_from((uint8_t)3), 1); + out("PMF_CONST_3_DIV_2", x); + + x = pmfl_multiply(pmfl_from((uint32_t)16e6), pmfl_from((uint32_t)8e6)); + out("PMF_CONST_128E12", x); + + x = pmfl_from((uint32_t)16e6); + out("PMF_CONST_16E6", x); + + x = pmfl_from((uint16_t)500); + out("PMF_CONST_500", x); + + x = pmfl_from((uint16_t)1000); + out("PMF_CONST_1000", x); + + x = pmfl_from((uint16_t)2000); + out("PMF_CONST_2000", x); + + x = pmfl_from((uint16_t)32000); + out("PMF_CONST_32000", x); + + x = pmfl_divide(pmfl_from((uint32_t)16e6), pmfl_sqrt(pmfl_from((uint8_t)2))); + out("PMF_CONST_16E6_DIV_SQRT_OF_2", x); + + x = pmfl_from((uint32_t)21e6); + out("PMF_CONST_21E6", x); + + x = pmfl_from((uint16_t)42000); + out("PMF_CONST_42000", x); + + x = pmfl_divide(pmfl_from((uint32_t)21e6), pmfl_sqrt(pmfl_from((uint8_t)2))); + out("PMF_CONST_21E6_DIV_SQRT_OF_2", x); + + x = pmfl_square(pmfl_from((uint32_t)21e6)); + x = pmfl_shr(x, 1); + out("PMF_CONST_2205E11", x); + + puts("#endif"); + + return 0; +} diff --git a/extras/tests/pc_based/test_03.h b/extras/tests/pc_based/test_03.h index 35fa20df..d76ca812 100644 --- a/extras/tests/pc_based/test_03.h +++ b/extras/tests/pc_based/test_03.h @@ -22,7 +22,7 @@ bool perform_test() { {42000, 1, false, PMF_CONST_42000}, {14849242, 1, false, PMF_CONST_21E6_DIV_SQRT_OF_2}, {16000000, 2, true, PMF_CONST_128E12}, // (16e6)^2 / 2 - {22100000, 2, true, PMF_CONST_2205E11} // (21e6)^2 / 2 + {21000000, 2, true, PMF_CONST_2205E11} // (21e6)^2 / 2 }; uint16_t l1; pmf_logarithmic p1; diff --git a/src/PoorManFloat.h b/src/PoorManFloat.h index 3ec2c0f5..60debf6e 100644 --- a/src/PoorManFloat.h +++ b/src/PoorManFloat.h @@ -5,6 +5,7 @@ typedef int16_t pmf_logarithmic; #define PMF_CONST_INVALID ((pmf_logarithmic)0x8000) +#define PMF_CONST_MAX ((pmf_logarithmic)0x7fff) pmf_logarithmic pmfl_from(uint8_t x); pmf_logarithmic pmfl_from(uint16_t x); diff --git a/src/PoorManFloatConst.h b/src/PoorManFloatConst.h index 68453082..d39ab32e 100644 --- a/src/PoorManFloatConst.h +++ b/src/PoorManFloatConst.h @@ -1,9 +1,10 @@ +// Autogenerated by extras/gen_pmf_const/main +// DO NOT EDIT #ifndef POORMANFLOATCONST_H #define POORMANFLOATCONST_H #include -#define PMF_CONST_MAX ((pmf_logarithmic)0x7fff) #define PMF_CONST_1 ((pmf_logarithmic)0x0000) #define PMF_CONST_3_DIV_2 ((pmf_logarithmic)0x012c) #define PMF_CONST_128E12 ((pmf_logarithmic)0x5dba) @@ -16,5 +17,5 @@ #define PMF_CONST_21E6 ((pmf_logarithmic)0x30a5) #define PMF_CONST_42000 ((pmf_logarithmic)0x1eb7) #define PMF_CONST_21E6_DIV_SQRT_OF_2 ((pmf_logarithmic)0x2fa5) -#define PMF_CONST_2205E11 ((pmf_logarithmic)0x5f96) +#define PMF_CONST_2205E11 ((pmf_logarithmic)0x5f4a) #endif