Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / mini / basic-float.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  * public 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 /* A comparison made to same variable. */
27 #pragma warning disable 1718
28
29 #if __MOBILE__
30 class FloatTests
31 #else
32 class Tests
33 #endif
34 {
35
36 #if !__MOBILE__
37         public static int Main (string[] args) {
38                 return TestDriver.RunTests (typeof (Tests), args);
39         }
40 #endif
41         
42         public static int test_0_beq () {
43                 double a = 2.0;
44                 if (a != 2.0)
45                         return 1;
46                 return 0;
47         }
48
49         public static int test_0_bne_un () {
50                 double a = 2.0;
51                 if (a == 1.0)
52                         return 1;
53                 return 0;
54         }
55
56         public static int test_0_conv_r8 () {
57                 double a = 2;
58                 if (a != 2.0)
59                         return 1;
60                 return 0;
61         }
62
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 */
97                 /*
98                 d = Double.MaxValue;
99                 i = (int)d;
100                 if (i != -2147483648)
101                         return 10;
102                 */
103
104                 return 0;
105         }
106
107         public static int test_5_conv_r4 () {
108                 int i = 5;
109                 float f = (float)i;
110                 return (int)f;
111         }
112
113         public static int test_0_conv_r4_m1 () {
114                 int i = -1;
115                 float f = (float)i;
116                 return (int)f + 1;
117         }
118
119         public static int test_5_double_conv_r4 () {
120                 double d = 5.0;
121                 float f = (float)d;
122                 return (int)f;
123         }
124
125         public static int test_5_float_conv_r8 () {
126                 float f = 5.0F;
127                 double d = (double)f;
128                 return (int)d;
129         }
130
131         public static int test_5_conv_r8 () {
132                 int i = 5;
133                 double f = (double)i;
134                 return (int)f;
135         }
136
137         public static int test_5_add () {
138                 double a = 2.0;
139                 double b = 3.0;         
140                 return (int)(a + b);
141         }
142
143         public static int test_5_sub () {
144                 double a = 8.0;
145                 double b = 3.0;         
146                 return (int)(a - b);
147         }       
148
149         public static int test_24_mul () {
150                 double a = 8.0;
151                 double b = 3.0;         
152                 return (int)(a * b);
153         }       
154
155         public static int test_4_div () {
156                 double a = 8.0;
157                 double b = 2.0;         
158                 return (int)(a / b);
159         }       
160
161         public static int test_2_rem () {
162                 double a = 8.0;
163                 double b = 3.0;         
164                 return (int)(a % b);
165         }       
166
167         public static int test_2_neg () {
168                 double a = -2.0;                
169                 return (int)(-a);
170         }
171         
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;
183
184                 return (int)(1.0 + (a + (b + (c + (d + (e + (f + (g + (h + i)))))))));
185         }
186
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;
198
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)))))))));
201         }
202
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;
214
215                 return (int)(1.0 * (a * (b * (c * (d * (e * (f * (g * (h * i)))))))));
216         }
217
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;
230         }
231
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;
243         }
244
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;
257         }
258
259         public static int test_0_lconv_to_r8 () {
260                 long a = 150;
261                 double b = (double) a;
262
263                 if (b != 150.0)
264                         return 1;
265                 return 0;
266         }
267
268         public static int test_0_lconv_to_r4 () {
269                 long a = 3000;
270                 float b = (float) a;
271
272                 if (b != 3000.0F)
273                         return 1;
274                 return 0;
275         }
276
277         static void doit (double value, out long m) {
278                 m = (long) value;
279         }
280
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;
287         }
288
289         public static int test_0_rounding () {
290                 long ticks = 631502475130080000L;
291                 long ticksperday = 864000000000L;
292
293                 double days = (double) ticks / ticksperday;
294
295                 if ((int)days != 730905)
296                         return 1;
297
298                 return 0;
299         }
300
301         /* FIXME: This only works on little-endian machines */
302         /*
303         static unsafe int test_2_negative_zero () {
304                 int result = 0;
305                 double d = -0.0;
306                 float f = -0.0f;
307
308                 byte *ptr = (byte*)&d;
309                 if (ptr [7] == 0)
310                         return result;
311                 result ++;
312
313                 ptr = (byte*)&f;
314                 if (ptr [3] == 0)
315                         return result;
316                 result ++;
317
318                 return result;
319         }
320         */
321
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;
327                 
328                 val = a == a;
329                 if (!val)
330                         return result;
331                 result++;
332
333                 val = (a != a);
334                 if (val)
335                         return result;
336                 result++;
337
338                 val = a < a;
339                 if (val)
340                         return result;
341                 result++;
342
343                 val = a > a;
344                 if (val)
345                         return result;
346                 result++;
347
348                 val = a <= a;
349                 if (!val)
350                         return result;
351                 result++;
352
353                 val = a >= a;
354                 if (!val)
355                         return result;
356                 result++;
357
358                 val = b == a;
359                 if (val)
360                         return result;
361                 result++;
362
363                 val = b < a;
364                 if (!val)
365                         return result;
366                 result++;
367
368                 val = b > a;
369                 if (val)
370                         return result;
371                 result++;
372
373                 val = b <= a;
374                 if (!val)
375                         return result;
376                 result++;
377
378                 val = b >= a;
379                 if (val)
380                         return result;
381                 result++;
382
383                 val = a == b;
384                 if (val)
385                         return result;
386                 result++;
387
388                 val = a < b;
389                 if (val)
390                         return result;
391                 result++;
392
393                 val = a > b;
394                 if (!val)
395                         return result;
396                 result++;
397
398                 val = a <= b;
399                 if (val)
400                         return result;
401                 result++;
402
403                 val = a >= b;
404                 if (!val)
405                         return result;
406                 result++;
407
408                 return result;
409         }
410
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;
416                 
417                 val = a == a;
418                 if (val)
419                         return result;
420                 result++;
421
422                 val = a < a;
423                 if (val)
424                         return result;
425                 result++;
426
427                 val = a > a;
428                 if (val)
429                         return result;
430                 result++;
431
432                 val = a <= a;
433                 if (val)
434                         return result;
435                 result++;
436
437                 val = a >= a;
438                 if (val)
439                         return result;
440                 result++;
441
442                 val = b == a;
443                 if (val)
444                         return result;
445                 result++;
446
447                 val = b < a;
448                 if (val)
449                         return result;
450                 result++;
451
452                 val = b > a;
453                 if (val)
454                         return result;
455                 result++;
456
457                 val = b <= a;
458                 if (val)
459                         return result;
460                 result++;
461
462                 val = b >= a;
463                 if (val)
464                         return result;
465                 result++;
466
467                 val = a == b;
468                 if (val)
469                         return result;
470                 result++;
471
472                 val = a < b;
473                 if (val)
474                         return result;
475                 result++;
476
477                 val = a > b;
478                 if (val)
479                         return result;
480                 result++;
481
482                 val = a <= b;
483                 if (val)
484                         return result;
485                 result++;
486
487                 val = a >= b;
488                 if (val)
489                         return result;
490                 result++;
491
492                 return result;
493         }
494
495         public static int test_15_float_branch () {
496                 double a = 2.0;
497                 double b = 1.0;
498                 int result = 0;
499                 
500                 if (!(a == a))
501                         return result;
502                 result++;
503
504                 if (a < a)
505                         return result;
506                 result++;
507
508                 if (a > a)
509                         return result;
510                 result++;
511
512                 if (!(a <= a))
513                         return result;
514                 result++;
515
516                 if (!(a >= a))
517                         return result;
518                 result++;
519
520                 if (b == a)
521                         return result;
522                 result++;
523
524                 if (!(b < a))
525                         return result;
526                 result++;
527
528                 if (b > a)
529                         return result;
530                 result++;
531
532                 if (!(b <= a))
533                         return result;
534                 result++;
535
536                 if (b >= a)
537                         return result;
538                 result++;
539
540                 if (a == b)
541                         return result;
542                 result++;
543
544                 if (a < b)
545                         return result;
546                 result++;
547
548                 if (!(a > b))
549                         return result;
550                 result++;
551
552                 if (a <= b)
553                         return result;
554                 result++;
555
556                 if (!(a >= b))
557                         return result;
558                 result++;
559
560                 return result;
561         }
562
563         public static int test_15_float_branch_un () {
564                 double a = Double.NaN;
565                 double b = 1.0;
566                 int result = 0;
567                 
568                 if (a == a)
569                         return result;
570                 result++;
571
572                 if (a < a)
573                         return result;
574                 result++;
575
576                 if (a > a)
577                         return result;
578                 result++;
579
580                 if (a <= a)
581                         return result;
582                 result++;
583
584                 if (a >= a)
585                         return result;
586                 result++;
587
588                 if (b == a)
589                         return result;
590                 result++;
591
592                 if (b < a)
593                         return result;
594                 result++;
595
596                 if (b > a)
597                         return result;
598                 result++;
599
600                 if (b <= a)
601                         return result;
602                 result++;
603
604                 if (b >= a)
605                         return result;
606                 result++;
607
608                 if (a == b)
609                         return result;
610                 result++;
611
612                 if (a < b)
613                         return result;
614                 result++;
615
616                 if (a > b)
617                         return result;
618                 result++;
619
620                 if (a <= b)
621                         return result;
622                 result++;
623
624                 if (a >= b)
625                         return result;
626                 result++;
627
628                 return result;
629         }
630
631         public static int test_0_float_precision () {
632                 float f1 = 3.40282346638528859E+38f;
633                 float f2 = 3.40282346638528859E+38f;            
634                 float PositiveInfinity =  (float)(1.0f / 0.0f);
635                 float f = (float)(f1 + f2);
636
637                 return f == PositiveInfinity ? 0 : 1;
638         }
639
640         static double VALUE = 0.19975845134874831D;
641
642         public static int test_0_float_conversion_reduces_double_precision () {
643                 double d = (float)VALUE;
644                 if (d != 0.19975845515727997d)
645                         return 1;
646
647                 return 0;
648         }
649
650         /* This doesn't work with llvm */
651         /*
652     public static int test_0_long_to_double_conversion ()
653     {
654                 long l = 9223372036854775807L;
655                 long conv = (long)((double)l);
656                 if (conv != -9223372036854775808L)
657                         return 1;
658
659                 return 0;
660     }
661         */
662
663         public static int INT_VAL = 0x13456799;
664
665         public static int test_0_int4_to_float_convertion ()
666     {
667                 double d = (double)(float)INT_VAL;
668
669                 if (d != 323315616)
670                         return 1;
671                 return 0;
672         }
673
674         public static int test_0_int8_to_float_convertion ()
675     {
676                 double d = (double)(float)(long)INT_VAL;
677
678                 if (d != 323315616)
679                         return 1;
680                 return 0;
681         }
682
683         public static int test_5_r4_fadd () {
684                 float f1 = 3.0f;
685                 float f2 = 2.0f;
686                 return (int)(f1 + f2);
687         }
688
689         public static int test_1_r4_fsub () {
690                 float f1 = 3.0f;
691                 float f2 = 2.0f;
692                 return (int)(f1 - f2);
693         }
694
695         public static int test_6_fmul_r4 () {
696                 float f1 = 2.0f;
697                 float f2 = 3.0f;
698                 return (int)(f1 * f2);
699         }
700
701         public static int test_3_fdiv_r4 () {
702                 float f1 = 6.0f;
703                 float f2 = 2.0f;
704                 return (int)(f1 / f2);
705         }
706
707         public static int test_1_frem_r4 () {
708                 float f1 = 7.0f;
709                 float f2 = 2.0f;
710                 return (int)(f1 % f2);
711         }
712
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;
717         }
718
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;
723         }
724
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;
729         }
730
731         public static int test_3_iconv_to_r4 () {
732                 int i = 3;
733                 float f = (float)i;
734                 return (int)f;
735         }
736
737         public static int test_2_neg_r4 () {
738                 float a = -2.0f;
739                 return (int)(-a);
740         }
741
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;
747         }
748
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;
755         }
756
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;
763         }
764
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;
771         }
772
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;
779         }
780
781         public static int test_0_fconv_to_u4_r4 () {
782                 float a = 10.0f;
783
784                 uint b = (uint)a;
785                 return b == 10 ? 0 : 1;
786         }
787
788         public static int test_0_fconv_to_u1_r4 () {
789                 float a = 10.0f;
790
791                 byte b = (byte)a;
792                 return b == 10 ? 0 : 1;
793         }
794
795         public static int test_0_fconv_to_i1_r4 () {
796                 float a = 127.0f;
797
798                 sbyte b = (sbyte)a;
799                 return b == 127 ? 0 : 1;
800         }
801
802         public static int test_0_fconv_to_u2_r4 () {
803                 float a = 10.0f;
804
805                 ushort b = (ushort)a;
806                 return b == 10 ? 0 : 1;
807         }
808
809         public static int test_0_fconv_to_i2_r4 () {
810                 float a = 127.0f;
811
812                 short b = (short)a;
813                 return b == 127 ? 0 : 1;
814         }
815
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;
821         }
822
823 }
824