NEWS.txt: prepare version 6.5.4
[xcsoar.git] / tools / GenerateSineTables.cpp
blobe880f242c1201c6f979f1e7ece57f3829d9249b6
1 #ifndef FIXED_MATH
2 #define FIXED_MATH
3 #endif
5 #include "Math/Constants.h"
6 #include "Math/fixed.hpp"
7 #include "Computer/ThermalLocator.hpp"
9 #include <math.h>
10 #include <stdio.h>
12 static inline double
13 thermal_fn(int x)
15 return exp((-0.2/ThermalLocator::TLOCATOR_NMAX)*pow((double)x, 1.5));
18 static inline double
19 INT_TO_DEG(int x)
21 return DEG_TO_RAD * ((double)x * 360) / 4096;
24 int
25 main(int argc, char **argv)
27 (void)argc;
28 (void)argv;
30 puts("#ifdef FIXED_MATH");
31 puts("const int SINETABLE[4096] = {");
32 for (unsigned i = 0; i < 4096; i++)
33 printf(" %d,\n",
34 (int)(sin(INT_TO_DEG(i)) * (double)fixed::resolution));
35 puts("#else");
36 puts("const fixed SINETABLE[4096] = {");
37 for (unsigned i = 0; i < 4096; i++)
38 printf(" fixed(%.20e),\n", sin(INT_TO_DEG(i)));
39 puts("#endif");
40 puts("};");
42 puts("#ifdef FIXED_MATH");
43 puts("const int COSTABLE[4096] = {");
44 for (unsigned i = 0; i < 4096; i++)
45 printf(" %d,\n",
46 (int)(cos(INT_TO_DEG(i)) * (double)fixed::resolution));
47 puts("#else");
48 puts("const fixed COSTABLE[4096] = {");
49 for (unsigned i = 0; i < 4096; i++)
50 printf(" fixed(%.20e),\n", cos(INT_TO_DEG(i)));
51 puts("#endif");
52 puts("};");
54 puts("const short ISINETABLE[4096] = {");
55 for (unsigned i = 0; i < 4096; i++)
56 printf(" %d,\n", (int)lround(sin(INT_TO_DEG(i)) * 1024));
57 puts("};");
59 puts("const short ICOSTABLE[4096] = {");
60 for (unsigned i = 0; i < 4096; i++)
61 printf(" %d,\n", (int)lround(cos(INT_TO_DEG(i)) * 1024));
62 puts("};");
64 puts("#ifdef FIXED_MATH");
65 puts("const fixed::value_t INVCOSINETABLE[4096] = {");
66 for (unsigned i = 0; i < 4096; i++) {
67 double x = cos(INT_TO_DEG(i));
68 if ((x >= 0) && (x < 1.0e-8))
69 x = 1.0e-8;
71 if ((x < 0) && (x > -1.0e-8))
72 x = -1.0e-8;
74 #if defined(HAVE_MSVCRT) && !defined(__CYGWIN__)
75 // Due to non-standard behavior of the microsoft implementation
76 // this hack is needed to compile on windows machines
77 printf(" %I64dLL,\n",
78 #else
79 printf(" %lldLL,\n",
80 #endif
81 (long long)((double)fixed::resolution / x));
83 puts("#else");
84 puts("const fixed INVCOSINETABLE[4096] = {");
85 for (unsigned i = 0; i < 4096; i++) {
86 double x = cos(INT_TO_DEG(i));
87 if ((x >= 0) && (x < 1.0e-8))
88 x = 1.0e-8;
90 if ((x < 0) && (x > -1.0e-8))
91 x = -1.0e-8;
93 printf(" fixed(%.20e),\n", 1.0 / x);
95 puts("#endif");
96 puts("};");
98 printf("#define THERMALRECENCY_SIZE %d\n", ThermalLocator::TLOCATOR_NMAX);
99 puts("#ifdef FIXED_MATH");
100 printf("const unsigned THERMALRECENCY[] = {\n");
101 for (unsigned i = 0; i < ThermalLocator::TLOCATOR_NMAX; i++)
102 printf(" %u,\n", (unsigned)(thermal_fn(i) * (double)fixed::resolution));
103 puts("#else");
104 printf("const fixed THERMALRECENCY[%d] = {", ThermalLocator::TLOCATOR_NMAX);
105 for (unsigned i = 0; i < ThermalLocator::TLOCATOR_NMAX; i++)
106 printf(" fixed(%.20e),\n", thermal_fn(i));
107 puts("#endif");
108 puts("};");
110 return 0;