2008-10-07 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / mini / basic-simd.cs
1 using System;
2 using Mono.Simd;
3
4 public class SimdTests {
5
6         public static int test_accessors () {
7                 Vector4f a = new Vector4f (1, 2, 3, 4);
8                 if (a.X != 1f)
9                         return 1;
10                 if (a.Y != 2f)
11                         return 2;
12                 if (a.Z != 3f)
13                         return 3;
14                 if (a.W != 4f)
15                         return 4;
16                 return 0;
17         }
18
19         public static int test_packed_add_with_stack_tmp () {
20                 Vector4f a = new Vector4f (1, 2, 3, 4);
21                 Vector4f b = new Vector4f (5, 6, 7, 8);
22                 Vector4f c = new Vector4f (-1, -3, -4, -5);
23                 Vector4f d = a + b + c;
24                 if (d.X != 5f)
25                         return 1;
26                 if (d.Y != 5f)
27                         return 2;
28                 if (d.Z != 6f)
29                         return 3;
30                 if (d.W != 7f)
31                         return 4;
32                 return 0;
33         }
34
35         public static int test_simple_packed_add () {
36                 Vector4f a = new Vector4f (1, 2, 3, 4);
37                 Vector4f b = new Vector4f (5, 6, 7, 8);
38                 Vector4f c;
39                 c = a + b;
40                 if (c.X != 6f)
41                         return 1;
42                 if (c.Y != 8f)
43                         return 2;
44                 if (c.Z != 10f)
45                         return 3;
46                 if (c.W != 12f)
47                         return 4;
48                 return 0;
49         }
50
51         public static int test_simple_packed_sub () {
52                 Vector4f a = new Vector4f (1, 2, 3, 4);
53                 Vector4f b = new Vector4f (5, 6, 7, 8);
54                 Vector4f c = b - a;
55                 if (c.X != 4f)
56                         return 1;
57                 if (c.Y != 4f)
58                         return 2;
59                 if (c.Z != 4f)
60                         return 3;
61                 if (c.W != 4f)
62                         return 4;
63                 return 0;
64         }
65
66         public static int test_simple_packed_mul () {
67                 Vector4f a = new Vector4f (1, 2, 3, 4);
68                 Vector4f b = new Vector4f (5, 6, 7, 8);
69                 Vector4f c = b * a;
70                 if (c.X != 5f)
71                         return 1;
72                 if (c.Y != 12f)
73                         return 2;
74                 if (c.Z != 21f)
75                         return 3;
76                 if (c.W != 32f)
77                         return 4;
78                 return 0;
79         }
80
81         public static int test_simple_packed_div () {
82                 Vector4f a = new Vector4f (2, 2, 3, 4);
83                 Vector4f b = new Vector4f (20, 10, 33, 12);
84                 Vector4f c = b / a;
85                 if (c.X != 10f)
86                         return 1;
87                 if (c.Y != 5f)
88                         return 2;
89                 if (c.Z != 11f)
90                         return 3;
91                 if (c.W != 3f)
92                         return 4;
93                 return 0;
94         }
95
96         public static int test_simple_packed_sqrt () {
97                 Vector4f a = new Vector4f (16, 4, 9, 25);
98                 a = Vector4f.Sqrt (a);
99                 if (a.X != 4f)
100                         return 1;
101                 if (a.Y != 2f)
102                         return 2;
103                 if (a.Z != 3f)
104                         return 3;
105                 if (a.W != 5f)
106                         return 4;
107                 return 0;
108         }
109
110         public static int test_simple_packed_invsqrt () {
111                 Vector4f a = new Vector4f (16, 4, 100, 25);
112                 //this function has VERY low precision
113                 a = Vector4f.InvSqrt (a);
114                 if (a.X < (1/4f - 0.01f) || a.X > (1/4f + 0.01f))
115                         return 1;
116                 if (a.Y < (1/2f - 0.01f) || a.Y > (1/2f + 0.01f))
117                         return 2;
118                 if (a.Z < (1/10f - 0.01f) || a.Z > (1/10f + 0.01f))
119                         return 3;
120                 if (a.W < (1/5f - 0.01f) || a.W > (1/5f + 0.01f))
121                         return 4;
122                 return 0;
123         }
124
125         public static int test_simple_packed_min () {
126                 Vector4f a = new Vector4f (16, -4, 9, 25);
127                 Vector4f b = new Vector4f (5, 3, 9, 0);
128                 Vector4f c = Vector4f.Min (a, b);
129                 if (c.X != 5f)
130                         return 1;
131                 if (c.Y != -4f)
132                         return 2;
133                 if (c.Z != 9f)
134                         return 3;
135                 if (c.W != 0f)
136                         return 4;
137                 return 0;
138         }
139
140         public static int test_simple_packed_max () {
141                 Vector4f a = new Vector4f (16, -4, 9, 25);
142                 Vector4f b = new Vector4f (5, 3, 9, 0);
143                 Vector4f c = Vector4f.Max (a, b);
144                 if (c.X != 16f)
145                         return 1;
146                 if (c.Y != 3f)
147                         return 2;
148                 if (c.Z != 9f)
149                         return 3;
150                 if (c.W != 25f)
151                         return 4;
152                 return 0;
153         }
154
155         public static int test_simple_packed_hadd () {
156                 Vector4f a = new Vector4f (5, 5, 6, 6);
157                 Vector4f b = new Vector4f (7, 7, 8, 8);
158                 Vector4f c = Vector4f.HorizontalAdd (a, b);
159                 if (c.X != 10f)
160                         return 1;
161                 if (c.Y != 12f)
162                         return 2;
163                 if (c.Z != 14f)
164                         return 3;
165                 if (c.W != 16f)
166                         return 4;
167                 return 0;
168         }
169
170         public static int test_simple_packed_hsub () {
171                 Vector4f a = new Vector4f (5, 2, 6, 1);
172                 Vector4f b = new Vector4f (7, 0, 8, 3);
173                 Vector4f c = Vector4f.HorizontalSub (a, b);
174                 if (c.X != 3f)
175                         return 1;
176                 if (c.Y != 5f)
177                         return 2;
178                 if (c.Z != 7f)
179                         return 3;
180                 if (c.W != 5f)
181                         return 4;
182                 return 0;
183         }
184
185         public static int test_simple_packed_addsub () {
186                 Vector4f a = new Vector4f (5, 2, 6, 1);
187                 Vector4f b = new Vector4f (7, 0, 8, 3);
188                 Vector4f c = Vector4f.AddSub (a, b);
189                 if (c.X != -2f)
190                         return 1;
191                 if (c.Y != 2f)
192                         return 2;
193                 if (c.Z != -2f)
194                         return 3;
195                 if (c.W != 4f)
196                         return 4;
197                 return 0;
198         }
199
200         public static int test_simple_packed_shuffle () {
201                 Vector4f a = new Vector4f (1, 2, 3, 4);
202                 a = Vector4f.Shuffle(a, ShuffleSel.XFromY | ShuffleSel.YFromW | ShuffleSel.ZFromX | ShuffleSel.WFromZ);
203                 if (a.X != 2f)
204                         return 1;
205                 if (a.Y != 4f)
206                         return 2;
207                 if (a.Z != 1f)
208                         return 3;
209                 if (a.W != 3f)
210                         return 4;
211                 return 0;
212         }
213
214         public static int test_0_packed_shuffle_with_reg_pressure () {
215                 Vector4f v = new Vector4f (1, 2, 3, 4);
216                 Vector4f m0 = v + v, m1 = v - v, m2 = v * v, m3 = v + v + v;
217                 if (ff) v = v + v -v    ;
218
219                 Vector4f r0 = Vector4f.Shuffle (v, ShuffleSel.XFromY | ShuffleSel.YFromW | ShuffleSel.ZFromX | ShuffleSel.WFromZ);
220                 Vector4f r1 = Vector4f.Shuffle (v, ShuffleSel.XFromY | ShuffleSel.YFromW | ShuffleSel.ZFromX | ShuffleSel.WFromZ);
221                 Vector4f x = Vector4f.Shuffle (v, ShuffleSel.XFromY | ShuffleSel.YFromW | ShuffleSel.ZFromX | ShuffleSel.WFromZ);
222                 Vector4f r2 = Vector4f.Shuffle (v, ShuffleSel.XFromY | ShuffleSel.YFromW | ShuffleSel.ZFromX | ShuffleSel.WFromZ);
223                 Vector4f r3 = Vector4f.Shuffle (v, ShuffleSel.XFromY | ShuffleSel.YFromW | ShuffleSel.ZFromX | ShuffleSel.WFromZ);
224                 Vector4f a = x;
225
226                 r0 = r0 * m0 + x;
227                 r1 = r1 * m1 + x;
228                 x = x - v + v;
229                 r2 = r2 * m2 + x;
230                 r3 = r3 * m3 + x;
231                 Vector4f result = r0 + r1 + r2 + r3;
232
233                 if (a.X != 2f)
234                         return 1;
235                 if (a.Y != 4f)
236                         return 2;
237                 if (a.Z != 1f)
238                         return 3;
239                 if (a.W != 3f)
240                         return 4;
241                 if (result.Y != result.Y)
242                         return 0;
243                 return 0;
244         }
245         
246         public static int test_24_regs_pressure_a () {
247                 Vector4f a = new Vector4f (1, 2, 3, 4);
248                 Vector4f b = a + a;
249                 Vector4f c = b * a;
250                 Vector4f d = a - b;
251                 c = a + b + c + d;
252                 return (int)c.Z;
253         }
254
255         public static int test_54_regs_pressure_b () {
256                 Vector4f a = new Vector4f (1, 2, 3, 4);
257                 Vector4f b = a + a;
258                 Vector4f c = b - a;
259                 Vector4f d = c - a;
260                 Vector4f e = a + b + c;
261                 Vector4f f = d - b + a - c;
262                 Vector4f g = a - d * f - c + b;
263                 Vector4f h = a * b - c + e;
264                 Vector4f i = h - g - f - e - d - c - b - a;
265                 Vector4f j = a + b + c + d + e + f + g + h + i;
266                 return (int)j.Z;
267         }
268
269         static bool ff;
270         public static int test_3_single_block_var_is_properly_promoted () {
271                 Vector4f a = new Vector4f (4, 5, 6, 7);
272                 if (ff)
273                         a = a - a;
274                 else {
275                         Vector4f b = new Vector4f (1, 2, 3, 4);
276                         Vector4f c = b;
277                         a = a - b;
278                         if (ff) {
279                                 c = a;
280                                 a = c;
281                         }
282                 }
283                 return (int)a.X;
284         }
285
286         static float float_val = 45f;
287
288         public static int test_0_sse2_opt_and_simd_intrinsic_proper_regalloc () {
289                 Vector4f v = new Vector4f (1, 2, 3, 4);
290                 float f = float_val;
291                 int x = (int)f;
292                 if (v.X != 1f)
293                         return 1;
294                 if (x != 45f)
295                         return 2;
296                 return 0;
297         }
298
299         public static int Main () {
300                 return TestDriver.RunTests (typeof (SimdTests));
301         }
302 }
303