if we're intentionally using power-of-two sizing for chunks, don't add the header...
[mono-project/dkf.git] / mono / mini / basic-long.cs
blob5373a5ed92dd3f602f53b305fb96a9fa1d595e94
1 using System;
2 using System.Reflection;
4 /*
5 * Regression tests for the mono JIT.
7 * Each test needs to be of the form:
9 * public static int test_<result>_<name> ();
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.
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)
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.
26 class Tests {
28 public static int Main () {
29 return TestDriver.RunTests (typeof (Tests));
32 public static int test_10_simple_cast () {
33 long a = 10;
34 return (int)a;
37 public static int test_1_bigmul1 () {
38 int a;
39 int b;
40 long c;
41 a = 10;
42 b = 10;
43 c = (long)a * (long)b;
44 if (c == 100)
45 return 1;
46 return 0;
49 public static int test_1_bigmul2 () {
50 int a = System.Int32.MaxValue, b = System.Int32.MaxValue;
51 long s = System.Int64.MinValue;
52 long c;
53 c = s + (long) a * (long) b;
54 if (c == -4611686022722355199)
55 return 1;
56 return 0;
59 public static int test_1_bigmul3 () {
60 int a = 10, b = 10;
61 ulong c;
62 c = (ulong) a * (ulong) b;
63 if (c == 100)
64 return 1;
65 return 0;
68 public static int test_1_bigmul4 () {
69 int a = System.Int32.MaxValue, b = System.Int32.MaxValue;
70 ulong c;
71 c = (ulong) a * (ulong) b;
72 if (c == 4611686014132420609)
73 return 1;
74 return 0;
77 public static int test_1_bigmul5 () {
78 int a = System.Int32.MaxValue, b = System.Int32.MinValue;
79 long c;
80 c = (long) a * (long) b;
81 if (c == -4611686016279904256)
82 return 1;
83 return 0;
86 public static int test_1_bigmul6 () {
87 uint a = System.UInt32.MaxValue, b = System.UInt32.MaxValue/(uint)2;
88 ulong c;
89 c = (ulong) a * (ulong) b;
90 if (c == 9223372030412324865)
91 return 1;
92 return 0;
95 public static int test_0_beq () {
96 long a = 0xffffffffff;
97 if (a != 0xffffffffff)
98 return 1;
99 return 0;
102 public static int test_0_bne_un () {
103 long a = 0xffffffffff;
104 if (a == 0xfffffffffe)
105 return 1;
106 if (a == 0xfeffffffff)
107 return 2;
108 return 0;
111 public static int test_0_ble () {
112 long a = 0xffffffffff;
113 if (a > 0xffffffffff)
114 return 1;
116 if (a > 0x1ffffffffff)
117 return 2;
119 if (a > 0xff00000000) {} else
120 return 3;
122 if (a > 0xfeffffffff) {} else
123 return 4;
125 a = 0xff00000000;
126 if (a > 0xffffffffff)
127 return 5;
129 return 0;
132 public static int test_0_ble_un () {
133 ulong a = 0xffffffffff;
134 if (a > 0xffffffffff)
135 return 1;
137 if (a > 0x1ffffffffff)
138 return 2;
140 if (a > 0xff00000000) {} else
141 return 3;
143 if (a > 0xfeffffffff) {} else
144 return 4;
146 a = 0xff00000000;
147 if (a > 0xffffffffff)
148 return 5;
150 return 0;
153 public static int test_0_bge () {
154 long a = 0xffffffffff;
155 if (a < 0xffffffffff)
156 return 1;
158 if (a < 0x1ffffffffff) {} else
159 return 2;
161 if (a < 0xff00000000)
162 return 3;
164 if (a < 0xfeffffffff)
165 return 4;
167 a = 0xff00000000;
168 if (a < 0xffffffffff) {} else
169 return 5;
171 return 0;
174 public static int test_0_bge_un () {
175 ulong a = 0xffffffffff;
176 if (a < 0xffffffffff)
177 return 1;
179 if (a < 0x1ffffffffff) {} else
180 return 2;
182 if (a < 0xff00000000)
183 return 3;
185 if (a < 0xfeffffffff)
186 return 4;
188 a = 0xff00000000;
189 if (a < 0xffffffffff) {} else
190 return 5;
192 return 0;
195 public static int test_0_blt () {
196 long a = 0xfffffffffe;
197 if (a >= 0xffffffffff)
198 return 1;
200 if (a >= 0x1fffffffffe)
201 return 2;
203 if (a >= 0xff00000000) {} else
204 return 3;
206 if (a >= 0xfefffffffe) {} else
207 return 4;
209 a = 0xff00000000;
210 if (a >= 0xffffffffff)
211 return 5;
213 return 0;
216 public static int test_0_blt_un () {
217 ulong a = 0xfffffffffe;
218 if (a >= 0xffffffffff)
219 return 1;
221 if (a >= 0x1fffffffffe)
222 return 2;
224 if (a >= 0xff00000000) {} else
225 return 3;
227 if (a >= 0xfefffffffe) {} else
228 return 4;
230 a = 0xff00000000;
231 if (a >= 0xffffffffff)
232 return 5;
234 return 0;
237 public static int test_0_bgt () {
238 long a = 0xffffffffff;
239 if (a <= 0xfffffffffe)
240 return 1;
242 if (a <= 0x1ffffffffff) {} else
243 return 2;
245 if (a <= 0xff00000000)
246 return 3;
248 if (a <= 0xfeffffffff)
249 return 4;
251 a = 0xff00000000;
252 if (a <= 0xffffffffff) {} else
253 return 5;
255 return 0;
258 public static int test_0_bgt_un () {
259 ulong a = 0xffffffffff;
260 if (a <= 0xfffffffffe)
261 return 1;
263 if (a <= 0x1ffffffffff) {} else
264 return 2;
266 if (a <= 0xff00000000)
267 return 3;
269 if (a <= 0xfeffffffff)
270 return 4;
272 a = 0xff00000000;
273 if (a <= 0xffffffffff) {} else
274 return 5;
276 return 0;
279 public static int test_0_conv_to_i4 () {
280 long a = 0;
282 return (int)a;
285 public static int test_32_conv_to_u4 () {
286 long a = 32;
288 return (int)(uint)a;
291 public static int test_15_conv_to_u4_2 () {
292 long a = 0x10000000f;
294 return (int)(uint)a;
297 public static int test_0_conv_from_i4 () {
298 long a = 2;
299 if (a != 2)
300 return 1;
302 int b = 2;
304 if (a != b)
305 return 2;
306 return 0;
309 public static int test_0_conv_from_i4_negative () {
310 long a = -2;
311 if (a != -2)
312 return 1;
314 int b = -2;
316 if (a != b)
317 return 2;
318 return 0;
322 public static int test_0_conv_from_r8 () {
323 double b = 2.0;
324 long a = (long)b;
326 if (a != 2)
327 return 1;
328 return 0;
331 public static int test_0_conv_from_r4 () {
332 float b = 2.0F;
333 long a = (long)b;
335 if (a != 2)
336 return 1;
337 return 0;
341 public static int test_8_and () {
342 long a = 0xffffffffff;
343 long b = 8;
344 return (int)(a & b);
347 public static int test_8_and_imm () {
348 long a = 0xffffffffff;
349 return (int)(a & 8);
352 public static int get_high_bit (ulong a) {
353 if ((a & 0x8000000000000000) != 0)
354 return 1;
355 return 0;
358 public static int test_1_and () {
359 ulong a = 0xabcd1234deadbeef;
360 return get_high_bit (a);
363 public static int test_10_or () {
364 long a = 8;
365 long b = 2;
366 return (int)(a | b);
369 public static int test_10_or_imm () {
370 long a = 8;
371 return (int)(a | 2);
374 public static int test_5_xor () {
375 long a = 7;
376 long b = 2;
377 return (int)(a ^ b);
380 public static int test_5_xor_imm () {
381 long a = 7;
382 return (int)(a ^ 2);
385 public static int test_5_add () {
386 long a = 2;
387 long b = 3;
388 return (int)(a + b);
391 public static int test_5_add_imm () {
392 long a = 2;
393 return (int)(a + 3);
396 public static int test_0_add_imm_carry () {
397 long a = -1;
398 return (int)(a + 1);
401 public static int test_0_add_imm_no_inc () {
402 // we can't blindly convert an add x, 1 to an inc x
403 long a = 0x1ffffffff;
404 long c;
405 c = a + 2;
406 if (c == ((a + 1) + 1))
407 return 0;
408 return 1;
411 public static int test_4_addcc_imm () {
412 long a = 3;
413 long b = 0;
414 return (int)(a - b + 1);
417 public static int test_5_sub () {
418 long a = 8;
419 long b = 3;
420 return (int)(a - b);
423 public static int test_5_sub_imm () {
424 long a = 8;
425 return (int)(a - 3);
428 public static int test_0_sub_imm_carry () {
429 long a = 0;
430 return (int)((a - 1) + 1);
433 public static int test_0_add_ovf () {
434 long i, j, k;
436 checked {
437 i = System.Int64.MinValue;
438 j = 0;
439 k = i + j;
442 if (k != System.Int64.MinValue)
443 return 1;
445 checked {
446 i = System.Int64.MaxValue;
447 j = 0;
448 k = i + j;
451 if (k != System.Int64.MaxValue)
452 return 2;
454 checked {
455 i = System.Int64.MinValue;
456 j = System.Int64.MaxValue;
457 k = i + j;
460 if (k != -1)
461 return 3;
463 checked {
464 i = System.Int64.MaxValue;
465 j = System.Int64.MinValue;
466 k = i + j;
469 if (k != -1)
470 return 4;
472 checked {
473 i = System.Int64.MinValue + 1234;
474 j = -1234;
475 k = i + j;
478 if (k != System.Int64.MinValue)
479 return 5;
481 checked {
482 i = System.Int64.MaxValue - 1234;
483 j = 1234;
484 k = i + j;
487 if (k != System.Int64.MaxValue)
488 return 6;
490 return 0;
493 public static int test_0_add_un_ovf () {
494 ulong n = (ulong)134217728 * 16;
495 ulong number = checked (n + (uint)0);
497 return number == n ? 0 : 1;
500 public static int test_0_sub_ovf () {
501 long i, j, k;
503 checked {
504 i = System.Int64.MinValue;
505 j = 0;
506 k = i - j;
509 if (k != System.Int64.MinValue)
510 return 1;
512 checked {
513 i = System.Int64.MaxValue;
514 j = 0;
515 k = i - j;
518 if (k != System.Int64.MaxValue)
519 return 2;
521 checked {
522 i = System.Int64.MinValue;
523 j = System.Int64.MinValue + 1234;
524 k = i - j;
527 if (k != -1234)
528 return 3;
530 checked {
531 i = System.Int64.MaxValue;
532 j = 1234;
533 k = i - j;
536 if (k != System.Int64.MaxValue - 1234)
537 return 4;
539 checked {
540 i = System.Int64.MaxValue - 1234;
541 j = -1234;
542 k = i - j;
545 if (k != System.Int64.MaxValue)
546 return 5;
548 checked {
549 i = System.Int64.MinValue + 1234;
550 j = 1234;
551 k = i - j;
554 if (k != System.Int64.MinValue)
555 return 6;
557 return 0;
560 public static int test_0_sub_ovf_un () {
561 ulong i, j, k;
563 checked {
564 i = System.UInt64.MaxValue;
565 j = 0;
566 k = i - j;
569 if (k != System.UInt64.MaxValue)
570 return 1;
572 checked {
573 i = System.UInt64.MaxValue;
574 j = System.UInt64.MaxValue;
575 k = i - j;
578 if (k != 0)
579 return 2;
581 return 0;
584 public static int test_2_neg () {
585 long a = -2;
586 return (int)(-a);
589 public static int test_0_neg_large () {
590 long min = -9223372036854775808;
591 unchecked {
592 ulong ul = (ulong)min;
593 return (min == -(long)ul) ? 0 : 1;
597 public static int test_5_shift ()
599 long a = 9;
600 int b = 1;
601 int count = 0;
603 if ((a >> b) != 4)
604 return count;
605 count++;
607 if ((a >> 63) != 0)
608 return count;
609 count++;
611 if ((a << 1) != 18)
612 return count;
613 count++;
615 if ((a << b) != 18)
616 return count;
617 count++;
619 a = -9;
620 if ((a >> b) != -5)
621 return count;
622 count++;
624 return count;
627 public static int test_1_shift_u ()
629 ulong a;
630 int count = 0;
632 // The JIT optimizes this
633 a = 8589934592UL;
634 if ((a >> 32) != 2)
635 return 0;
636 count ++;
638 return count;
641 public static int test_1_shift_u_32 ()
643 ulong a;
644 int count = 0;
646 a = UInt64.MaxValue;
647 // Avoid constant folding
648 for (int i = 0; i < 32; ++i)
649 count ++;
651 if ((a >> count) != 0xFFFFFFFFUL)
652 return 0;
653 else
654 return 1;
657 public static int test_1_simple_neg () {
658 long a = 9;
660 if (-a != -9)
661 return 0;
662 return 1;
665 public static int test_2_compare () {
666 long a = 1;
667 long b = 1;
669 if (a != b)
670 return 0;
671 return 2;
674 public static int test_9_alu ()
676 long a = 9, b = 6;
677 int count = 0;
679 if ((a + b) != 15)
680 return count;
681 count++;
683 if ((a - b) != 3)
684 return count;
685 count++;
687 if ((a & 8) != 8)
688 return count;
689 count++;
691 if ((a | 2) != 11)
692 return count;
693 count++;
695 if ((a * b) != 54)
696 return count;
697 count++;
699 if ((a / 4) != 2)
700 return count;
701 count++;
703 if ((a % 4) != 1)
704 return count;
705 count++;
707 if (-a != -9)
708 return count;
709 count++;
711 b = -1;
712 if (~b != 0)
713 return count;
714 count++;
716 return count;
719 public static int test_24_mul () {
720 long a = 8;
721 long b = 3;
722 return (int)(a * b);
725 public static int test_24_mul_ovf () {
726 long a = 8;
727 long b = 3;
728 long res;
730 checked {
731 res = a * b;
733 return (int)res;
736 public static int test_24_mul_un () {
737 ulong a = 8;
738 ulong b = 3;
739 return (int)(a * b);
742 public static int test_24_mul_ovf_un () {
743 ulong a = 8;
744 ulong b = 3;
745 ulong res;
747 checked {
748 res = a * b;
750 return (int)res;
753 public static int test_0_mul_imm () {
754 long i = 4;
756 if ((i * 0) != 0)
757 return 1;
758 if ((i * 1) != 4)
759 return 2;
760 if ((i * 2) != 8)
761 return 3;
762 if ((i * 3) != 12)
763 return 4;
764 if ((i * 1234) != 4936)
765 return 5;
766 if ((i * -1) != -4)
767 return 6;
768 if ((i * -2) != -8)
769 return 7;
770 if ((i * -3) != -12)
771 return 8;
772 if ((i * -1234) != -4936)
773 return 9;
775 return 0;
778 public static int test_0_mul_imm_opt ()
780 long i;
782 i = 1;
783 if ((i * 2) != 2)
784 return 1;
785 i = -1;
786 if ((i * 2) != -2)
787 return 2;
788 i = 1;
789 if ((i * 3) != 3)
790 return 3;
791 i = -1;
792 if ((i * 3) != -3)
793 return 4;
794 i = 1;
795 if ((i * 5) != 5)
796 return 5;
797 i = -1;
798 if ((i * 5) != -5)
799 return 6;
800 i = 1;
801 if ((i * 6) != 6)
802 return 7;
803 i = -1;
804 if ((i * 6) != -6)
805 return 8;
806 i = 1;
807 if ((i * 9) != 9)
808 return 9;
809 i = -1;
810 if ((i * 9) != -9)
811 return 10;
812 i = 1;
813 if ((i * 10) != 10)
814 return 11;
815 i = -1;
816 if ((i * 10) != -10)
817 return 12;
818 i = 1;
819 if ((i * 12) != 12)
820 return 13;
821 i = -1;
822 if ((i * 12) != -12)
823 return 14;
824 i = 1;
825 if ((i * 25) != 25)
826 return 15;
827 i = -1;
828 if ((i * 25) != -25)
829 return 16;
830 i = 1;
831 if ((i * 100) != 100)
832 return 17;
833 i = -1;
834 if ((i * 100) != -100)
835 return 18;
837 return 0;
840 public static int test_4_divun () {
841 uint b = 12;
842 int a = 3;
843 return (int)(b / a);
846 public static int test_1431655764_bigdivun_imm () {
847 unchecked {
848 uint b = (uint)-2;
849 return (int)(b / 3);
853 public static int test_1431655764_bigdivun () {
854 unchecked {
855 uint b = (uint)-2;
856 int a = 3;
857 return (int)(b / a);
861 public static int test_1_remun () {
862 uint b = 13;
863 int a = 3;
864 return (int)(b % a);
867 public static int test_2_bigremun () {
868 unchecked {
869 uint b = (uint)-2;
870 int a = 3;
871 return (int)(b % a);
875 public static int test_0_ceq () {
876 long a = 2;
877 long b = 2;
878 long c = 3;
879 long d = 0xff00000002;
881 bool val = (a == b); // this should produce a ceq
882 if (!val)
883 return 1;
885 val = (a == c); // this should produce a ceq
886 if (val)
887 return 2;
889 val = (a == d); // this should produce a ceq
890 if (val)
891 return 3;
893 return 0;
896 public static int test_0_ceq_complex () {
897 long l = 1, ll = 2;
899 if (l < 0 != ll < 0)
900 return 1;
902 return 0;
905 public static int test_0_clt () {
906 long a = 2;
907 long b = 2;
908 long c = 3;
909 long d = 0xff00000002L;
910 long e = -1;
912 bool val = (a < b); // this should produce a clt
913 if (val)
914 return 1;
916 val = (a < c); // this should produce a clt
917 if (!val)
918 return 2;
920 val = (c < a); // this should produce a clt
921 if (val)
922 return 3;
924 val = (e < d); // this should produce a clt
925 if (!val)
926 return 4;
928 val = (d < e); // this should produce a clt
929 if (val)
930 return 5;
932 return 0;
935 public static int test_0_clt_un () {
936 ulong a = 2;
937 ulong b = 2;
938 ulong c = 3;
939 ulong d = 0xff00000002;
940 ulong e = 0xffffffffffffffff;
942 bool val = (a < b); // this should produce a clt_un
943 if (val)
944 return 1;
946 val = (a < c); // this should produce a clt_un
947 if (!val)
948 return 1;
950 val = (d < e); // this should produce a clt_un
951 if (!val)
952 return 1;
954 val = (e < d); // this should produce a clt_un
955 if (val)
956 return 1;
958 return 0;
961 public static int test_0_cgt () {
962 long a = 2;
963 long b = 2;
964 long c = 3;
965 long d = 0xff00000002L;
966 long e = -1;
968 bool val = (a > b); // this should produce a cgt
969 if (val)
970 return 1;
972 val = (a > c); // this should produce a cgt
973 if (val)
974 return 2;
976 val = (c > a); // this should produce a cgt
977 if (!val)
978 return 3;
980 val = (e > d); // this should produce a cgt
981 if (val)
982 return 4;
984 val = (d > e); // this should produce a cgt
985 if (!val)
986 return 5;
988 return 0;
991 public static int test_0_cgt_un () {
992 ulong a = 2;
993 ulong b = 2;
994 ulong c = 3;
995 ulong d = 0xff00000002;
996 ulong e = 0xffffffffffffffff;
998 bool val = (a > b); // this should produce a cgt_un
999 if (val)
1000 return 1;
1002 val = (a > c); // this should produce a cgt_un
1003 if (val)
1004 return 1;
1006 val = (d > e); // this should produce a cgt_un
1007 if (val)
1008 return 1;
1010 val = (e > d); // this should produce a cgt_un
1011 if (!val)
1012 return 1;
1014 return 0;
1017 public static int test_3_byte_cast () {
1018 ulong val = 0xff00ff00f0f0f0f0;
1019 byte b;
1020 b = (byte) (val & 0xFF);
1021 if (b != 0xf0)
1022 return 1;
1024 return 3;
1027 public static int test_4_ushort_cast () {
1028 ulong val = 0xff00ff00f0f0f0f0;
1029 ushort b;
1030 b = (ushort) (val & 0xFFFF);
1031 if (b != 0xf0f0)
1032 return 1;
1033 return 4;
1036 public static int test_500_mul_div () {
1037 long val = 1000;
1038 long exp = 10;
1039 long maxexp = 20;
1040 long res = val * exp / maxexp;
1042 return (int)res;
1045 public static int test_3_checked_cast_un () {
1046 ulong i = 2;
1047 long j;
1049 checked { j = (long)i; }
1051 if (j != 2)
1052 return 0;
1053 return 3;
1056 public static int test_4_checked_cast () {
1057 long i = 3;
1058 ulong j;
1060 checked { j = (ulong)i; }
1062 if (j != 3)
1063 return 0;
1064 return 4;
1067 public static int test_12_checked_i1_cast () {
1068 long l = 12;
1070 checked {
1071 return (sbyte)l;
1075 public static int test_127_checked_i1_cast_un () {
1076 ulong l = 127;
1078 checked {
1079 return (sbyte)l;
1083 public static int test_1234_checked_i2_cast () {
1084 long l = 1234;
1086 checked {
1087 return (short)l;
1091 public static int test_32767_checked_i2_cast_un () {
1092 ulong l = 32767;
1094 checked {
1095 return (ushort)l;
1099 public static int test_1234_checked_i4_cast () {
1100 ulong ul = 1234;
1102 checked {
1103 return (int)ul;
1107 public static int test_10_int_uint_compare () {
1108 uint size = 10;
1109 int j = 0;
1110 for (int i = 0; i < size; ++i) {
1111 j++;
1113 return j;
1116 public static int test_0_ulong_regress () {
1117 ulong u = 4257145737;
1118 u --;
1119 return (u == 4257145736) ? 0 : 1;
1122 public static int test_0_ulong_regress2 () {
1123 int p2 = 31;
1124 ulong sum_p = 2897079476 + (ulong) (1 << p2);
1125 return (sum_p == 749595828) ? 0 : 1;
1128 public static int test_0_assemble_long ()
1130 uint a = 5;
1131 ulong x = 0x12345678;
1132 ulong y = 1;
1135 ulong z = ((x - y) << 32) | a;
1137 if (z != 0x1234567700000005)
1138 return 1;
1140 return 0;
1143 public static int test_0_hash ()
1145 ulong x = 0x1234567887654321;
1146 int h = (int)(x & 0xffffffff) ^ (int)(x >> 32);
1147 if (h != unchecked ((int)(0x87654321 ^ 0x12345678)))
1148 return h;
1149 return 0;
1153 public static int test_0_shift_regress () {
1154 long a = 0;
1155 int b = 6;
1156 UInt16 c = 3;
1158 return ((a >> (b - c)) == 0) ? 0 : 1;
1161 public static int test_1234_conv_ovf_u8 () {
1162 int i = 1234;
1164 checked {
1165 ulong l = (ulong)i;
1166 return (int)l;
1170 public static int test_0_regress_cprop_80738 () {
1171 int hours = Int32.MinValue;
1172 int hrssec = (hours * 3600);
1173 long t = ((long)(hrssec) * 1000L);
1175 return t == 0 ? 0 : 1;
1178 public static int test_0_conv_u () {
1179 unsafe {
1180 int** dead = (int**) 0xdeadbeaf;
1181 long i = (long)dead;
1182 return (i == 0xdeadbeaf) ? 0 : 1;
1186 public static int test_0_lconv_to_u2 () {
1187 unchecked {
1188 ulong value = (ulong)(short)-10;
1189 value = (ushort)value;
1190 return (value == 65526) ? 0 : 1;