### H8/300 example: source file routines.c

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