update rx (mobile builds).
[mono-project.git] / mono / mini / basic-float.cs
blobdf8f6666f0d4b2c3144cd7730a74f52a09851393
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 class Tests {
31 public static int Main () {
32 return TestDriver.RunTests (typeof (Tests));
35 public static int test_0_beq () {
36 double a = 2.0;
37 if (a != 2.0)
38 return 1;
39 return 0;
42 public static int test_0_bne_un () {
43 double a = 2.0;
44 if (a == 1.0)
45 return 1;
46 return 0;
49 public static int test_0_conv_r8 () {
50 double a = 2;
51 if (a != 2.0)
52 return 1;
53 return 0;
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;
79 public static int test_5_conv_r4 () {
80 int i = 5;
81 float f = (float)i;
82 return (int)f;
85 public static int test_0_conv_r4_m1 () {
86 int i = -1;
87 float f = (float)i;
88 return (int)f + 1;
91 public static int test_5_double_conv_r4 () {
92 double d = 5.0;
93 float f = (float)d;
94 return (int)f;
97 public static int test_5_float_conv_r8 () {
98 float f = 5.0F;
99 double d = (double)f;
100 return (int)d;
103 public static int test_5_conv_r8 () {
104 int i = 5;
105 double f = (double)i;
106 return (int)f;
109 public static int test_5_add () {
110 double a = 2.0;
111 double b = 3.0;
112 return (int)(a + b);
115 public static int test_5_sub () {
116 double a = 8.0;
117 double b = 3.0;
118 return (int)(a - b);
121 public static int test_24_mul () {
122 double a = 8.0;
123 double b = 3.0;
124 return (int)(a * b);
127 public static int test_4_div () {
128 double a = 8.0;
129 double b = 2.0;
130 return (int)(a / b);
133 public static int test_2_rem () {
134 double a = 8.0;
135 double b = 3.0;
136 return (int)(a % b);
139 public static int test_2_neg () {
140 double a = -2.0;
141 return (int)(-a);
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;
156 return (int)(1.0 + (a + (b + (c + (d + (e + (f + (g + (h + i)))))))));
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;
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)))))))));
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;
187 return (int)(1.0 * (a * (b * (c * (d * (e * (f * (g * (h * i)))))))));
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;
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;
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;
231 public static int test_0_lconv_to_r8 () {
232 long a = 150;
233 double b = (double) a;
235 if (b != 150.0)
236 return 1;
237 return 0;
240 public static int test_0_lconv_to_r4 () {
241 long a = 3000;
242 float b = (float) a;
244 if (b != 3000.0F)
245 return 1;
246 return 0;
249 static void doit (double value, out long m) {
250 m = (long) value;
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;
261 public static int test_0_rounding () {
262 long ticks = 631502475130080000L;
263 long ticksperday = 864000000000L;
265 double days = (double) ticks / ticksperday;
267 if ((int)days != 730905)
268 return 1;
270 return 0;
273 /* FIXME: This only works on little-endian machines */
275 static unsafe int test_2_negative_zero () {
276 int result = 0;
277 double d = -0.0;
278 float f = -0.0f;
280 byte *ptr = (byte*)&d;
281 if (ptr [7] == 0)
282 return result;
283 result ++;
285 ptr = (byte*)&f;
286 if (ptr [3] == 0)
287 return result;
288 result ++;
290 return result;
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;
300 val = a == a;
301 if (!val)
302 return result;
303 result++;
305 val = (a != a);
306 if (val)
307 return result;
308 result++;
310 val = a < a;
311 if (val)
312 return result;
313 result++;
315 val = a > a;
316 if (val)
317 return result;
318 result++;
320 val = a <= a;
321 if (!val)
322 return result;
323 result++;
325 val = a >= a;
326 if (!val)
327 return result;
328 result++;
330 val = b == a;
331 if (val)
332 return result;
333 result++;
335 val = b < a;
336 if (!val)
337 return result;
338 result++;
340 val = b > a;
341 if (val)
342 return result;
343 result++;
345 val = b <= a;
346 if (!val)
347 return result;
348 result++;
350 val = b >= a;
351 if (val)
352 return result;
353 result++;
355 val = a == b;
356 if (val)
357 return result;
358 result++;
360 val = a < b;
361 if (val)
362 return result;
363 result++;
365 val = a > b;
366 if (!val)
367 return result;
368 result++;
370 val = a <= b;
371 if (val)
372 return result;
373 result++;
375 val = a >= b;
376 if (!val)
377 return result;
378 result++;
380 return result;
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;
389 val = a == a;
390 if (val)
391 return result;
392 result++;
394 val = a < a;
395 if (val)
396 return result;
397 result++;
399 val = a > a;
400 if (val)
401 return result;
402 result++;
404 val = a <= a;
405 if (val)
406 return result;
407 result++;
409 val = a >= a;
410 if (val)
411 return result;
412 result++;
414 val = b == a;
415 if (val)
416 return result;
417 result++;
419 val = b < a;
420 if (val)
421 return result;
422 result++;
424 val = b > a;
425 if (val)
426 return result;
427 result++;
429 val = b <= a;
430 if (val)
431 return result;
432 result++;
434 val = b >= a;
435 if (val)
436 return result;
437 result++;
439 val = a == b;
440 if (val)
441 return result;
442 result++;
444 val = a < b;
445 if (val)
446 return result;
447 result++;
449 val = a > b;
450 if (val)
451 return result;
452 result++;
454 val = a <= b;
455 if (val)
456 return result;
457 result++;
459 val = a >= b;
460 if (val)
461 return result;
462 result++;
464 return result;
467 public static int test_15_float_branch () {
468 double a = 2.0;
469 double b = 1.0;
470 int result = 0;
472 if (!(a == a))
473 return result;
474 result++;
476 if (a < a)
477 return result;
478 result++;
480 if (a > a)
481 return result;
482 result++;
484 if (!(a <= a))
485 return result;
486 result++;
488 if (!(a >= a))
489 return result;
490 result++;
492 if (b == a)
493 return result;
494 result++;
496 if (!(b < a))
497 return result;
498 result++;
500 if (b > a)
501 return result;
502 result++;
504 if (!(b <= a))
505 return result;
506 result++;
508 if (b >= a)
509 return result;
510 result++;
512 if (a == b)
513 return result;
514 result++;
516 if (a < b)
517 return result;
518 result++;
520 if (!(a > b))
521 return result;
522 result++;
524 if (a <= b)
525 return result;
526 result++;
528 if (!(a >= b))
529 return result;
530 result++;
532 return result;
535 public static int test_15_float_branch_un () {
536 double a = Double.NaN;
537 double b = 1.0;
538 int result = 0;
540 if (a == a)
541 return result;
542 result++;
544 if (a < a)
545 return result;
546 result++;
548 if (a > a)
549 return result;
550 result++;
552 if (a <= a)
553 return result;
554 result++;
556 if (a >= a)
557 return result;
558 result++;
560 if (b == a)
561 return result;
562 result++;
564 if (b < a)
565 return result;
566 result++;
568 if (b > a)
569 return result;
570 result++;
572 if (b <= a)
573 return result;
574 result++;
576 if (b >= a)
577 return result;
578 result++;
580 if (a == b)
581 return result;
582 result++;
584 if (a < b)
585 return result;
586 result++;
588 if (a > b)
589 return result;
590 result++;
592 if (a <= b)
593 return result;
594 result++;
596 if (a >= b)
597 return result;
598 result++;
600 return result;
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;
609 return f == PositiveInfinity ? 0 : 1;
613 Disabled until they can be fixed to run on amd64
615 static double VALUE = 0.19975845134874831D;
617 public static int test_0_float_conversion_reduces_double_precision () {
618 double d = (float)VALUE;
619 if (d != 0.19975845515727997d)
620 return 1;
622 return 0;
626 public static int test_0_long_to_double_conversion ()
628 long l = 9223372036854775807L;
629 long conv = (long)((double)l);
630 if (conv != -9223372036854775808L)
631 return 1;
633 return 0;
636 public static int INT_VAL = 0x13456799;
638 public static int test_0_int4_to_float_convertion ()
640 double d = (double)(float)INT_VAL;
642 if (d != 323315616)
643 return 1;
644 return 0;