[jit] Add unaligned tests.
[mono.git] / mono / mini / unaligned.cs
1 using System;
2 using System.Runtime.CompilerServices;
3 using Mono;
4
5 /*
6  * Regression tests for the mono JIT.
7  *
8  * Each test needs to be of the form:
9  *
10  * static int test_<result>_<name> ();
11  *
12  * where <result> is an integer (the value that needs to be returned by
13  * the method to make it pass.
14  * <name> is a user-displayed name used to identify the test.
15  *
16  * The tests can be driven in two ways:
17  * *) running the program directly: Main() uses reflection to find and invoke
18  *      the test methods (this is useful mostly to check that the tests are correct)
19  * *) with the --regression switch of the jit (this is the preferred way since
20  *      all the tests will be run with optimizations on and off)
21  *
22  * The reflection logic could be moved to a .dll since we need at least another
23  * regression test file written in IL code to have better control on how
24  * the IL code looks.
25  */
26
27 #if __MOBILE__
28 namespace UnalignedTests
29 {
30 #endif
31
32
33 class Tests {
34
35 #if !__MOBILE__
36         public static int Main (string[] args) {
37                 return TestDriver.RunTests (typeof (Tests), args);
38         }
39 #endif
40
41
42         public static unsafe int test_0_ldobj_r4 ()
43         {
44                 byte *ptr = stackalloc byte [32];
45                 float f = (float)123.44f;
46                 *(float*)ptr = (float)f;
47
48                 int expected = *(int*)ptr;
49
50                 Intrinsics.UnalignedStobj<int> (ptr + 1, expected);
51                 if (Intrinsics.UnalignedLdobj<float> (ptr + 1) != f)
52                         return 1;
53
54                 return 0;
55         }
56
57         public static unsafe int test_0_ldobj_r8 ()
58         {
59                 byte *ptr = stackalloc byte [32];
60                 double f = 34423.44f;
61                 *(double*)ptr = (double)f;
62
63                 long expected = *(long*)ptr;
64
65                 Intrinsics.UnalignedStobj<long> (ptr + 3, expected);
66                 if (Intrinsics.UnalignedLdobj<double> (ptr + 3) != f)
67                         return 1;
68
69                 return 0;
70         }
71
72         public static unsafe int test_0_ldobj ()
73         {
74                 byte *ptr = stackalloc byte [20];
75                 for (int i = 0; i < 20; ++i)
76                         ptr [i] = (byte)i;
77
78
79                 if (Intrinsics.UnalignedLdobj<short> (ptr + 0) != 0x0100)
80                         return 1;
81
82                 if (Intrinsics.UnalignedLdobj<short> (ptr + 1) != 0x0201)
83                         return 2;
84
85                 if (Intrinsics.UnalignedLdobj<short> (ptr + 2) != 0x0302)
86                         return 3;
87
88                 if (Intrinsics.UnalignedLdobj<int> (ptr + 1) != 0x04030201)
89                         return 4;
90
91                 if (Intrinsics.UnalignedLdobj<int> (ptr + 2) != 0x05040302)
92                         return 5;
93
94                 if (Intrinsics.UnalignedLdobj<long> (ptr + 1) != 0x0807060504030201)
95                         return 6;
96
97                 if (Intrinsics.UnalignedLdobj<long> (ptr + 6) != 0xD0C0B0A09080706)
98                         return 7;
99
100                 return 0;
101         }
102
103         public static unsafe int test_0_ldind ()
104         {
105                 byte *ptr = stackalloc byte [20];
106                 for (int i = 0; i < 20; ++i)
107                         ptr [i] = (byte)i;
108
109
110                 if (Intrinsics.UnalignedLdInd2 (ptr + 0) != 0x0100)
111                         return 1;
112
113                 if (Intrinsics.UnalignedLdInd2 (ptr + 1) != 0x0201)
114                         return 2;
115
116                 if (Intrinsics.UnalignedLdInd2 (ptr + 2) != 0x0302)
117                         return 3;
118
119                 if (Intrinsics.UnalignedLdInd4 (ptr + 1) != 0x04030201)
120                         return 4;
121
122                 if (Intrinsics.UnalignedLdInd4 (ptr + 2) != 0x05040302)
123                         return 5;
124
125                 if (Intrinsics.UnalignedLdInd8 (ptr + 1) != 0x0807060504030201)
126                         return 6;
127
128                 if (Intrinsics.UnalignedLdInd8 (ptr + 6) != 0xD0C0B0A09080706)
129                         return 7;
130
131                 return 0;
132         }
133         public static unsafe int test_0_cpobj ()
134         {
135                 byte *dest = stackalloc byte [20];
136                 byte *src = stackalloc byte [20];
137                 for (int i = 0; i < 20; ++i)
138                         src [i] = (byte)i;
139
140                 Intrinsics.UnalignedCpobj<short> (dest + 0, src + 0);
141                 if (dest [0] != src [0] || dest [1] != src [1])
142                         return 1;
143
144                 Intrinsics.UnalignedCpobj<short> (dest + 1, src + 0);
145                 if (dest [1] != src [0] || dest [2] != src [1])
146                         return 2;
147
148                 Intrinsics.UnalignedCpobj<short> (dest + 0, src + 1);
149                 if (dest [0] != src [1] || dest [1] != src [2])
150                         return 3;
151
152                 Intrinsics.UnalignedCpobj<short> (dest + 1, src + 1);
153                 if (dest [1] != src [1] || dest [2] != src [2])
154                         return 3;
155
156                 Intrinsics.UnalignedCpobj<int> (dest + 3, src);
157                 for (int i = 0; i < 4; ++i) {
158                         if (dest [i + 3] != src [i])
159                                 return 4;
160                 }
161
162                 Intrinsics.UnalignedCpobj<int> (dest + 1, src + 2);
163                 for (int i = 0; i < 4; ++i) {
164                         if (dest [i + 1] != src [i + 2])
165                                 return 5;
166                 }
167
168                 Intrinsics.UnalignedCpobj<long> (dest + 1, src + 2);
169                 for (int i = 0; i < 8; ++i) {
170                         if (dest [i + 1] != src [i + 2])
171                                 return 6;
172                 }
173
174                 Intrinsics.UnalignedCpobj<long> (dest + 7, src + 2);
175                 for (int i = 0; i < 8; ++i) {
176                         if (dest [i + 7] != src [i + 2])
177                                 return 7;
178                 }
179
180                 return 0;
181         }
182
183         public static unsafe int test_0_stobj ()
184         {
185                 byte *ptr = stackalloc byte [20];
186
187                 Intrinsics.UnalignedStobj <short> (ptr + 0, 0x6688);
188                 if (ptr [0] != 0x88 || ptr [1] != 0x66)
189                         return 1;
190
191                 Intrinsics.UnalignedStobj <short> (ptr + 1, 0x6589);
192                 if (ptr [1] != 0x89 || ptr [2] != 0x65)
193                         return 2;
194
195                 Intrinsics.UnalignedStobj <int> (ptr + 1, 0x60708090);
196                 if (ptr [1] != 0x90 || ptr [2] != 0x80 || ptr [3] != 0x70 || ptr [4] != 0x60)
197                         return 3;
198
199                 Intrinsics.UnalignedStobj <long> (ptr + 1, 0x405060708090);
200                 if (ptr [1] != 0x90 || ptr [2] != 0x80 || ptr [3] != 0x70 || ptr [4] != 0x60 || ptr [5] != 0x50 || ptr [6] != 0x40)
201                         return 4;
202
203                 return 0;
204         }
205
206         public static unsafe int test_0_ldobj_stobj ()
207         {
208                 byte *dest = stackalloc byte [20];
209                 byte *src = stackalloc byte [20];
210
211                 for (int i = 0; i < 20; ++i)
212                         src [i] = (byte)i;
213
214                 Intrinsics.UnalignedLdobjStObjPair<short> (dest + 0, src + 0);
215                 if (dest [0] != src [0] || dest [1] != src [1])
216                         return 1;
217
218                 Intrinsics.UnalignedLdobjStObjPair<short> (dest + 1, src + 0);
219                 if (dest [1] != src [0] || dest [2] != src [1])
220                         return 2;
221
222                 Intrinsics.UnalignedLdobjStObjPair<short> (dest + 0, src + 1);
223                 if (dest [0] != src [1] || dest [1] != src [2])
224                         return 3;
225
226                 Intrinsics.UnalignedLdobjStObjPair<short> (dest + 1, src + 1);
227                 if (dest [1] != src [1] || dest [2] != src [2])
228                         return 3;
229
230                 Intrinsics.UnalignedLdobjStObjPair<int> (dest + 1, src + 1);
231                 if (dest [1] != src [1] || dest [2] != src [2])
232                         return 4;
233
234                 Intrinsics.UnalignedLdobjStObjPair<long> (dest + 1, src + 1);
235                 if (dest [1] != src [1] || dest [2] != src [2])
236                         return 5;
237
238
239                 return 0;
240         }
241
242
243         public static unsafe int test_0_cpblk ()
244         {
245                 byte *dest = stackalloc byte [20];
246                 byte *src = stackalloc byte [20];
247                 for (int i = 0; i < 20; ++i)
248                         src [i] = (byte)i;
249
250
251                 Intrinsics.UnalignedCpblk (dest + 0, src + 0, 2);
252                 if (dest [0] != src [0] || dest [1] != src [1])
253                         return 1;
254
255                 Intrinsics.UnalignedCpblk (dest + 1, src + 0, 2);
256                 if (dest [1] != src [0] || dest [2] != src [1])
257                         return 2;
258
259                 Intrinsics.UnalignedCpblk (dest + 0, src + 1, 2);
260                 if (dest [0] != src [1] || dest [1] != src [2])
261                         return 3;
262
263                 Intrinsics.UnalignedCpblk (dest + 1, src + 1, 2);
264                 if (dest [1] != src [1] || dest [2] != src [2])
265                         return 3;
266
267                 Intrinsics.UnalignedCpblk (dest + 1, src + 1, 4);
268                 for (int i = 0; i < 4; ++i) {
269                         if (dest [i + 1] != src [i + 1])
270                                 return 4;
271                 }
272
273                 Intrinsics.UnalignedCpblk (dest + 1, src + 1, 8);
274                 for (int i = 0; i < 8; ++i) {
275                         if (dest [i + 1] != src [i + 1])
276                                 return 5;
277                 }
278
279                 return 0;
280         }
281
282
283         public static unsafe int test_0_initblk ()
284         {
285                 byte *ptr = stackalloc byte [20];
286
287                 for (int i = 0; i < 20; ++i)
288                         ptr [i] = (byte)i;
289
290                 Intrinsics.UnalignedInit (ptr, 30, 2);
291                 if (ptr [0] != 30 || ptr [1] != 30)
292                         return 1;
293
294                 Intrinsics.UnalignedInit (ptr + 1, 31, 2);
295                 if (ptr[0] != 30 || ptr [1] != 31 || ptr [2] != 31)
296                         return 2;
297
298                 return 0;
299         }
300 }
301
302 #if __MOBILE__
303 }
304 #endif
305
306
307