• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (c) 2017 The Khronos Group Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //    http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 #include "harness/compat.h"
17 
18 #include <stdio.h>
19 #include <string.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 
23 #include "procs.h"
24 #include "harness/conversions.h"
25 
26 extern     MTdata          d;
27 
28 // The tests we are running
29 const char *tests[] = {
30     "+",
31     "-",
32     "*",
33     "/",
34     "%",
35     "&",
36     "|",
37     "^",
38     ">>",
39     "<<",
40     ">>",
41     "<<",
42     "~",
43     "?:",
44     "&&",
45     "||",
46     "<",
47     ">",
48     "<=",
49     ">=",
50     "==",
51     "!=",
52     "!",
53 };
54 
55 // The names of the tests
56 const char *test_names[] = {
57     "+", // 0
58     "-", // 1
59     "*", // 2
60     "/", // 3
61     "%", // 4
62     "&", // 5
63     "|", // 6
64     "^", // 7
65     ">> by vector", // 8
66     "<< by vector", // 9
67     ">> by scalar", // 10
68     "<< by scalar", // 11
69     "~",  // 12
70     "?:", // 13
71     "&&", // 14
72     "||", // 15
73     "<",  // 16
74     ">",  // 17
75     "<=", // 18
76     ">=", // 19
77     "==", // 20
78     "!=", // 21
79     "!",  // 22
80 };
81 
82 const size_t vector_aligns[] = {0, 1, 2, 4, 4,
83     8, 8, 8, 8,
84     16, 16, 16, 16,
85     16, 16, 16, 16};
86 
87 // =======================================
88 // long
89 // =======================================
90 int
verify_long(int test,size_t vector_size,cl_long * inptrA,cl_long * inptrB,cl_long * outptr,size_t n)91 verify_long(int test, size_t vector_size, cl_long *inptrA, cl_long *inptrB, cl_long *outptr, size_t n)
92 {
93     cl_long            r, shift_mask = (sizeof(cl_long)*8)-1;
94     size_t         i, j;
95     int count=0;
96 
97     for (j=0; j<n; j += vector_size )
98     {
99         for( i = j; i < j + vector_size; i++ )
100         {
101             switch (test) {
102                 case 0:
103                     r = inptrA[i] + inptrB[i];
104                     break;
105                 case 1:
106                     r = inptrA[i] - inptrB[i];
107                     break;
108                 case 2:
109                     r = inptrA[i] * inptrB[i];
110                     break;
111                 case 3:
112                     if (inptrB[i] == 0 || (inptrB[i] == -1 && inptrA[i] == CL_LONG_MIN))
113                         continue;
114                     else
115                         r = inptrA[i] / inptrB[i];
116                     break;
117                 case 4:
118                     if (inptrB[i] == 0 || (inptrB[i] == -1 && inptrA[i] == CL_LONG_MIN))
119                         continue;
120                     else
121                         r = inptrA[i] % inptrB[i];
122                     break;
123                 case 5:
124                     r = inptrA[i] & inptrB[i];
125                     break;
126                 case 6:
127                     r = inptrA[i] | inptrB[i];
128                     break;
129                 case 7:
130                     r = inptrA[i] ^ inptrB[i];
131                     break;
132                 case 8:
133                     r = inptrA[i] >> (inptrB[i] & shift_mask);
134                     break;
135                 case 9:
136                     r = inptrA[i] << (inptrB[i] & shift_mask);
137                     break;
138                 case 10:
139                     r = inptrA[i] >> (inptrB[j] & shift_mask);
140                     break;
141                 case 11:
142                     r = inptrA[i] << (inptrB[j] & shift_mask);
143                     break;
144                 case 12:
145                     r = ~inptrA[i];
146                     break;
147                 case 13:
148                     r = (inptrA[j] < inptrB[j]) ? inptrA[i] : inptrB[i];
149                     break;
150                 case 14:
151                     // Scalars are set to 1/0
152                     r = inptrA[i] && inptrB[i];
153                     // Vectors are set to -1/0
154                     if (vector_size != 1 && r) {
155                         r = -1;
156                     }
157                     break;
158                 case 15:
159                     // Scalars are set to 1/0
160                     r = inptrA[i] || inptrB[i];
161                     // Vectors are set to -1/0
162                     if (vector_size != 1 && r) {
163                         r = -1;
164                     }
165                     break;
166                 case 16:
167                     // Scalars are set to 1/0
168                     r = inptrA[i] < inptrB[i];
169                     // Vectors are set to -1/0
170                     if (vector_size != 1 && r) {
171                         r = -1;
172                     }
173                     break;
174                 case 17:
175                     // Scalars are set to 1/0
176                     r = inptrA[i] > inptrB[i];
177                     // Vectors are set to -1/0
178                     if (vector_size != 1 && r) {
179                         r = -1;
180                     }
181                     break;
182                 case 18:
183                     // Scalars are set to 1/0
184                     r = inptrA[i] <= inptrB[i];
185                     // Vectors are set to -1/0
186                     if (vector_size != 1 && r) {
187                         r = -1;
188                     }
189                     break;
190                 case 19:
191                     // Scalars are set to 1/0
192                     r = inptrA[i] >= inptrB[i];
193                     // Vectors are set to -1/0
194                     if (vector_size != 1 && r) {
195                         r = -1;
196                     }
197                     break;
198                 case 20:
199                     // Scalars are set to 1/0
200                     r = inptrA[i] == inptrB[i];
201                     // Vectors are set to -1/0
202                     if (vector_size != 1 && r) {
203                         r = -1;
204                     }
205                     break;
206                 case 21:
207                     // Scalars are set to 1/0
208                     r = inptrA[i] != inptrB[i];
209                     // Vectors are set to -1/0
210                     if (vector_size != 1 && r) {
211                         r = -1;
212                     }
213                     break;
214                 case 22:
215                     // Scalars are set to 1/0
216                     r = !inptrA[i];
217                     // Vectors are set to -1/0
218                     if (vector_size != 1 && r) {
219                         r = -1;
220                     }
221                     break;
222                 default:
223                     log_error("Invalid test: %d\n", test);
224                     return -1;
225                     break;
226             }
227             if (r != outptr[i]) {
228                 // Shift is tricky
229                 if (test == 8 || test == 9) {
230                     log_error("cl_long Verification failed at element %ld of %ld : 0x%llx %s 0x%llx = 0x%llx, got 0x%llx\n", i, n, inptrA[i], tests[test], inptrB[i], r, outptr[i]);
231                     log_error("\t1) Vector shift failure at element %ld: original is 0x%llx %s %d (0x%llx)\n", i, inptrA[i], tests[test], (int)inptrB[i], inptrB[i]);
232                     log_error("\t2) Take the %d LSBs of the shift to get the final shift amount %lld (0x%llx).\n", (int)log2(sizeof(cl_long)*8),  inptrB[i]&shift_mask, inptrB[i]&shift_mask);
233                 }
234                 else if (test == 10 || test == 11) {
235 
236                     log_error("cl_long Verification failed at element %ld of %ld (%ld): 0x%llx %s 0x%llx = 0x%llx, got 0x%llx\n", i, n, j, inptrA[i], tests[test], inptrB[j], r, outptr[i]);
237                     log_error("\t1) Scalar shift failure at element %ld: original is 0x%llx %s %d (0x%llx)\n", i, inptrA[i], tests[test], (int)inptrB[j], inptrB[j]);
238                     log_error("\t2) Take the %d LSBs of the shift to get the final shift amount %lld (0x%llx).\n", (int)log2(sizeof(cl_long)*8),  inptrB[j]&shift_mask, inptrB[j]&shift_mask);
239                 } else if (test == 13) {
240                     log_error("cl_int Verification failed at element %ld (%ld): (0x%llx < 0x%llx) ? 0x%llx : 0x%llx = 0x%llx, got 0x%llx\n", i, j, inptrA[j], inptrB[j],
241                               inptrA[i], inptrB[i], r, outptr[i]);
242                 } else {
243                     log_error("cl_long Verification failed at element %ld of %ld: 0x%llx %s 0x%llx = 0x%llx, got 0x%llx\n", i, n, inptrA[i], tests[test], inptrB[i], r, outptr[i]);
244                 }
245                 count++;
246                 if (count >= MAX_ERRORS_TO_PRINT) {
247                     log_error("Further errors ignored.\n");
248                     return -1;
249                 }
250             }
251         }
252     }
253 
254     if (count) return -1; else return 0;
255 }
256 
257 void
init_long_data(uint64_t indx,int num_elements,cl_long * input_ptr[],MTdata d)258 init_long_data(uint64_t indx, int num_elements, cl_long *input_ptr[], MTdata d)
259 {
260     cl_ulong        *p = (cl_ulong *)input_ptr[0];
261     int         j;
262 
263     if (indx == 0) {
264         // Do the tricky values the first time around
265         fill_test_values( input_ptr[ 0 ], input_ptr[ 1 ], (size_t)num_elements, d );
266     } else {
267         // Then just test lots of random ones.
268         for (j=0; j<num_elements; j++) {
269             cl_uint a = (cl_uint)genrand_int32(d);
270             cl_uint b = (cl_uint)genrand_int32(d);
271             p[j] = ((cl_ulong)a <<32 | b);
272         }
273         p = (cl_ulong *)input_ptr[1];
274         for (j=0; j<num_elements; j++) {
275             cl_uint a = (cl_uint)genrand_int32(d);
276             cl_uint b = (cl_uint)genrand_int32(d);
277             p[j] = ((cl_ulong)a <<32 | b);
278         }
279     }
280 }
281 
282 
283 // =======================================
284 // ulong
285 // =======================================
286 int
verify_ulong(int test,size_t vector_size,cl_ulong * inptrA,cl_ulong * inptrB,cl_ulong * outptr,size_t n)287 verify_ulong(int test, size_t vector_size, cl_ulong *inptrA, cl_ulong *inptrB, cl_ulong *outptr, size_t n)
288 {
289     cl_ulong        r, shift_mask = (sizeof(cl_ulong)*8)-1;
290     size_t          i, j;
291     int count=0;
292 
293     for (j=0; j<n; j += vector_size )
294     {
295         for( i = j; i < j + vector_size; i++ )
296         {
297             switch (test) {
298                 case 0:
299                     r = inptrA[i] + inptrB[i];
300                     break;
301                 case 1:
302                     r = inptrA[i] - inptrB[i];
303                     break;
304                 case 2:
305                     r = inptrA[i] * inptrB[i];
306                     break;
307                 case 3:
308                     if (inptrB[i] == 0)
309                         continue;
310                     else
311                         r = inptrA[i] / inptrB[i];
312                     break;
313                 case 4:
314                     if (inptrB[i] == 0)
315                         continue;
316                     else
317                         r = inptrA[i] % inptrB[i];
318                     break;
319                 case 5:
320                     r = inptrA[i] & inptrB[i];
321                     break;
322                 case 6:
323                     r = inptrA[i] | inptrB[i];
324                     break;
325                 case 7:
326                     r = inptrA[i] ^ inptrB[i];
327                     break;
328                 case 8:
329                     r = inptrA[i] >> (inptrB[i] & shift_mask);
330                     break;
331                 case 9:
332                     r = inptrA[i] << (inptrB[i] & shift_mask);
333                     break;
334                 case 10:
335                     r = inptrA[i] >> (inptrB[j] & shift_mask);
336                     break;
337                 case 11:
338                     r = inptrA[i] << (inptrB[j] & shift_mask);
339                     break;
340                 case 12:
341                     r = ~inptrA[i];
342                     break;
343                 case 13:
344                     r = (inptrA[j] < inptrB[j]) ? inptrA[i] : inptrB[i];
345                     break;
346                 case 14:
347                     // Scalars are set to 1/0
348                     r = inptrA[i] && inptrB[i];
349                     // Vectors are set to -1/0
350                     if (vector_size != 1 && r) {
351                         r = -1;
352                     }
353                     break;
354                 case 15:
355                     // Scalars are set to 1/0
356                     r = inptrA[i] || inptrB[i];
357                     // Vectors are set to -1/0
358                     if (vector_size != 1 && r) {
359                         r = -1;
360                     }
361                     break;
362                 case 16:
363                     // Scalars are set to 1/0
364                     r = inptrA[i] < inptrB[i];
365                     // Vectors are set to -1/0
366                     if (vector_size != 1 && r) {
367                         r = -1;
368                     }
369                     break;
370                 case 17:
371                     // Scalars are set to 1/0
372                     r = inptrA[i] > inptrB[i];
373                     // Vectors are set to -1/0
374                     if (vector_size != 1 && r) {
375                         r = -1;
376                     }
377                     break;
378                 case 18:
379                     // Scalars are set to 1/0
380                     r = inptrA[i] <= inptrB[i];
381                     // Vectors are set to -1/0
382                     if (vector_size != 1 && r) {
383                         r = -1;
384                     }
385                     break;
386                 case 19:
387                     // Scalars are set to 1/0
388                     r = inptrA[i] >= inptrB[i];
389                     // Vectors are set to -1/0
390                     if (vector_size != 1 && r) {
391                         r = -1;
392                     }
393                     break;
394                 case 20:
395                     // Scalars are set to 1/0
396                     r = inptrA[i] == inptrB[i];
397                     // Vectors are set to -1/0
398                     if (vector_size != 1 && r) {
399                         r = -1;
400                     }
401                     break;
402                 case 21:
403                     // Scalars are set to 1/0
404                     r = inptrA[i] != inptrB[i];
405                     // Vectors are set to -1/0
406                     if (vector_size != 1 && r) {
407                         r = -1;
408                     }
409                     break;
410                 case 22:
411                     // Scalars are set to 1/0
412                     r = !inptrA[i];
413                     // Vectors are set to -1/0
414                     if (vector_size != 1 && r) {
415                         r = -1;
416                     }
417                     break;
418                 default:
419                     log_error("Invalid test: %d\n", test);
420                     return -1;
421                     break;
422             }
423             if (r != outptr[i]) {
424                 // Shift is tricky
425                 if (test == 8 || test == 9) {
426                     log_error("cl_ulong Verification failed at element %ld of %ld: 0x%llx %s 0x%llx = 0x%llx, got 0x%llx\n", i, n, inptrA[i], tests[test], inptrB[i], r, outptr[i]);
427                     log_error("\t1) Shift failure at element %ld: original is 0x%llx %s %d (0x%llx)\n", i, inptrA[i], tests[test], (int)inptrB[i], inptrB[i]);
428                     log_error("\t2) Take the %d LSBs of the shift to get the final shift amount %llu (0x%llx).\n", (int)log2(sizeof(cl_ulong)*8),  inptrB[i]&shift_mask, inptrB[i]&shift_mask);
429                 }
430                 else if (test == 10 || test == 11) {
431                     log_error("cl_ulong Verification failed at element %ld of %ld (%ld): 0x%llx %s 0x%llx = 0x%llx, got 0x%llx\n", i, n, j, inptrA[i], tests[test], inptrB[j], r, outptr[i]);
432                     log_error("\t1) Scalar shift failure at element %ld: original is 0x%llx %s %d (0x%llx)\n", i, inptrA[i], tests[test], (int)inptrB[j], inptrB[j]);
433                     log_error("\t2) Take the %d LSBs of the shift to get the final shift amount %lld (0x%llx).\n", (int)log2(sizeof(cl_long)*8),  inptrB[j]&shift_mask, inptrB[j]&shift_mask);
434                 } else if (test == 13) {
435                     log_error("cl_int Verification failed at element %ld of %ld (%ld): (0x%llx < 0x%llx) ? 0x%llx : 0x%llx = 0x%llx, got 0x%llx\n", i, n, j, inptrA[j], inptrB[j],
436                               inptrA[i], inptrB[i], r, outptr[i]);
437                 } else {
438                     log_error("cl_ulong Verification failed at element %ld of %ld: 0x%llx %s 0x%llx = 0x%llx, got 0x%llx\n", i, n, inptrA[i], tests[test], inptrB[i], r, outptr[i]);
439                 }
440                 count++;
441                 if (count >= MAX_ERRORS_TO_PRINT) {
442                     log_error("Further errors ignored.\n");
443                     return -1;
444                 }
445             }
446         }
447     }
448     if (count) return -1; else return 0;
449 }
450 
451 void
init_ulong_data(uint64_t indx,int num_elements,cl_ulong * input_ptr[],MTdata d)452 init_ulong_data(uint64_t indx, int num_elements, cl_ulong *input_ptr[], MTdata d)
453 {
454     cl_ulong        *p = (cl_ulong *)input_ptr[0];
455     int            j;
456 
457     if (indx == 0)
458     {
459         // Do the tricky values the first time around
460         fill_test_values( (cl_long*)input_ptr[ 0 ], (cl_long*)input_ptr[ 1 ], (size_t)num_elements, d );
461     }
462     else
463     {
464         // Then just test lots of random ones.
465         for (j=0; j<num_elements; j++)
466         {
467             cl_ulong a = genrand_int32(d);
468             cl_ulong b = genrand_int32(d);
469             // Fill in the top, bottom, and middle, remembering that random only sets 31 bits.
470             p[j] = (a <<32) | b;
471         }
472         p = (cl_ulong *)input_ptr[1];
473         for (j=0; j<num_elements; j++)
474         {
475             cl_ulong a = genrand_int32(d);
476             cl_ulong b = genrand_int32(d);
477             // Fill in the top, bottom, and middle, remembering that random only sets 31 bits.
478             p[j] = (a <<32) | b;
479         }
480     }
481 }
482 
483 
484 // =======================================
485 // int
486 // =======================================
487 int
verify_int(int test,size_t vector_size,cl_int * inptrA,cl_int * inptrB,cl_int * outptr,size_t n)488 verify_int(int test, size_t vector_size, cl_int *inptrA, cl_int *inptrB, cl_int *outptr, size_t n)
489 {
490     cl_int            r, shift_mask = (sizeof(cl_int)*8)-1;
491     size_t          i, j;
492     int count=0;
493 
494     for (j=0; j<n; j += vector_size )
495     {
496         for( i = j; i < j + vector_size; i++ )
497         {
498             switch (test) {
499                 case 0:
500                     r = inptrA[i] + inptrB[i];
501                     break;
502                 case 1:
503                     r = inptrA[i] - inptrB[i];
504                     break;
505                 case 2:
506                     r = inptrA[i] * inptrB[i];
507                     break;
508                 case 3:
509                     if (inptrB[i] == 0 || (inptrB[i] == -1 && inptrA[i] == CL_INT_MIN))
510                         continue;
511                     else
512                         r = inptrA[i] / inptrB[i];
513                     break;
514                 case 4:
515                     if (inptrB[i] == 0 || (inptrB[i] == -1 && inptrA[i] == CL_INT_MIN))
516                         continue;
517                     else
518                         r = inptrA[i] % inptrB[i];
519                     break;
520                 case 5:
521                     r = inptrA[i] & inptrB[i];
522                     break;
523                 case 6:
524                     r = inptrA[i] | inptrB[i];
525                     break;
526                 case 7:
527                     r = inptrA[i] ^ inptrB[i];
528                     break;
529                 case 8:
530                     r = inptrA[i] >> (inptrB[i] & shift_mask);
531                     break;
532                 case 9:
533                     r = inptrA[i] << (inptrB[i] & shift_mask);
534                     break;
535                 case 10:
536                     r = inptrA[i] >> (inptrB[j] & shift_mask);
537                     break;
538                 case 11:
539                     r = inptrA[i] << (inptrB[j] & shift_mask);
540                     break;
541                 case 12:
542                     r = ~inptrA[i];
543                     break;
544                 case 13:
545                     r = (inptrA[j] < inptrB[j]) ? inptrA[i] : inptrB[i];
546                     break;
547                 case 14:
548                     // Scalars are set to 1/0
549                     r = inptrA[i] && inptrB[i];
550                     // Vectors are set to -1/0
551                     if (vector_size != 1 && r) {
552                         r = -1;
553                     }
554                     break;
555                 case 15:
556                     // Scalars are set to 1/0
557                     r = inptrA[i] || inptrB[i];
558                     // Vectors are set to -1/0
559                     if (vector_size != 1 && r) {
560                         r = -1;
561                     }
562                     break;
563                 case 16:
564                     // Scalars are set to 1/0
565                     r = inptrA[i] < inptrB[i];
566                     // Vectors are set to -1/0
567                     if (vector_size != 1 && r) {
568                         r = -1;
569                     }
570                     break;
571                 case 17:
572                     // Scalars are set to 1/0
573                     r = inptrA[i] > inptrB[i];
574                     // Vectors are set to -1/0
575                     if (vector_size != 1 && r) {
576                         r = -1;
577                     }
578                     break;
579                 case 18:
580                     // Scalars are set to 1/0
581                     r = inptrA[i] <= inptrB[i];
582                     // Vectors are set to -1/0
583                     if (vector_size != 1 && r) {
584                         r = -1;
585                     }
586                     break;
587                 case 19:
588                     // Scalars are set to 1/0
589                     r = inptrA[i] >= inptrB[i];
590                     // Vectors are set to -1/0
591                     if (vector_size != 1 && r) {
592                         r = -1;
593                     }
594                     break;
595                 case 20:
596                     // Scalars are set to 1/0
597                     r = inptrA[i] == inptrB[i];
598                     // Vectors are set to -1/0
599                     if (vector_size != 1 && r) {
600                         r = -1;
601                     }
602                     break;
603                 case 21:
604                     // Scalars are set to 1/0
605                     r = inptrA[i] != inptrB[i];
606                     // Vectors are set to -1/0
607                     if (vector_size != 1 && r) {
608                         r = -1;
609                     }
610                     break;
611                 case 22:
612                     // Scalars are set to 1/0
613                     r = !inptrA[i];
614                     // Vectors are set to -1/0
615                     if (vector_size != 1 && r) {
616                         r = -1;
617                     }
618                     break;
619                 default:
620                     log_error("Invalid test: %d\n", test);
621                     return -1;
622                     break;
623             }
624             if (r != outptr[i]) {
625                 // Shift is tricky
626                 if (test == 8 || test == 9) {
627                     log_error("cl_int Verification failed at element %ld: 0x%x %s 0x%x = 0x%x, got 0x%x\n", i, inptrA[i], tests[test], inptrB[i], r, outptr[i]);
628                     log_error("\t1) Shift failure at element %ld: original is 0x%x %s %d (0x%x)\n", i, inptrA[i], tests[test], (int)inptrB[i], inptrB[i]);
629                     log_error("\t2) Take the %d LSBs of the shift to get the final shift amount %d (0x%x).\n", (int)log2(sizeof(cl_int)*8),  inptrB[i]&shift_mask, inptrB[i]&shift_mask);
630                 }
631                 else if (test == 10 || test == 11) {
632                     log_error("cl_int Verification failed at element %ld (%ld): 0x%x %s 0x%x = 0x%x, got 0x%x\n", i, j, inptrA[i], tests[test], inptrB[j], r, outptr[i]);
633                     log_error("\t1) Scalar shift failure at element %ld: original is 0x%x %s %d (0x%x)\n", i, inptrA[i], tests[test], (int)inptrB[j], inptrB[j]);
634                     log_error("\t2) Take the %d LSBs of the shift to get the final shift amount %d (0x%x).\n", (int)log2(sizeof(cl_int)*8),  inptrB[j]&shift_mask, inptrB[j]&shift_mask);
635                 } else if (test == 13) {
636                     log_error("cl_int Verification failed at element %ld (%ld): (0x%x < 0x%x) ? 0x%x : 0x%x = 0x%x, got 0x%x\n", i, j, inptrA[j], inptrB[j],
637                               inptrA[i], inptrB[i], r, outptr[i]);
638                 } else {
639                     log_error("cl_int Verification failed at element %ld: 0x%x %s 0x%x = 0x%x, got 0x%x\n", i, inptrA[i], tests[test], inptrB[i], r, outptr[i]);
640                 }
641                 count++;
642                 if (count >= MAX_ERRORS_TO_PRINT) {
643                     log_error("Further errors ignored.\n");
644                     return -1;
645                 }
646             }
647         }
648     }
649 
650     if (count) return -1; else return 0;
651 }
652 
653 void
init_int_data(uint64_t indx,int num_elements,cl_int * input_ptr[],MTdata d)654 init_int_data(uint64_t indx, int num_elements, cl_int *input_ptr[], MTdata d)
655 {
656     static const cl_int specialCaseList[] = { 0, -1, 1, CL_INT_MIN, CL_INT_MIN + 1, CL_INT_MAX };
657     int            j;
658 
659     // Set the inputs to a random number
660     for (j=0; j<num_elements; j++)
661     {
662         ((cl_int *)input_ptr[0])[j] = (cl_int)genrand_int32(d);
663         ((cl_int *)input_ptr[1])[j] = (cl_int)genrand_int32(d);
664     }
665 
666     // Init the first few values to test special cases
667     {
668         size_t x, y, index = 0;
669         for( x = 0; x < sizeof( specialCaseList ) / sizeof( specialCaseList[0] ); x++ )
670             for( y = 0; y < sizeof( specialCaseList ) / sizeof( specialCaseList[0] ); y++ )
671             {
672                 ((cl_int *)input_ptr[0])[index] = specialCaseList[x];
673                 ((cl_int *)input_ptr[1])[index++] = specialCaseList[y];
674             }
675     }
676 }
677 
678 
679 // =======================================
680 // uint
681 // =======================================
682 int
verify_uint(int test,size_t vector_size,cl_uint * inptrA,cl_uint * inptrB,cl_uint * outptr,size_t n)683 verify_uint(int test, size_t vector_size, cl_uint *inptrA, cl_uint *inptrB, cl_uint *outptr, size_t n)
684 {
685     cl_uint            r, shift_mask = (sizeof(cl_uint)*8)-1;
686     size_t          i, j;
687     int count=0;
688 
689     for (j=0; j<n; j += vector_size )
690     {
691         for( i = j; i < j + vector_size; i++ )
692         {
693             switch (test) {
694                 case 0:
695                     r = inptrA[i] + inptrB[i];
696                     break;
697                 case 1:
698                     r = inptrA[i] - inptrB[i];
699                     break;
700                 case 2:
701                     r = inptrA[i] * inptrB[i];
702                     break;
703                 case 3:
704                     if (inptrB[i] == 0)
705                         continue;
706                     else
707                         r = inptrA[i] / inptrB[i];
708                     break;
709                 case 4:
710                     if (inptrB[i] == 0)
711                         continue;
712                     else
713                         r = inptrA[i] % inptrB[i];
714                     break;
715                 case 5:
716                     r = inptrA[i] & inptrB[i];
717                     break;
718                 case 6:
719                     r = inptrA[i] | inptrB[i];
720                     break;
721                 case 7:
722                     r = inptrA[i] ^ inptrB[i];
723                     break;
724                 case 8:
725                     r = inptrA[i] >> (inptrB[i] & shift_mask);
726                     break;
727                 case 9:
728                     r = inptrA[i] << (inptrB[i] & shift_mask);
729                     break;
730                 case 10:
731                     r = inptrA[i] >> (inptrB[j] & shift_mask);
732                     break;
733                 case 11:
734                     r = inptrA[i] << (inptrB[j] & shift_mask);
735                     break;
736                 case 12:
737                     r = ~inptrA[i];
738                     break;
739                 case 13:
740                     r = (inptrA[j] < inptrB[j]) ? inptrA[i] : inptrB[i];
741                     break;
742                 case 14:
743                     // Scalars are set to 1/0
744                     r = inptrA[i] && inptrB[i];
745                     // Vectors are set to -1/0
746                     if (vector_size != 1 && r) {
747                         r = -1;
748                     }
749                     break;
750                 case 15:
751                     // Scalars are set to 1/0
752                     r = inptrA[i] || inptrB[i];
753                     // Vectors are set to -1/0
754                     if (vector_size != 1 && r) {
755                         r = -1;
756                     }
757                     break;
758                 case 16:
759                     // Scalars are set to 1/0
760                     r = inptrA[i] < inptrB[i];
761                     // Vectors are set to -1/0
762                     if (vector_size != 1 && r) {
763                         r = -1;
764                     }
765                     break;
766                 case 17:
767                     // Scalars are set to 1/0
768                     r = inptrA[i] > inptrB[i];
769                     // Vectors are set to -1/0
770                     if (vector_size != 1 && r) {
771                         r = -1;
772                     }
773                     break;
774                 case 18:
775                     // Scalars are set to 1/0
776                     r = inptrA[i] <= inptrB[i];
777                     // Vectors are set to -1/0
778                     if (vector_size != 1 && r) {
779                         r = -1;
780                     }
781                     break;
782                 case 19:
783                     // Scalars are set to 1/0
784                     r = inptrA[i] >= inptrB[i];
785                     // Vectors are set to -1/0
786                     if (vector_size != 1 && r) {
787                         r = -1;
788                     }
789                     break;
790                 case 20:
791                     // Scalars are set to 1/0
792                     r = inptrA[i] == inptrB[i];
793                     // Vectors are set to -1/0
794                     if (vector_size != 1 && r) {
795                         r = -1;
796                     }
797                     break;
798                 case 21:
799                     // Scalars are set to 1/0
800                     r = inptrA[i] != inptrB[i];
801                     // Vectors are set to -1/0
802                     if (vector_size != 1 && r) {
803                         r = -1;
804                     }
805                     break;
806                 case 22:
807                     // Scalars are set to 1/0
808                     r = !inptrA[i];
809                     // Vectors are set to -1/0
810                     if (vector_size != 1 && r) {
811                         r = -1;
812                     }
813                     break;
814                 default:
815                     log_error("Invalid test: %d\n", test);
816                     return -1;
817                     break;
818             }
819             if (r != outptr[i]) {
820                 // Shift is tricky
821                 if (test == 8 || test == 9) {
822                     log_error("cl_uint Verification failed at element %ld: 0x%x %s 0x%x = 0x%x, got 0x%x\n", i, inptrA[i], tests[test], inptrB[i], r, outptr[i]);
823                     log_error("\t1) Shift failure at element %ld: original is 0x%x %s %d (0x%x)\n", i, inptrA[i], tests[test], (int)inptrB[i], inptrB[i]);
824                     log_error("\t2) Take the %d LSBs of the shift to get the final shift amount %d (0x%x).\n", (int)log2(sizeof(cl_uint)*8),  inptrB[i]&shift_mask, inptrB[i]&shift_mask);
825                 }
826                 else if (test == 10 || test == 11) {
827                     log_error("cl_uint Verification failed at element %ld (%ld): 0x%x %s 0x%x = 0x%x, got 0x%x\n", i, j, inptrA[i], tests[test], inptrB[j], r, outptr[i]);
828                     log_error("\t1) Scalar shift failure at element %ld: original is 0x%x %s %d (0x%x)\n", i, inptrA[i], tests[test], (int)inptrB[j], inptrB[j]);
829                     log_error("\t2) Take the %d LSBs of the shift to get the final shift amount %d (0x%x).\n", (int)log2(sizeof(cl_uint)*8),  inptrB[j]&shift_mask, inptrB[j]&shift_mask);
830                 } else if (test == 13) {
831                     log_error("cl_int Verification failed at element %ld (%ld): (0x%x < 0x%x) ? 0x%x : 0x%x = 0x%x, got 0x%x\n", i, j, inptrA[j], inptrB[j],
832                               inptrA[i], inptrB[i], r, outptr[i]);
833                 } else {
834                     log_error("cl_uint Verification failed at element %ld: 0x%x %s 0x%x = 0x%x, got 0x%x\n", i, inptrA[i], tests[test], inptrB[i], r, outptr[i]);
835                 }
836                 count++;
837                 if (count >= MAX_ERRORS_TO_PRINT) {
838                     log_error("Further errors ignored.\n");
839                     return -1;
840                 }
841             }
842         }
843     }
844     if (count) return -1; else return 0;
845 }
846 
847 void
init_uint_data(uint64_t indx,int num_elements,cl_uint * input_ptr[],MTdata d)848 init_uint_data(uint64_t indx, int num_elements, cl_uint *input_ptr[], MTdata d)
849 {
850     static cl_uint specialCaseList[] = { 0, (cl_uint) CL_INT_MAX, (cl_uint) CL_INT_MAX + 1, CL_UINT_MAX-1, CL_UINT_MAX };
851     int            j;
852 
853     // Set the first input to an incrementing number
854     // Set the second input to a random number
855     for (j=0; j<num_elements; j++)
856     {
857         ((cl_uint *)input_ptr[0])[j] = genrand_int32(d);
858         ((cl_uint *)input_ptr[1])[j] = genrand_int32(d);
859     }
860 
861     // Init the first few values to test special cases
862     {
863         size_t x, y, index = 0;
864         for( x = 0; x < sizeof( specialCaseList ) / sizeof( specialCaseList[0] ); x++ )
865             for( y = 0; y < sizeof( specialCaseList ) / sizeof( specialCaseList[0] ); y++ )
866             {
867                 ((cl_uint *)input_ptr[0])[index] = specialCaseList[x];
868                 ((cl_uint *)input_ptr[1])[index++] = specialCaseList[y];
869             }
870     }
871 }
872 
873 // =======================================
874 // short
875 // =======================================
876 int
verify_short(int test,size_t vector_size,cl_short * inptrA,cl_short * inptrB,cl_short * outptr,size_t n)877 verify_short(int test, size_t vector_size, cl_short *inptrA, cl_short *inptrB, cl_short *outptr, size_t n)
878 {
879     cl_short r;
880     cl_int   shift_mask = vector_size == 1 ? (cl_int)(sizeof(cl_int)*8)-1
881     : (cl_int)(sizeof(cl_short)*8)-1;
882     size_t   i, j;
883     int      count=0;
884 
885     for (j=0; j<n; j += vector_size )
886     {
887         for( i = j; i < j + vector_size; i++ )
888         {
889             switch (test) {
890                 case 0:
891                     r = inptrA[i] + inptrB[i];
892                     break;
893                 case 1:
894                     r = inptrA[i] - inptrB[i];
895                     break;
896                 case 2:
897                     r = inptrA[i] * inptrB[i];
898                     break;
899                 case 3:
900                     if (inptrB[i] == 0 || (inptrB[i] == -1 && inptrA[i] == CL_SHRT_MIN))
901                         continue;
902                     else
903                         r = inptrA[i] / inptrB[i];
904                     break;
905                 case 4:
906                     if (inptrB[i] == 0 || (inptrB[i] == -1 && inptrA[i] == CL_SHRT_MIN))
907                         continue;
908                     else
909                         r = inptrA[i] % inptrB[i];
910                     break;
911                 case 5:
912                     r = inptrA[i] & inptrB[i];
913                     break;
914                 case 6:
915                     r = inptrA[i] | inptrB[i];
916                     break;
917                 case 7:
918                     r = inptrA[i] ^ inptrB[i];
919                     break;
920                 case 8:
921                     r = inptrA[i] >> (inptrB[i] & shift_mask);
922                     break;
923                 case 9:
924                     r = inptrA[i] << (inptrB[i] & shift_mask);
925                     break;
926                 case 10:
927                     r = inptrA[i] >> (inptrB[j] & shift_mask);
928                     break;
929                 case 11:
930                     r = inptrA[i] << (inptrB[j] & shift_mask);
931                     break;
932                 case 12:
933                     r = ~inptrA[i];
934                     break;
935                 case 13:
936                     r = (inptrA[j] < inptrB[j]) ? inptrA[i] : inptrB[i];
937                     break;
938                 case 14:
939                     // Scalars are set to 1/0
940                     r = inptrA[i] && inptrB[i];
941                     // Vectors are set to -1/0
942                     if (vector_size != 1 && r) {
943                         r = -1;
944                     }
945                     break;
946                 case 15:
947                     // Scalars are set to 1/0
948                     r = inptrA[i] || inptrB[i];
949                     // Vectors are set to -1/0
950                     if (vector_size != 1 && r) {
951                         r = -1;
952                     }
953                     break;
954                 case 16:
955                     // Scalars are set to 1/0
956                     r = inptrA[i] < inptrB[i];
957                     // Vectors are set to -1/0
958                     if (vector_size != 1 && r) {
959                         r = -1;
960                     }
961                     break;
962                 case 17:
963                     // Scalars are set to 1/0
964                     r = inptrA[i] > inptrB[i];
965                     // Vectors are set to -1/0
966                     if (vector_size != 1 && r) {
967                         r = -1;
968                     }
969                     break;
970                 case 18:
971                     // Scalars are set to 1/0
972                     r = inptrA[i] <= inptrB[i];
973                     // Vectors are set to -1/0
974                     if (vector_size != 1 && r) {
975                         r = -1;
976                     }
977                     break;
978                 case 19:
979                     // Scalars are set to 1/0
980                     r = inptrA[i] >= inptrB[i];
981                     // Vectors are set to -1/0
982                     if (vector_size != 1 && r) {
983                         r = -1;
984                     }
985                     break;
986                 case 20:
987                     // Scalars are set to 1/0
988                     r = inptrA[i] == inptrB[i];
989                     // Vectors are set to -1/0
990                     if (vector_size != 1 && r) {
991                         r = -1;
992                     }
993                     break;
994                 case 21:
995                     // Scalars are set to 1/0
996                     r = inptrA[i] != inptrB[i];
997                     // Vectors are set to -1/0
998                     if (vector_size != 1 && r) {
999                         r = -1;
1000                     }
1001                     break;
1002                 case 22:
1003                     // Scalars are set to 1/0
1004                     r = !inptrA[i];
1005                     // Vectors are set to -1/0
1006                     if (vector_size != 1 && r) {
1007                         r = -1;
1008                     }
1009                     break;
1010                 default:
1011                     log_error("Invalid test: %d\n", test);
1012                     return -1;
1013                     break;
1014             }
1015             if (r != outptr[i]) {
1016                 // Shift is tricky
1017                 if (test == 8 || test == 9) {
1018                     log_error("cl_short Verification failed at element %ld: 0x%x %s 0x%x = 0x%x, got 0x%x\n", i, inptrA[i], tests[test], inptrB[i], r, outptr[i]);
1019                     log_error("\t1) Shift failure at element %ld: original is 0x%x %s %d (0x%x)\n", i, inptrA[i], tests[test], (int)inptrB[i], inptrB[i]);
1020                     log_error("\t2) Take the %d LSBs of the shift to get the final shift amount %d (0x%x).\n", (int)log2(sizeof(cl_short)*8),  inptrB[i]&shift_mask, inptrB[i]&shift_mask);
1021                 }
1022                 else if (test == 10 || test == 11) {
1023                     log_error("cl_short Verification failed at element %ld (%ld): 0x%x %s 0x%x = 0x%x, got 0x%x\n", i, j, inptrA[i], tests[test], inptrB[j], r, outptr[i]);
1024                     log_error("\t1) Scalar shift failure at element %ld: original is 0x%x %s %d (0x%x)\n", i, inptrA[i], tests[test], (int)inptrB[j], inptrB[j]);
1025                     log_error("\t2) Take the %d LSBs of the shift to get the final shift amount %d (0x%x).\n", (int)log2(sizeof(cl_short)*8),  inptrB[j]&shift_mask, inptrB[j]&shift_mask);
1026                 } else if (test == 13) {
1027                     log_error("cl_int Verification failed at element %ld (%ld): (0x%x < 0x%x) ? 0x%x : 0x%x = 0x%x, got 0x%x\n", i, j, inptrA[j], inptrB[j],
1028                               inptrA[i], inptrB[i], r, outptr[i]);
1029                 } else {
1030                     log_error("cl_short Verification failed at element %ld: 0x%x %s 0x%x = 0x%x, got 0x%x\n", i, inptrA[i], tests[test], inptrB[i], r, outptr[i]);
1031                 }
1032                 count++;
1033                 if (count >= MAX_ERRORS_TO_PRINT) {
1034                     log_error("Further errors ignored.\n");
1035                     return -1;
1036                 }
1037             }
1038         }
1039     }
1040 
1041     if (count) return -1; else return 0;
1042 }
1043 
1044 void
init_short_data(uint64_t indx,int num_elements,cl_short * input_ptr[],MTdata d)1045 init_short_data(uint64_t indx, int num_elements, cl_short *input_ptr[], MTdata d)
1046 {
1047     static const cl_short specialCaseList[] = { 0, -1, 1, CL_SHRT_MIN, CL_SHRT_MIN + 1, CL_SHRT_MAX };
1048     int            j;
1049 
1050     // Set the inputs to a random number
1051     for (j=0; j<num_elements; j++)
1052     {
1053         cl_uint bits = genrand_int32(d);
1054         ((cl_short *)input_ptr[0])[j] = (cl_short) bits;
1055         ((cl_short *)input_ptr[1])[j] = (cl_short) (bits >> 16);
1056     }
1057 
1058     // Init the first few values to test special cases
1059     {
1060         size_t x, y, index = 0;
1061         for( x = 0; x < sizeof( specialCaseList ) / sizeof( specialCaseList[0] ); x++ )
1062             for( y = 0; y < sizeof( specialCaseList ) / sizeof( specialCaseList[0] ); y++ )
1063             {
1064                 ((cl_short *)input_ptr[0])[index] = specialCaseList[x];
1065                 ((cl_short *)input_ptr[1])[index++] = specialCaseList[y];
1066             }
1067     }
1068 }
1069 
1070 
1071 // =======================================
1072 // ushort
1073 // =======================================
1074 int
verify_ushort(int test,size_t vector_size,cl_ushort * inptrA,cl_ushort * inptrB,cl_ushort * outptr,size_t n)1075 verify_ushort(int test, size_t vector_size, cl_ushort *inptrA, cl_ushort *inptrB, cl_ushort *outptr, size_t n)
1076 {
1077     cl_ushort       r;
1078     cl_uint   shift_mask = vector_size == 1 ? (cl_uint)(sizeof(cl_uint)*8)-1
1079     : (cl_uint)(sizeof(cl_ushort)*8)-1;
1080     size_t          i, j;
1081     int             count=0;
1082 
1083     for (j=0; j<n; j += vector_size )
1084     {
1085         for( i = j; i < j + vector_size; i++ )
1086         {
1087             switch (test) {
1088                 case 0:
1089                     r = inptrA[i] + inptrB[i];
1090                     break;
1091                 case 1:
1092                     r = inptrA[i] - inptrB[i];
1093                     break;
1094                 case 2:
1095                     r = inptrA[i] * inptrB[i];
1096                     break;
1097                 case 3:
1098                     if (inptrB[i] == 0)
1099                         continue;
1100                     else
1101                         r = inptrA[i] / inptrB[i];
1102                     break;
1103                 case 4:
1104                     if (inptrB[i] == 0)
1105                         continue;
1106                     else
1107                         r = inptrA[i] % inptrB[i];
1108                     break;
1109                 case 5:
1110                     r = inptrA[i] & inptrB[i];
1111                     break;
1112                 case 6:
1113                     r = inptrA[i] | inptrB[i];
1114                     break;
1115                 case 7:
1116                     r = inptrA[i] ^ inptrB[i];
1117                     break;
1118                 case 8:
1119                     r = inptrA[i] >> (inptrB[i] & shift_mask);
1120                     break;
1121                 case 9:
1122                     r = inptrA[i] << (inptrB[i] & shift_mask);
1123                     break;
1124                 case 10:
1125                     r = inptrA[i] >> (inptrB[j] & shift_mask);
1126                     break;
1127                 case 11:
1128                     r = inptrA[i] << (inptrB[j] & shift_mask);
1129                     break;
1130                 case 12:
1131                     r = ~inptrA[i];
1132                     break;
1133                 case 13:
1134                     r = (inptrA[j] < inptrB[j]) ? inptrA[i] : inptrB[i];
1135                     break;
1136                 case 14:
1137                     // Scalars are set to 1/0
1138                     r = inptrA[i] && inptrB[i];
1139                     // Vectors are set to -1/0
1140                     if (vector_size != 1 && r) {
1141                         r = -1;
1142                     }
1143                     break;
1144                 case 15:
1145                     // Scalars are set to 1/0
1146                     r = inptrA[i] || inptrB[i];
1147                     // Vectors are set to -1/0
1148                     if (vector_size != 1 && r) {
1149                         r = -1;
1150                     }
1151                     break;
1152                 case 16:
1153                     // Scalars are set to 1/0
1154                     r = inptrA[i] < inptrB[i];
1155                     // Vectors are set to -1/0
1156                     if (vector_size != 1 && r) {
1157                         r = -1;
1158                     }
1159                     break;
1160                 case 17:
1161                     // Scalars are set to 1/0
1162                     r = inptrA[i] > inptrB[i];
1163                     // Vectors are set to -1/0
1164                     if (vector_size != 1 && r) {
1165                         r = -1;
1166                     }
1167                     break;
1168                 case 18:
1169                     // Scalars are set to 1/0
1170                     r = inptrA[i] <= inptrB[i];
1171                     // Vectors are set to -1/0
1172                     if (vector_size != 1 && r) {
1173                         r = -1;
1174                     }
1175                     break;
1176                 case 19:
1177                     // Scalars are set to 1/0
1178                     r = inptrA[i] >= inptrB[i];
1179                     // Vectors are set to -1/0
1180                     if (vector_size != 1 && r) {
1181                         r = -1;
1182                     }
1183                     break;
1184                 case 20:
1185                     // Scalars are set to 1/0
1186                     r = inptrA[i] == inptrB[i];
1187                     // Vectors are set to -1/0
1188                     if (vector_size != 1 && r) {
1189                         r = -1;
1190                     }
1191                     break;
1192                 case 21:
1193                     // Scalars are set to 1/0
1194                     r = inptrA[i] != inptrB[i];
1195                     // Vectors are set to -1/0
1196                     if (vector_size != 1 && r) {
1197                         r = -1;
1198                     }
1199                     break;
1200                 case 22:
1201                     // Scalars are set to 1/0
1202                     r = !inptrA[i];
1203                     // Vectors are set to -1/0
1204                     if (vector_size != 1 && r) {
1205                         r = -1;
1206                     }
1207                     break;
1208                 default:
1209                     log_error("Invalid test: %d\n", test);
1210                     return -1;
1211                     break;
1212             }
1213             if (r != outptr[i]) {
1214                 // Shift is tricky
1215                 if (test == 8 || test == 9) {
1216                     log_error("cl_ushort Verification failed at element %ld: 0x%x %s 0x%x = 0x%x, got 0x%x\n", i, inptrA[i], tests[test], inptrB[i], r, outptr[i]);
1217                     log_error("\t1) Shift failure at element %ld: original is 0x%x %s %d (0x%x)\n", i, inptrA[i], tests[test], (int)inptrB[i], inptrB[i]);
1218                     log_error("\t2) Take the %d LSBs of the shift to get the final shift amount %d (0x%x).\n", (int)log2(sizeof(cl_ushort)*8),  inptrB[i]&shift_mask, inptrB[i]&shift_mask);
1219                 }
1220                 else if (test == 10 || test == 11) {
1221                     log_error("cl_ushort Verification failed at element %ld (%ld): 0x%x %s 0x%x = 0x%x, got 0x%x\n", i, j, inptrA[i], tests[test], inptrB[j], r, outptr[i]);
1222                     log_error("\t1) Scalar shift failure at element %ld: original is 0x%x %s %d (0x%x)\n", i, inptrA[i], tests[test], (int)inptrB[j], inptrB[j]);
1223                     log_error("\t2) Take the %d LSBs of the shift to get the final shift amount %d (0x%x).\n", (int)log2(sizeof(cl_ushort)*8),  inptrB[j]&shift_mask, inptrB[j]&shift_mask);
1224                 } else if (test == 13) {
1225                     log_error("cl_int Verification failed at element %ld (%ld): (0x%x < 0x%x) ? 0x%x : 0x%x = 0x%x, got 0x%x\n", i, j, inptrA[j], inptrB[j],
1226                               inptrA[i], inptrB[i], r, outptr[i]);
1227                 } else {
1228                     log_error("cl_ushort Verification failed at element %ld: 0x%x %s 0x%x = 0x%x, got 0x%x\n", i, inptrA[i], tests[test], inptrB[i], r, outptr[i]);
1229                 }
1230                 count++;
1231                 if (count >= MAX_ERRORS_TO_PRINT) {
1232                     log_error("Further errors ignored.\n");
1233                     return -1;
1234                 }
1235             }
1236         }
1237     }
1238 
1239     if (count) return -1; else return 0;
1240 }
1241 
1242 void
init_ushort_data(uint64_t indx,int num_elements,cl_ushort * input_ptr[],MTdata d)1243 init_ushort_data(uint64_t indx, int num_elements, cl_ushort *input_ptr[], MTdata d)
1244 {
1245     static const cl_ushort specialCaseList[] = { 0, -1, 1, CL_SHRT_MAX, CL_SHRT_MAX + 1, CL_USHRT_MAX };
1246     int            j;
1247 
1248     // Set the inputs to a random number
1249     for (j=0; j<num_elements; j++)
1250     {
1251         cl_uint bits = genrand_int32(d);
1252         ((cl_ushort *)input_ptr[0])[j] = (cl_ushort) bits;
1253         ((cl_ushort *)input_ptr[1])[j] = (cl_ushort) (bits >> 16);
1254     }
1255 
1256     // Init the first few values to test special cases
1257     {
1258         size_t x, y, index = 0;
1259         for( x = 0; x < sizeof( specialCaseList ) / sizeof( specialCaseList[0] ); x++ )
1260             for( y = 0; y < sizeof( specialCaseList ) / sizeof( specialCaseList[0] ); y++ )
1261             {
1262                 ((cl_ushort *)input_ptr[0])[index] = specialCaseList[x];
1263                 ((cl_ushort *)input_ptr[1])[index++] = specialCaseList[y];
1264             }
1265     }
1266 }
1267 
1268 
1269 
1270 // =======================================
1271 // char
1272 // =======================================
1273 int
verify_char(int test,size_t vector_size,cl_char * inptrA,cl_char * inptrB,cl_char * outptr,size_t n)1274 verify_char(int test, size_t vector_size, cl_char *inptrA, cl_char *inptrB, cl_char *outptr, size_t n)
1275 {
1276     cl_char   r;
1277     cl_int    shift_mask = vector_size == 1 ? (cl_int)(sizeof(cl_int)*8)-1
1278     : (cl_int)(sizeof(cl_char)*8)-1;
1279     size_t    i, j;
1280     int       count=0;
1281 
1282     for (j=0; j<n; j += vector_size )
1283     {
1284         for( i = j; i < j + vector_size; i++ )
1285         {
1286 
1287             switch (test) {
1288                 case 0:
1289                     r = inptrA[i] + inptrB[i];
1290                     break;
1291                 case 1:
1292                     r = inptrA[i] - inptrB[i];
1293                     break;
1294                 case 2:
1295                     r = inptrA[i] * inptrB[i];
1296                     break;
1297                 case 3:
1298                     if (inptrB[i] == 0 || (inptrB[i] == -1 && inptrA[i] == CL_CHAR_MIN))
1299                         continue;
1300                     else
1301                         r = inptrA[i] / inptrB[i];
1302                     break;
1303                 case 4:
1304                     if (inptrB[i] == 0 || (inptrB[i] == -1 && inptrA[i] == CL_CHAR_MIN))
1305                         continue;
1306                     else
1307                         r = inptrA[i] % inptrB[i];
1308                     break;
1309                 case 5:
1310                     r = inptrA[i] & inptrB[i];
1311                     break;
1312                 case 6:
1313                     r = inptrA[i] | inptrB[i];
1314                     break;
1315                 case 7:
1316                     r = inptrA[i] ^ inptrB[i];
1317                     break;
1318                 case 8:
1319                     r = inptrA[i] >> (inptrB[i] & shift_mask);
1320                     break;
1321                 case 9:
1322                     r = inptrA[i] << (inptrB[i] & shift_mask);
1323                     break;
1324                 case 10:
1325                     r = inptrA[i] >> (inptrB[j] & shift_mask);
1326                     break;
1327                 case 11:
1328                     r = inptrA[i] << (inptrB[j] & shift_mask);
1329                     break;
1330                 case 12:
1331                     r = ~inptrA[i];
1332                     break;
1333                 case 13:
1334                     r = (inptrA[j] < inptrB[j]) ? inptrA[i] : inptrB[i];
1335                     break;
1336                 case 14:
1337                     // Scalars are set to 1/0
1338                     r = inptrA[i] && inptrB[i];
1339                     // Vectors are set to -1/0
1340                     if (vector_size != 1 && r) {
1341                         r = -1;
1342                     }
1343                     break;
1344                 case 15:
1345                     // Scalars are set to 1/0
1346                     r = inptrA[i] || inptrB[i];
1347                     // Vectors are set to -1/0
1348                     if (vector_size != 1 && r) {
1349                         r = -1;
1350                     }
1351                     break;
1352                 case 16:
1353                     // Scalars are set to 1/0
1354                     r = inptrA[i] < inptrB[i];
1355                     // Vectors are set to -1/0
1356                     if (vector_size != 1 && r) {
1357                         r = -1;
1358                     }
1359                     break;
1360                 case 17:
1361                     // Scalars are set to 1/0
1362                     r = inptrA[i] > inptrB[i];
1363                     // Vectors are set to -1/0
1364                     if (vector_size != 1 && r) {
1365                         r = -1;
1366                     }
1367                     break;
1368                 case 18:
1369                     // Scalars are set to 1/0
1370                     r = inptrA[i] <= inptrB[i];
1371                     // Vectors are set to -1/0
1372                     if (vector_size != 1 && r) {
1373                         r = -1;
1374                     }
1375                     break;
1376                 case 19:
1377                     // Scalars are set to 1/0
1378                     r = inptrA[i] >= inptrB[i];
1379                     // Vectors are set to -1/0
1380                     if (vector_size != 1 && r) {
1381                         r = -1;
1382                     }
1383                     break;
1384                 case 20:
1385                     // Scalars are set to 1/0
1386                     r = inptrA[i] == inptrB[i];
1387                     // Vectors are set to -1/0
1388                     if (vector_size != 1 && r) {
1389                         r = -1;
1390                     }
1391                     break;
1392                 case 21:
1393                     // Scalars are set to 1/0
1394                     r = inptrA[i] != inptrB[i];
1395                     // Vectors are set to -1/0
1396                     if (vector_size != 1 && r) {
1397                         r = -1;
1398                     }
1399                     break;
1400                 case 22:
1401                     // Scalars are set to 1/0
1402                     r = !inptrA[i];
1403                     // Vectors are set to -1/0
1404                     if (vector_size != 1 && r) {
1405                         r = -1;
1406                     }
1407                     break;
1408                 default:
1409                     log_error("Invalid test: %d\n", test);
1410                     return -1;
1411                     break;
1412             }
1413             if (r != outptr[i]) {
1414                 // Shift is tricky
1415                 if (test == 8 || test == 9) {
1416                     log_error("cl_char Verification failed at element %ld: 0x%x %s 0x%x = 0x%x, got 0x%x\n", i, inptrA[i], tests[test], inptrB[i], r, outptr[i]);
1417                     log_error("\t1) Shift failure at element %ld: original is 0x%x %s %d (0x%x)\n", i, inptrA[i], tests[test], (int)inptrB[i], inptrB[i]);
1418                     log_error("\t2) Take the %d LSBs of the shift to get the final shift amount %d (0x%x).\n", (int)log2(sizeof(cl_char)*8),  inptrB[i]&shift_mask, inptrB[i]&shift_mask);
1419                 }
1420                 else if (test == 10 || test == 11) {
1421                     log_error("cl_char Verification failed at element %ld (%ld): 0x%x %s 0x%x = 0x%x, got 0x%x\n", i, j, inptrA[i], tests[test], inptrB[j], r, outptr[i]);
1422                     log_error("\t1) Scalar shift failure at element %ld: original is 0x%x %s %d (0x%x)\n", i, inptrA[i], tests[test], (int)inptrB[j], inptrB[j]);
1423                     log_error("\t2) Take the %d LSBs of the shift to get the final shift amount %d (0x%x).\n", (int)log2(sizeof(cl_long)*8),  inptrB[j]&shift_mask, inptrB[j]&shift_mask);
1424                 } else if (test == 13) {
1425                     log_error("cl_int Verification failed at element %ld (%ld): (0x%x < 0x%x) ? 0x%x : 0x%x = 0x%x, got 0x%x\n", i, j, inptrA[j], inptrB[j],
1426                               inptrA[i], inptrB[i], r, outptr[i]);
1427                 } else {
1428                     log_error("cl_char Verification failed at element %ld: 0x%x %s 0x%x = 0x%x, got 0x%x\n", i, inptrA[i], tests[test], inptrB[i], r, outptr[i]);
1429                 }
1430                 count++;
1431                 if (count >= MAX_ERRORS_TO_PRINT) {
1432                     log_error("Further errors ignored.\n");
1433                     return -1;
1434                 }
1435             }
1436         }
1437     }
1438     if (count) return -1; else return 0;
1439 }
1440 
1441 void
init_char_data(uint64_t indx,int num_elements,cl_char * input_ptr[],MTdata d)1442 init_char_data(uint64_t indx, int num_elements, cl_char *input_ptr[], MTdata d)
1443 {
1444     static const cl_char specialCaseList[] = { 0, -1, 1, CL_CHAR_MIN, CL_CHAR_MIN + 1, CL_CHAR_MAX };
1445     int            j;
1446 
1447     // FIXME comment below might not be appropriate for
1448     // vector data.  Yes, checking every scalar char against every
1449     // scalar char is only 2^16 ~ 64000 tests, but once we get to vec3,
1450     // vec4, vec8...
1451 
1452     // in the meantime, this means I can use [] to access vec3 instead of
1453     // vload3 / vstore3 :D
1454 
1455     // FIXME: we really should just check every char against every char here
1456     // Set the inputs to a random number
1457     for (j=0; j<num_elements; j++)
1458     {
1459         cl_uint bits = genrand_int32(d);
1460         ((cl_char *)input_ptr[0])[j] = (cl_char) bits;
1461         ((cl_char *)input_ptr[1])[j] = (cl_char) (bits >> 16);
1462     }
1463 
1464     // Init the first few values to test special cases
1465     {
1466         size_t x, y, index = 0;
1467         for( x = 0; x < sizeof( specialCaseList ) / sizeof( specialCaseList[0] ); x++ )
1468             for( y = 0; y < sizeof( specialCaseList ) / sizeof( specialCaseList[0] ); y++ )
1469             {
1470                 ((cl_char *)input_ptr[0])[index] = specialCaseList[x];
1471                 ((cl_char *)input_ptr[1])[index++] = specialCaseList[y];
1472             }
1473     }
1474 }
1475 
1476 
1477 // =======================================
1478 // uchar
1479 // =======================================
1480 int
verify_uchar(int test,size_t vector_size,cl_uchar * inptrA,cl_uchar * inptrB,cl_uchar * outptr,size_t n)1481 verify_uchar(int test, size_t vector_size, cl_uchar *inptrA, cl_uchar *inptrB, cl_uchar *outptr, size_t n)
1482 {
1483     cl_uchar r;
1484     cl_uint shift_mask = vector_size == 1 ? (cl_uint)(sizeof(cl_uint) * 8) - 1
1485                                           : (cl_uint)(sizeof(cl_uchar) * 8) - 1;
1486     size_t   i, j;
1487     int      count=0;
1488 
1489     for (j=0; j<n; j += vector_size )
1490     {
1491         for( i = j; i < j + vector_size; i++ )
1492         {
1493             switch (test) {
1494                 case 0:
1495                     r = inptrA[i] + inptrB[i];
1496                     break;
1497                 case 1:
1498                     r = inptrA[i] - inptrB[i];
1499                     break;
1500                 case 2:
1501                     r = inptrA[i] * inptrB[i];
1502                     break;
1503                 case 3:
1504                     if (inptrB[i] == 0)
1505                         continue;
1506                     else
1507                         r = inptrA[i] / inptrB[i];
1508                     break;
1509                 case 4:
1510                     if (inptrB[i] == 0)
1511                         continue;
1512                     else
1513                         r = inptrA[i] % inptrB[i];
1514                     break;
1515                 case 5:
1516                     r = inptrA[i] & inptrB[i];
1517                     break;
1518                 case 6:
1519                     r = inptrA[i] | inptrB[i];
1520                     break;
1521                 case 7:
1522                     r = inptrA[i] ^ inptrB[i];
1523                     break;
1524                 case 8:
1525                     r = inptrA[i] >> (inptrB[i] & shift_mask);
1526                     break;
1527                 case 9:
1528                     r = inptrA[i] << (inptrB[i] & shift_mask);
1529                     break;
1530                 case 10:
1531                     r = inptrA[i] >> (inptrB[j] & shift_mask);
1532                     break;
1533                 case 11:
1534                     r = inptrA[i] << (inptrB[j] & shift_mask);
1535                     break;
1536                 case 12:
1537                     r = ~inptrA[i];
1538                     break;
1539                 case 13:
1540                     r = (inptrA[j] < inptrB[j]) ? inptrA[i] : inptrB[i];
1541                     break;
1542                 case 14:
1543                     // Scalars are set to 1/0
1544                     r = inptrA[i] && inptrB[i];
1545                     // Vectors are set to -1/0
1546                     if (vector_size != 1 && r) {
1547                         r = -1;
1548                     }
1549                     break;
1550                 case 15:
1551                     // Scalars are set to 1/0
1552                     r = inptrA[i] || inptrB[i];
1553                     // Vectors are set to -1/0
1554                     if (vector_size != 1 && r) {
1555                         r = -1;
1556                     }
1557                     break;
1558                 case 16:
1559                     // Scalars are set to 1/0
1560                     r = inptrA[i] < inptrB[i];
1561                     // Vectors are set to -1/0
1562                     if (vector_size != 1 && r) {
1563                         r = -1;
1564                     }
1565                     break;
1566                 case 17:
1567                     // Scalars are set to 1/0
1568                     r = inptrA[i] > inptrB[i];
1569                     // Vectors are set to -1/0
1570                     if (vector_size != 1 && r) {
1571                         r = -1;
1572                     }
1573                     break;
1574                 case 18:
1575                     // Scalars are set to 1/0
1576                     r = inptrA[i] <= inptrB[i];
1577                     // Vectors are set to -1/0
1578                     if (vector_size != 1 && r) {
1579                         r = -1;
1580                     }
1581                     break;
1582                 case 19:
1583                     // Scalars are set to 1/0
1584                     r = inptrA[i] >= inptrB[i];
1585                     // Vectors are set to -1/0
1586                     if (vector_size != 1 && r) {
1587                         r = -1;
1588                     }
1589                     break;
1590                 case 20:
1591                     // Scalars are set to 1/0
1592                     r = inptrA[i] == inptrB[i];
1593                     // Vectors are set to -1/0
1594                     if (vector_size != 1 && r) {
1595                         r = -1;
1596                     }
1597                     break;
1598                 case 21:
1599                     // Scalars are set to 1/0
1600                     r = inptrA[i] != inptrB[i];
1601                     // Vectors are set to -1/0
1602                     if (vector_size != 1 && r) {
1603                         r = -1;
1604                     }
1605                     break;
1606                 case 22:
1607                     // Scalars are set to 1/0
1608                     r = !inptrA[i];
1609                     // Vectors are set to -1/0
1610                     if (vector_size != 1 && r) {
1611                         r = -1;
1612                     }
1613                     break;
1614                 default:
1615                     log_error("Invalid test: %d\n", test);
1616                     return -1;
1617                     break;
1618             }
1619             if (r != outptr[i]) {
1620                 // Shift is tricky
1621                 if (test == 8 || test == 9) {
1622                     log_error("cl_uchar Verification failed at element %ld: 0x%x %s 0x%x = 0x%x, got 0x%x\n", i, inptrA[i], tests[test], inptrB[i], r, outptr[i]);
1623                     log_error("\t1) Shift failure at element %ld: original is 0x%x %s %d (0x%x)\n", i, inptrA[i], tests[test], (int)inptrB[i], inptrB[i]);
1624                     log_error("\t2) Take the %d LSBs of the shift to get the final shift amount %d (0x%x).\n", (int)log2(sizeof(cl_uchar)*8),  inptrB[i]&shift_mask, inptrB[i]&shift_mask);
1625                 }
1626                 else if (test == 10 || test == 11) {
1627                     log_error("cl_uchar Verification failed at element %ld (%ld): 0x%x %s 0x%x = 0x%x, got 0x%x\n", i, j, inptrA[i], tests[test], inptrB[j], r, outptr[i]);
1628                     log_error("\t1) Scalar shift failure at element %ld: original is 0x%x %s %d (0x%x)\n", i, inptrA[i], tests[test], (int)inptrB[j], inptrB[j]);
1629                     log_error("\t2) Take the %d LSBs of the shift to get the final shift amount %d (0x%x).\n", (int)log2(sizeof(cl_uchar)*8),  inptrB[j]&shift_mask, inptrB[j]&shift_mask);
1630                 } else if (test == 13) {
1631                     log_error("cl_int Verification failed at element %ld (%ld): (0x%x < 0x%x) ? 0x%x : 0x%x = 0x%x, got 0x%x\n", i, j, inptrA[j], inptrB[j],
1632                               inptrA[i], inptrB[i], r, outptr[i]);
1633                 } else {
1634                     log_error("cl_uchar Verification failed at element %ld: 0x%x %s 0x%x = 0x%x, got 0x%x\n", i, inptrA[i], tests[test], inptrB[i], r, outptr[i]);
1635                 }
1636                 count++;
1637                 if (count >= MAX_ERRORS_TO_PRINT) {
1638                     log_error("Further errors ignored.\n");
1639                     return -1;
1640                 }
1641             }
1642         }
1643     }
1644 
1645     if (count) return -1; else return 0;
1646 }
1647 
1648 void
init_uchar_data(uint64_t indx,int num_elements,cl_uchar * input_ptr[],MTdata d)1649 init_uchar_data(uint64_t indx, int num_elements, cl_uchar *input_ptr[], MTdata d)
1650 {
1651     static const cl_uchar specialCaseList[] = { 0, -1, 1, CL_CHAR_MAX, CL_CHAR_MAX + 1, CL_UCHAR_MAX };
1652     int            j;
1653 
1654     // FIXME: we really should just check every char against every char here
1655 
1656     // Set the inputs to a random number
1657     for (j=0; j<num_elements; j++)
1658     {
1659         cl_uint bits = genrand_int32(d);
1660         ((cl_uchar *)input_ptr[0])[j] = (cl_uchar) bits;
1661         ((cl_uchar *)input_ptr[1])[j] = (cl_uchar) (bits >> 16);
1662     }
1663 
1664     // Init the first few values to test special cases
1665     {
1666         size_t x, y, index = 0;
1667         for( x = 0; x < sizeof( specialCaseList ) / sizeof( specialCaseList[0] ); x++ )
1668             for( y = 0; y < sizeof( specialCaseList ) / sizeof( specialCaseList[0] ); y++ )
1669             {
1670                 ((cl_uchar *)input_ptr[0])[index] = specialCaseList[x];
1671                 ((cl_uchar *)input_ptr[1])[index++] = specialCaseList[y];
1672             }
1673     }
1674 }
1675 
1676