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