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