Blame view

RIOT/tests/libfixmath/main.c 5.38 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
  /*
   * Copyright (C) 2014  René Kijewski  <rene.kijewski@fu-berlin.de>
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
   * version 2.1 of the License, or (at your option) any later version.
   *
   * This library is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this library; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
   */
  
  /**
   * @ingroup   tests
   * @{
   *
   * @file
   * @brief     Print some calculations for libfixmath
   *
   * @author    René Kijewski <rene.kijewski@fu-berlin.de>
   *
   * @}
   */
  
  #include <stdio.h>
  
  #include "fix16.h"
  
  #ifndef M_PI
  #   define M_PI 3.14159265359
  #endif
  
  static void binary_ops(void)
  {
      static const struct {
          const char *op;
          fix16_t (*fun)(fix16_t, fix16_t);
      } ops[] = {
          { "add", fix16_add },
          { "sub", fix16_sub },
          { "mul", fix16_mul },
          { "div", fix16_div },
          { "mod", fix16_mod },
  
          { "sadd", fix16_sadd },
          { "ssub", fix16_ssub },
          { "smul", fix16_smul },
          { "sdiv", fix16_sdiv },
  
          { "min", fix16_min },
          { "max", fix16_max },
      };
  
      for (fix16_t a = fix16_from_dbl(-5.0); a < fix16_from_dbl(5.0); a += fix16_from_dbl(0.25)) {
          for (fix16_t b = fix16_from_dbl(-5.0); b < fix16_from_dbl(5.0); b += fix16_from_dbl(0.25)) {
              if (b == 0) {
                  continue;
              }
  
  
              for (unsigned o = 0; o < sizeof(ops) / sizeof(*ops); ++o) {
                  fix16_t c = ops[o].fun(a, b);
  
                  char buf[3][14];
                  fix16_to_str(a, buf[0], 12);
                  fix16_to_str(b, buf[1], 12);
                  fix16_to_str(c, buf[2], 12);
  
                  printf("%s(%s, %s) = %s\n", ops[o].op, buf[0], buf[1], buf[2]);
              }
          }
      }
  }
  
  static void unary_ops(void)
  {
      /* range [-10 : +10 : 0.25] */
      {
          static const struct {
              const char *op;
              fix16_t (*fun)(fix16_t);
          } ops[] = {
              { "abs", fix16_abs },
              { "sq", fix16_sq },
  
              { "atan", fix16_atan },
  
              { "exp", fix16_exp },
          };
  
          for (fix16_t input = fix16_from_dbl(-10.0); input < fix16_from_dbl(+10.0); input += fix16_from_dbl(0.25)) {
              for (unsigned o = 0; o < sizeof(ops) / sizeof(*ops); ++o) {
                  fix16_t result = ops[o].fun(input);
  
                  char buf[2][14];
                  fix16_to_str(input, buf[0], 12);
                  fix16_to_str(result, buf[1], 12);
  
                  printf("%s(%s) = %s\n", ops[o].op, buf[0], buf[1]);
              }
          }
      }
  
      /* range [-pi/2 : +pi/2 : 0.05] */
      {
          static const struct {
              const char *op;
              fix16_t (*fun)(fix16_t);
          } ops[] = {
              /* { "sin_parabola", fix16_sin_parabola }, FIXME: what is sin_parabola? */
              { "sin", fix16_sin },
              { "cos", fix16_cos },
              { "tan", fix16_tan },
          };
  
          for (fix16_t input = fix16_from_dbl(-M_PI/2); input < fix16_from_dbl(+M_PI/2); input += fix16_from_dbl(0.05)) {
              for (unsigned o = 0; o < sizeof(ops) / sizeof(*ops); ++o) {
                  fix16_t result = ops[o].fun(input);
  
                  char buf[2][14];
                  fix16_to_str(input, buf[0], 12);
                  fix16_to_str(result, buf[1], 12);
  
                  printf("%s(%s) = %s\n", ops[o].op, buf[0], buf[1]);
              }
          }
      }
  
      /* range [-1 : +1 : 0.05] */
      {
          static const struct {
              const char *op;
              fix16_t (*fun)(fix16_t);
          } ops[] = {
              { "asin", fix16_asin },
              { "acos", fix16_acos },
          };
  
          for (fix16_t input = fix16_from_dbl(-1.0); input < fix16_from_dbl(+1.0); input += fix16_from_dbl(0.05)) {
              for (unsigned o = 0; o < sizeof(ops) / sizeof(*ops); ++o) {
                  fix16_t result = ops[o].fun(input);
  
                  char buf[2][14];
                  fix16_to_str(input, buf[0], 12);
                  fix16_to_str(result, buf[1], 12);
  
                  printf("%s(%s) = %s\n", ops[o].op, buf[0], buf[1]);
              }
          }
      }
  
      /* range [+0.05 : +10 : 0.25] */
      {
          static const struct {
              const char *op;
              fix16_t (*fun)(fix16_t);
          } ops[] = {
              { "sqrt", fix16_sqrt },
  
              { "log", fix16_log },
              { "log2", fix16_log2 },
              { "slog2", fix16_slog2 },
          };
  
          for (fix16_t input = fix16_from_dbl(0.05); input < fix16_from_dbl(+10.0); input += fix16_from_dbl(0.25)) {
              for (unsigned o = 0; o < sizeof(ops) / sizeof(*ops); ++o) {
                  fix16_t result = ops[o].fun(input);
  
                  char buf[2][14];
                  fix16_to_str(input, buf[0], 12);
                  fix16_to_str(result, buf[1], 12);
  
                  printf("%s(%s) = %s\n", ops[o].op, buf[0], buf[1]);
              }
          }
      }
  }
  
  int main(void)
  {
      puts("Unary.");
      unary_ops();
  
      puts("Binary.");
      binary_ops();
  
      puts("Done.");
      return 0;
  }