Steve Tell fixed a nested for-loop bug in the PIC Port
[fw/sdcc] / src / regression / compare8.c
1 #define __16F873
2 #include "p16f873.h"
3 // Signed comparisons of the form:  (variable>LIT)
4 //
5 // This regression test exercises all of the boundary
6 // conditions in literal less than comparisons. There
7 // are numerous opportunities to optimize these comparison
8 // and each one has an astonishing capability of failing
9 // a boundary condition.
10
11 unsigned char success = 0;
12 unsigned char failures = 0;
13 unsigned char dummy = 0;
14 unsigned char result = 0;
15
16 //bit bit0 = 0;
17 int int0 = 0;
18 int int1 = 0;
19 unsigned char uchar0 = 0;
20 unsigned char uchar1 = 0;
21 signed char char0 = 0;
22 signed char char1 = 0;
23 char long0 = 0;
24 char long1 = 0;
25
26
27 /* copied from 16f877.inc file supplied with gpasm */
28
29 #define _CP_ALL          0x0FCF
30 #define _CP_HALF         0x1FDF
31 #define _CP_UPPER_256    0x2FEF
32 #define _CP_OFF          0x3FFF
33 #define _DEBUG_ON        0x37FF
34 #define _DEBUG_OFF       0x3FFF
35 #define _WRT_ENABLE_ON   0x3FFF
36 #define _WRT_ENABLE_OFF  0x3DFF
37 #define _CPD_ON          0x3EFF
38 #define _CPD_OFF         0x3FFF
39 #define _LVP_ON          0x3FFF
40 #define _LVP_OFF         0x3F7F
41 #define _BODEN_ON        0x3FFF
42 #define _BODEN_OFF       0x3FBF
43 #define _PWRTE_OFF       0x3FFF
44 #define _PWRTE_ON        0x3FF7
45 #define _WDT_ON          0x3FFF
46 #define _WDT_OFF         0x3FFB
47 #define _LP_OSC          0x3FFC
48 #define _XT_OSC          0x3FFD
49 #define _HS_OSC          0x3FFE
50 #define _RC_OSC          0x3FFF
51
52 /* *** NOTE ***  This particular test takes quite a while to run 
53  * ~ 10,000,000 instruction cycles. (2.5 seconds on a 20Mhz PIC).
54  * The WDT will reset the CPU if it's enabled. So disable it...
55 */
56
57 typedef unsigned int word;
58
59 word at 0x2007  CONFIG = _WDT_OFF & _PWRTE_ON;
60
61 void
62 done ()
63 {
64   dummy++;
65 }
66
67
68 void c_char_gt_lit1(unsigned char expected_result)
69 {
70   result = 0;
71
72   if(char0 > -0x7f)
73     result |= 1;
74
75
76   if(char0 > -1)
77     result |= 2;
78
79   if(char0 > 0)
80     result |= 4;
81
82   if(char0 > 1)
83     result |= 8;
84
85   if(char0 > 0x7e)
86     result |= 0x10;
87   
88   if(char0 > 0x7f)
89     result |= 0x20;
90   
91   if(result != expected_result)
92     failures++;
93 }
94
95
96 void char_compare(void)
97 {
98   char0 = 0x7f;
99   c_char_gt_lit1(0x1f);
100
101   char0 = 0x7e;
102   c_char_gt_lit1(0x0f);
103
104   char0 = 0x40;
105   c_char_gt_lit1(0x0f);
106
107   char0 = 0x2;
108   c_char_gt_lit1(0x0f);
109
110   char0 = 0x1;
111   c_char_gt_lit1(0x07);
112
113   char0 = 0;
114   c_char_gt_lit1(0x03);
115
116   char0 = -1;
117   c_char_gt_lit1(0x01);
118
119   char0 = -2;
120   c_char_gt_lit1(0x01);
121
122   char0 = -0x40;
123   c_char_gt_lit1(0x01);
124
125   char0 = -0x7e;
126   c_char_gt_lit1(0x01);
127
128   char0 = -0x7f;
129   c_char_gt_lit1(0x00);
130
131   char0 = 0x80;
132   c_char_gt_lit1(0x00);
133
134
135   /* Now test entire range */
136
137   for(char0=2; char0 != 0x7f; char0++)
138     c_char_gt_lit1(0x0f);
139
140
141   for(char0=-0x7e; char0 != -1; char0++)
142     c_char_gt_lit1(0x01);
143
144
145 }
146
147
148 void c_int_gt_lit1(unsigned char expected_result)
149 {
150   result = 0;
151
152   if(int0 > 0)
153     result |= 1;
154
155   if(int0 > 1)
156     result |= 2;
157
158
159   if(int0 > 0xff)
160     result |= 4;
161
162   if(int0 > 0x100)
163     result |= 8;
164
165   if(int0 > 0x0101)
166     result |= 0x10;
167   
168   if(int0 > 0x01ff)
169     result |= 0x20;
170   
171   if(int0 > 0x0200)
172     result |= 0x40;
173
174   if(int0 > 0x0201)
175     result |= 0x80;
176
177   if(result != expected_result)
178     failures=1;
179
180 }
181
182
183 void int_compare1(void)
184 {
185   int0 = -1;
186   c_int_gt_lit1(0x00);
187
188   int0 = 0;
189   c_int_gt_lit1(0x00);
190
191   int0 = 1;
192   c_int_gt_lit1(0x01);
193
194   int0 = 2;
195   c_int_gt_lit1(0x03);
196
197   int0 = 0xfe;
198   c_int_gt_lit1(0x03);
199
200   int0 = 0xff;
201   c_int_gt_lit1(0x03);
202
203   int0 = 0x100;
204   c_int_gt_lit1(0x07);
205
206   int0 = 0x101;
207   c_int_gt_lit1(0x0f);
208
209   int0 = 0x102;
210   c_int_gt_lit1(0x1f);
211
212   int0 = 0x1fe;
213   c_int_gt_lit1(0x1f);
214
215   int0 = 0x1ff;
216   c_int_gt_lit1(0x1f);
217
218   int0 = 0x200;
219   c_int_gt_lit1(0x3f);
220
221   int0 = 0x201;
222   c_int_gt_lit1(0x7f);
223
224   int0 = 0x7f00;
225   c_int_gt_lit1(0xff);
226
227   /* now check contiguous ranges */
228
229   for(int0 = -0x7fff; int0 != -1; int0++)
230     c_int_gt_lit1(0x00);
231
232   for(int0 = 2; int0 != 0xff; int0++)
233     c_int_gt_lit1(0x03);
234
235   for(int0 = 0x202; int0 != 0x7fff; int0++)
236     c_int_gt_lit1(0xff);
237
238 }
239
240
241 void c_int_gt_lit2(unsigned char expected_result)
242 {
243   result = 0;
244
245   if(int0 > -0x7fff)
246     result |= 1;
247
248   if(int0 > -0x7f00)
249     result |= 2;
250
251   if(int0 > -0x7eff)
252     result |= 4;
253
254   if(int0 > -0x7e00)
255     result |= 8;
256
257   if(int0 > -0x0101)
258     result |= 0x10;
259   
260   if(int0 > -0x0100)
261     result |= 0x20;
262   
263   if(int0 > -0xff)
264     result |= 0x40;
265
266   if(int0 > -1)
267     result |= 0x80;
268
269   if(result != expected_result)
270     failures=1;
271 }
272
273 void int_compare2(void)
274 {
275   int0 = -0x7fff;
276   c_int_gt_lit2(0x00);
277
278   int0 = -0x7f00;
279   c_int_gt_lit2(0x01);
280
281   int0 = -0x7eff;
282   c_int_gt_lit2(0x03);
283
284   int0 = -0x7e00;
285   c_int_gt_lit2(0x07);
286
287   int0 = -0x7dff;
288   c_int_gt_lit2(0x0f);
289
290   int0 = -0x4567;
291   c_int_gt_lit2(0x0f);
292
293   int0 = -0x200;
294   c_int_gt_lit2(0x0f);
295
296   int0 = -0x102;
297   c_int_gt_lit2(0x0f);
298
299   int0 = -0x101;
300   c_int_gt_lit2(0x0f);
301
302   int0 = -0x100;
303   c_int_gt_lit2(0x1f);
304
305   int0 = -0xff;
306   c_int_gt_lit2(0x3f);
307
308   int0 = -0x02;
309   c_int_gt_lit2(0x7f);
310
311   int0 = -0x01;
312   c_int_gt_lit2(0x7f);
313
314   int0 = 0;
315   c_int_gt_lit2(0xff);
316
317   int0 = 1;
318   c_int_gt_lit2(0xff);
319
320   int0 = 0x7fff;
321   c_int_gt_lit2(0xff);
322
323   /* now check contiguous ranges */
324
325   for(int0 = -0x7ffe; int0 != -0x7f01; int0++)
326     c_int_gt_lit2(0x01);
327
328   for(int0 = -0x7dff; int0 != -0x101; int0++)
329     c_int_gt_lit2(0x0f);
330
331   for(int0 = 0; int0 != 0x7fff; int0++)
332     c_int_gt_lit2(0xff);
333
334 }
335
336
337
338
339 void
340 main (void)
341 {
342   char_compare();
343   int_compare1();
344   int_compare2();
345
346   success = failures;
347   done ();
348 }