Skip to content

Commit

Permalink
autogenerate pmfl constants
Browse files Browse the repository at this point in the history
  • Loading branch information
gin66 committed Sep 20, 2024
1 parent 1b70ecc commit e47a33d
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions extras/gen_pmf_const/Makefile
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions extras/gen_pmf_const/README.md
Original file line number Diff line number Diff line change
@@ -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.
63 changes: 63 additions & 0 deletions extras/gen_pmf_const/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <stdio.h>

#include <PoorManFloat.h>

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 <PoorManFloat.h>");
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;
}
2 changes: 1 addition & 1 deletion extras/tests/pc_based/test_03.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/PoorManFloat.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions src/PoorManFloatConst.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Autogenerated by extras/gen_pmf_const/main
// DO NOT EDIT
#ifndef POORMANFLOATCONST_H
#define POORMANFLOATCONST_H

#include <PoorManFloat.h>

#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)
Expand All @@ -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

0 comments on commit e47a33d

Please sign in to comment.