Blame view

RIOT/tests/bitarithm_timings/main.c 2.19 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
  /*
   * 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     Measure the speed of the function in bitarithm.c
   *
   * @author    René Kijewski <rene.kijewski@fu-berlin.de>
   *
   * @}
   */
  
  #include <stdio.h>
  
  #include "bitarithm.h"
  #include "xtimer.h"
  
  #define TIMEOUT_S (5ul)
  #define TIMEOUT (TIMEOUT_S * US_PER_SEC)
  #define PER_ITERATION (4)
  
  static void callback(void *done_)
  {
      volatile int *done = done_;
      *done = 1;
  }
  
  static void run_test(const char *name, unsigned (*test)(unsigned))
  {
      volatile int done = 0;
      unsigned i = 0;
      unsigned long count = 0;
  
      xtimer_t xtimer;
      xtimer.callback = callback;
      xtimer.arg = (void *) &done;
  
      xtimer_set(&xtimer, TIMEOUT);
  
      do {
          if (i++ == -1u) {
              i = 1;
          }
  
          for (unsigned j = 0; j < PER_ITERATION; ++j) {
              volatile unsigned r;
              r = test(i);
              (void) r;
              r = test(-1u - i);
              (void) r;
              r = test(~i);
              (void) r;
              r = test(~(-1u - i));
              (void) r;
          }
  
          ++count;
      } while (done == 0);
  
      printf("+ %s: %lu iterations per second\r\n", name, (4*PER_ITERATION) * count / TIMEOUT_S);
  }
  
  #define run_test(test) run_test(#test, test)
  
  int main(void)
  {
      printf("Start.\r\n");
  
      run_test(bitarithm_msb);
      run_test(bitarithm_lsb);
      run_test(bitarithm_bits_set);
  
      printf("Done.\r\n");
      return 0;
  }