FreeRDP
Loading...
Searching...
No Matches
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
22static BOOL test_lShift_16s_func(void)
23{
24 pstatus_t status = 0;
25 INT16 src[FUNC_TEST_SIZE + 3] = WINPR_C_ARRAY_INIT;
26 INT16 d1[FUNC_TEST_SIZE + 3] = WINPR_C_ARRAY_INIT;
27 UINT32 val = 0;
28 if (winpr_RAND(&val, sizeof(val)) < 0)
29 return FALSE;
30 if (winpr_RAND(src, sizeof(src)) < 0)
31 return FALSE;
32 val = val % 16;
33 /* Negative tests */
34 status = generic->lShiftC_16s(src + 1, 16, d1 + 1, FUNC_TEST_SIZE);
35
36 if (status == PRIMITIVES_SUCCESS)
37 return FALSE;
38
39 status = optimized->lShiftC_16s(src + 1, 16, d1 + 1, FUNC_TEST_SIZE);
40
41 if (status == PRIMITIVES_SUCCESS)
42 return FALSE;
43
44 /* Aligned */
45 status = generic->lShiftC_16s(src + 1, val, d1 + 1, FUNC_TEST_SIZE);
46
47 if (status != PRIMITIVES_SUCCESS)
48 return FALSE;
49
50 status = optimized->lShiftC_16s(src + 1, val, d1 + 1, FUNC_TEST_SIZE);
51
52 if (status != PRIMITIVES_SUCCESS)
53 return FALSE;
54
55 /* Unaligned */
56 status = generic->lShiftC_16s(src + 1, val, d1 + 2, FUNC_TEST_SIZE);
57
58 if (status != PRIMITIVES_SUCCESS)
59 return FALSE;
60
61 status = optimized->lShiftC_16s(src + 1, val, d1 + 2, FUNC_TEST_SIZE);
62
63 return (status == PRIMITIVES_SUCCESS);
64}
65
66static BOOL test_lShift_16s_inplace_func(void)
67{
68 const UINT32 lengths[] = { 1, 7, 15, 16, 17, 33, FUNC_TEST_SIZE };
69 INT16 d1[FUNC_TEST_SIZE + 3] = WINPR_C_ARRAY_INIT;
70 INT16 d2[FUNC_TEST_SIZE + 3] = WINPR_C_ARRAY_INIT;
71
72 if (winpr_RAND(d1, sizeof(d1)) < 0)
73 return FALSE;
74 memcpy(d2, d1, sizeof(d1));
75
76 /* Negative tests */
77 if (generic->lShiftC_16s_inplace(d1 + 1, 16, FUNC_TEST_SIZE) == PRIMITIVES_SUCCESS)
78 return FALSE;
79 if (optimized->lShiftC_16s_inplace(d2 + 1, 16, FUNC_TEST_SIZE) == PRIMITIVES_SUCCESS)
80 return FALSE;
81 if (memcmp(d1, d2, sizeof(d1)) != 0)
82 return FALSE;
83
84 for (UINT32 val = 0; val < 16; val++)
85 {
86 for (size_t i = 0; i < ARRAYSIZE(lengths); i++)
87 {
88 /* Aligned and unaligned */
89 for (size_t offset = 0; offset < 2; offset++)
90 {
91 if (winpr_RAND(d1, sizeof(d1)) < 0)
92 return FALSE;
93 memcpy(d2, d1, sizeof(d1));
94
95 if (generic->lShiftC_16s_inplace(d1 + offset, val, lengths[i]) !=
96 PRIMITIVES_SUCCESS)
97 return FALSE;
98 if (optimized->lShiftC_16s_inplace(d2 + offset, val, lengths[i]) !=
99 PRIMITIVES_SUCCESS)
100 return FALSE;
101 if (memcmp(d1, d2, sizeof(d1)) != 0)
102 {
103 (void)fprintf(stderr,
104 "lShiftC_16s_inplace mismatch, val=%" PRIu32 " len=%" PRIu32 "\n",
105 val, lengths[i]);
106 return FALSE;
107 }
108 }
109 }
110 }
111
112 return TRUE;
113}
114
115static BOOL test_lShift_16u_func(void)
116{
117 pstatus_t status = 0;
118 UINT16 src[FUNC_TEST_SIZE + 3] = WINPR_C_ARRAY_INIT;
119 UINT16 d1[FUNC_TEST_SIZE + 3] = WINPR_C_ARRAY_INIT;
120 UINT32 val = 0;
121 if (winpr_RAND(&val, sizeof(val)) < 0)
122 return FALSE;
123 if (winpr_RAND(src, sizeof(src)) < 0)
124 return FALSE;
125 val = val % 16;
126
127 /* Negative tests */
128 status = generic->lShiftC_16u(src + 1, 16, d1 + 1, FUNC_TEST_SIZE);
129
130 if (status == PRIMITIVES_SUCCESS)
131 return FALSE;
132
133 status = optimized->lShiftC_16u(src + 1, 16, d1 + 1, FUNC_TEST_SIZE);
134
135 if (status == PRIMITIVES_SUCCESS)
136 return FALSE;
137
138 /* Aligned */
139 status = generic->lShiftC_16u(src + 1, val, d1 + 1, FUNC_TEST_SIZE);
140
141 if (status != PRIMITIVES_SUCCESS)
142 return FALSE;
143
144 status = optimized->lShiftC_16u(src + 1, val, d1 + 1, FUNC_TEST_SIZE);
145
146 if (status != PRIMITIVES_SUCCESS)
147 return FALSE;
148
149 /* Unaligned */
150 status = generic->lShiftC_16u(src + 1, val, d1 + 2, FUNC_TEST_SIZE);
151
152 if (status != PRIMITIVES_SUCCESS)
153 return FALSE;
154
155 status = optimized->lShiftC_16u(src + 1, val, d1 + 2, FUNC_TEST_SIZE);
156
157 return (status == PRIMITIVES_SUCCESS);
158}
159
160static BOOL test_rShift_16s_func(void)
161{
162 pstatus_t status = 0;
163 INT16 src[FUNC_TEST_SIZE + 3] = WINPR_C_ARRAY_INIT;
164 INT16 d1[FUNC_TEST_SIZE + 3] = WINPR_C_ARRAY_INIT;
165 UINT32 val = 0;
166 if (winpr_RAND(&val, sizeof(val)) < 0)
167 return FALSE;
168 if (winpr_RAND(src, sizeof(src)) < 0)
169 return FALSE;
170 val = val % 16;
171
172 /* Negative Tests */
173 status = generic->rShiftC_16s(src + 1, 16, d1 + 1, FUNC_TEST_SIZE);
174
175 if (status == PRIMITIVES_SUCCESS)
176 return FALSE;
177
178 status = optimized->rShiftC_16s(src + 1, 16, d1 + 1, FUNC_TEST_SIZE);
179
180 if (status == PRIMITIVES_SUCCESS)
181 return FALSE;
182
183 /* Aligned */
184 status = generic->rShiftC_16s(src + 1, val, d1 + 1, FUNC_TEST_SIZE);
185
186 if (status != PRIMITIVES_SUCCESS)
187 return FALSE;
188
189 status = optimized->rShiftC_16s(src + 1, val, d1 + 1, FUNC_TEST_SIZE);
190
191 if (status != PRIMITIVES_SUCCESS)
192 return FALSE;
193
194 /* Unaligned */
195 status = generic->rShiftC_16s(src + 1, val, d1 + 2, FUNC_TEST_SIZE);
196
197 if (status != PRIMITIVES_SUCCESS)
198 return FALSE;
199
200 status = optimized->rShiftC_16s(src + 1, val, d1 + 2, FUNC_TEST_SIZE);
201
202 return (status == PRIMITIVES_SUCCESS);
203}
204
205static BOOL test_rShift_16u_func(void)
206{
207 pstatus_t status = 0;
208 UINT16 src[FUNC_TEST_SIZE + 3] = WINPR_C_ARRAY_INIT;
209 UINT16 d1[FUNC_TEST_SIZE + 3] = WINPR_C_ARRAY_INIT;
210 UINT32 val = 0;
211 if (winpr_RAND(&val, sizeof(val)) < 0)
212 return FALSE;
213 if (winpr_RAND(src, sizeof(src)) < 0)
214 return FALSE;
215 val = val % 16;
216 /* Negative tests */
217 status = generic->rShiftC_16u(src + 1, 16, d1 + 1, FUNC_TEST_SIZE);
218
219 if (status == PRIMITIVES_SUCCESS)
220 return FALSE;
221
222 status = optimized->rShiftC_16u(src + 1, 16, d1 + 1, FUNC_TEST_SIZE);
223
224 if (status == PRIMITIVES_SUCCESS)
225 return FALSE;
226
227 /* Aligned */
228 status = generic->rShiftC_16u(src + 1, val, d1 + 1, FUNC_TEST_SIZE);
229
230 if (status != PRIMITIVES_SUCCESS)
231 return FALSE;
232
233 status = optimized->rShiftC_16u(src + 1, val, d1 + 1, FUNC_TEST_SIZE);
234
235 if (status != PRIMITIVES_SUCCESS)
236 return FALSE;
237
238 /* Unaligned */
239 status = generic->rShiftC_16u(src + 1, val, d1 + 2, FUNC_TEST_SIZE);
240
241 if (status != PRIMITIVES_SUCCESS)
242 return FALSE;
243
244 status = optimized->rShiftC_16u(src + 1, val, d1 + 2, FUNC_TEST_SIZE);
245
246 return (status == PRIMITIVES_SUCCESS);
247}
248
249static BOOL test_ShiftWrapper_16s_func(void)
250{
251 pstatus_t status = 0;
252 INT16 src[FUNC_TEST_SIZE + 3] = WINPR_C_ARRAY_INIT;
253 INT16 d1[FUNC_TEST_SIZE + 3] = WINPR_C_ARRAY_INIT;
254 UINT32 tmp = 0;
255 if (winpr_RAND(&tmp, sizeof(tmp)) < 0)
256 return FALSE;
257 if (winpr_RAND(src, sizeof(src)) < 0)
258 return FALSE;
259 INT32 val = WINPR_ASSERTING_INT_CAST(int32_t, tmp % 16);
260
261 /* Negative tests */
262 status = generic->shiftC_16s(src + 1, 16, d1 + 1, FUNC_TEST_SIZE);
263
264 if (status == PRIMITIVES_SUCCESS)
265 return FALSE;
266
267 status = optimized->shiftC_16s(src + 1, 16, d1 + 1, FUNC_TEST_SIZE);
268
269 if (status == PRIMITIVES_SUCCESS)
270 return FALSE;
271
272 /* Aligned */
273 status = generic->shiftC_16s(src + 1, val, d1 + 1, FUNC_TEST_SIZE);
274
275 if (status != PRIMITIVES_SUCCESS)
276 return FALSE;
277
278 status = optimized->shiftC_16s(src + 1, val, d1 + 1, FUNC_TEST_SIZE);
279
280 if (status != PRIMITIVES_SUCCESS)
281 return FALSE;
282
283 status = generic->shiftC_16s(src + 1, -val, d1 + 1, FUNC_TEST_SIZE);
284
285 if (status != PRIMITIVES_SUCCESS)
286 return FALSE;
287
288 status = optimized->shiftC_16s(src + 1, -val, d1 + 1, FUNC_TEST_SIZE);
289
290 if (status != PRIMITIVES_SUCCESS)
291 return FALSE;
292
293 /* Unaligned */
294 status = generic->shiftC_16s(src + 1, val, d1 + 2, FUNC_TEST_SIZE);
295
296 if (status != PRIMITIVES_SUCCESS)
297 return FALSE;
298
299 status = optimized->shiftC_16s(src + 1, val, d1 + 2, FUNC_TEST_SIZE);
300
301 if (status != PRIMITIVES_SUCCESS)
302 return FALSE;
303
304 status = generic->shiftC_16s(src + 1, -val, d1 + 2, FUNC_TEST_SIZE);
305
306 if (status != PRIMITIVES_SUCCESS)
307 return FALSE;
308
309 status = optimized->shiftC_16s(src + 1, -val, d1 + 2, FUNC_TEST_SIZE);
310
311 return (status == PRIMITIVES_SUCCESS);
312}
313
314static BOOL test_ShiftWrapper_16u_func(void)
315{
316 pstatus_t status = 0;
317 UINT16 src[FUNC_TEST_SIZE + 3] = WINPR_C_ARRAY_INIT;
318 UINT16 d1[FUNC_TEST_SIZE + 3] = WINPR_C_ARRAY_INIT;
319 UINT32 tmp = 0;
320 if (winpr_RAND(&tmp, sizeof(tmp)) < 0)
321 return FALSE;
322 if (winpr_RAND(src, sizeof(src)) < 0)
323 return FALSE;
324 INT32 val = WINPR_ASSERTING_INT_CAST(int32_t, tmp % 16);
325
326 /* Negative */
327 status = generic->shiftC_16u(src + 1, 16, d1 + 1, FUNC_TEST_SIZE);
328 if (status == PRIMITIVES_SUCCESS)
329 return FALSE;
330
331 status = optimized->shiftC_16u(src + 1, 16, d1 + 1, FUNC_TEST_SIZE);
332
333 if (status == PRIMITIVES_SUCCESS)
334 return FALSE;
335
336 /* Aligned */
337 status = generic->shiftC_16u(src + 1, val, d1 + 1, FUNC_TEST_SIZE);
338
339 if (status != PRIMITIVES_SUCCESS)
340 return FALSE;
341
342 status = optimized->shiftC_16u(src + 1, val, d1 + 1, FUNC_TEST_SIZE);
343
344 if (status != PRIMITIVES_SUCCESS)
345 return FALSE;
346
347 status = generic->shiftC_16u(src + 1, -val, d1 + 1, FUNC_TEST_SIZE);
348
349 if (status != PRIMITIVES_SUCCESS)
350 return FALSE;
351
352 status = optimized->shiftC_16u(src + 1, -val, d1 + 1, FUNC_TEST_SIZE);
353
354 if (status != PRIMITIVES_SUCCESS)
355 return FALSE;
356
357 /* Unaligned */
358 status = generic->shiftC_16u(src + 1, val, d1 + 2, FUNC_TEST_SIZE);
359
360 if (status != PRIMITIVES_SUCCESS)
361 return FALSE;
362
363 status = optimized->shiftC_16u(src + 1, val, d1 + 2, FUNC_TEST_SIZE);
364
365 if (status != PRIMITIVES_SUCCESS)
366 return FALSE;
367
368 status = generic->shiftC_16u(src + 1, -val, d1 + 2, FUNC_TEST_SIZE);
369
370 if (status != PRIMITIVES_SUCCESS)
371 return FALSE;
372
373 status = optimized->shiftC_16u(src + 1, -val, d1 + 2, FUNC_TEST_SIZE);
374
375 return (status == PRIMITIVES_SUCCESS);
376}
377
378/* ------------------------------------------------------------------------- */
379static BOOL test_lShift_16s_speed(void)
380{
381 UINT32 val = 0;
382 INT16 src[MAX_TEST_SIZE + 1] = WINPR_C_ARRAY_INIT;
383 INT16 dst[MAX_TEST_SIZE + 1] = WINPR_C_ARRAY_INIT;
384 if (winpr_RAND(src, sizeof(src)) < 0)
385 return FALSE;
386 if (winpr_RAND(&val, sizeof(val)))
387 return FALSE;
388
389 val = val % 16;
390 if (!speed_test("lShift_16s", "aligned", g_Iterations, (speed_test_fkt)generic->lShiftC_16s,
391 (speed_test_fkt)optimized->lShiftC_16s, src, val, dst, MAX_TEST_SIZE))
392 return FALSE;
393
394 if (!speed_test("lShift_16s", "unaligned", g_Iterations, (speed_test_fkt)generic->lShiftC_16s,
395 (speed_test_fkt)optimized->lShiftC_16s, src + 1, val, dst, MAX_TEST_SIZE))
396 return FALSE;
397
398 return TRUE;
399}
400
401/* ------------------------------------------------------------------------- */
402static BOOL test_lShift_16u_speed(void)
403{
404 UINT32 val = 0;
405 UINT16 src[MAX_TEST_SIZE + 1] = WINPR_C_ARRAY_INIT;
406 UINT16 dst[MAX_TEST_SIZE + 1] = WINPR_C_ARRAY_INIT;
407 if (winpr_RAND(&val, sizeof(val)) < 0)
408 return FALSE;
409 if (winpr_RAND(src, sizeof(src)) < 0)
410 return FALSE;
411
412 val = val % 16;
413 if (!speed_test("lShift_16u", "aligned", g_Iterations, (speed_test_fkt)generic->lShiftC_16u,
414 (speed_test_fkt)optimized->lShiftC_16u, src, val, dst, MAX_TEST_SIZE))
415 return FALSE;
416
417 if (!speed_test("lShift_16u", "unaligned", g_Iterations, (speed_test_fkt)generic->lShiftC_16u,
418 (speed_test_fkt)optimized->lShiftC_16u, src + 1, val, dst, MAX_TEST_SIZE))
419 return FALSE;
420
421 return TRUE;
422}
423
424/* ------------------------------------------------------------------------- */
425static BOOL test_rShift_16s_speed(void)
426{
427 UINT32 val = 0;
428 INT16 src[MAX_TEST_SIZE + 1] = WINPR_C_ARRAY_INIT;
429 INT16 dst[MAX_TEST_SIZE + 1] = WINPR_C_ARRAY_INIT;
430 if (winpr_RAND(src, sizeof(src)) < 0)
431 return FALSE;
432 if (winpr_RAND(&val, sizeof(val)) < 0)
433 return FALSE;
434
435 val = val % 16;
436 if (!speed_test("rShift_16s", "aligned", g_Iterations, (speed_test_fkt)generic->rShiftC_16s,
437 (speed_test_fkt)optimized->rShiftC_16s, src, val, dst, MAX_TEST_SIZE))
438 return FALSE;
439
440 if (!speed_test("rShift_16s", "unaligned", g_Iterations, (speed_test_fkt)generic->rShiftC_16s,
441 (speed_test_fkt)optimized->rShiftC_16s, src + 1, val, dst, MAX_TEST_SIZE))
442 return FALSE;
443
444 return TRUE;
445}
446
447/* ------------------------------------------------------------------------- */
448static BOOL test_rShift_16u_speed(void)
449{
450 UINT32 val = 0;
451 UINT16 src[MAX_TEST_SIZE + 1] = WINPR_C_ARRAY_INIT;
452 UINT16 dst[MAX_TEST_SIZE + 1] = WINPR_C_ARRAY_INIT;
453 if (winpr_RAND(&val, sizeof(val)) < 0)
454 return FALSE;
455 if (winpr_RAND(src, sizeof(src)) < 0)
456 return FALSE;
457
458 val = val % 16;
459 if (!speed_test("rShift_16u", "aligned", g_Iterations, (speed_test_fkt)generic->rShiftC_16u,
460 (speed_test_fkt)optimized->rShiftC_16u, src, val, dst, MAX_TEST_SIZE))
461 return FALSE;
462
463 if (!speed_test("rShift_16u", "unaligned", g_Iterations, (speed_test_fkt)generic->rShiftC_16u,
464 (speed_test_fkt)optimized->rShiftC_16u, src + 1, val, dst, MAX_TEST_SIZE))
465 return FALSE;
466
467 return TRUE;
468}
469
470int TestPrimitivesShift(int argc, char* argv[])
471{
472 WINPR_UNUSED(argc);
473 WINPR_UNUSED(argv);
474 prim_test_setup(FALSE);
475
476 if (!test_lShift_16s_func())
477 return 1;
478
479 if (g_TestPrimitivesPerformance)
480 {
481 if (!test_lShift_16s_speed())
482 return 1;
483 }
484
485 if (!test_lShift_16s_inplace_func())
486 return 1;
487
488 if (!test_lShift_16u_func())
489 return 1;
490
491 if (g_TestPrimitivesPerformance)
492 {
493 if (!test_lShift_16u_speed())
494 return 1;
495 }
496
497 if (!test_rShift_16s_func())
498 return 1;
499
500 if (g_TestPrimitivesPerformance)
501 {
502 if (!test_rShift_16s_speed())
503 return 1;
504 }
505
506 if (!test_rShift_16u_func())
507 return 1;
508
509 if (g_TestPrimitivesPerformance)
510 {
511 if (!test_rShift_16u_speed())
512 return 1;
513 }
514
515 if (!test_ShiftWrapper_16s_func())
516 return 1;
517
518 if (!test_ShiftWrapper_16u_func())
519 return 1;
520
521 return 0;
522}