aaba95819a25d48e1ce88fc2bf859d4a95cbb86e
[mono.git] / mono / mini / basic-simd.cs
1 using System;
2 using Mono.Simd;
3
4 public class SimdTests {
5         static unsafe Vector8us bad_method_regression_2 (Vector16b va, Vector16b vb) {
6                 Vector8us res = new Vector8us ();
7                 byte *a = (byte*)&va;
8                 byte *b = (byte*)&vb;
9
10                 int tmp = 0;
11                 for (int i = 0; i < 8; ++i)
12                         tmp += System.Math.Abs ((int)*a++ - (int)*b++);
13                 res.V0 = (ushort)tmp;
14
15                 tmp = 0;
16                 for (int i = 0; i < 8; ++i)
17                         tmp += System.Math.Abs ((int)*a++ - (int)*b++);
18                 res.V4 = (ushort)tmp;
19                 return res;
20         }
21
22         /*This bug was caused the simplifier not taking notice of LDADDR on the remaining blocks.*/
23         public static int test_2_local_simplifier_regression_other_blocks () {
24                 Vector16b a = new Vector16b (1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1);
25                 Vector16b b = new Vector16b (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
26                 Vector8us res = bad_method_regression_2 (a,b);
27                 return (int)res.V0 + res.V4;
28         }
29
30         static unsafe Vector8us bad_method_regression (Vector16b va, Vector16b vb) {
31                 Vector8us res = new Vector8us ();
32                 byte *a = (byte*)&va;
33                 byte *b = (byte*)&vb;
34                 *((ushort*)&res) = 10;
35
36                 int tmp = 0;
37                 if (*b != 0)
38                         tmp++;
39
40                 Vector8us dd = res;
41                 dd = dd + dd - dd;
42                 return dd;
43         }
44
45         /*This bug was caused the simplifier not taking notice of LDADDR on the first block.*/
46         public static int test_10_local_simplifier_regression_first_block () {
47                 Vector16b a = new Vector16b ();
48                 Vector16b b = new Vector16b ();
49                 Vector8us res = bad_method_regression (a,b);
50                 return (int)res.V0;
51         }
52         
53
54         public static int test_0_vecto8us_extract_mask () {
55                 Vector8us a = new Vector8us (0xF0F0, 0x700F, 0xAABB, 0x0000, 0x00F0, 0xF0F0, 0, 0);
56                 int c = Vector8us.ExtractByteMask (a);
57
58                 if (c != 0xD33)
59                         return 1;
60                 return 0;
61         }
62
63         public static int test_0_vecto8us_shuffle_low () {
64                 Vector8us a = new Vector8us (1, 2, 3, 4, 5, 6, 7, 8);
65                 Vector8us c = Vector8us.ShuffleLow (a, ShuffleSel.XFromY | ShuffleSel.YFromW | ShuffleSel.ZFromX | ShuffleSel.WFromZ);
66
67                 if (c.V0 != 2)
68                         return 1;
69                 if (c.V1 != 4)
70                         return 2;
71                 if (c.V2 != 1)
72                         return 3;
73                 if (c.V3 != 3)
74                         return 4;
75                 if (c.V4 != 5)
76                         return 5;
77                 if (c.V5 != 6)
78                         return 6;
79                 if (c.V6 != 7)
80                         return 7;
81                 if (c.V7 != 8)
82                         return 8;
83                 return 0;
84         }
85
86         public static int test_0_vecto8us_shuffle_high () {
87                 Vector8us a = new Vector8us (1, 2, 3, 4, 5, 6, 7, 8);
88                 Vector8us c = Vector8us.ShuffleHigh (a, ShuffleSel.XFromY | ShuffleSel.YFromW | ShuffleSel.ZFromX | ShuffleSel.WFromZ);
89
90                 if (c.V0 != 1)
91                         return 1;
92                 if (c.V1 != 2)
93                         return 2;
94                 if (c.V2 != 3)
95                         return 3;
96                 if (c.V3 != 4)
97                         return 4;
98                 if (c.V4 != 6)
99                         return 5;
100                 if (c.V5 != 8)
101                         return 6;
102                 if (c.V6 != 5)
103                         return 7;
104                 if (c.V7 != 7)
105                         return 8;
106
107                 return 0;
108         }
109
110         public static int test_0_vecto8us_max () {
111                 Vector8us a = new Vector8us (1, 2, 3, 4, 5, 6, 7, 8);
112                 Vector8us b = new Vector8us (9, 1, 1, 2, 9, 6, 5, 1000);
113                 Vector8us c = Vector8us.Max (a, b);
114
115                 if (c.V0 != 9)
116                         return 1;
117                 if (c.V1 != 2)
118                         return 2;
119                 if (c.V2 != 3)
120                         return 3;
121                 if (c.V3 != 4)
122                         return 4;
123                 if (c.V4 != 9)
124                         return 5;
125                 if (c.V5 != 6)
126                         return 6;
127                 if (c.V6 != 7)
128                         return 7;
129                 if (c.V7 != 1000)
130                         return 0;
131
132                 return 0;
133         }
134
135         public static int test_0_vecto8us_min () {
136                 Vector8us a = new Vector8us (1, 2, 3, 0, 5, 6, 5, 4);
137                 Vector8us b = new Vector8us (9, 1, 1, 2, 3, 4, 5, 6);
138                 Vector8us c = Vector8us.Min (a, b);
139
140                 if (c.V0 != 1)
141                         return 1;
142                 if (c.V1 != 1)
143                         return 2;
144                 if (c.V2 != 1)
145                         return 3;
146                 if (c.V3 != 0)
147                         return 4;
148                 if (c.V4 != 3)
149                         return 5;
150                 if (c.V5 != 4)
151                         return 6;
152                 if (c.V6 != 5)
153                         return 7;
154                 if (c.V7 != 4)
155                         return 8;
156                 return 0;
157         }
158
159         public static int test_0_vecto8us_avg () {
160                 Vector8us a = new Vector8us (1, 2, 3, 4, 5, 6, 7, 8);
161                 Vector8us b = new Vector8us (9, 1, 1, 2, 3, 4, 5, 6);
162                 Vector8us c = Vector8us.Average (a, b);
163
164                 if (c.V0 != 5)
165                         return 1;
166                 if (c.V1 != 2)
167                         return 2;
168                 if (c.V2 != 2)
169                         return 3;
170                 if (c.V3 != 3)
171                         return 4;
172                 if (c.V4 != 4)
173                         return 5;
174                 if (c.V5 != 5)
175                         return 6;
176                 if (c.V6 != 6)
177                         return 7;
178                 if (c.V7 != 7)
179                         return 8;
180                 return 0;
181         }
182
183         static void store_helper (ref Vector4f x) {
184                 Vector4f k;
185                 k = new Vector4f(9,9,9,9);
186                 x = k;
187         }
188
189         public static int test_0_vector4f_byref_store ()
190         {
191                 Vector4f k;
192                 k = new Vector4f(1,2,3,4);
193                 store_helper (ref k);
194                 if (k.X != 9)
195                         return 1;
196                 return 0;
197         }
198
199         public static int test_0_vector4f_init_array_element ()
200         {
201                 Vector4f[] v = new Vector4f[1];
202                 v[0] = new Vector4f(9,9,9,9);
203                 if (v [0].X != 9)
204                         return 1;
205                 return 0;
206         }
207
208         public static int test_0_vector4f_dup_high () {
209                 Vector4f a = new Vector4f (1, 2, 3, 4);
210                 Vector4f c = Vector4f.DuplicateHigh(a);
211
212                 if (c.X != 2)
213                         return 1;
214                 if (c.Y != 2)
215                         return 2;
216                 if (c.Z != 4)
217                         return 3;
218                 if (c.W != 4)
219                         return 4;
220                 return 0;
221         }
222
223         public static int test_0_vector4f_dup_low () {
224                 Vector4f a = new Vector4f (1, 2, 3, 4);
225                 Vector4f c = Vector4f.DuplicateLow (a);
226
227                 if (c.X != 1)
228                         return 1;
229                 if (c.Y != 1)
230                         return 2;
231                 if (c.Z != 3)
232                         return 3;
233                 if (c.W != 3)
234                         return 4;
235                 return 0;
236         }
237
238
239         public static int test_0_vector4f_interleave_high () {
240                 Vector4f a = new Vector4f (1, 2, 3, 4);
241                 Vector4f b = new Vector4f (5, 6, 7, 8);
242                 Vector4f c = Vector4f.InterleaveHigh (a, b);
243
244                 if (c.X != 3)
245                         return 1;
246                 if (c.Y != 7)
247                         return 2;
248                 if (c.Z != 4)
249                         return 3;
250                 if (c.W != 8)
251                         return 4;
252                 return 0;
253         }
254
255         public static int test_0_vector4f_interleave_low () {
256                 Vector4f a = new Vector4f (1, 2, 3, 4);
257                 Vector4f b = new Vector4f (5, 6, 7, 8);
258                 Vector4f c = Vector4f.InterleaveLow (a, b);
259
260                 if (c.X != 1)
261                         return 1;
262                 if (c.Y != 5)
263                         return 2;
264                 if (c.Z != 2)
265                         return 3;
266                 if (c.W != 6)
267                         return 4;
268                 return 0;
269         }
270
271         public static int test_0_vector4f_rcp () {
272                 Vector4f a = new Vector4f (1, 2, 4, 8);
273                 Vector4f c = Vector4f.Reciprocal (a);
274
275                 //Test with ranges due to the terrible precision.
276                 if (c.X < (1 - 0.01f) || c.X > (1 + 0.01f))
277                         return 1;
278                 if (c.Y < (0.5 - 0.01f) || c.Y > (0.5 + 0.01f))
279                         return 2;
280                 if (c.Z < (0.25 - 0.01f) || c.Z > (0.25 + 0.01f))
281                         return 3;
282                 if (c.W < (0.125 - 0.01f) || c.W > (0.125 + 0.01f))
283                         return 4;
284                 return 0;
285         }
286
287         public static int test_0_vector4f_xor () {
288                 Vector4f a = new Vector4f (1, 2, 3, 4);
289                 Vector4f b = new Vector4f (1, 3, 3, 8);
290                 Vector4f c = a ^ b;
291
292                 if (((Vector4ui)c).X != 0)
293                         return 1;
294                 if (((Vector4ui)c).Y != 0x400000)
295                         return 2;
296                 if (((Vector4ui)c).Z != 0)
297                         return 3;
298                 if (((Vector4ui)c).W != 0x1800000)
299                         return 4;
300                 return 0;
301         }
302
303         public static int test_0_vector4f_or () {
304                 Vector4f a = new Vector4f (1, 2, 3, 4);
305                 Vector4f b = new Vector4f (1, 3, 3, 8);
306                 Vector4f c = a | b;
307
308                 if (((Vector4ui)c).X != 0x3F800000)
309                         return 1;
310                 if (((Vector4ui)c).Y != 0x40400000)
311                         return 2;
312                 if (((Vector4ui)c).Z != 0x40400000)
313                         return 3;
314                 if (((Vector4ui)c).W != 0x41800000)
315                         return 4;
316                 return 0;
317         }
318         public static int test_0_vector4f_andn () {
319                 Vector4f a = new Vector4f (1, 2, 3, 4);
320                 Vector4f b = new Vector4f (1, 3, 3, 8);
321                 Vector4f c = Vector4f.AndNot (a ,b);
322
323                 if (((Vector4ui)c).X != 0)
324                         return 1;
325                 if (((Vector4ui)c).Y != 0x400000)
326                         return 2;
327                 if (((Vector4ui)c).Z != 0)
328                         return 3;
329                 if (((Vector4ui)c).W != 0x1000000)
330                         return 4;
331                 return 0;
332         }
333
334         public static int test_0_vector4f_and () {
335                 Vector4f a = new Vector4f (1, 2, 3, 4);
336                 Vector4f b = new Vector4f (1, 3, 3, 8);
337                 Vector4f c = a & b;
338
339                 if (((Vector4ui)c).X != 0x3F800000)
340                         return 1;
341                 if (((Vector4ui)c).Y != 0x40000000)
342                         return 2;
343                 if (((Vector4ui)c).Z != 0x40400000)
344                         return 3;
345                 if (((Vector4ui)c).W != 0x40000000)
346                         return 4;
347                 return 0;
348         }
349
350         public static int test_0_vector4f_cmpord () {
351                 Vector4f a = new Vector4f (float.NaN, 2,         3, 4);
352                 Vector4f b = new Vector4f (1,         float.NaN, 3, 6);
353                 Vector4f c = Vector4f.CompareOrdered (a, b);
354
355                 if (((Vector4ui)c).X != 0)
356                         return 1;
357                 if (((Vector4ui)c).Y != 0)
358                         return 2;
359                 if (((Vector4ui)c).Z != 0xFFFFFFFF)
360                         return 3;
361                 if (((Vector4ui)c).W != 0xFFFFFFFF)
362                         return 4;
363                 return 0;
364         }
365
366         public static int test_0_vector4f_cmpnle () {
367                 Vector4f a = new Vector4f (float.NaN, 2,         3, 4);
368                 Vector4f b = new Vector4f (1,         float.NaN, 3, 6);
369                 Vector4f c = Vector4f.CompareNotLessEqual (a, b);
370
371                 if (((Vector4ui)c).X != 0xFFFFFFFF)
372                         return 1;
373                 if (((Vector4ui)c).Y != 0xFFFFFFFF)
374                         return 2;
375                 if (((Vector4ui)c).Z != 0)
376                         return 3;
377                 if (((Vector4ui)c).W != 0)
378                         return 4;
379                 return 0;
380         }
381
382         public static int test_0_vector4f_cmpnlt () {
383                 Vector4f a = new Vector4f (float.NaN, 2,         3, 4);
384                 Vector4f b = new Vector4f (1,         float.NaN, 3, 6);
385                 Vector4f c = Vector4f.CompareNotLessThan (a, b);
386
387                 if (((Vector4ui)c).X != 0xFFFFFFFF)
388                         return 1;
389                 if (((Vector4ui)c).Y != 0xFFFFFFFF)
390                         return 2;
391                 if (((Vector4ui)c).Z != 0xFFFFFFFF)
392                         return 3;
393                 if (((Vector4ui)c).W != 0)
394                         return 4;
395                 return 0;
396         }
397
398         public static int test_0_vector4f_cmpneq () {
399                 Vector4f a = new Vector4f (float.NaN, 2,         3, 4);
400                 Vector4f b = new Vector4f (1,         float.NaN, 3, 6);
401                 Vector4f c = Vector4f.CompareNotEqual (a, b);
402
403                 if (((Vector4ui)c).X != 0xFFFFFFFF)
404                         return 1;
405                 if (((Vector4ui)c).Y != 0xFFFFFFFF)
406                         return 2;
407                 if (((Vector4ui)c).Z != 0)
408                         return 3;
409                 if (((Vector4ui)c).W != 0xFFFFFFFF)
410                         return 4;
411                 return 0;
412         }
413
414         public static int test_0_vector4f_cmpunord () {
415                 Vector4f a = new Vector4f (float.NaN, 2,         3, 4);
416                 Vector4f b = new Vector4f (1,         float.NaN, 3, 6);
417                 Vector4f c = Vector4f.CompareUnordered (a, b);
418
419                 if (((Vector4ui)c).X != 0xFFFFFFFF)
420                         return 1;
421                 if (((Vector4ui)c).Y != 0xFFFFFFFF)
422                         return 2;
423                 if (((Vector4ui)c).Z != 0)
424                         return 3;
425                 if (((Vector4ui)c).W != 0)
426                         return 4;
427                 return 0;
428         }
429
430         public static int test_0_vector4f_cmple () {
431                 Vector4f a = new Vector4f (float.NaN, 2,         3, 4);
432                 Vector4f b = new Vector4f (1,         float.NaN, 3, 6);
433                 Vector4f c = Vector4f.CompareLessEqual (a, b);
434
435                 if (((Vector4ui)c).X != 0)
436                         return 1;
437                 if (((Vector4ui)c).Y != 0)
438                         return 2;
439                 if (((Vector4ui)c).Z != 0xFFFFFFFF)
440                         return 3;
441                 if (((Vector4ui)c).W != 0xFFFFFFFF)
442                         return 4;
443                 return 0;
444         }
445
446         public static int test_0_vector4f_cmplt () {
447                 Vector4f a = new Vector4f (float.NaN, 2,         3, 4);
448                 Vector4f b = new Vector4f (1,         float.NaN, 3, 6);
449                 Vector4f c = Vector4f.CompareLessThan (a, b);
450
451                 if (((Vector4ui)c).X != 0)
452                         return 1;
453                 if (((Vector4ui)c).Y != 0)
454                         return 2;
455                 if (((Vector4ui)c).Z != 0)
456                         return 3;
457                 if (((Vector4ui)c).W != 0xFFFFFFFF)
458                         return 4;
459                 return 0;
460         }
461
462         public static int test_0_vector4f_cmpeq () {
463                 Vector4f a = new Vector4f (float.NaN, 2,         3, 6);
464                 Vector4f b = new Vector4f (1,         float.NaN, 3, 4);
465                 Vector4f c = Vector4f.CompareEquals (a, b);
466
467                 if (((Vector4ui)c).X != 0)
468                         return 1;
469                 if (((Vector4ui)c).Y != 0)
470                         return 2;
471                 if (((Vector4ui)c).Z != 0xFFFFFFFF)
472                         return 3;
473                 if (((Vector4ui)c).W != 0)
474                         return 4;
475                 return 0;
476         }
477
478         public static int test_0_vector4ui_sar () {
479                 Vector4ui a = new Vector4ui (0xF0000000u,20,3,40);
480                 
481                 Vector4ui c = Vector4ui.ShiftRightArithmetic (a, 2);
482         
483                 if (c.X != 0xFC000000)
484                         return 1;
485                 if (c.Y != 5)
486                         return 2;
487                 if (c.Z != 0)
488                         return 3;
489                 if (c.W != 10)
490                         return 4;
491                 return 0;
492         }
493
494         public static int test_0_vector4ui_unpack_high () {
495                 Vector4ui a = new Vector4ui (1,2,3,4);
496                 Vector4ui b = new Vector4ui (5,6,7,8);
497                 
498                 Vector4ui c = Vector4ui.UnpackHigh(a, b);
499         
500                 if (c.X != 3)
501                         return 1;
502                 if (c.Y != 7)
503                         return 2;
504                 if (c.Z != 4)
505                         return 3;
506                 if (c.W != 8)
507                         return 4;
508                 return 0;
509         }
510
511         public  static int test_0_vector4ui_unpack_low () {
512                 Vector4ui a = new Vector4ui (1,2,3,4);
513                 Vector4ui b = new Vector4ui (5,6,7,8);
514                 
515                 Vector4ui c = Vector4ui.UnpackLow (a, b);
516         
517                 if (c.X != 1)
518                         return 1;
519                 if (c.Y != 5)
520                         return 2;
521                 if (c.Z != 2)
522                         return 3;
523                 if (c.W != 6)
524                         return 4;
525                 return 0;
526         }
527
528         public  static int test_0_vector4ui_xor () {
529                 Vector4ui a = new Vector4ui (1,2,3,4);
530                 Vector4ui b = new Vector4ui (7,5,3,1);
531                 
532                 Vector4ui c = a ^ b;
533         
534                 if (c.X != 6)
535                         return 1;
536                 if (c.Y != 7)
537                         return 2;
538                 if (c.Z != 0)
539                         return 3;
540                 if (c.W != 5)
541                         return 4;
542                 return 0;
543         }
544
545         public  static int test_0_vector4ui_or () {
546                 Vector4ui a = new Vector4ui (1,2,3,4);
547                 Vector4ui b = new Vector4ui (7,5,3,1);
548                 
549                 Vector4ui c = a | b;
550         
551                 if (c.X != 7)
552                         return 1;
553                 if (c.Y != 7)
554                         return 2;
555                 if (c.Z != 3)
556                         return 3;
557                 if (c.W != 5)
558                         return 4;
559                 return 0;
560         }
561         public  static int test_0_vector4ui_and () {
562                 Vector4ui a = new Vector4ui (1,2,3,4);
563                 Vector4ui b = new Vector4ui (7,5,3,1);
564                 
565                 Vector4ui c = a & b;
566         
567                 if (c.X != 1)
568                         return 1;
569                 if (c.Y != 0)
570                         return 2;
571                 if (c.Z != 3)
572                         return 3;
573                 if (c.W != 0)
574                         return 4;
575                 return 0;
576         }
577
578         public  static int test_0_vector4ui_shr () {
579                 Vector4ui a = new Vector4ui (0xF0000000u,20,3,40);
580                 
581                 Vector4ui c = a >> 2;
582         
583                 if (c.X != 0x3C000000)
584                         return 1;
585                 if (c.Y != 5)
586                         return 2;
587                 if (c.Z != 0)
588                         return 3;
589                 if (c.W != 10)
590                         return 4;
591                 return 0;
592         }
593
594         public  static int test_0_vector4ui_shl () {
595                 Vector4ui a = new Vector4ui (10,20,3,40);
596                 
597                 Vector4ui c = a << 2;
598         
599                 if (c.X != 40)
600                         return 1;
601                 if (c.Y != 80)
602                         return 2;
603                 if (c.Z != 12)
604                         return 3;
605                 if (c.W != 160)
606                         return 4;
607                 return 0;
608         }
609
610         public  static int test_0_vector4ui_mul () {
611                 Vector4ui a = new Vector4ui (0x8888,20,3,40);
612                 Vector4ui b = new Vector4ui (0xFF00FF00u,2,3,4);
613                 
614                 Vector4ui c = a * b;
615         
616                 if (c.X != 0xffff7800)
617                         return 1;
618                 if (c.Y != 40)
619                         return 2;
620                 if (c.Z != 9)
621                         return 3;
622                 if (c.W != 160)
623                         return 4;
624                 return 0;
625         }
626         public  static int test_0_vector4ui_sub () {
627                 Vector4ui a = new Vector4ui (1,20,3,40);
628                 Vector4ui b = new Vector4ui (0xFF00FF00u,2,3,4);
629                 
630                 Vector4ui c = a - b;
631         
632                 if (c.X != 0xff0101)
633                         return 1;
634                 if (c.Y != 18)
635                         return 2;
636                 if (c.Z != 0)
637                         return 3;
638                 if (c.W != 36)
639                         return 4;
640                 return 0;
641         }
642
643         public  static int test_0_vector4ui_add () {
644                 Vector4ui a = new Vector4ui (0xFF00FF00u,2,3,4);
645                 Vector4ui b = new Vector4ui (0xFF00FF00u,2,3,4);
646                 
647                 Vector4ui c = a + b;
648         
649                 if (c.X != 0xfe01fe00)
650                         return 1;
651                 if (c.Y != 4)
652                         return 2;
653                 if (c.Z != 6)
654                         return 3;
655                 if (c.W != 8)
656                         return 4;
657                 return 0;
658         }
659
660
661         static int test_0_vector4ui_accessors () {
662                 Vector4ui a = new Vector4ui (1,2,3,4);
663
664                 if (a.X != 1)
665                         return 1;
666                 if (a.Y != 2)
667                         return 2;
668                 if (a.Z != 3)
669                         return 3;
670                 if (a.W != 4)
671                         return 4;
672                 a.X = 10;
673                 a.Y = 20;
674                 a.Z = 30;
675                 a.W = 40;
676
677                 if (a.X != 10)
678                         return 5;
679                 if (a.Y != 20)
680                         return 6;
681                 if (a.Z != 30)
682                         return 7;
683                 if (a.W != 40)
684                         return 8;
685                 return 0;
686         }
687
688         static int test_0_vector8us_sub_sat () {
689                 Vector8us a = new Vector8us (0xF000,1,20,3,4,5,6,7);
690                 Vector8us b = new Vector8us (0xFF00,4,5,6,7,8,9,10);
691                 Vector8us c = Vector8us.SubWithSaturation (a, b);
692
693                 if (c.V0 != 0)
694                         return 1;
695                 if (c.V1 != 0)
696                         return 2;
697                 if (c.V2 != 15)
698                         return 3;
699                 if (c.V3 != 0)
700                         return 4;
701                 if (c.V4 != 0)
702                         return 5;
703                 if (c.V5 != 0)
704                         return 6;
705                 if (c.V6 != 0)
706                         return 7;
707                 if (c.V7 != 0)
708                         return 8;
709                 return 0;
710         }
711
712         static int test_0_vector8us_add_sat () {
713                 Vector8us a = new Vector8us (0xFF00,1,2,3,4,5,6,7);
714                 Vector8us b = new Vector8us (0xFF00,4,5,6,7,8,9,10);
715                 Vector8us c = Vector8us.AddWithSaturation (a, b);
716
717                 if (c.V0 != 0xFFFF)
718                         return 1;
719                 if (c.V1 != 5)
720                         return 2;
721                 if (c.V2 != 7)
722                         return 3;
723                 if (c.V3 != 9)
724                         return 4;
725                 if (c.V4 != 11)
726                         return 5;
727                 if (c.V5 != 13)
728                         return 6;
729                 if (c.V6 != 15)
730                         return 7;
731                 if (c.V7 != 17)
732                         return 8;
733                 return 0;
734         }
735
736         static int test_0_vector8us_unpack_low () {
737                 Vector8us a = new Vector8us (0,1,2,3,4,5,6,7);
738                 Vector8us b = new Vector8us (3,4,5,6,7,8,9,10);
739                 Vector8us c = Vector8us.UnpackLow (a, b);
740
741                 if (c.V0 != 0)
742                         return 1;
743                 if (c.V1 != 3)
744                         return 2;
745                 if (c.V2 != 1)
746                         return 3;
747                 if (c.V3 != 4)
748                         return 4;
749                 if (c.V4 != 2)
750                         return 5;
751                 if (c.V5 != 5)
752                         return 6;
753                 if (c.V6 != 3)
754                         return 7;
755                 if (c.V7 != 6)
756                         return 8;
757                 return 0;
758         }
759
760
761         static int test_0_vector8us_shift_left () {
762                 Vector8us a = new Vector8us (0xFF00,1,2,3,4,5,6,7);
763                 int amt = 2;
764                 Vector8us c = a << amt;
765         
766                 if (c.V0 != 0xFC00)
767                         return 1;
768                 if (c.V1 != 4)
769                         return 2;
770                 if (c.V7 != 28)
771                         return 3;
772                 return 0;
773         }
774         
775         static int test_0_vector8us_shift_right_arithmetic () {
776                 Vector8us a = new Vector8us (0xFF00,1,2,3,4,5,6,7);
777                 int amt = 2;
778                 Vector8us c = Vector8us.ShiftRightArithmetic (a, amt);
779         
780                 if (c.V0 != 0xFFC0)
781                         return 1;
782                 if (c.V1 != 0)
783                         return 2;
784                 if (c.V7 != 1)
785                         return 3;
786                 return 0;
787         }
788
789         static int test_0_vector8us_shift_variable_offset () {
790                 int off = 2;
791                 Vector8us a = new Vector8us (0xF000,1,2,3,4,5,6,7);
792                 Vector8us b = a;
793                 Vector8us c = b >> off;
794                 a = b + b;
795
796                 if (c.V0 != 0x3C00)
797                         return 1;
798                 if (c.V1 != 0)
799                         return 2;
800                 if (c.V7 != 1)
801                         return 3;
802                 if (a.V1 != 2)
803                         return 4;
804                 if (a.V7 != 14)
805                         return 5;
806                 return 0;
807         }
808         
809         
810         static int test_0_vector8us_shift_operand_is_live_after_op () {
811                 Vector8us a = new Vector8us (0xF000,1,2,3,4,5,6,7);
812                 Vector8us b = a;
813                 Vector8us c = b >> 2;
814                 a = b + b;
815
816                 if (c.V0 != 0x3C00)
817                         return 1;
818                 if (c.V1 != 0)
819                         return 2;
820                 if (c.V7 != 1)
821                         return 3;
822                 if (a.V1 != 2)
823                         return 4;
824                 if (a.V7 != 14)
825                         return 5;
826                 return 0;
827         }
828
829         static int test_0_vector8us_shr_constant () {
830                 Vector8us a = new Vector8us (0xF000,1,2,3,4,5,6,7);
831                 Vector8us c = a >> 2;
832
833                 if (c.V0 != 0x3C00)
834                         return 1;
835                 if (c.V1 != 0)
836                         return 2;
837                 if (c.V7 != 1)
838                         return 3;
839                 return 0;
840         }
841
842         static int test_0_vector8us_mul () {
843                 Vector8us a = new Vector8us (0x0F00,4,5,6,7,8,9,10);
844                 Vector8us b = new Vector8us (0x0888,1,2,3,4,5,6,8);
845
846                 Vector8us c = a * b;
847                 if (c.V0 != 63488)
848                         return 1;
849                 if (c.V1 != 4)
850                         return 2;
851                 if (c.V7 != 80)
852                         return 3;
853                 return 0;
854         }
855
856         static int test_0_vector8us_add () {
857                 Vector8us a = new Vector8us (0xFF00,4,5,6,7,8,9,10);
858                 Vector8us b = new Vector8us (0x8888,1,2,3,4,5,6,8);
859
860                 Vector8us c = a + b;
861                 if (c.V0 != 34696)
862                         return 1;
863                 if (c.V1 != 5)
864                         return 2;
865                 if (c.V7 != 18)
866                         return 3;
867                 return 0;
868         }
869
870
871         static int test_0_vector8us_sub () {
872                 Vector8us a = new Vector8us (3,4,5,6,7,8,9,10);
873                 Vector8us b = new Vector8us (10,1,2,3,4,5,6,8);
874
875                 Vector8us c = a - b;
876
877                 if (c.V0 != 65529)
878                         return 1;
879                 if (c.V1 != 3)
880                         return 2;
881                 if (c.V7 != 2)
882                         return 3;
883                 return 0;
884         }
885
886
887         static int test_0_vector8us_accessors () {
888                 Vector8us a = new Vector8us (0,1,2,3,4,5,6,7);
889
890                 if (a.V0 != 0)
891                         return 1;
892                 if (a.V1 != 1)
893                         return 2;
894                 if (a.V2 != 2)
895                         return 3;
896                 if (a.V3 != 3)
897                         return 4;
898                 if (a.V4 != 4)
899                         return 5;
900                 if (a.V5 != 5)
901                         return 6;
902                 if (a.V6 != 6)
903                         return 7;
904                 if (a.V7 != 7)
905                         return 8;
906                 a.V0 = 10;
907                 a.V1 = 20;
908                 a.V2 = 30;
909                 a.V3 = 40;
910                 a.V4 = 50;
911                 a.V5 = 60;
912                 a.V6 = 70;
913                 a.V7 = 80;
914
915                 if (a.V0 != 10)
916                         return 17;
917                 if (a.V1 != 20)
918                         return 18;
919                 if (a.V2 != 30)
920                         return 19;
921                 if (a.V3 != 40)
922                         return 20;
923                 if (a.V4 != 50)
924                         return 21;
925                 if (a.V5 != 60)
926                         return 22;
927                 if (a.V6 != 70)
928                         return 23;
929                 if (a.V7 != 80)
930                         return 24;
931
932                 return 0;
933         }
934
935
936         static int test_0_vector16b_unpack_high () {
937                 Vector16b a = new Vector16b (0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
938                 Vector16b b = new Vector16b (9,10,11,12,13,14,15,0,1,2,3,4,5,6,7,8);
939                 Vector16b c = Vector16b.UnpackHigh (a, b);
940
941                 if (c.V0 != 8)
942                         return 1;
943                 if (c.V1 != 1)
944                         return 2;
945                 if (c.V2 != 9)
946                         return 3;
947                 if (c.V3 != 2)
948                         return 4;
949                 if (c.V4 != 10)
950                         return 5;
951                 if (c.V5 != 3)
952                         return 6;
953                 if (c.V14 != 15)
954                         return 7;
955                 if (c.V15 != 8)
956                         return 8;
957                 return 0;
958         }
959
960         static int test_0_vector16b_unpack_low () {
961                 Vector16b a = new Vector16b (0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
962                 Vector16b b = new Vector16b (9,10,11,12,13,14,15,0,1,2,3,4,5,6,7,8);
963                 Vector16b c = Vector16b.UnpackLow (a, b);
964
965                 if (c.V0 != 0)
966                         return 1;
967                 if (c.V1 != 9)
968                         return 2;
969                 if (c.V2 != 1)
970                         return 3;
971                 if (c.V3 != 10)
972                         return 4;
973                 if (c.V4 != 2)
974                         return 5;
975                 if (c.V5 != 11)
976                         return 6;
977                 if (c.V14 != 7)
978                         return 7;
979                 if (c.V15 != 0)
980                         return 8;
981                 return 0;
982         }
983
984         static int test_0_vector16b_sar () {
985                 Vector16b a = new Vector16b (0xF0,20,3,40,0,0,0,0,0,0,0,0,0,0,0,0);
986                 
987                 Vector16b c = Vector16b.ShiftRightArithmetic (a, 2);
988                 if (c.V0 != 0xFC)
989                         return 1;
990                 if (c.V1 != 5)
991                         return 1;
992                 if (c.V2 != 0)
993                         return 2;
994                 if (c.V3 != 10)
995                         return 3;
996                 return 0;
997         }
998
999         static int test_0_vector16b_sub_sat () {
1000                 Vector16b a = new Vector16b (100,10,11,12,13,14,15,0,1,2,3,4,5,6,7,8);
1001                 Vector16b b = new Vector16b (200,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
1002                 Vector16b c = Vector16b.SubWithSaturation (a, b);
1003
1004                 if (c.V0 != 0)
1005                         return 1;
1006                 if (c.V1 != 9)
1007                         return 2;
1008                 if (c.V15 != 0)
1009                         return 3;
1010                 return 0;
1011         }
1012         
1013         static int test_0_vector16b_add_sat () {
1014                 Vector16b a = new Vector16b (200,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
1015                 Vector16b b = new Vector16b (200,10,11,12,13,14,15,0,1,2,3,4,5,6,7,8);
1016                 Vector16b c = Vector16b.AddWithSaturation (a, b);
1017
1018                 if (c.V0 != 255)
1019                         return 1;
1020                 if (c.V1 != 11)
1021                         return 2;
1022                 if (c.V15 != 23)
1023                         return 3;
1024                 return 0;
1025         }
1026
1027         static int test_0_vector16b_add_ovf () {
1028                 Vector16b a = new Vector16b (200,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
1029                 Vector16b b = new Vector16b (200,10,11,12,13,14,15,0,1,2,3,4,5,6,7,8);
1030                 Vector16b c = a + b;
1031
1032                 if (c.V0 != 144)
1033                         return 1;
1034                 if (c.V1 != 11)
1035                         return 2;
1036                 if (c.V15 != 23)
1037                         return 3;
1038                 return 0;
1039         }
1040
1041         static int test_0_vector16b_accessors () {
1042                 Vector16b a = new Vector16b (0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
1043
1044                 if (a.V0 != 0)
1045                         return 1;
1046                 if (a.V1 != 1)
1047                         return 2;
1048                 if (a.V2 != 2)
1049                         return 3;
1050                 if (a.V3 != 3)
1051                         return 4;
1052                 if (a.V4 != 4)
1053                         return 5;
1054                 if (a.V5 != 5)
1055                         return 6;
1056                 if (a.V6 != 6)
1057                         return 7;
1058                 if (a.V7 != 7)
1059                         return 8;
1060                 if (a.V8 != 8)
1061                         return 9;
1062                 if (a.V9 != 9)
1063                         return 10;
1064                 if (a.V10 != 10)
1065                         return 11;
1066                 if (a.V11 != 11)
1067                         return 12;
1068                 if (a.V12 != 12)
1069                         return 13;
1070                 if (a.V13 != 13)
1071                         return 14;
1072                 if (a.V14 != 14)
1073                         return 15;
1074                 if (a.V15 != 15)
1075                         return 16;
1076
1077                 a.V0 = 10;
1078                 a.V1 = 20;
1079                 a.V2 = 30;
1080                 a.V3 = 40;
1081                 a.V4 = 50;
1082                 a.V5 = 60;
1083                 a.V6 = 70;
1084                 a.V7 = 80;
1085                 a.V8 = 90;
1086                 a.V9 = 100;
1087                 a.V10 = 110;
1088                 a.V11 = 120;
1089                 a.V12 = 130;
1090                 a.V13 = 140;
1091                 a.V14 = 150;
1092                 a.V15 = 160;
1093
1094                 if (a.V0 != 10)
1095                         return 17;
1096                 if (a.V1 != 20)
1097                         return 18;
1098                 if (a.V2 != 30)
1099                         return 19;
1100                 if (a.V3 != 40)
1101                         return 20;
1102                 if (a.V4 != 50)
1103                         return 21;
1104                 if (a.V5 != 60)
1105                         return 22;
1106                 if (a.V6 != 70)
1107                         return 23;
1108                 if (a.V7 != 80)
1109                         return 24;
1110                 if (a.V8 != 90)
1111                         return 25;
1112                 if (a.V9 != 100)
1113                         return 26;
1114                 if (a.V10 != 110)
1115                         return 27;
1116                 if (a.V11 != 120)
1117                         return 28;
1118                 if (a.V12 != 130)
1119                         return 29;
1120                 if (a.V13 != 140)
1121                         return 30;
1122                 if (a.V14 != 150)
1123                         return 31;
1124                 if (a.V15 != 160)
1125                         return 32;
1126                 return 0;
1127         }
1128
1129         public static int test_0_accessors () {
1130                 Vector4f a = new Vector4f (1, 2, 3, 4);
1131                 if (a.X != 1f)
1132                         return 1;
1133                 if (a.Y != 2f)
1134                         return 2;
1135                 if (a.Z != 3f)
1136                         return 3;
1137                 if (a.W != 4f)
1138                         return 4;
1139                 return 0;
1140         }
1141
1142         public static int test_0_packed_add_with_stack_tmp () {
1143                 Vector4f a = new Vector4f (1, 2, 3, 4);
1144                 Vector4f b = new Vector4f (5, 6, 7, 8);
1145                 Vector4f c = new Vector4f (-1, -3, -4, -5);
1146                 Vector4f d = a + b + c;
1147                 if (d.X != 5f)
1148                         return 1;
1149                 if (d.Y != 5f)
1150                         return 2;
1151                 if (d.Z != 6f)
1152                         return 3;
1153                 if (d.W != 7f)
1154                         return 4;
1155                 return 0;
1156         }
1157
1158         public static int test_0_simple_packed_add () {
1159                 Vector4f a = new Vector4f (1, 2, 3, 4);
1160                 Vector4f b = new Vector4f (5, 6, 7, 8);
1161                 Vector4f c;
1162                 c = a + b;
1163                 if (c.X != 6f)
1164                         return 1;
1165                 if (c.Y != 8f)
1166                         return 2;
1167                 if (c.Z != 10f)
1168                         return 3;
1169                 if (c.W != 12f)
1170                         return 4;
1171                 return 0;
1172         }
1173
1174         public static int test_0_simple_packed_sub () {
1175                 Vector4f a = new Vector4f (1, 2, 3, 4);
1176                 Vector4f b = new Vector4f (5, 6, 7, 8);
1177                 Vector4f c = b - a;
1178                 if (c.X != 4f)
1179                         return 1;
1180                 if (c.Y != 4f)
1181                         return 2;
1182                 if (c.Z != 4f)
1183                         return 3;
1184                 if (c.W != 4f)
1185                         return 4;
1186                 return 0;
1187         }
1188
1189         public static int test_0_simple_packed_mul () {
1190                 Vector4f a = new Vector4f (1, 2, 3, 4);
1191                 Vector4f b = new Vector4f (5, 6, 7, 8);
1192                 Vector4f c = b * a;
1193                 if (c.X != 5f)
1194                         return 1;
1195                 if (c.Y != 12f)
1196                         return 2;
1197                 if (c.Z != 21f)
1198                         return 3;
1199                 if (c.W != 32f)
1200                         return 4;
1201                 return 0;
1202         }
1203
1204         public static int test_0_simple_packed_div () {
1205                 Vector4f a = new Vector4f (2, 2, 3, 4);
1206                 Vector4f b = new Vector4f (20, 10, 33, 12);
1207                 Vector4f c = b / a;
1208                 if (c.X != 10f)
1209                         return 1;
1210                 if (c.Y != 5f)
1211                         return 2;
1212                 if (c.Z != 11f)
1213                         return 3;
1214                 if (c.W != 3f)
1215                         return 4;
1216                 return 0;
1217         }
1218
1219         public static int test_0_simple_packed_sqrt () {
1220                 Vector4f a = new Vector4f (16, 4, 9, 25);
1221                 a = Vector4f.Sqrt (a);
1222                 if (a.X != 4f)
1223                         return 1;
1224                 if (a.Y != 2f)
1225                         return 2;
1226                 if (a.Z != 3f)
1227                         return 3;
1228                 if (a.W != 5f)
1229                         return 4;
1230                 return 0;
1231         }
1232
1233         public static int test_0_simple_packed_invsqrt () {
1234                 Vector4f a = new Vector4f (16, 4, 100, 25);
1235                 //this function has VERY low precision
1236                 a = Vector4f.InvSqrt (a);
1237                 if (a.X < (1/4f - 0.01f) || a.X > (1/4f + 0.01f))
1238                         return 1;
1239                 if (a.Y < (1/2f - 0.01f) || a.Y > (1/2f + 0.01f))
1240                         return 2;
1241                 if (a.Z < (1/10f - 0.01f) || a.Z > (1/10f + 0.01f))
1242                         return 3;
1243                 if (a.W < (1/5f - 0.01f) || a.W > (1/5f + 0.01f))
1244                         return 4;
1245                 return 0;
1246         }
1247
1248         public static int test_0_simple_packed_min () {
1249                 Vector4f a = new Vector4f (16, -4, 9, 25);
1250                 Vector4f b = new Vector4f (5, 3, 9, 0);
1251                 Vector4f c = Vector4f.Min (a, b);
1252                 if (c.X != 5f)
1253                         return 1;
1254                 if (c.Y != -4f)
1255                         return 2;
1256                 if (c.Z != 9f)
1257                         return 3;
1258                 if (c.W != 0f)
1259                         return 4;
1260                 return 0;
1261         }
1262
1263         public static int test_0_simple_packed_max () {
1264                 Vector4f a = new Vector4f (16, -4, 9, 25);
1265                 Vector4f b = new Vector4f (5, 3, 9, 0);
1266                 Vector4f c = Vector4f.Max (a, b);
1267                 if (c.X != 16f)
1268                         return 1;
1269                 if (c.Y != 3f)
1270                         return 2;
1271                 if (c.Z != 9f)
1272                         return 3;
1273                 if (c.W != 25f)
1274                         return 4;
1275                 return 0;
1276         }
1277
1278         public static int test_0_simple_packed_hadd () {
1279                 Vector4f a = new Vector4f (5, 5, 6, 6);
1280                 Vector4f b = new Vector4f (7, 7, 8, 8);
1281                 Vector4f c = Vector4f.HorizontalAdd (a, b);
1282                 if (c.X != 10f)
1283                         return 1;
1284                 if (c.Y != 12f)
1285                         return 2;
1286                 if (c.Z != 14f)
1287                         return 3;
1288                 if (c.W != 16f)
1289                         return 4;
1290                 return 0;
1291         }
1292
1293         public static int test_0_simple_packed_hsub () {
1294                 Vector4f a = new Vector4f (5, 2, 6, 1);
1295                 Vector4f b = new Vector4f (7, 0, 8, 3);
1296                 Vector4f c = Vector4f.HorizontalSub (a, b);
1297                 if (c.X != 3f)
1298                         return 1;
1299                 if (c.Y != 5f)
1300                         return 2;
1301                 if (c.Z != 7f)
1302                         return 3;
1303                 if (c.W != 5f)
1304                         return 4;
1305                 return 0;
1306         }
1307
1308         public static int test_0_simple_packed_addsub () {
1309                 Vector4f a = new Vector4f (5, 2, 6, 1);
1310                 Vector4f b = new Vector4f (7, 0, 8, 3);
1311                 Vector4f c = Vector4f.AddSub (a, b);
1312                 if (c.X != -2f)
1313                         return 1;
1314                 if (c.Y != 2f)
1315                         return 2;
1316                 if (c.Z != -2f)
1317                         return 3;
1318                 if (c.W != 4f)
1319                         return 4;
1320                 return 0;
1321         }
1322
1323         public static int test_0_simple_packed_shuffle () {
1324                 Vector4f a = new Vector4f (1, 2, 3, 4);
1325                 a = Vector4f.Shuffle(a, ShuffleSel.XFromY | ShuffleSel.YFromW | ShuffleSel.ZFromX | ShuffleSel.WFromZ);
1326                 if (a.X != 2f)
1327                         return 1;
1328                 if (a.Y != 4f)
1329                         return 2;
1330                 if (a.Z != 1f)
1331                         return 3;
1332                 if (a.W != 3f)
1333                         return 4;
1334                 return 0;
1335         }
1336
1337         public static int test_0_packed_shuffle_with_reg_pressure () {
1338                 Vector4f v = new Vector4f (1, 2, 3, 4);
1339                 Vector4f m0 = v + v, m1 = v - v, m2 = v * v, m3 = v + v + v;
1340                 if (ff) v = v + v -v    ;
1341
1342                 Vector4f r0 = Vector4f.Shuffle (v, ShuffleSel.XFromY | ShuffleSel.YFromW | ShuffleSel.ZFromX | ShuffleSel.WFromZ);
1343                 Vector4f r1 = Vector4f.Shuffle (v, ShuffleSel.XFromY | ShuffleSel.YFromW | ShuffleSel.ZFromX | ShuffleSel.WFromZ);
1344                 Vector4f x = Vector4f.Shuffle (v, ShuffleSel.XFromY | ShuffleSel.YFromW | ShuffleSel.ZFromX | ShuffleSel.WFromZ);
1345                 Vector4f r2 = Vector4f.Shuffle (v, ShuffleSel.XFromY | ShuffleSel.YFromW | ShuffleSel.ZFromX | ShuffleSel.WFromZ);
1346                 Vector4f r3 = Vector4f.Shuffle (v, ShuffleSel.XFromY | ShuffleSel.YFromW | ShuffleSel.ZFromX | ShuffleSel.WFromZ);
1347                 Vector4f a = x;
1348
1349                 r0 = r0 * m0 + x;
1350                 r1 = r1 * m1 + x;
1351                 x = x - v + v;
1352                 r2 = r2 * m2 + x;
1353                 r3 = r3 * m3 + x;
1354                 Vector4f result = r0 + r1 + r2 + r3;
1355
1356                 if (a.X != 2f)
1357                         return 1;
1358                 if (a.Y != 4f)
1359                         return 2;
1360                 if (a.Z != 1f)
1361                         return 3;
1362                 if (a.W != 3f)
1363                         return 4;
1364                 if (result.Y != result.Y)
1365                         return 0;
1366                 return 0;
1367         }
1368         
1369         public static int test_24_regs_pressure_a () {
1370                 Vector4f a = new Vector4f (1, 2, 3, 4);
1371                 Vector4f b = a + a;
1372                 Vector4f c = b * a;
1373                 Vector4f d = a - b;
1374                 c = a + b + c + d;
1375                 return (int)c.Z;
1376         }
1377
1378         public static int test_54_regs_pressure_b () {
1379                 Vector4f a = new Vector4f (1, 2, 3, 4);
1380                 Vector4f b = a + a;
1381                 Vector4f c = b - a;
1382                 Vector4f d = c - a;
1383                 Vector4f e = a + b + c;
1384                 Vector4f f = d - b + a - c;
1385                 Vector4f g = a - d * f - c + b;
1386                 Vector4f h = a * b - c + e;
1387                 Vector4f i = h - g - f - e - d - c - b - a;
1388                 Vector4f j = a + b + c + d + e + f + g + h + i;
1389                 return (int)j.Z;
1390         }
1391
1392         static bool ff;
1393         public static int test_3_single_block_var_is_properly_promoted () {
1394                 Vector4f a = new Vector4f (4, 5, 6, 7);
1395                 if (ff)
1396                         a = a - a;
1397                 else {
1398                         Vector4f b = new Vector4f (1, 2, 3, 4);
1399                         Vector4f c = b;
1400                         a = a - b;
1401                         if (ff) {
1402                                 c = a;
1403                                 a = c;
1404                         }
1405                 }
1406                 return (int)a.X;
1407         }
1408
1409         static float float_val = 45f;
1410
1411         public static int test_0_sse2_opt_and_simd_intrinsic_proper_regalloc () {
1412                 Vector4f v = new Vector4f (1, 2, 3, 4);
1413                 float f = float_val;
1414                 int x = (int)f;
1415                 if (v.X != 1f)
1416                         return 1;
1417                 if (x != 45f)
1418                         return 2;
1419                 return 0;
1420         }
1421
1422         public static int Main () {
1423                 return TestDriver.RunTests (typeof (SimdTests));
1424         }
1425 }
1426