[mono-api-info] Use XmlWriter instead of XmlDocument to make this faster.
[mono-project.git] / mono / mini / basic-float.cs
blob9415256e94fd78049813135bd7df3a1b2e723629
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 /* A comparison made to same variable. */
27 #pragma warning disable 1718
29 #if __MOBILE__
30 class FloatTests
31 #else
32 class Tests
33 #endif
36 #if !__MOBILE__
37 public static int Main (string[] args) {
38 return TestDriver.RunTests (typeof (Tests), args);
40 #endif
42 public static int test_0_beq () {
43 double a = 2.0;
44 if (a != 2.0)
45 return 1;
46 return 0;
49 public static int test_0_bne_un () {
50 double a = 2.0;
51 if (a == 1.0)
52 return 1;
53 return 0;
56 public static int test_0_conv_r8 () {
57 double a = 2;
58 if (a != 2.0)
59 return 1;
60 return 0;
63 public static int test_0_conv_i () {
64 double a = 2.0;
65 int i = (int)a;
66 if (i != 2)
67 return 1;
68 uint ui = (uint)a;
69 if (ui != 2)
70 return 2;
71 short s = (short)a;
72 if (s != 2)
73 return 3;
74 ushort us = (ushort)a;
75 if (us != 2)
76 return 4;
77 byte b = (byte)a;
78 if (b != 2)
79 return 5;
80 sbyte sb = (sbyte)a;
81 if (sb != 2)
82 return 6;
83 /* MS.NET special cases these */
84 double d = Double.NaN;
85 ui = (uint)d;
86 if (ui != 0)
87 return 7;
88 d = Double.PositiveInfinity;
89 ui = (uint)d;
90 if (ui != 0)
91 return 8;
92 d = Double.NegativeInfinity;
93 ui = (uint)d;
94 if (ui != 0)
95 return 9;
96 /* FIXME: This fails with llvm and with gcc -O2 on osx/linux */
98 d = Double.MaxValue;
99 i = (int)d;
100 if (i != -2147483648)
101 return 10;
104 return 0;
107 public static int test_5_conv_r4 () {
108 int i = 5;
109 float f = (float)i;
110 return (int)f;
113 public static int test_0_conv_r4_m1 () {
114 int i = -1;
115 float f = (float)i;
116 return (int)f + 1;
119 public static int test_5_double_conv_r4 () {
120 double d = 5.0;
121 float f = (float)d;
122 return (int)f;
125 public static int test_5_float_conv_r8 () {
126 float f = 5.0F;
127 double d = (double)f;
128 return (int)d;
131 public static int test_5_conv_r8 () {
132 int i = 5;
133 double f = (double)i;
134 return (int)f;
137 public static int test_5_add () {
138 double a = 2.0;
139 double b = 3.0;
140 return (int)(a + b);
143 public static int test_5_sub () {
144 double a = 8.0;
145 double b = 3.0;
146 return (int)(a - b);
149 public static int test_24_mul () {
150 double a = 8.0;
151 double b = 3.0;
152 return (int)(a * b);
155 public static int test_4_div () {
156 double a = 8.0;
157 double b = 2.0;
158 return (int)(a / b);
161 public static int test_2_rem () {
162 double a = 8.0;
163 double b = 3.0;
164 return (int)(a % b);
167 public static int test_2_neg () {
168 double a = -2.0;
169 return (int)(-a);
172 public static int test_46_float_add_spill () {
173 // we overflow the FP stack
174 double a = 1;
175 double b = 2;
176 double c = 3;
177 double d = 4;
178 double e = 5;
179 double f = 6;
180 double g = 7;
181 double h = 8;
182 double i = 9;
184 return (int)(1.0 + (a + (b + (c + (d + (e + (f + (g + (h + i)))))))));
187 public static int test_4_float_sub_spill () {
188 // we overflow the FP stack
189 double a = 1;
190 double b = 2;
191 double c = 3;
192 double d = 4;
193 double e = 5;
194 double f = 6;
195 double g = 7;
196 double h = 8;
197 double i = 9;
199 return -(int)(1.0 - (a - (b - (c - (d - (e - (f - (g - (h - i)))))))));
200 ////// -(int)(1.0 - (1 - (2 - (3 - (4 - (5 - (6 - (7 - (8 - 9)))))))));
203 public static int test_362880_float_mul_spill () {
204 // we overflow the FP stack
205 double a = 1;
206 double b = 2;
207 double c = 3;
208 double d = 4;
209 double e = 5;
210 double f = 6;
211 double g = 7;
212 double h = 8;
213 double i = 9;
215 return (int)(1.0 * (a * (b * (c * (d * (e * (f * (g * (h * i)))))))));
218 public static int test_4_long_cast () {
219 long a = 1000;
220 double d = (double)a;
221 long b = (long)d;
222 if (b != 1000)
223 return 0;
224 a = -1;
225 d = (double)a;
226 b = (long)d;
227 if (b != -1)
228 return 1;
229 return 4;
232 public static int test_4_ulong_cast () {
233 ulong a = 1000;
234 double d = (double)a;
235 ulong b = (ulong)d;
236 if (b != 1000)
237 return 0;
238 a = 0xffffffffffffffff;
239 float f = (float)a;
240 if (!(f > 0f))
241 return 1;
242 return 4;
245 public static int test_4_single_long_cast () {
246 long a = 1000;
247 float d = (float)a;
248 long b = (long)d;
249 if (b != 1000)
250 return 0;
251 a = -1;
252 d = (float)a;
253 b = (long)d;
254 if (b != -1)
255 return 1;
256 return 4;
259 public static int test_0_lconv_to_r8 () {
260 long a = 150;
261 double b = (double) a;
263 if (b != 150.0)
264 return 1;
265 return 0;
268 public static int test_0_lconv_to_r4 () {
269 long a = 3000;
270 float b = (float) a;
272 if (b != 3000.0F)
273 return 1;
274 return 0;
277 static void doit (double value, out long m) {
278 m = (long) value;
281 public static int test_0_ftol_clobber () {
282 long m;
283 doit (1.3, out m);
284 if (m != 1)
285 return 2;
286 return 0;
289 public static int test_0_rounding () {
290 long ticks = 631502475130080000L;
291 long ticksperday = 864000000000L;
293 double days = (double) ticks / ticksperday;
295 if ((int)days != 730905)
296 return 1;
298 return 0;
301 /* FIXME: This only works on little-endian machines */
303 static unsafe int test_2_negative_zero () {
304 int result = 0;
305 double d = -0.0;
306 float f = -0.0f;
308 byte *ptr = (byte*)&d;
309 if (ptr [7] == 0)
310 return result;
311 result ++;
313 ptr = (byte*)&f;
314 if (ptr [3] == 0)
315 return result;
316 result ++;
318 return result;
322 public static int test_16_float_cmp () {
323 double a = 2.0;
324 double b = 1.0;
325 int result = 0;
326 bool val;
328 val = a == a;
329 if (!val)
330 return result;
331 result++;
333 val = (a != a);
334 if (val)
335 return result;
336 result++;
338 val = a < a;
339 if (val)
340 return result;
341 result++;
343 val = a > a;
344 if (val)
345 return result;
346 result++;
348 val = a <= a;
349 if (!val)
350 return result;
351 result++;
353 val = a >= a;
354 if (!val)
355 return result;
356 result++;
358 val = b == a;
359 if (val)
360 return result;
361 result++;
363 val = b < a;
364 if (!val)
365 return result;
366 result++;
368 val = b > a;
369 if (val)
370 return result;
371 result++;
373 val = b <= a;
374 if (!val)
375 return result;
376 result++;
378 val = b >= a;
379 if (val)
380 return result;
381 result++;
383 val = a == b;
384 if (val)
385 return result;
386 result++;
388 val = a < b;
389 if (val)
390 return result;
391 result++;
393 val = a > b;
394 if (!val)
395 return result;
396 result++;
398 val = a <= b;
399 if (val)
400 return result;
401 result++;
403 val = a >= b;
404 if (!val)
405 return result;
406 result++;
408 return result;
411 public static int test_15_float_cmp_un () {
412 double a = Double.NaN;
413 double b = 1.0;
414 int result = 0;
415 bool val;
417 val = a == a;
418 if (val)
419 return result;
420 result++;
422 val = a < a;
423 if (val)
424 return result;
425 result++;
427 val = a > a;
428 if (val)
429 return result;
430 result++;
432 val = a <= a;
433 if (val)
434 return result;
435 result++;
437 val = a >= a;
438 if (val)
439 return result;
440 result++;
442 val = b == a;
443 if (val)
444 return result;
445 result++;
447 val = b < a;
448 if (val)
449 return result;
450 result++;
452 val = b > a;
453 if (val)
454 return result;
455 result++;
457 val = b <= a;
458 if (val)
459 return result;
460 result++;
462 val = b >= a;
463 if (val)
464 return result;
465 result++;
467 val = a == b;
468 if (val)
469 return result;
470 result++;
472 val = a < b;
473 if (val)
474 return result;
475 result++;
477 val = a > b;
478 if (val)
479 return result;
480 result++;
482 val = a <= b;
483 if (val)
484 return result;
485 result++;
487 val = a >= b;
488 if (val)
489 return result;
490 result++;
492 return result;
495 public static int test_15_float_branch () {
496 double a = 2.0;
497 double b = 1.0;
498 int result = 0;
500 if (!(a == a))
501 return result;
502 result++;
504 if (a < a)
505 return result;
506 result++;
508 if (a > a)
509 return result;
510 result++;
512 if (!(a <= a))
513 return result;
514 result++;
516 if (!(a >= a))
517 return result;
518 result++;
520 if (b == a)
521 return result;
522 result++;
524 if (!(b < a))
525 return result;
526 result++;
528 if (b > a)
529 return result;
530 result++;
532 if (!(b <= a))
533 return result;
534 result++;
536 if (b >= a)
537 return result;
538 result++;
540 if (a == b)
541 return result;
542 result++;
544 if (a < b)
545 return result;
546 result++;
548 if (!(a > b))
549 return result;
550 result++;
552 if (a <= b)
553 return result;
554 result++;
556 if (!(a >= b))
557 return result;
558 result++;
560 return result;
563 public static int test_15_float_branch_un () {
564 double a = Double.NaN;
565 double b = 1.0;
566 int result = 0;
568 if (a == a)
569 return result;
570 result++;
572 if (a < a)
573 return result;
574 result++;
576 if (a > a)
577 return result;
578 result++;
580 if (a <= a)
581 return result;
582 result++;
584 if (a >= a)
585 return result;
586 result++;
588 if (b == a)
589 return result;
590 result++;
592 if (b < a)
593 return result;
594 result++;
596 if (b > a)
597 return result;
598 result++;
600 if (b <= a)
601 return result;
602 result++;
604 if (b >= a)
605 return result;
606 result++;
608 if (a == b)
609 return result;
610 result++;
612 if (a < b)
613 return result;
614 result++;
616 if (a > b)
617 return result;
618 result++;
620 if (a <= b)
621 return result;
622 result++;
624 if (a >= b)
625 return result;
626 result++;
628 return result;
631 public static int test_0_float_precision () {
632 float f1 = 3.40282346638528859E+38f;
633 float f2 = 3.40282346638528859E+38f;
634 float PositiveInfinity = 1.0f / 0.0f;
635 float f = f1 + f2;
637 return f == PositiveInfinity ? 0 : 1;
640 static double VALUE = 0.19975845134874831D;
642 public static int test_0_float_conversion_reduces_double_precision () {
643 double d = (float)VALUE;
644 if (d != 0.19975845515727997d)
645 return 1;
647 return 0;
650 /* This doesn't work with llvm */
652 public static int test_0_long_to_double_conversion ()
654 long l = 9223372036854775807L;
655 long conv = (long)((double)l);
656 if (conv != -9223372036854775808L)
657 return 1;
659 return 0;
663 public static int INT_VAL = 0x13456799;
665 public static int test_0_int4_to_float_convertion ()
667 double d = (double)(float)INT_VAL;
669 if (d != 323315616)
670 return 1;
671 return 0;
674 public static int test_0_int8_to_float_convertion ()
676 double d = (double)(float)(long)INT_VAL;
678 if (d != 323315616)
679 return 1;
680 return 0;
683 public static int test_5_r4_fadd () {
684 float f1 = 3.0f;
685 float f2 = 2.0f;
686 return (int)(f1 + f2);
689 public static int test_1_r4_fsub () {
690 float f1 = 3.0f;
691 float f2 = 2.0f;
692 return (int)(f1 - f2);
695 public static int test_6_fmul_r4 () {
696 float f1 = 2.0f;
697 float f2 = 3.0f;
698 return (int)(f1 * f2);
701 public static int test_3_fdiv_r4 () {
702 float f1 = 6.0f;
703 float f2 = 2.0f;
704 return (int)(f1 / f2);
707 public static int test_1_frem_r4 () {
708 float f1 = 7.0f;
709 float f2 = 2.0f;
710 return (int)(f1 % f2);
713 public static int test_0_fcmp_eq_r4 () {
714 float f1 = 1.0f;
715 float f2 = 1.0f;
716 return f1 == f2 ? 0 : 1;
719 public static int test_0_fcmp_eq_2_r4 () {
720 float f1 = 1.0f;
721 float f2 = 2.0f;
722 return f1 == f2 ? 1 : 0;
725 public static int test_0_fcmp_eq_r4_mixed () {
726 float f1 = 1.0f;
727 double f2 = 1.0;
728 return f1 == f2 ? 0 : 1;
731 public static int test_3_iconv_to_r4 () {
732 int i = 3;
733 float f = (float)i;
734 return (int)f;
737 public static int test_2_neg_r4 () {
738 float a = -2.0f;
739 return (int)(-a);
742 public static int test_0_fceq_r4 () {
743 float f1 = 1.0f;
744 float f2 = 1.0f;
745 bool res = f1 == f2;
746 return res ? 0 : 1;
749 public static int test_0_fcgt_r4 () {
750 float f1 = 2.0f;
751 float f2 = 1.0f;
752 bool res = f1 > f2;
753 bool res2 = f2 > f1;
754 return res && !res2 ? 0 : 1;
757 public static int test_0_fclt_r4 () {
758 float f1 = 1.0f;
759 float f2 = 2.0f;
760 bool res = f1 < f2;
761 bool res2 = f2 < f1;
762 return res && !res2 ? 0 : 1;
765 public static int test_0_fclt_un_r4 () {
766 float f1 = 2.0f;
767 float f2 = 1.0f;
768 bool res = f1 >= f2;
769 bool res2 = f2 >= f1;
770 return res && !res2 ? 0 : 1;
773 public static int test_0_fcgt_un_r4 () {
774 float f1 = 1.0f;
775 float f2 = 2.0f;
776 bool res = f1 <= f2;
777 bool res2 = f2 <= f1;
778 return res && !res2 ? 0 : 1;
781 public static int test_0_fconv_to_u4_r4 () {
782 float a = 10.0f;
784 uint b = (uint)a;
785 return b == 10 ? 0 : 1;
788 public static int test_0_fconv_to_u1_r4 () {
789 float a = 10.0f;
791 byte b = (byte)a;
792 return b == 10 ? 0 : 1;
795 public static int test_0_fconv_to_i1_r4 () {
796 float a = 127.0f;
798 sbyte b = (sbyte)a;
799 return b == 127 ? 0 : 1;
802 public static int test_0_fconv_to_u2_r4 () {
803 float a = 10.0f;
805 ushort b = (ushort)a;
806 return b == 10 ? 0 : 1;
809 public static int test_0_fconv_to_i2_r4 () {
810 float a = 127.0f;
812 short b = (short)a;
813 return b == 127 ? 0 : 1;
816 public static int test_10_rconv_to_u8 () {
817 ulong l = 10;
818 float f = (float)l;
819 l = (ulong)f;
820 return (int)l;