Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / mini / basic.cs
1 using System;
2 using System.Reflection;
3
4 /*
5  * Regression tests for the mono JIT.
6  *
7  * Each test needs to be of the form:
8  *
9  * static int test_<result>_<name> ();
10  *
11  * where <result> is an integer (the value that needs to be returned by
12  * the method to make it pass.
13  * <name> is a user-displayed name used to identify the test.
14  *
15  * The tests can be driven in two ways:
16  * *) running the program directly: Main() uses reflection to find and invoke
17  *      the test methods (this is useful mostly to check that the tests are correct)
18  * *) with the --regression switch of the jit (this is the preferred way since
19  *      all the tests will be run with optimizations on and off)
20  *
21  * The reflection logic could be moved to a .dll since we need at least another
22  * regression test file written in IL code to have better control on how
23  * the IL code looks.
24  */
25
26 #if __MOBILE__
27 class BasicTests
28 #else
29 class Tests
30 #endif
31 {
32
33 #if !__MOBILE__
34         public static int Main (string[] args) {
35                 return TestDriver.RunTests (typeof (Tests), args);
36         }
37 #endif
38         
39         public static int test_0_return () {
40                 return 0;
41         }
42
43         public static int test_100000_return_large () {
44                 return 100000;
45         }
46
47         public static int test_1_load_bool () {
48                 bool a = true;
49                 return a? 1: 0;
50         }
51
52         public static int test_0_load_bool_false () {
53                 bool a = false;
54                 return a? 1: 0;
55         }
56
57         public static int test_200_load_byte () {
58                 byte a = 200;
59                 return a;
60         }
61
62         public static int test_100_load_sbyte () {
63                 sbyte a = 100;
64                 return a;
65         }
66
67         public static int test_200_load_short () {
68                 short a = 200;
69                 return a;
70         }
71
72         public static int test_100_load_ushort () {
73                 ushort a = 100;
74                 return a;
75         }
76
77         public static int test_3_add_simple () {
78                 int a = 1; 
79                 int b = 2;
80                 return a + b;
81         }
82
83         public static int test_3_add_imm () {
84                 int a = 1; 
85                 return a + 2;
86         }
87
88         public static int test_13407573_add_largeimm () {
89                 int a = 1; 
90                 return a + 13407572;
91         }
92
93         public static int test_1_sub_simple () {
94                 int a = 1; 
95                 int b = 2;
96                 return b - a;
97         }
98
99         public static int test_1_sub_simple_un () {
100                 uint a = 1; 
101                 uint b = 2;
102                 return (int)(b - a);
103         }
104
105         public static int test_1_sub_imm () {
106                 int b = 2;
107                 return b - 1;
108         }
109
110         public static int test_2_sub_large_imm () {
111                 int b = 0xff0f0f;
112                 return b - 0xff0f0d;
113         }
114
115         public static int test_0_sub_inv_imm () {
116                 int b = 2;
117                 return 2 - b;
118         }
119
120         public static int test_2_and () {
121                 int b = 2;
122                 int a = 3;
123                 return b & a;
124         }
125
126         public static int test_0_and_imm () {
127                 int b = 2;
128                 return b & 0x10;
129         }
130
131         public static int test_0_and_large_imm () {
132                 int b = 2;
133                 return b & 0x10000000;
134         }
135
136         public static int test_0_and_large_imm2 () {
137                 int b = 2;
138                 return b & 0x100000f0;
139         }
140
141         public static int test_2_div () {
142                 int b = 6;
143                 int a = 3;
144                 return b / a;
145         }
146
147         public static int test_4_div_imm () {
148                 int b = 12;
149                 return b / 3;
150         }
151
152         public static int test_4_divun_imm () {
153                 uint b = 12;
154                 return (int)(b / 3);
155         }
156
157         public static int test_0_div_fold () {
158                 int b = -1;
159                 return b / 2;
160         }
161
162         public static int test_2_div_fold4 () {
163                 int b = -8;
164                 return -(b / 4);
165         }
166
167         public static int test_2_div_fold16 () {
168                 int b = 32;
169                 return b / 16;
170         }
171
172         public static int test_719177_div_destreg () {
173                 int year = 1970;
174                 return ((365* (year-1)) + ((year-1)/4));
175         }
176
177         public static int test_1_remun_imm () {
178                 uint b = 13;
179                 return (int)(b % 3);
180         }
181
182         public static int test_2_bigremun_imm () {
183                 unchecked {
184                         uint b = (uint)-2;
185                         return (int)(b % 3);
186                 }
187         }
188
189         public static int test_2_rem () {
190                 int b = 5;
191                 int a = 3;
192                 return b % a;
193         }
194
195         public static int test_4_rem_imm () {
196                 int b = 12;
197                 return b % 8;
198         }
199
200         public static int test_0_rem_imm_0 () {
201                 int b = 12;
202                 return b % 1;
203         }
204
205         public static int test_0_rem_imm_0_neg () {
206                 int b = -2;
207                 return b % 1;
208         }
209
210         public static int test_4_rem_big_imm () {
211                 int b = 10004;
212                 return b % 10000;
213         }
214
215         public static int test_9_mul () {
216                 int b = 3;
217                 int a = 3;
218                 return b * a;
219         }
220
221         public static int test_15_mul_imm () {
222                 int b = 3;
223                 return b * 5;
224         }
225
226         public static int test_24_mul () {
227                 int a = 3;
228                 int b = 8;
229                 int res;
230
231                 res = a * b;
232                 
233                 return res;
234         }
235
236         public static int test_24_mul_ovf () {
237                 int a = 3;
238                 int b = 8;
239                 int res;
240
241                 checked {
242                         res = a * b;
243                 }
244                 
245                 return res;
246         }
247
248         public static int test_24_mul_un () {
249                 uint a = 3;
250                 uint b = 8;
251                 uint res;
252
253                 res = a * b;
254                 
255                 return (int)res;
256         }
257
258         public static int test_24_mul_ovf_un () {
259                 uint a = 3;
260                 uint b = 8;
261                 uint res;
262
263                 checked {
264                         res = a * b;
265                 }
266                 
267                 return (int)res;
268         }
269
270         public static int test_0_add_ovf1 () {
271                 int i, j, k;
272
273                 checked {
274                         i = System.Int32.MinValue;
275                         j = 0;
276                         k = i + j;
277                 }
278
279                 if (k != System.Int32.MinValue)
280                         return 1;
281                 return 0;
282         }
283
284         public static int test_0_add_ovf2 () {
285                 int i, j, k;
286
287                 checked {
288                         i = System.Int32.MaxValue;
289                         j = 0;
290                         k = i + j;
291                 }
292
293                 if (k != System.Int32.MaxValue)
294                         return 2;
295                 return 0;
296         }
297
298         public static int test_0_add_ovf3 () {
299                 int i, j, k;
300
301                 checked {
302                         i = System.Int32.MinValue;
303                         j = System.Int32.MaxValue;
304                         k = i + j;
305                 }
306
307                 if (k != -1)
308                         return 3;
309                 return 0;
310         }
311
312         public static int test_0_add_ovf4 () {
313                 int i, j, k;
314
315                 checked {
316                         i = System.Int32.MaxValue;
317                         j = System.Int32.MinValue;
318                         k = i + j;
319                 }
320
321                 if (k != -1)
322                         return 4;
323                 return 0;
324         }
325
326         public static int test_0_add_ovf5 () {
327                 int i, j, k;
328
329                 checked {
330                         i = System.Int32.MinValue + 1234;
331                         j = -1234;
332                         k = i + j;
333                 }
334
335                 if (k != System.Int32.MinValue)
336                         return 5;
337                 return 0;
338         }
339
340         public static int test_0_add_ovf6 () {
341                 int i, j, k;
342
343                 checked {
344                         i = System.Int32.MaxValue - 1234;
345                         j = 1234;
346                         k = i + j;
347                 }
348
349                 if (k != System.Int32.MaxValue)
350                         return 6;
351
352                 return 0;
353         }
354
355         public static int test_0_add_un_ovf () {
356                 uint n = (uint)134217728 * 16;
357                 uint number = checked (n + (uint)0);
358
359                 return number == n ? 0 : 1;
360         }
361
362         public static int test_0_sub_ovf1 () {
363                 int i, j, k;
364
365                 checked {
366                         i = System.Int32.MinValue;
367                         j = 0;
368                         k = i - j;
369                 }
370
371                 if (k != System.Int32.MinValue)
372                         return 1;
373
374                 return 0;
375         }
376
377         public static int test_0_sub_ovf2 () {
378                 int i, j, k;
379
380                 checked {
381                         i = System.Int32.MaxValue;
382                         j = 0;
383                         k = i - j;
384                 }
385
386                 if (k != System.Int32.MaxValue)
387                         return 2;
388
389                 return 0;
390         }
391
392         public static int test_0_sub_ovf3 () {
393                 int i, j, k;
394
395                 checked {
396                         i = System.Int32.MinValue;
397                         j = System.Int32.MinValue + 1234;
398                         k = i - j;
399                 }
400
401                 if (k != -1234)
402                         return 3;
403
404                 return 0;
405         }
406
407         public static int test_0_sub_ovf4 () {
408                 int i, j, k;
409
410                 checked {
411                         i = System.Int32.MaxValue;
412                         j = 1234;
413                         k = i - j;
414                 }
415
416                 if (k != System.Int32.MaxValue - 1234)
417                         return 4;
418
419                 return 0;
420         }
421
422         public static int test_0_sub_ovf5 () {
423                 int i, j, k;
424
425                 checked {
426                         i = System.Int32.MaxValue - 1234;
427                         j = -1234;
428                         k = i - j;
429                 }
430
431                 if (k != System.Int32.MaxValue)
432                         return 5;
433
434                 return 0;
435         }
436
437         public static int test_0_sub_ovf6 () {
438                 int i, j, k;
439
440                 checked {
441                         i = System.Int32.MinValue + 1234;
442                         j = 1234;
443                         k = i - j;
444                 }
445
446                 if (k != System.Int32.MinValue)
447                         return 6;
448
449                 return 0;
450         }
451
452         public static int test_0_sub_ovf_un () {
453                 uint i, j, k;
454
455                 checked {
456                         i = System.UInt32.MaxValue;
457                         j = 0;
458                         k = i - j;
459                 }
460
461                 if (k != System.UInt32.MaxValue)
462                         return 1;
463
464                 checked {
465                         i = System.UInt32.MaxValue;
466                         j = System.UInt32.MaxValue;
467                         k = i - j;
468                 }
469
470                 if (k != 0)
471                         return 2;
472
473                 return 0;
474         }
475
476         public static int test_3_or () {
477                 int b = 2;
478                 int a = 3;
479                 return b | a;
480         }
481
482         public static int test_3_or_un () {
483                 uint b = 2;
484                 uint a = 3;
485                 return (int)(b | a);
486         }
487
488         public static int test_3_or_short_un () {
489                 ushort b = 2;
490                 ushort a = 3;
491                 return (int)(b | a);
492         }
493
494         public static int test_18_or_imm () {
495                 int b = 2;
496                 return b | 0x10;
497         }
498
499         public static int test_268435458_or_large_imm () {
500                 int b = 2;
501                 return b | 0x10000000;
502         }
503
504         public static int test_268435459_or_large_imm2 () {
505                 int b = 2;
506                 return b | 0x10000001;
507         }
508
509         public static int test_1_xor () {
510                 int b = 2;
511                 int a = 3;
512                 return b ^ a;
513         }
514
515         public static int test_1_xor_imm () {
516                 int b = 2;
517                 return b ^ 3;
518         }
519
520         public static int test_983041_xor_imm_large () {
521                 int b = 2;
522                 return b ^ 0xf0003;
523         }
524
525         public static int test_1_neg () {
526                 int b = -2;
527                 b++;
528                 return -b;
529         }
530
531         public static int test_2_not () {
532                 int b = ~2;
533                 b = ~b;
534                 return b;
535         }
536
537         public static int test_16_shift () {
538                 int b = 2;
539                 int a = 3;
540                 return b << a;
541         }
542         
543         public static int test_16_shift_add () {
544                 int b = 2;
545                 int a = 3;
546                 int c = 0;
547                 return b << (a + c);
548         }
549         
550         public static int test_16_shift_add2 () {
551                 int b = 2;
552                 int a = 3;
553                 int c = 0;
554                 return (b + c) << a;
555         }
556         
557         public static int test_16_shift_imm () {
558                 int b = 2;
559                 return b << 3;
560         }
561         
562         public static int test_524288_shift_imm_large () {
563                 int b = 2;
564                 return b << 18;
565         }
566         
567         public static int test_12_shift_imm_inv () {
568                 int b = 2;
569                 return 3 << 2;
570         }
571
572         public static int test_12_shift_imm_inv_sbyte () {
573                 sbyte b = 2;
574                 return 3 << 2;
575         }
576
577         public static int test_1_rshift_imm () {
578                 int b = 8;
579                 return b >> 3;
580         }
581         
582         public static int test_2_unrshift_imm () {
583                 uint b = 16;
584                 return (int)(b >> 3);
585         }
586         
587         public static int test_0_bigunrshift_imm () {
588                 unchecked {
589                         uint b = (uint)-1;
590                         b = b >> 1;
591                         if (b != 0x7fffffff)
592                                 return 1;
593                         return 0;
594                 }
595         }
596         
597         public static int test_0_bigrshift_imm () {
598                 int b = -1;
599                 b = b >> 1;
600                 if (b != -1)
601                         return 1;
602                 return 0;
603         }
604         
605         public static int test_1_rshift () {
606                 int b = 8;
607                 int a = 3;
608                 return b >> a;
609         }
610         
611         public static int test_2_unrshift () {
612                 uint b = 16;
613                 int a = 3;
614                 return (int)(b >> a);
615         }
616         
617         public static int test_0_bigunrshift () {
618                 unchecked {
619                         uint b = (uint)-1;
620                         int a = 1;
621                         b = b >> a;
622                         if (b != 0x7fffffff)
623                                 return 1;
624                         return 0;
625                 }
626         }
627         
628         public static int test_0_bigrshift () {
629                 int b = -1;
630                 int a = 1;
631                 b = b >> a;
632                 if (b != -1)
633                         return 1;
634                 return 0;
635         }
636         
637         public static int test_2_cond () {
638                 int b = 2, a = 3, c;
639                 if (a == b)
640                         return 0;
641                 return 2;
642         }
643         
644         public static int test_2_cond_short () {
645                 short b = 2, a = 3, c;
646                 if (a == b)
647                         return 0;
648                 return 2;
649         }
650         
651         public static int test_2_cond_sbyte () {
652                 sbyte b = 2, a = 3, c;
653                 if (a == b)
654                         return 0;
655                 return 2;
656         }
657         
658         public static int test_6_cascade_cond () {
659                 int b = 2, a = 3, c;
660                 if (a == b)
661                         return 0;
662                 else if (b > a)
663                         return 1;
664                 else if (b != b)
665                         return 2;
666                 else {
667                         c = 1;
668                 }
669                 return a + b + c;
670         }
671         
672         public static int test_6_cascade_short () {
673                 short b = 2, a = 3, c;
674                 if (a == b)
675                         return 0;
676                 else if (b > a)
677                         return 1;
678                 else if (b != b)
679                         return 2;
680                 else {
681                         c = 1;
682                 }
683                 return a + b + c;
684         }
685
686         public static int test_0_short_sign_extend () {
687                 int t1 = 0xffeedd;
688                 short s1 = (short)t1;
689                 int t2 = s1;
690
691                 if ((uint)t2 != 0xffffeedd) 
692                         return 1;
693                 else
694                         return 0;
695         }
696
697         public static int test_127_iconv_to_i1 () {
698                 int i = 0x100017f;
699                 sbyte s = (sbyte)i;
700
701                 return s;
702         }
703
704         public static int test_384_iconv_to_i2 () {
705                 int i = 0x1000180;
706                 short s = (short)i;
707
708                 return s;
709         }
710         
711         public static int test_15_for_loop () {
712                 int i;
713                 for (i = 0; i < 15; ++i) {
714                 }
715                 return i;
716         }
717         
718         public static int test_11_nested_for_loop () {
719                 int i, j = 0; /* mcs bug here if j not set */
720                 for (i = 0; i < 15; ++i) {
721                         for (j = 200; j >= 5; --j) ;
722                 }
723                 return i - j;
724         }
725
726         public static int test_11_several_nested_for_loops () {
727                 int i, j = 0; /* mcs bug here if j not set */
728                 for (i = 0; i < 15; ++i) {
729                         for (j = 200; j >= 5; --j) ;
730                 }
731                 i = j = 0;
732                 for (i = 0; i < 15; ++i) {
733                         for (j = 200; j >= 5; --j) ;
734                 }
735                 return i - j;
736         }
737
738         public static int test_0_conv_ovf_i1 () {
739                 int c;
740
741                 //for (int j = 0; j < 10000000; j++)
742                 checked {
743                         c = 127;
744                         sbyte b = (sbyte)c;
745                         c = -128;
746                         b = (sbyte)c;
747                 }
748
749                 return 0;
750         }
751         
752         public static int test_0_conv_ovf_i1_un () {
753                 uint c;
754
755                 checked {
756                         c = 127;
757                         sbyte b = (sbyte)c;
758                 }
759                 
760                 return 0;
761         }
762         
763         public static int test_0_conv_ovf_i2 () {
764                 int c;
765
766                 checked {
767                         c = 32767;
768                         Int16 b = (Int16)c;
769                         c = -32768;
770                         b = (Int16)c;
771                         unchecked {
772                                 uint u = 0xfffffffd;
773                                 c = (int)u;
774                         }
775                         b = (Int16)c;
776                 }
777                 
778                 return 0;
779         }
780         
781         public static int test_0_conv_ovf_i2_un () {
782                 uint c;
783
784                 checked {
785                         c = 32767;
786                         Int16 b = (Int16)c;
787                 }
788                 
789                 return 0;
790         }
791         
792         public static int test_0_conv_ovf_u2 () {
793                 int c;
794
795                 checked {
796                         c = 65535;
797                         UInt16 b = (UInt16)c;
798                 }
799                 
800                 return 0;
801         }
802         
803         public static int test_0_conv_ovf_u2_un () {
804                 uint c;
805
806                 checked {
807                         c = 65535;
808                         UInt16 b = (UInt16)c;
809                 }
810                 
811                 return 0;
812         }
813         
814         public static int test_0_conv_ovf_u4 () {
815                 int c;
816
817                 checked {
818                         c = 0x7fffffff;
819                         uint b = (uint)c;
820                 }
821                 
822                 return 0;
823         }
824
825         public static int test_0_conv_ovf_i4_un () {
826                 uint c;
827
828                 checked {
829                         c = 0x7fffffff;
830                         int b = (int)c;
831                 }
832
833                 return 0;
834         }
835         
836         public static int test_0_bool () {
837                 bool val = true;
838                 if (val)
839                         return 0;
840                 return 1;
841         }
842         
843         public static int test_1_bool_inverted () {
844                 bool val = true;
845                 if (!val)
846                         return 0;
847                 return 1;
848         }
849
850         public static int test_1_bool_assign () {
851                 bool val = true;
852                 val = !val; // this should produce a ceq
853                 if (val)
854                         return 0;
855                 return 1;
856         }
857
858         public static int test_1_bool_multi () {
859                 bool val = true;
860                 bool val2 = true;
861                 val = !val;
862                 if ((val && !val2) && (!val2 && val))
863                         return 0;
864                 return 1;
865         }
866
867         public static int test_16_spill () {
868                 int a = 1;
869                 int b = 2;
870                 int c = 3;
871                 int d = 4;
872                 int e = 5;
873
874                 return (1 + (a + (b + (c + (d + e)))));
875         }
876
877         public static int test_1_switch () {
878                 int n = 0;
879
880                 switch (n) {
881                 case 0: return 1;
882                 case 1: return 2;
883                 case -1: return 3;
884                 default:
885                         return 4;
886                 }
887                 return 1;
888         }
889
890         public static int test_0_switch_constprop () {
891                 int n = -1;
892
893                 switch (n) {
894                 case 0: return 2;
895                 case 1: return 3;
896                 case 2: return 3;                       
897                 default:
898                         return 0;
899                 }
900                 return 3;
901         }
902
903         public static int test_0_switch_constprop2 () {
904                 int n = 3;
905
906                 switch (n) {
907                 case 0: return 2;
908                 case 1: return 3;
909                 case 2: return 3;                       
910                 default:
911                         return 0;
912                 }
913                 return 3;
914         }
915
916         public static int test_0_while_loop_1 () {
917
918                 int value = 255;
919                 
920                 do {
921                         value = value >> 4;
922                 } while (value != 0);
923                 
924                 return 0;
925         }
926
927         public static int test_0_while_loop_2 () {
928                 int value = 255;
929                 int position = 5;
930                 
931                 do {
932                         value = value >> 4;
933                 } while (value != 0 && position > 1);
934         
935                 return 0;
936         }
937
938         public static int test_0_char_conv () {
939                 int i = 1;
940                 
941                 char tc = (char) ('0' + i);
942
943                 if (tc != '1')
944                         return 1;
945                 
946                 return 0;
947         }
948
949         public static int test_3_shift_regalloc () {
950                 int shift = 8;
951                 int orig = 1;
952                 byte value = 0xfe;
953
954                 orig &= ~(0xff << shift);
955                 orig |= value << shift;
956
957                 if (orig == 0xfe01)
958                         return 3;
959                 return 0;
960         }
961
962         enum E {A, B};
963         
964         public static int test_2_optimize_branches () {
965                 switch (E.A) {
966                 case E.A:
967                         if (E.A == E.B) {
968                         }
969                         break;
970                 }
971                 return 2;
972         }
973
974         public static int test_0_checked_byte_cast () {
975                 int v = 250;
976                 int b = checked ((byte) (v));
977
978                 if (b != 250)
979                         return 1;
980                 return 0;
981         }
982
983         public static int test_0_checked_byte_cast_un () {
984                 uint v = 250;
985                 uint b = checked ((byte) (v));
986
987                 if (b != 250)
988                         return 1;
989                 return 0;
990         }
991
992         public static int test_0_checked_short_cast () {
993                 int v = 250;
994                 int b = checked ((ushort) (v));
995
996                 if (b != 250)
997                         return 1;
998                 return 0;
999         }
1000
1001         public static int test_0_checked_short_cast_un () {
1002                 uint v = 250;
1003                 uint b = checked ((ushort) (v));
1004
1005                 if (b != 250)
1006                         return 1;
1007                 return 0;
1008         }
1009         
1010         public static int test_1_a_eq_b_plus_a () {
1011                 int a = 0, b = 1;
1012                 a = b + a;
1013                 return a;
1014         }
1015
1016         public static int test_0_comp () {
1017                 int a = 0;
1018                 int b = -1;
1019                 int error = 1;
1020                 bool val;
1021
1022                 val = a < b;
1023                 if (val)
1024                         return error;
1025                 error++;
1026
1027                 val = a > b;
1028                 if (!val)
1029                         return error;
1030                 error ++;
1031
1032                 val = a == b;
1033                 if (val)
1034                         return error;
1035                 error ++;
1036
1037                 val = a == a;
1038                 if (!val)
1039                         return error;
1040                 error ++;
1041
1042                 return 0;
1043         }
1044
1045         public static int test_0_comp_unsigned () {
1046                 uint a = 1;
1047                 uint b = 0xffffffff;
1048                 int error = 1;
1049                 bool val;
1050
1051                 val = a < b;
1052                 if (!val)
1053                         return error;
1054                 error++;
1055
1056                 val = a <= b;
1057                 if (!val)
1058                         return error;
1059                 error++;
1060
1061                 val = a == b;
1062                 if (val)
1063                         return error;
1064                 error++;
1065
1066                 val = a >= b;
1067                 if (val)
1068                         return error;
1069                 error++;
1070
1071                 val = a > b;
1072                 if (val)
1073                         return error;
1074                 error++;
1075
1076                 val = b < a;
1077                 if (val)
1078                         return error;
1079                 error++;
1080
1081                 val = b <= a;
1082                 if (val)
1083                         return error;
1084                 error++;
1085
1086                 val = b == a;
1087                 if (val)
1088                         return error;
1089                 error++;
1090
1091                 val = b > a;
1092                 if (!val)
1093                         return error;
1094                 error++;
1095
1096                 val = b >= a;
1097                 if (!val)
1098                         return error;
1099                 error++;
1100
1101                 return 0;
1102         }
1103         
1104         public static int test_16_cmov () 
1105         {
1106                 int n = 0;
1107                 if (n == 0)
1108                         n = 16;
1109                 
1110                 return n;
1111         }
1112
1113         public static int test_0_and_cmp ()
1114         {
1115                 /* test esi, imm */
1116                 int local = 0x01020304;
1117                 
1118                 if ((local & 0x01020304) == 0)
1119                         return 7;
1120                 
1121                 if ((local & 0x00000304) == 0)
1122                         return 8;
1123                 
1124                 if ((local & 0x00000004) == 0)
1125                         return 9;
1126                 
1127                 if ((local & 0x00000300) == 0)
1128                         return 10;
1129                 
1130                 if ((local & 0x00020000) == 0)
1131                         return 11;
1132                 
1133                 if ((local & 0x01000000) == 0)
1134                         return 12;
1135
1136                 return 0;
1137         }
1138
1139         public static int test_0_mul_imm_opt ()
1140         {
1141                 int i;
1142
1143                 i = 1;
1144                 if ((i * 2) != 2)
1145                         return 1;
1146                 i = -1;
1147                 if ((i * 2) != -2)
1148                         return 2;
1149                 i = 1;
1150                 if ((i * 3) != 3)
1151                         return 3;
1152                 i = -1;
1153                 if ((i * 3) != -3)
1154                         return 4;
1155                 i = 1;
1156                 if ((i * 5) != 5)
1157                         return 5;
1158                 i = -1;
1159                 if ((i * 5) != -5)
1160                         return 6;
1161                 i = 1;
1162                 if ((i * 6) != 6)
1163                         return 7;
1164                 i = -1;
1165                 if ((i * 6) != -6)
1166                         return 8;
1167                 i = 1;
1168                 if ((i * 9) != 9)
1169                         return 9;
1170                 i = -1;
1171                 if ((i * 9) != -9)
1172                         return 10;
1173                 i = 1;
1174                 if ((i * 10) != 10)
1175                         return 11;
1176                 i = -1;
1177                 if ((i * 10) != -10)
1178                         return 12;
1179                 i = 1;
1180                 if ((i * 12) != 12)
1181                         return 13;
1182                 i = -1;
1183                 if ((i * 12) != -12)
1184                         return 14;
1185                 i = 1;
1186                 if ((i * 25) != 25)
1187                         return 15;
1188                 i = -1;
1189                 if ((i * 25) != -25)
1190                         return 16;
1191                 i = 1;
1192                 if ((i * 100) != 100)
1193                         return 17;
1194                 i = -1;
1195                 if ((i * 100) != -100)
1196                         return 18;
1197                 
1198                 return 0;
1199         }
1200         
1201         public static int test_0_cne ()
1202         {
1203                 int x = 0;
1204                 int y = 1;
1205                 
1206                 bool b = x != y;
1207                 bool bb = x != x;
1208                 
1209                 if (!b)
1210                         return 1;
1211                 if (bb)
1212                         return 2;
1213                 
1214                 return 0;
1215         }
1216
1217         public static int test_0_cmp_regvar_zero ()
1218         {
1219                 int n = 10;
1220                 
1221                 if (!(n > 0 && n >= 0 && n != 0))
1222                         return 1;
1223                 if (n < 0 || n <= 0 || n == 0)
1224                         return 1;
1225                 
1226                 return 0;
1227         }
1228
1229         public static int test_5_div_un_cfold ()
1230         {
1231                 uint i = 10;
1232                 uint j = 2;
1233
1234                 return (int)(i / j);
1235         }
1236
1237         public static int test_1_rem_un_cfold ()
1238         {
1239                 uint i = 11;
1240                 uint j = 2;
1241
1242                 return (int)(i % j);
1243         }
1244
1245         public static int test_0_div_opt () {
1246                 int i;
1247
1248                 // Avoid cfolding this
1249                 i = 0;
1250                 for (int j = 0; j < 567; ++j)
1251                         i ++;
1252                 i += 1234000;
1253                 if ((i / 2) != 617283)
1254                         return 1;
1255                 if ((i / 4) != 308641)
1256                         return 2;
1257                 if ((i / 8) != 154320)
1258                         return 3;
1259                 if ((i / 16) != 77160)
1260                         return 4;
1261
1262                 // Avoid cfolding this
1263                 i = 0;
1264                 for (int j = 0; j < 567; ++j)
1265                         i --;
1266                 i -= 1234000;
1267                 if ((i / 2) != -617283)
1268                         return 5;
1269                 if ((i / 4) != -308641)
1270                         return 6;
1271                 if ((i / 8) != -154320)
1272                         return 7;
1273                 if ((i / 16) != -77160)
1274                         return 8;
1275
1276                 return 0;
1277         }
1278
1279         public static int test_0_udiv_opt () {
1280                 uint i;
1281
1282                 // Avoid cfolding this
1283                 i = 0;
1284                 for (int j = 0; j < 567; ++j)
1285                         i ++;
1286                 i += 1234000;
1287                 if ((i / 2) != 617283)
1288                         return 1;
1289                 if ((i / 4) != 308641)
1290                         return 2;
1291                 if ((i / 8) != 154320)
1292                         return 3;
1293                 if ((i / 16) != 77160)
1294                         return 4;
1295
1296                 return 0;
1297         }
1298
1299         public static int test_0_signed_ct_div () {
1300                 int n = 2147483647;
1301                 bool divide_by_zero = false;
1302                 bool overflow = false;
1303
1304                 if ((n / 2147483647) != 1)
1305                         return 1;
1306                 if ((n / -2147483647) != -1)
1307                         return 2;
1308                 n = -n;
1309                 if ((n / 2147483647) != -1)
1310                         return 3;
1311                 n--; /* MinValue */
1312                 if ((n / -2147483648) != 1)
1313                         return 4;
1314                 if ((n / 2147483647) != -1)
1315                         return 5;
1316                 if ((n / 1) != n)
1317                         return 6;
1318
1319                 try {
1320                         int r = n / (-1);
1321                 } catch (OverflowException) {
1322                         overflow = true;
1323                 }
1324                 if (!overflow)
1325                         return 7;
1326
1327                 try {
1328                         int r = n / 0;
1329                 } catch (DivideByZeroException) {
1330                         divide_by_zero = true;
1331                 }
1332                 if (!divide_by_zero)
1333                         return 8;
1334
1335                 if ((n / 35) != -61356675)
1336                         return 9;
1337                 if ((n / -35) != 61356675)
1338                         return 10;
1339                 n = -(n + 1);  /* MaxValue */
1340                 if ((n / 35) != 61356675)
1341                         return 11;
1342                 if ((n / -35) != -61356675)
1343                         return 12;
1344
1345                 return 0;
1346         }
1347
1348         public static int test_0_unsigned_ct_div () {
1349                 uint n = 4294967295;
1350                 bool divide_by_zero = false;
1351
1352                 if ((n / 4294967295) != 1)
1353                         return 1;
1354                 n--;
1355                 if ((n / 4294967295) != 0)
1356                         return 2;
1357                 n++;
1358                 if ((n / 4294967294) != 1)
1359                         return 3;
1360                 if ((n / 1) != n)
1361                         return 4;
1362
1363                 try {
1364                         uint a = n / 0;
1365                 } catch (DivideByZeroException) {
1366                         divide_by_zero = true;
1367                 }
1368
1369                 if (!divide_by_zero)
1370                         return 5;
1371
1372                 if ((n / 35) != 122713351)
1373                         return 9;
1374
1375                 return 0;
1376         }
1377
1378         public static int test_0_rem_opt () {
1379                 int i;
1380
1381                 // Avoid cfolding this
1382                 i = 0;
1383                 for (int j = 0; j < 29; ++j)
1384                         i ++;
1385                 if ((i % 2) != 1)
1386                         return 1;
1387                 if ((i % 4) != 1)
1388                         return 2;
1389                 if ((i % 8) != 5)
1390                         return 3;
1391                 if ((i % 16) != 13)
1392                         return 4;
1393
1394                 // Avoid cfolding this
1395                 i = 0;
1396                 for (int j = 0; j < 29; ++j)
1397                         i --;
1398                 if ((i % 2) != -1)
1399                         return 5;
1400                 if ((i % 4) != -1)
1401                         return 6;
1402                 if ((i % 8) != -5)
1403                         return 7;
1404                 if ((i % 16) != -13)
1405                         return 8;
1406
1407                 return 0;
1408         }
1409
1410         public static int cmov (int i) {
1411                 int j = 0;
1412
1413                 if (i > 0)
1414                         j = 1;
1415
1416                 return j;
1417         }
1418
1419         public static int cmov2 (int i) {
1420                 int j = 0;
1421
1422                 if (i <= 0)
1423                         ;
1424                 else
1425                         j = 1;
1426
1427                 return j;
1428         }
1429                 
1430         public static int test_0_branch_to_cmov_opt () {
1431                 if (cmov (0) != 0)
1432                         return 1;
1433                 if (cmov (1) != 1)
1434                         return 2;
1435                 if (cmov2 (0) != 0)
1436                         return 1;
1437                 if (cmov2 (1) != 1)
1438                         return 2;
1439                 return 0;
1440         }
1441
1442         public static unsafe int test_0_ishr_sign_extend () {
1443                 // Check that ishr does sign extension from bit 31 on 64 bit platforms
1444                 uint val = 0xF0000000u;
1445
1446                 uint *a = &val;
1447                 uint ui = (uint)((int)(*a) >> 2);
1448
1449                 if (ui != 0xfc000000)
1450                         return 1;
1451
1452                 // Same with non-immediates
1453                 int amount = 2;
1454
1455                 ui = (uint)((int)(*a) >> amount);
1456
1457                 if (ui != 0xfc000000)
1458                         return 2;
1459
1460                 return 0;
1461         }
1462
1463         public static unsafe int test_0_ishr_sign_extend_cfold () {
1464                 int i = 32768;
1465                 int j = i << 16;
1466                 int k = j >> 16;
1467
1468                 return k == -32768 ? 0 : 1;
1469         }
1470 }