Atmel AVR example: source file routines.c


 1  /*
 2  
 3  A part of the Bound-T test program tp_bro_int.
 4  This is the example program in the Bound-T brochure.
 5  
 6  $Id: routines.html,v 1.1 2010-02-27 09:52:18 niklas Exp $
 7  */
 8  
 9  
10  #include "types.h"
11  #include "routines.h"
12  
13  
14  void Count25 (uint *x)
15  {
16     count_t u = 25;
17  
18     for (; u > 0; u -= 2)
19     {
20        *x = *x + u;
21     }
22  }
23  
24  
25  void Count (count_t u, uint *x)
26  {
27     for (; u > 0; u -= 2)
28     {
29        *x = *x + u;
30     }
31  }
32  
33  
34  void Foo (count_t num, uint *x)
35  {
36     Count (num + 3, x);
37     /* The loop in Count depends on Count.u = num + 3. */
38  }
39  
40  
41  void Foo7 (uint *x)
42  {
43     *x = *x + 10;
44  
45     Count (7, x);
46     /* The loop in Count depends on Count.u = 7. */
47  
48     *x = *x - 8;
49  }
50  
51  
52  void Solve (uint *x)
53  {
54     count_t i;
55     volatile count_t k;
56  
57     for (i = 0; i < 8; i++)
58     /* The bounds on this loop are static. */
59     {
60        k = Ones (*x);
61        /* k is now the number of '1' bits in *x.          */
62        /* This would be quite hard to analyse statically. */
63  
64        if (k == 0) break;
65        /* This can make the for-loop stop before its full    */
66        /* number of iterations. Bound-T uses the full number */
67        /* for the Worst Case Time.                           */
68  
69        Count (k, x);
70        /* The loop in Count depends on Count.u = k, which is  */
71        /* hard to analyse statically. An assertion is needed. */
72     }
73  }
74  
75  
76  count_t Ones (uint x)
77  {
78     count_t v = 0;
79  
80     while (x)
81     /* This is not a 'counter' loop, so Bound-T cannot find */
82     /* its bounds automatically. An assertion is needed.    */
83     {
84        if (x & 1) v ++;
85        x >>= 1;
86     }
87  
88     return v;
89  }