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