1// TESTING: abs
2// TESTING: bitselect
3// TESTING: clz
4// TESTING: max
5// TESTING: min
6// TESTING: popcount
7
8#include "common.cl"
9
10
11DEFINE_BODY_G(test_bitselect, ({
12   int const bits = count_bits(sgtype);
13   for (int iter=0; iter<nrandoms; ++iter) {
14     typedef union {
15       gtype  v;
16       sgtype s[16];
17     } Tvec;
18     typedef union {
19       ugtype  v;
20       sugtype s[16];
21     } UTvec;
22     Tvec sel, left, right;
23     UTvec res_abs;
24     Tvec res_bitselect, res_clz, res_max, res_min, res_popcount;
25     for (int n=0; n<vecsize; ++n) {
26       sel.s[n]   = randoms[(iter+n   ) % nrandoms];
27       left.s[n]  = randoms[(iter+n+20) % nrandoms];
28       right.s[n] = randoms[(iter+n+40) % nrandoms];
29       if (bits>32) {
30         sel.s[n]   = (sel.s[n]   << (bits/2)) | randoms[(iter+n+100) % nrandoms];
31         left.s[n]  = (left.s[n]  << (bits/2)) | randoms[(iter+n+120) % nrandoms];
32         right.s[n] = (right.s[n] << (bits/2)) | randoms[(iter+n+140) % nrandoms];
33       }
34     }
35     res_abs.v = abs(left.v);
36     res_bitselect.v = bitselect(left.v, right.v, sel.v);
37     res_clz.v = clz(left.v);
38     res_max.v = max(left.v, right.v);
39     res_min.v = min(left.v, right.v);
40     res_popcount.v = popcount(left.v);
41     bool equal;
42     // abs
43     equal = true;
44     for (int n=0; n<vecsize; ++n) {
45       sgtype signbit = (sgtype)((sgtype)1 << (sgtype)(count_bits(sgtype)-1));
46       // Note: left.s[n] < 0 leads to a compiler warning for unsigned types,
47       // so we check the sign bit explicitly
48       sugtype absval =
49         is_signed(sgtype) ?
50         (left.s[n] & signbit ? -left.s[n] : left.s[n]) :
51         left.s[n];
52       if (res_abs.s[n] != absval) {
53         equal = false;
54         printf("FAIL: abs(a)[%d] type=%s a=0x%08x want=0x%08x got=0x%08x\n",
55                n, typename,
56                (uint)left.s[n], (uint)absval,
57                (uint)res_abs.s[n]);
58       }
59     }
60     // bitselect
61     for (int n=0; n<vecsize; ++n) {
62       sgtype selval = (left.s[n] & ~sel.s[n]) | (right.s[n] & sel.s[n]);
63       if (res_bitselect.s[n] != selval) {
64         equal = false;
65         printf("FAIL: bitselect(a,b,c)[%d] type=%s a=0x%08x b=0x%08x c=0x%08x want=0x%08x got=0x%08x\n",
66                n, typename,
67                (uint)left.s[n], (uint)right.s[n], (uint)sel.s[n], (uint)selval,
68                (uint)res_bitselect.s[n]);
69       }
70     }
71     // clz
72     for (int n=0; n<vecsize; ++n) {
73       int b=0;
74       while (b<bits) {
75         sgtype mask = (sgtype)1 << (sgtype)(bits - 1 - b);
76         if (left.s[n] & mask) break;
77         ++b;
78       }
79       if (res_clz.s[n] != (sgtype)b) {
80         equal = false;
81         printf("FAIL: clz(a)[%d] type=%s a=0x%08x want=0x%08x got=0x%08x\n",
82                n, typename,
83                (uint)left.s[n], (uint)b,
84                (uint)res_clz.s[n]);
85       }
86     }
87     // max
88     for (int n=0; n<vecsize; ++n) {
89       sgtype maxval = left.s[n] > right.s[n] ? left.s[n] : right.s[n];
90       if (res_max.s[n] != maxval) {
91         equal = false;
92         printf("FAIL: max(a,b)[%d] type=%s a=0x%08x b=0x%08x want=0x%08x got=0x%08x\n",
93                n, typename,
94                (uint)left.s[n], (uint)right.s[n], (uint)maxval,
95                (uint)res_max.s[n]);
96       }
97     }
98     // min
99     for (int n=0; n<vecsize; ++n) {
100       sgtype minval = left.s[n] < right.s[n] ? left.s[n] : right.s[n];
101       if (res_min.s[n] != minval) {
102         equal = false;
103         printf("FAIL: min(a,b)[%d] type=%s a=0x%08x b=0x%08x want=0x%08x got=0x%08x\n",
104                n, typename,
105                (uint)left.s[n], (uint)right.s[n], (uint)minval,
106                (uint)res_min.s[n]);
107       }
108     }
109     // popcount
110     for (int n=0; n<vecsize; ++n) {
111       int c=0;
112       for (int b=0; b<bits; ++b) {
113         sgtype mask = (sgtype)1 << (sgtype)b;
114         if (left.s[n] & mask) ++c;
115       }
116       if (res_popcount.s[n] != (sgtype)c) {
117         equal = false;
118         printf("FAIL: popcount(a)[%d] type=%s a=0x%08x want=0x%08x got=0x%08x\n",
119                n, typename,
120                (uint)left.s[n], (uint)c,
121                (uint)res_clz.s[n]);
122       }
123     }
124     if (!equal) return;
125   }
126 })
127)
128
129kernel void test_bitselect()
130{
131  CALL_FUNC_G(test_bitselect)
132}
133