NaCl runtime fixes
[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 (string[] args) {
32                 return TestDriver.RunTests (typeof (Tests), args);
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                 a = 0xffffffffffffffff;
211                 float f = (float)a;
212                 if (!(f > 0f))
213                         return 1;
214                 return 4;
215         }
216
217         public static int test_4_single_long_cast () {
218                 long a = 1000;
219                 float d = (float)a;
220                 long b = (long)d;
221                 if (b != 1000)
222                         return 0;
223                 a = -1;
224                 d = (float)a;
225                 b = (long)d;
226                 if (b != -1)
227                         return 1;
228                 return 4;
229         }
230
231         public static int test_0_lconv_to_r8 () {
232                 long a = 150;
233                 double b = (double) a;
234
235                 if (b != 150.0)
236                         return 1;
237                 return 0;
238         }
239
240         public static int test_0_lconv_to_r4 () {
241                 long a = 3000;
242                 float b = (float) a;
243
244                 if (b != 3000.0F)
245                         return 1;
246                 return 0;
247         }
248
249         static void doit (double value, out long m) {
250                 m = (long) value;
251         }
252
253         public static int test_0_ftol_clobber () {
254                 long m;
255                 doit (1.3, out m);
256                 if (m != 1)
257                         return 2;
258                 return 0;
259         }
260
261         public static int test_0_rounding () {
262                 long ticks = 631502475130080000L;
263                 long ticksperday = 864000000000L;
264
265                 double days = (double) ticks / ticksperday;
266
267                 if ((int)days != 730905)
268                         return 1;
269
270                 return 0;
271         }
272
273         /* FIXME: This only works on little-endian machines */
274         /*
275         static unsafe int test_2_negative_zero () {
276                 int result = 0;
277                 double d = -0.0;
278                 float f = -0.0f;
279
280                 byte *ptr = (byte*)&d;
281                 if (ptr [7] == 0)
282                         return result;
283                 result ++;
284
285                 ptr = (byte*)&f;
286                 if (ptr [3] == 0)
287                         return result;
288                 result ++;
289
290                 return result;
291         }
292         */
293
294         public static int test_16_float_cmp () {
295                 double a = 2.0;
296                 double b = 1.0;
297                 int result = 0;
298                 bool val;
299                 
300                 val = a == a;
301                 if (!val)
302                         return result;
303                 result++;
304
305                 val = (a != a);
306                 if (val)
307                         return result;
308                 result++;
309
310                 val = a < a;
311                 if (val)
312                         return result;
313                 result++;
314
315                 val = a > a;
316                 if (val)
317                         return result;
318                 result++;
319
320                 val = a <= a;
321                 if (!val)
322                         return result;
323                 result++;
324
325                 val = a >= a;
326                 if (!val)
327                         return result;
328                 result++;
329
330                 val = b == a;
331                 if (val)
332                         return result;
333                 result++;
334
335                 val = b < a;
336                 if (!val)
337                         return result;
338                 result++;
339
340                 val = b > a;
341                 if (val)
342                         return result;
343                 result++;
344
345                 val = b <= a;
346                 if (!val)
347                         return result;
348                 result++;
349
350                 val = b >= a;
351                 if (val)
352                         return result;
353                 result++;
354
355                 val = a == b;
356                 if (val)
357                         return result;
358                 result++;
359
360                 val = a < b;
361                 if (val)
362                         return result;
363                 result++;
364
365                 val = a > b;
366                 if (!val)
367                         return result;
368                 result++;
369
370                 val = a <= b;
371                 if (val)
372                         return result;
373                 result++;
374
375                 val = a >= b;
376                 if (!val)
377                         return result;
378                 result++;
379
380                 return result;
381         }
382
383         public static int test_15_float_cmp_un () {
384                 double a = Double.NaN;
385                 double b = 1.0;
386                 int result = 0;
387                 bool val;
388                 
389                 val = a == a;
390                 if (val)
391                         return result;
392                 result++;
393
394                 val = a < a;
395                 if (val)
396                         return result;
397                 result++;
398
399                 val = a > a;
400                 if (val)
401                         return result;
402                 result++;
403
404                 val = a <= a;
405                 if (val)
406                         return result;
407                 result++;
408
409                 val = a >= a;
410                 if (val)
411                         return result;
412                 result++;
413
414                 val = b == a;
415                 if (val)
416                         return result;
417                 result++;
418
419                 val = b < a;
420                 if (val)
421                         return result;
422                 result++;
423
424                 val = b > a;
425                 if (val)
426                         return result;
427                 result++;
428
429                 val = b <= a;
430                 if (val)
431                         return result;
432                 result++;
433
434                 val = b >= a;
435                 if (val)
436                         return result;
437                 result++;
438
439                 val = a == b;
440                 if (val)
441                         return result;
442                 result++;
443
444                 val = a < b;
445                 if (val)
446                         return result;
447                 result++;
448
449                 val = a > b;
450                 if (val)
451                         return result;
452                 result++;
453
454                 val = a <= b;
455                 if (val)
456                         return result;
457                 result++;
458
459                 val = a >= b;
460                 if (val)
461                         return result;
462                 result++;
463
464                 return result;
465         }
466
467         public static int test_15_float_branch () {
468                 double a = 2.0;
469                 double b = 1.0;
470                 int result = 0;
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 (!(a >= 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 (b >= a)
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                 if (!(a >= b))
529                         return result;
530                 result++;
531
532                 return result;
533         }
534
535         public static int test_15_float_branch_un () {
536                 double a = Double.NaN;
537                 double b = 1.0;
538                 int result = 0;
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 (a >= 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 (b >= a)
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                 if (a >= b)
597                         return result;
598                 result++;
599
600                 return result;
601         }
602
603         public static int test_0_float_precision () {
604                 float f1 = 3.40282346638528859E+38f;
605                 float f2 = 3.40282346638528859E+38f;            
606                 float PositiveInfinity =  1.0f / 0.0f;
607                 float f = f1 + f2;
608
609                 return f == PositiveInfinity ? 0 : 1;
610         }
611
612         /* 
613            Disabled until they can be fixed to run on amd64
614
615         static double VALUE = 0.19975845134874831D;
616
617         public static int test_0_float_conversion_reduces_double_precision () {
618                 double d = (float)VALUE;
619                 if (d != 0.19975845515727997d)
620                         return 1;
621
622                 return 0;
623         }
624
625
626     public static int test_0_long_to_double_conversion ()
627     {
628                 long l = 9223372036854775807L;
629                 long conv = (long)((double)l);
630                 if (conv != -9223372036854775808L)
631                         return 1;
632
633                 return 0;
634     }
635
636         public static int INT_VAL = 0x13456799;
637
638         public static int test_0_int4_to_float_convertion ()
639     {
640                 double d = (double)(float)INT_VAL;
641
642                 if (d != 323315616)
643                         return 1;
644                 return 0;
645         }
646         */
647 }
648