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