Merge pull request #649 from DavidS/feature/implement-additional-reference-path
[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                 return 0;
84         }
85
86         public static int test_5_conv_r4 () {
87                 int i = 5;
88                 float f = (float)i;
89                 return (int)f;
90         }
91
92         public static int test_0_conv_r4_m1 () {
93                 int i = -1;
94                 float f = (float)i;
95                 return (int)f + 1;
96         }
97
98         public static int test_5_double_conv_r4 () {
99                 double d = 5.0;
100                 float f = (float)d;
101                 return (int)f;
102         }
103
104         public static int test_5_float_conv_r8 () {
105                 float f = 5.0F;
106                 double d = (double)f;
107                 return (int)d;
108         }
109
110         public static int test_5_conv_r8 () {
111                 int i = 5;
112                 double f = (double)i;
113                 return (int)f;
114         }
115
116         public static int test_5_add () {
117                 double a = 2.0;
118                 double b = 3.0;         
119                 return (int)(a + b);
120         }
121
122         public static int test_5_sub () {
123                 double a = 8.0;
124                 double b = 3.0;         
125                 return (int)(a - b);
126         }       
127
128         public static int test_24_mul () {
129                 double a = 8.0;
130                 double b = 3.0;         
131                 return (int)(a * b);
132         }       
133
134         public static int test_4_div () {
135                 double a = 8.0;
136                 double b = 2.0;         
137                 return (int)(a / b);
138         }       
139
140         public static int test_2_rem () {
141                 double a = 8.0;
142                 double b = 3.0;         
143                 return (int)(a % b);
144         }       
145
146         public static int test_2_neg () {
147                 double a = -2.0;                
148                 return (int)(-a);
149         }
150         
151         public static int test_46_float_add_spill () {
152                 // we overflow the FP stack
153                 double a = 1;
154                 double b = 2;
155                 double c = 3;
156                 double d = 4;
157                 double e = 5;
158                 double f = 6;
159                 double g = 7;
160                 double h = 8;
161                 double i = 9;
162
163                 return (int)(1.0 + (a + (b + (c + (d + (e + (f + (g + (h + i)))))))));
164         }
165
166         public static int test_4_float_sub_spill () {
167                 // we overflow the FP stack
168                 double a = 1;
169                 double b = 2;
170                 double c = 3;
171                 double d = 4;
172                 double e = 5;
173                 double f = 6;
174                 double g = 7;
175                 double h = 8;
176                 double i = 9;
177
178                 return -(int)(1.0 - (a - (b - (c - (d - (e - (f - (g - (h - i)))))))));
179                 ////// -(int)(1.0 - (1 - (2 - (3 - (4 - (5 - (6 - (7 - (8 - 9)))))))));
180         }
181
182         public static int test_362880_float_mul_spill () {
183                 // we overflow the FP stack
184                 double a = 1;
185                 double b = 2;
186                 double c = 3;
187                 double d = 4;
188                 double e = 5;
189                 double f = 6;
190                 double g = 7;
191                 double h = 8;
192                 double i = 9;
193
194                 return (int)(1.0 * (a * (b * (c * (d * (e * (f * (g * (h * i)))))))));
195         }
196
197         public static int test_4_long_cast () {
198                 long a = 1000;
199                 double d = (double)a;
200                 long b = (long)d;
201                 if (b != 1000)
202                         return 0;
203                 a = -1;
204                 d = (double)a;
205                 b = (long)d;
206                 if (b != -1)
207                         return 1;
208                 return 4;
209         }
210
211         public static int test_4_ulong_cast () {
212                 ulong a = 1000;
213                 double d = (double)a;
214                 ulong b = (ulong)d;
215                 if (b != 1000)
216                         return 0;
217                 a = 0xffffffffffffffff;
218                 float f = (float)a;
219                 if (!(f > 0f))
220                         return 1;
221                 return 4;
222         }
223
224         public static int test_4_single_long_cast () {
225                 long a = 1000;
226                 float d = (float)a;
227                 long b = (long)d;
228                 if (b != 1000)
229                         return 0;
230                 a = -1;
231                 d = (float)a;
232                 b = (long)d;
233                 if (b != -1)
234                         return 1;
235                 return 4;
236         }
237
238         public static int test_0_lconv_to_r8 () {
239                 long a = 150;
240                 double b = (double) a;
241
242                 if (b != 150.0)
243                         return 1;
244                 return 0;
245         }
246
247         public static int test_0_lconv_to_r4 () {
248                 long a = 3000;
249                 float b = (float) a;
250
251                 if (b != 3000.0F)
252                         return 1;
253                 return 0;
254         }
255
256         static void doit (double value, out long m) {
257                 m = (long) value;
258         }
259
260         public static int test_0_ftol_clobber () {
261                 long m;
262                 doit (1.3, out m);
263                 if (m != 1)
264                         return 2;
265                 return 0;
266         }
267
268         public static int test_0_rounding () {
269                 long ticks = 631502475130080000L;
270                 long ticksperday = 864000000000L;
271
272                 double days = (double) ticks / ticksperday;
273
274                 if ((int)days != 730905)
275                         return 1;
276
277                 return 0;
278         }
279
280         /* FIXME: This only works on little-endian machines */
281         /*
282         static unsafe int test_2_negative_zero () {
283                 int result = 0;
284                 double d = -0.0;
285                 float f = -0.0f;
286
287                 byte *ptr = (byte*)&d;
288                 if (ptr [7] == 0)
289                         return result;
290                 result ++;
291
292                 ptr = (byte*)&f;
293                 if (ptr [3] == 0)
294                         return result;
295                 result ++;
296
297                 return result;
298         }
299         */
300
301         public static int test_16_float_cmp () {
302                 double a = 2.0;
303                 double b = 1.0;
304                 int result = 0;
305                 bool val;
306                 
307                 val = a == a;
308                 if (!val)
309                         return result;
310                 result++;
311
312                 val = (a != a);
313                 if (val)
314                         return result;
315                 result++;
316
317                 val = a < a;
318                 if (val)
319                         return result;
320                 result++;
321
322                 val = a > a;
323                 if (val)
324                         return result;
325                 result++;
326
327                 val = a <= a;
328                 if (!val)
329                         return result;
330                 result++;
331
332                 val = a >= a;
333                 if (!val)
334                         return result;
335                 result++;
336
337                 val = b == a;
338                 if (val)
339                         return result;
340                 result++;
341
342                 val = b < a;
343                 if (!val)
344                         return result;
345                 result++;
346
347                 val = b > a;
348                 if (val)
349                         return result;
350                 result++;
351
352                 val = b <= a;
353                 if (!val)
354                         return result;
355                 result++;
356
357                 val = b >= a;
358                 if (val)
359                         return result;
360                 result++;
361
362                 val = a == b;
363                 if (val)
364                         return result;
365                 result++;
366
367                 val = a < b;
368                 if (val)
369                         return result;
370                 result++;
371
372                 val = a > b;
373                 if (!val)
374                         return result;
375                 result++;
376
377                 val = a <= b;
378                 if (val)
379                         return result;
380                 result++;
381
382                 val = a >= b;
383                 if (!val)
384                         return result;
385                 result++;
386
387                 return result;
388         }
389
390         public static int test_15_float_cmp_un () {
391                 double a = Double.NaN;
392                 double b = 1.0;
393                 int result = 0;
394                 bool val;
395                 
396                 val = a == a;
397                 if (val)
398                         return result;
399                 result++;
400
401                 val = a < a;
402                 if (val)
403                         return result;
404                 result++;
405
406                 val = a > a;
407                 if (val)
408                         return result;
409                 result++;
410
411                 val = a <= a;
412                 if (val)
413                         return result;
414                 result++;
415
416                 val = a >= a;
417                 if (val)
418                         return result;
419                 result++;
420
421                 val = b == a;
422                 if (val)
423                         return result;
424                 result++;
425
426                 val = b < a;
427                 if (val)
428                         return result;
429                 result++;
430
431                 val = b > a;
432                 if (val)
433                         return result;
434                 result++;
435
436                 val = b <= a;
437                 if (val)
438                         return result;
439                 result++;
440
441                 val = b >= a;
442                 if (val)
443                         return result;
444                 result++;
445
446                 val = a == b;
447                 if (val)
448                         return result;
449                 result++;
450
451                 val = a < b;
452                 if (val)
453                         return result;
454                 result++;
455
456                 val = a > b;
457                 if (val)
458                         return result;
459                 result++;
460
461                 val = a <= b;
462                 if (val)
463                         return result;
464                 result++;
465
466                 val = a >= b;
467                 if (val)
468                         return result;
469                 result++;
470
471                 return result;
472         }
473
474         public static int test_15_float_branch () {
475                 double a = 2.0;
476                 double b = 1.0;
477                 int result = 0;
478                 
479                 if (!(a == a))
480                         return result;
481                 result++;
482
483                 if (a < a)
484                         return result;
485                 result++;
486
487                 if (a > a)
488                         return result;
489                 result++;
490
491                 if (!(a <= a))
492                         return result;
493                 result++;
494
495                 if (!(a >= a))
496                         return result;
497                 result++;
498
499                 if (b == a)
500                         return result;
501                 result++;
502
503                 if (!(b < a))
504                         return result;
505                 result++;
506
507                 if (b > a)
508                         return result;
509                 result++;
510
511                 if (!(b <= a))
512                         return result;
513                 result++;
514
515                 if (b >= a)
516                         return result;
517                 result++;
518
519                 if (a == b)
520                         return result;
521                 result++;
522
523                 if (a < b)
524                         return result;
525                 result++;
526
527                 if (!(a > b))
528                         return result;
529                 result++;
530
531                 if (a <= b)
532                         return result;
533                 result++;
534
535                 if (!(a >= b))
536                         return result;
537                 result++;
538
539                 return result;
540         }
541
542         public static int test_15_float_branch_un () {
543                 double a = Double.NaN;
544                 double b = 1.0;
545                 int result = 0;
546                 
547                 if (a == a)
548                         return result;
549                 result++;
550
551                 if (a < a)
552                         return result;
553                 result++;
554
555                 if (a > a)
556                         return result;
557                 result++;
558
559                 if (a <= a)
560                         return result;
561                 result++;
562
563                 if (a >= a)
564                         return result;
565                 result++;
566
567                 if (b == a)
568                         return result;
569                 result++;
570
571                 if (b < a)
572                         return result;
573                 result++;
574
575                 if (b > a)
576                         return result;
577                 result++;
578
579                 if (b <= a)
580                         return result;
581                 result++;
582
583                 if (b >= a)
584                         return result;
585                 result++;
586
587                 if (a == b)
588                         return result;
589                 result++;
590
591                 if (a < b)
592                         return result;
593                 result++;
594
595                 if (a > b)
596                         return result;
597                 result++;
598
599                 if (a <= b)
600                         return result;
601                 result++;
602
603                 if (a >= b)
604                         return result;
605                 result++;
606
607                 return result;
608         }
609
610         public static int test_0_float_precision () {
611                 float f1 = 3.40282346638528859E+38f;
612                 float f2 = 3.40282346638528859E+38f;            
613                 float PositiveInfinity =  1.0f / 0.0f;
614                 float f = f1 + f2;
615
616                 return f == PositiveInfinity ? 0 : 1;
617         }
618
619         /* 
620            Disabled until they can be fixed to run on amd64
621
622         static double VALUE = 0.19975845134874831D;
623
624         public static int test_0_float_conversion_reduces_double_precision () {
625                 double d = (float)VALUE;
626                 if (d != 0.19975845515727997d)
627                         return 1;
628
629                 return 0;
630         }
631
632
633     public static int test_0_long_to_double_conversion ()
634     {
635                 long l = 9223372036854775807L;
636                 long conv = (long)((double)l);
637                 if (conv != -9223372036854775808L)
638                         return 1;
639
640                 return 0;
641     }
642
643         public static int INT_VAL = 0x13456799;
644
645         public static int test_0_int4_to_float_convertion ()
646     {
647                 double d = (double)(float)INT_VAL;
648
649                 if (d != 323315616)
650                         return 1;
651                 return 0;
652         }
653         */
654 }
655