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