FreeRDP
TestPrimitivesShift.c
1 /* test_shift.c
2  * vi:ts=4 sw=4
3  *
4  * (c) Copyright 2012 Hewlett-Packard Development Company, L.P.
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may
6  * not use this file except in compliance with the License. You may obtain
7  * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 #include <freerdp/config.h>
16 
17 #include <winpr/sysinfo.h>
18 #include "prim_test.h"
19 
20 #define FUNC_TEST_SIZE 65536
21 
22 static BOOL test_lShift_16s_func(void)
23 {
24  pstatus_t status = 0;
25  INT16 ALIGN(src[FUNC_TEST_SIZE + 3]);
26  INT16 ALIGN(d1[FUNC_TEST_SIZE + 3]);
27  UINT32 val = 0;
28  winpr_RAND(&val, sizeof(val));
29  winpr_RAND(src, sizeof(src));
30  val = val % 16;
31  /* Negative tests */
32  status = generic->lShiftC_16s(src + 1, 16, d1 + 1, FUNC_TEST_SIZE);
33 
34  if (status == PRIMITIVES_SUCCESS)
35  return FALSE;
36 
37  status = optimized->lShiftC_16s(src + 1, 16, d1 + 1, FUNC_TEST_SIZE);
38 
39  if (status == PRIMITIVES_SUCCESS)
40  return FALSE;
41 
42  /* Aligned */
43  status = generic->lShiftC_16s(src + 1, val, d1 + 1, FUNC_TEST_SIZE);
44 
45  if (status != PRIMITIVES_SUCCESS)
46  return FALSE;
47 
48  status = optimized->lShiftC_16s(src + 1, val, d1 + 1, FUNC_TEST_SIZE);
49 
50  if (status != PRIMITIVES_SUCCESS)
51  return FALSE;
52 
53  /* Unaligned */
54  status = generic->lShiftC_16s(src + 1, val, d1 + 2, FUNC_TEST_SIZE);
55 
56  if (status != PRIMITIVES_SUCCESS)
57  return FALSE;
58 
59  status = optimized->lShiftC_16s(src + 1, val, d1 + 2, FUNC_TEST_SIZE);
60 
61  if (status != PRIMITIVES_SUCCESS)
62  return FALSE;
63 
64  return TRUE;
65 }
66 
67 static BOOL test_lShift_16u_func(void)
68 {
69  pstatus_t status = 0;
70  UINT16 ALIGN(src[FUNC_TEST_SIZE + 3]);
71  UINT16 ALIGN(d1[FUNC_TEST_SIZE + 3]);
72  UINT32 val = 0;
73  winpr_RAND(&val, sizeof(val));
74  winpr_RAND(src, sizeof(src));
75  val = val % 16;
76 
77  /* Negative tests */
78  status = generic->lShiftC_16u(src + 1, 16, d1 + 1, FUNC_TEST_SIZE);
79 
80  if (status == PRIMITIVES_SUCCESS)
81  return FALSE;
82 
83  status = optimized->lShiftC_16u(src + 1, 16, d1 + 1, FUNC_TEST_SIZE);
84 
85  if (status == PRIMITIVES_SUCCESS)
86  return FALSE;
87 
88  /* Aligned */
89  status = generic->lShiftC_16u(src + 1, val, d1 + 1, FUNC_TEST_SIZE);
90 
91  if (status != PRIMITIVES_SUCCESS)
92  return FALSE;
93 
94  status = optimized->lShiftC_16u(src + 1, val, d1 + 1, FUNC_TEST_SIZE);
95 
96  if (status != PRIMITIVES_SUCCESS)
97  return FALSE;
98 
99  /* Unaligned */
100  status = generic->lShiftC_16u(src + 1, val, d1 + 2, FUNC_TEST_SIZE);
101 
102  if (status != PRIMITIVES_SUCCESS)
103  return FALSE;
104 
105  status = optimized->lShiftC_16u(src + 1, val, d1 + 2, FUNC_TEST_SIZE);
106 
107  if (status != PRIMITIVES_SUCCESS)
108  return FALSE;
109 
110  return TRUE;
111 }
112 
113 static BOOL test_rShift_16s_func(void)
114 {
115  pstatus_t status = 0;
116  INT16 ALIGN(src[FUNC_TEST_SIZE + 3]);
117  INT16 ALIGN(d1[FUNC_TEST_SIZE + 3]);
118  UINT32 val = 0;
119  winpr_RAND(&val, sizeof(val));
120  winpr_RAND(src, sizeof(src));
121  val = val % 16;
122 
123  /* Negative Tests */
124  status = generic->rShiftC_16s(src + 1, 16, d1 + 1, FUNC_TEST_SIZE);
125 
126  if (status == PRIMITIVES_SUCCESS)
127  return FALSE;
128 
129  status = optimized->rShiftC_16s(src + 1, 16, d1 + 1, FUNC_TEST_SIZE);
130 
131  if (status == PRIMITIVES_SUCCESS)
132  return FALSE;
133 
134  /* Aligned */
135  status = generic->rShiftC_16s(src + 1, val, d1 + 1, FUNC_TEST_SIZE);
136 
137  if (status != PRIMITIVES_SUCCESS)
138  return FALSE;
139 
140  status = optimized->rShiftC_16s(src + 1, val, d1 + 1, FUNC_TEST_SIZE);
141 
142  if (status != PRIMITIVES_SUCCESS)
143  return FALSE;
144 
145  /* Unaligned */
146  status = generic->rShiftC_16s(src + 1, val, d1 + 2, FUNC_TEST_SIZE);
147 
148  if (status != PRIMITIVES_SUCCESS)
149  return FALSE;
150 
151  status = optimized->rShiftC_16s(src + 1, val, d1 + 2, FUNC_TEST_SIZE);
152 
153  if (status != PRIMITIVES_SUCCESS)
154  return FALSE;
155 
156  return TRUE;
157 }
158 
159 static BOOL test_rShift_16u_func(void)
160 {
161  pstatus_t status = 0;
162  UINT16 ALIGN(src[FUNC_TEST_SIZE + 3]);
163  UINT16 ALIGN(d1[FUNC_TEST_SIZE + 3]);
164  UINT32 val = 0;
165  winpr_RAND(&val, sizeof(val));
166  winpr_RAND(src, sizeof(src));
167  val = val % 16;
168  /* Negative tests */
169  status = generic->rShiftC_16u(src + 1, 16, d1 + 1, FUNC_TEST_SIZE);
170 
171  if (status == PRIMITIVES_SUCCESS)
172  return FALSE;
173 
174  status = optimized->rShiftC_16u(src + 1, 16, d1 + 1, FUNC_TEST_SIZE);
175 
176  if (status == PRIMITIVES_SUCCESS)
177  return FALSE;
178 
179  /* Aligned */
180  status = generic->rShiftC_16u(src + 1, val, d1 + 1, FUNC_TEST_SIZE);
181 
182  if (status != PRIMITIVES_SUCCESS)
183  return FALSE;
184 
185  status = optimized->rShiftC_16u(src + 1, val, d1 + 1, FUNC_TEST_SIZE);
186 
187  if (status != PRIMITIVES_SUCCESS)
188  return FALSE;
189 
190  /* Unaligned */
191  status = generic->rShiftC_16u(src + 1, val, d1 + 2, FUNC_TEST_SIZE);
192 
193  if (status != PRIMITIVES_SUCCESS)
194  return FALSE;
195 
196  status = optimized->rShiftC_16u(src + 1, val, d1 + 2, FUNC_TEST_SIZE);
197 
198  if (status != PRIMITIVES_SUCCESS)
199  return FALSE;
200 
201  return TRUE;
202 }
203 
204 static BOOL test_ShiftWrapper_16s_func(void)
205 {
206  pstatus_t status = 0;
207  INT16 ALIGN(src[FUNC_TEST_SIZE + 3]) = { 0 };
208  INT16 ALIGN(d1[FUNC_TEST_SIZE + 3]) = { 0 };
209  UINT32 tmp = 0;
210  winpr_RAND(&tmp, sizeof(tmp));
211  winpr_RAND(src, sizeof(src));
212  INT32 val = WINPR_ASSERTING_INT_CAST(int32_t, tmp % 16);
213 
214  /* Negative tests */
215  status = generic->shiftC_16s(src + 1, 16, d1 + 1, FUNC_TEST_SIZE);
216 
217  if (status == PRIMITIVES_SUCCESS)
218  return FALSE;
219 
220  status = optimized->shiftC_16s(src + 1, 16, d1 + 1, FUNC_TEST_SIZE);
221 
222  if (status == PRIMITIVES_SUCCESS)
223  return FALSE;
224 
225  /* Aligned */
226  status = generic->shiftC_16s(src + 1, val, d1 + 1, FUNC_TEST_SIZE);
227 
228  if (status != PRIMITIVES_SUCCESS)
229  return FALSE;
230 
231  status = optimized->shiftC_16s(src + 1, val, d1 + 1, FUNC_TEST_SIZE);
232 
233  if (status != PRIMITIVES_SUCCESS)
234  return FALSE;
235 
236  status = generic->shiftC_16s(src + 1, -val, d1 + 1, FUNC_TEST_SIZE);
237 
238  if (status != PRIMITIVES_SUCCESS)
239  return FALSE;
240 
241  status = optimized->shiftC_16s(src + 1, -val, d1 + 1, FUNC_TEST_SIZE);
242 
243  if (status != PRIMITIVES_SUCCESS)
244  return FALSE;
245 
246  /* Unaligned */
247  status = generic->shiftC_16s(src + 1, val, d1 + 2, FUNC_TEST_SIZE);
248 
249  if (status != PRIMITIVES_SUCCESS)
250  return FALSE;
251 
252  status = optimized->shiftC_16s(src + 1, val, d1 + 2, FUNC_TEST_SIZE);
253 
254  if (status != PRIMITIVES_SUCCESS)
255  return FALSE;
256 
257  status = generic->shiftC_16s(src + 1, -val, d1 + 2, FUNC_TEST_SIZE);
258 
259  if (status != PRIMITIVES_SUCCESS)
260  return FALSE;
261 
262  status = optimized->shiftC_16s(src + 1, -val, d1 + 2, FUNC_TEST_SIZE);
263 
264  if (status != PRIMITIVES_SUCCESS)
265  return FALSE;
266 
267  return TRUE;
268 }
269 
270 static BOOL test_ShiftWrapper_16u_func(void)
271 {
272  pstatus_t status = 0;
273  UINT16 ALIGN(src[FUNC_TEST_SIZE + 3]) = { 0 };
274  UINT16 ALIGN(d1[FUNC_TEST_SIZE + 3]) = { 0 };
275  UINT32 tmp = 0;
276  winpr_RAND(&tmp, sizeof(tmp));
277  winpr_RAND(src, sizeof(src));
278  INT32 val = WINPR_ASSERTING_INT_CAST(int32_t, tmp % 16);
279 
280  /* Negative */
281  status = generic->shiftC_16u(src + 1, 16, d1 + 1, FUNC_TEST_SIZE);
282  if (status == PRIMITIVES_SUCCESS)
283  return FALSE;
284 
285  status = optimized->shiftC_16u(src + 1, 16, d1 + 1, FUNC_TEST_SIZE);
286 
287  if (status == PRIMITIVES_SUCCESS)
288  return FALSE;
289 
290  /* Aligned */
291  status = generic->shiftC_16u(src + 1, val, d1 + 1, FUNC_TEST_SIZE);
292 
293  if (status != PRIMITIVES_SUCCESS)
294  return FALSE;
295 
296  status = optimized->shiftC_16u(src + 1, val, d1 + 1, FUNC_TEST_SIZE);
297 
298  if (status != PRIMITIVES_SUCCESS)
299  return FALSE;
300 
301  status = generic->shiftC_16u(src + 1, -val, d1 + 1, FUNC_TEST_SIZE);
302 
303  if (status != PRIMITIVES_SUCCESS)
304  return FALSE;
305 
306  status = optimized->shiftC_16u(src + 1, -val, d1 + 1, FUNC_TEST_SIZE);
307 
308  if (status != PRIMITIVES_SUCCESS)
309  return FALSE;
310 
311  /* Unaligned */
312  status = generic->shiftC_16u(src + 1, val, d1 + 2, FUNC_TEST_SIZE);
313 
314  if (status != PRIMITIVES_SUCCESS)
315  return FALSE;
316 
317  status = optimized->shiftC_16u(src + 1, val, d1 + 2, FUNC_TEST_SIZE);
318 
319  if (status != PRIMITIVES_SUCCESS)
320  return FALSE;
321 
322  status = generic->shiftC_16u(src + 1, -val, d1 + 2, FUNC_TEST_SIZE);
323 
324  if (status != PRIMITIVES_SUCCESS)
325  return FALSE;
326 
327  status = optimized->shiftC_16u(src + 1, -val, d1 + 2, FUNC_TEST_SIZE);
328 
329  if (status != PRIMITIVES_SUCCESS)
330  return FALSE;
331 
332  return TRUE;
333 }
334 
335 /* ------------------------------------------------------------------------- */
336 static BOOL test_lShift_16s_speed(void)
337 {
338  UINT32 val = 0;
339  INT16 ALIGN(src[MAX_TEST_SIZE + 1]);
340  INT16 ALIGN(dst[MAX_TEST_SIZE + 1]);
341  winpr_RAND(src, sizeof(src));
342  winpr_RAND(&val, sizeof(val));
343 
344  val = val % 16;
345  if (!speed_test("lShift_16s", "aligned", g_Iterations, (speed_test_fkt)generic->lShiftC_16s,
346  (speed_test_fkt)optimized->lShiftC_16s, src, val, dst, MAX_TEST_SIZE))
347  return FALSE;
348 
349  if (!speed_test("lShift_16s", "unaligned", g_Iterations, (speed_test_fkt)generic->lShiftC_16s,
350  (speed_test_fkt)optimized->lShiftC_16s, src + 1, val, dst, MAX_TEST_SIZE))
351  return FALSE;
352 
353  return TRUE;
354 }
355 
356 /* ------------------------------------------------------------------------- */
357 static BOOL test_lShift_16u_speed(void)
358 {
359  UINT32 val = 0;
360  UINT16 ALIGN(src[MAX_TEST_SIZE + 1]);
361  UINT16 ALIGN(dst[MAX_TEST_SIZE + 1]);
362  winpr_RAND(&val, sizeof(val));
363  winpr_RAND(src, sizeof(src));
364 
365  val = val % 16;
366  if (!speed_test("lShift_16u", "aligned", g_Iterations, (speed_test_fkt)generic->lShiftC_16u,
367  (speed_test_fkt)optimized->lShiftC_16u, src, val, dst, MAX_TEST_SIZE))
368  return FALSE;
369 
370  if (!speed_test("lShift_16u", "unaligned", g_Iterations, (speed_test_fkt)generic->lShiftC_16u,
371  (speed_test_fkt)optimized->lShiftC_16u, src + 1, val, dst, MAX_TEST_SIZE))
372  return FALSE;
373 
374  return TRUE;
375 }
376 
377 /* ------------------------------------------------------------------------- */
378 static BOOL test_rShift_16s_speed(void)
379 {
380  UINT32 val = 0;
381  INT16 ALIGN(src[MAX_TEST_SIZE + 1]);
382  INT16 ALIGN(dst[MAX_TEST_SIZE + 1]);
383  winpr_RAND(src, sizeof(src));
384  winpr_RAND(&val, sizeof(val));
385 
386  val = val % 16;
387  if (!speed_test("rShift_16s", "aligned", g_Iterations, (speed_test_fkt)generic->rShiftC_16s,
388  (speed_test_fkt)optimized->rShiftC_16s, src, val, dst, MAX_TEST_SIZE))
389  return FALSE;
390 
391  if (!speed_test("rShift_16s", "unaligned", g_Iterations, (speed_test_fkt)generic->rShiftC_16s,
392  (speed_test_fkt)optimized->rShiftC_16s, src + 1, val, dst, MAX_TEST_SIZE))
393  return FALSE;
394 
395  return TRUE;
396 }
397 
398 /* ------------------------------------------------------------------------- */
399 static BOOL test_rShift_16u_speed(void)
400 {
401  UINT32 val = 0;
402  UINT16 ALIGN(src[MAX_TEST_SIZE + 1]);
403  UINT16 ALIGN(dst[MAX_TEST_SIZE + 1]);
404  winpr_RAND(&val, sizeof(val));
405  winpr_RAND(src, sizeof(src));
406 
407  val = val % 16;
408  if (!speed_test("rShift_16u", "aligned", g_Iterations, (speed_test_fkt)generic->rShiftC_16u,
409  (speed_test_fkt)optimized->rShiftC_16u, src, val, dst, MAX_TEST_SIZE))
410  return FALSE;
411 
412  if (!speed_test("rShift_16u", "unaligned", g_Iterations, (speed_test_fkt)generic->rShiftC_16u,
413  (speed_test_fkt)optimized->rShiftC_16u, src + 1, val, dst, MAX_TEST_SIZE))
414  return FALSE;
415 
416  return TRUE;
417 }
418 
419 int TestPrimitivesShift(int argc, char* argv[])
420 {
421  WINPR_UNUSED(argc);
422  WINPR_UNUSED(argv);
423  prim_test_setup(FALSE);
424 
425  if (!test_lShift_16s_func())
426  return 1;
427 
428  if (g_TestPrimitivesPerformance)
429  {
430  if (!test_lShift_16s_speed())
431  return 1;
432  }
433 
434  if (!test_lShift_16u_func())
435  return 1;
436 
437  if (g_TestPrimitivesPerformance)
438  {
439  if (!test_lShift_16u_speed())
440  return 1;
441  }
442 
443  if (!test_rShift_16s_func())
444  return 1;
445 
446  if (g_TestPrimitivesPerformance)
447  {
448  if (!test_rShift_16s_speed())
449  return 1;
450  }
451 
452  if (!test_rShift_16u_func())
453  return 1;
454 
455  if (g_TestPrimitivesPerformance)
456  {
457  if (!test_rShift_16u_speed())
458  return 1;
459  }
460 
461  if (!test_ShiftWrapper_16s_func())
462  return 1;
463 
464  if (!test_ShiftWrapper_16u_func())
465  return 1;
466 
467  return 0;
468 }