1 /**************************************************************************
2  *
3  * Copyright © 2010 Luca Barbieri
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  **************************************************************************/
25 
26 #include <stdio.h>
27 #include "translate/translate.h"
28 #include "util/u_memory.h"
29 #include "util/u_format.h"
30 #include "util/u_half.h"
31 #include "util/u_cpu_detect.h"
32 #include "rtasm/rtasm_cpu.h"
33 
34 /* don't use this for serious use */
rand_double()35 static double rand_double()
36 {
37    const double rm = (double)RAND_MAX + 1;
38    double div = 1;
39    double v = 0;
40    unsigned i;
41    for(i = 0; i < 4; ++i)
42    {
43       div *= rm;
44       v += (double)rand() / div;
45    }
46    return v;
47 }
48 
main(int argc,char ** argv)49 int main(int argc, char** argv)
50 {
51    struct translate *(*create_fn)(const struct translate_key *key) = 0;
52 
53    struct translate_key key;
54    unsigned output_format;
55    unsigned input_format;
56    unsigned buffer_size = 4096;
57    unsigned char* buffer[5];
58    unsigned char* byte_buffer;
59    float* float_buffer;
60    double* double_buffer;
61    uint16_t *half_buffer;
62    unsigned * elts;
63    unsigned count = 4;
64    unsigned i, j, k;
65    unsigned passed = 0;
66    unsigned total = 0;
67    const float error = 0.03125;
68 
69    create_fn = 0;
70 
71    util_cpu_detect();
72 
73    if (argc <= 1 ||
74        !strcmp(argv[1], "default") )
75       create_fn = translate_create;
76    else if (!strcmp(argv[1], "generic"))
77       create_fn = translate_generic_create;
78    else if (!strcmp(argv[1], "x86"))
79       create_fn = translate_sse2_create;
80    else if (!strcmp(argv[1], "nosse"))
81    {
82       util_cpu_caps.has_sse = 0;
83       util_cpu_caps.has_sse2 = 0;
84       util_cpu_caps.has_sse3 = 0;
85       util_cpu_caps.has_sse4_1 = 0;
86       create_fn = translate_sse2_create;
87    }
88    else if (!strcmp(argv[1], "sse"))
89    {
90       if(!util_cpu_caps.has_sse || !rtasm_cpu_has_sse())
91       {
92          printf("Error: CPU doesn't support SSE (test with qemu)\n");
93          return 2;
94       }
95       util_cpu_caps.has_sse2 = 0;
96       util_cpu_caps.has_sse3 = 0;
97       util_cpu_caps.has_sse4_1 = 0;
98       create_fn = translate_sse2_create;
99    }
100    else if (!strcmp(argv[1], "sse2"))
101    {
102       if(!util_cpu_caps.has_sse2 || !rtasm_cpu_has_sse())
103       {
104          printf("Error: CPU doesn't support SSE2 (test with qemu)\n");
105          return 2;
106       }
107       util_cpu_caps.has_sse3 = 0;
108       util_cpu_caps.has_sse4_1 = 0;
109       create_fn = translate_sse2_create;
110    }
111    else if (!strcmp(argv[1], "sse3"))
112    {
113       if(!util_cpu_caps.has_sse3 || !rtasm_cpu_has_sse())
114       {
115          printf("Error: CPU doesn't support SSE3 (test with qemu)\n");
116          return 2;
117       }
118       util_cpu_caps.has_sse4_1 = 0;
119       create_fn = translate_sse2_create;
120    }
121    else if (!strcmp(argv[1], "sse4.1"))
122    {
123       if(!util_cpu_caps.has_sse4_1 || !rtasm_cpu_has_sse())
124       {
125          printf("Error: CPU doesn't support SSE4.1 (test with qemu)\n");
126          return 2;
127       }
128       create_fn = translate_sse2_create;
129    }
130 
131    if (!create_fn)
132    {
133       printf("Usage: ./translate_test [default|generic|x86|nosse|sse|sse2|sse3|sse4.1]\n");
134       return 2;
135    }
136 
137    for (i = 1; i < ARRAY_SIZE(buffer); ++i)
138       buffer[i] = align_malloc(buffer_size, 4096);
139 
140    byte_buffer = align_malloc(buffer_size, 4096);
141    float_buffer = align_malloc(buffer_size, 4096);
142    double_buffer = align_malloc(buffer_size, 4096);
143    half_buffer = align_malloc(buffer_size, 4096);
144 
145    elts = align_malloc(count * sizeof *elts, 4096);
146 
147    key.nr_elements = 1;
148    key.element[0].input_buffer = 0;
149    key.element[0].input_offset = 0;
150    key.element[0].output_offset = 0;
151    key.element[0].type = TRANSLATE_ELEMENT_NORMAL;
152    key.element[0].instance_divisor = 0;
153 
154    srand(4359025);
155 
156    /* avoid negative values that work badly when converted to unsigned format*/
157    for (i = 0; i < buffer_size; ++i)
158       byte_buffer[i] = rand() & 0x7f7f7f7f;
159 
160    for (i = 0; i < buffer_size / sizeof(float); ++i)
161       float_buffer[i] = (float)rand_double();
162 
163    for (i = 0; i < buffer_size / sizeof(double); ++i)
164       double_buffer[i] = rand_double();
165 
166    for (i = 0; i < buffer_size / sizeof(double); ++i)
167       half_buffer[i] = util_float_to_half((float) rand_double());
168 
169    for (i = 0; i < count; ++i)
170       elts[i] = i;
171 
172    for (output_format = 1; output_format < PIPE_FORMAT_COUNT; ++output_format)
173    {
174       const struct util_format_description* output_format_desc = util_format_description(output_format);
175       unsigned output_format_size;
176       unsigned output_normalized = 0;
177 
178       if (!output_format_desc
179             || !output_format_desc->fetch_rgba_float
180             || !output_format_desc->pack_rgba_float
181             || output_format_desc->colorspace != UTIL_FORMAT_COLORSPACE_RGB
182             || output_format_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN
183             || !translate_is_output_format_supported(output_format))
184          continue;
185 
186       for(i = 0; i < output_format_desc->nr_channels; ++i)
187       {
188          if(output_format_desc->channel[i].type != UTIL_FORMAT_TYPE_FLOAT)
189             output_normalized |= (1 << output_format_desc->channel[i].normalized);
190       }
191 
192       output_format_size = util_format_get_stride(output_format, 1);
193 
194       for (input_format = 1; input_format < PIPE_FORMAT_COUNT; ++input_format)
195       {
196          const struct util_format_description* input_format_desc = util_format_description(input_format);
197          unsigned input_format_size;
198          struct translate* translate[2];
199          unsigned fail = 0;
200          unsigned used_generic = 0;
201          unsigned input_normalized = 0;
202          boolean input_is_float = FALSE;
203 
204          if (!input_format_desc
205                || !input_format_desc->fetch_rgba_float
206                || !input_format_desc->pack_rgba_float
207                || input_format_desc->colorspace != UTIL_FORMAT_COLORSPACE_RGB
208                || input_format_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN
209                || !translate_is_output_format_supported(input_format))
210             continue;
211 
212          input_format_size = util_format_get_stride(input_format, 1);
213 
214          for(i = 0; i < input_format_desc->nr_channels; ++i)
215          {
216             if(input_format_desc->channel[i].type == UTIL_FORMAT_TYPE_FLOAT)
217             {
218                input_is_float = 1;
219                input_normalized |= 1 << 1;
220             }
221             else
222                input_normalized |= (1 << input_format_desc->channel[i].normalized);
223          }
224 
225          if(((input_normalized | output_normalized) == 3)
226                || ((input_normalized & 1) && (output_normalized & 1)
227                      && input_format_size * output_format_desc->nr_channels > output_format_size * input_format_desc->nr_channels))
228             continue;
229 
230          key.element[0].input_format = input_format;
231          key.element[0].output_format = output_format;
232          key.output_stride = output_format_size;
233          translate[0] = create_fn(&key);
234          if (!translate[0])
235             continue;
236 
237          key.element[0].input_format = output_format;
238          key.element[0].output_format = input_format;
239          key.output_stride = input_format_size;
240          translate[1] = create_fn(&key);
241          if(!translate[1])
242          {
243             used_generic = 1;
244             translate[1] = translate_generic_create(&key);
245             if(!translate[1])
246                continue;
247          }
248 
249          for(i = 1; i < 5; ++i)
250             memset(buffer[i], 0xcd - (0x22 * i), 4096);
251 
252          if(input_is_float && input_format_desc->channel[0].size == 32)
253             buffer[0] = (unsigned char*)float_buffer;
254          else if(input_is_float && input_format_desc->channel[0].size == 64)
255             buffer[0] = (unsigned char*)double_buffer;
256          else if(input_is_float && input_format_desc->channel[0].size == 16)
257             buffer[0] = (unsigned char*)half_buffer;
258          else if(input_is_float)
259             abort();
260          else
261             buffer[0] = byte_buffer;
262 
263          translate[0]->set_buffer(translate[0], 0, buffer[0], input_format_size, count - 1);
264          translate[0]->run_elts(translate[0], elts, count, 0, 0, buffer[1]);
265          translate[1]->set_buffer(translate[1], 0, buffer[1], output_format_size, count - 1);
266          translate[1]->run_elts(translate[1], elts, count, 0, 0, buffer[2]);
267          translate[0]->set_buffer(translate[0], 0, buffer[2], input_format_size, count - 1);
268          translate[0]->run_elts(translate[0], elts, count, 0, 0, buffer[3]);
269          translate[1]->set_buffer(translate[1], 0, buffer[3], output_format_size, count - 1);
270          translate[1]->run_elts(translate[1], elts, count, 0, 0, buffer[4]);
271 
272          for (i = 0; i < count; ++i)
273          {
274             float a[4];
275             float b[4];
276             input_format_desc->fetch_rgba_float(a, buffer[2] + i * input_format_size, 0, 0);
277             input_format_desc->fetch_rgba_float(b, buffer[4] + i * input_format_size, 0, 0);
278 
279             for (j = 0; j < count; ++j)
280             {
281                float d = a[j] - b[j];
282                if (d > error || d < -error)
283                {
284                   fail = 1;
285                   break;
286                }
287             }
288          }
289 
290          printf("%s%s: %s -> %s -> %s -> %s -> %s\n",
291                fail ? "FAIL" : "PASS",
292                used_generic ? "[GENERIC]" : "",
293                input_format_desc->name, output_format_desc->name, input_format_desc->name, output_format_desc->name, input_format_desc->name);
294 
295          if (1)
296          {
297             for (i = 0; i < ARRAY_SIZE(buffer); ++i)
298             {
299                unsigned format_size = (i & 1) ? output_format_size : input_format_size;
300                printf("%c ", (i == 2 || i == 4) ? '*' : ' ');
301                for (j = 0; j < count; ++j)
302                {
303                   for (k = 0; k < format_size; ++k)
304                   {
305                      printf("%02x", buffer[i][j * format_size + k]);
306                   }
307                   printf(" ");
308                }
309                printf("\n");
310             }
311          }
312 
313          if (!fail)
314             ++passed;
315          ++total;
316 
317          if(translate[1])
318             translate[1]->release(translate[1]);
319          translate[0]->release(translate[0]);
320       }
321    }
322 
323    printf("%u/%u tests passed for translate_%s\n", passed, total, argv[1]);
324    return passed != total;
325 }
326