• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/format/u_format.h"
30 #include "util/half_float.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    extern struct util_cpu_caps_t util_cpu_caps;
54    struct translate_key key;
55    unsigned output_format;
56    unsigned input_format;
57    unsigned buffer_size = 4096;
58    unsigned char* buffer[5];
59    unsigned char* byte_buffer;
60    float* float_buffer;
61    double* double_buffer;
62    uint16_t *half_buffer;
63    unsigned * elts;
64    unsigned count = 4;
65    unsigned i, j, k;
66    unsigned passed = 0;
67    unsigned total = 0;
68    const float error = 0.03125;
69 
70    create_fn = 0;
71 
72    if (argc <= 1 ||
73        !strcmp(argv[1], "default") )
74       create_fn = translate_create;
75    else if (!strcmp(argv[1], "generic"))
76       create_fn = translate_generic_create;
77    else if (!strcmp(argv[1], "x86"))
78       create_fn = translate_sse2_create;
79    else if (!strcmp(argv[1], "nosse"))
80    {
81       util_cpu_caps.has_sse = 0;
82       util_cpu_caps.has_sse2 = 0;
83       util_cpu_caps.has_sse3 = 0;
84       util_cpu_caps.has_sse4_1 = 0;
85       create_fn = translate_sse2_create;
86    }
87    else if (!strcmp(argv[1], "sse"))
88    {
89       if(!util_get_cpu_caps()->has_sse || !rtasm_cpu_has_sse())
90       {
91          printf("Error: CPU doesn't support SSE (test with qemu)\n");
92          return 2;
93       }
94       util_cpu_caps.has_sse2 = 0;
95       util_cpu_caps.has_sse3 = 0;
96       util_cpu_caps.has_sse4_1 = 0;
97       create_fn = translate_sse2_create;
98    }
99    else if (!strcmp(argv[1], "sse2"))
100    {
101       if(!util_get_cpu_caps()->has_sse2 || !rtasm_cpu_has_sse())
102       {
103          printf("Error: CPU doesn't support SSE2 (test with qemu)\n");
104          return 2;
105       }
106       util_cpu_caps.has_sse3 = 0;
107       util_cpu_caps.has_sse4_1 = 0;
108       create_fn = translate_sse2_create;
109    }
110    else if (!strcmp(argv[1], "sse3"))
111    {
112       if(!util_get_cpu_caps()->has_sse3 || !rtasm_cpu_has_sse())
113       {
114          printf("Error: CPU doesn't support SSE3 (test with qemu)\n");
115          return 2;
116       }
117       util_cpu_caps.has_sse4_1 = 0;
118       create_fn = translate_sse2_create;
119    }
120    else if (!strcmp(argv[1], "sse4.1"))
121    {
122       if(!util_get_cpu_caps()->has_sse4_1 || !rtasm_cpu_has_sse())
123       {
124          printf("Error: CPU doesn't support SSE4.1 (test with qemu)\n");
125          return 2;
126       }
127       create_fn = translate_sse2_create;
128    }
129 
130    if (!create_fn)
131    {
132       printf("Usage: ./translate_test [default|generic|x86|nosse|sse|sse2|sse3|sse4.1]\n");
133       return 2;
134    }
135 
136    for (i = 1; i < ARRAY_SIZE(buffer); ++i)
137       buffer[i] = align_malloc(buffer_size, 4096);
138 
139    byte_buffer = align_malloc(buffer_size, 4096);
140    float_buffer = align_malloc(buffer_size, 4096);
141    double_buffer = align_malloc(buffer_size, 4096);
142    half_buffer = align_malloc(buffer_size, 4096);
143 
144    elts = align_malloc(count * sizeof *elts, 4096);
145 
146    key.nr_elements = 1;
147    key.element[0].input_buffer = 0;
148    key.element[0].input_offset = 0;
149    key.element[0].output_offset = 0;
150    key.element[0].type = TRANSLATE_ELEMENT_NORMAL;
151    key.element[0].instance_divisor = 0;
152 
153    srand(4359025);
154 
155    /* avoid negative values that work badly when converted to unsigned format*/
156    for (i = 0; i < buffer_size; ++i)
157       byte_buffer[i] = rand() & 0x7f7f7f7f;
158 
159    for (i = 0; i < buffer_size / sizeof(float); ++i)
160       float_buffer[i] = (float)rand_double();
161 
162    for (i = 0; i < buffer_size / sizeof(double); ++i)
163       double_buffer[i] = rand_double();
164 
165    for (i = 0; i < buffer_size / sizeof(double); ++i)
166       half_buffer[i] = _mesa_float_to_half((float) rand_double());
167 
168    for (i = 0; i < count; ++i)
169       elts[i] = i;
170 
171    for (output_format = 1; output_format < PIPE_FORMAT_COUNT; ++output_format)
172    {
173       const struct util_format_description* output_format_desc = util_format_description(output_format);
174       const struct util_format_pack_description* output_format_pack = util_format_pack_description(output_format);
175       util_format_fetch_rgba_func_ptr fetch_rgba =
176          util_format_fetch_rgba_func(output_format);
177       unsigned output_format_size;
178       unsigned output_normalized = 0;
179 
180       if (!fetch_rgba
181           || !output_format_pack->pack_rgba_float
182           || output_format_desc->colorspace != UTIL_FORMAT_COLORSPACE_RGB
183           || output_format_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN
184           || !translate_is_output_format_supported(output_format))
185          continue;
186 
187       for(i = 0; i < output_format_desc->nr_channels; ++i)
188       {
189          if(output_format_desc->channel[i].type != UTIL_FORMAT_TYPE_FLOAT)
190             output_normalized |= (1 << output_format_desc->channel[i].normalized);
191       }
192 
193       output_format_size = util_format_get_stride(output_format, 1);
194 
195       for (input_format = 1; input_format < PIPE_FORMAT_COUNT; ++input_format)
196       {
197          const struct util_format_description* input_format_desc = util_format_description(input_format);
198          const struct util_format_pack_description* input_format_pack = util_format_pack_description(input_format);
199          util_format_fetch_rgba_func_ptr fetch_rgba =
200             util_format_fetch_rgba_func(input_format);
201          unsigned input_format_size;
202          struct translate* translate[2];
203          unsigned fail = 0;
204          unsigned used_generic = 0;
205          unsigned input_normalized = 0;
206          boolean input_is_float = FALSE;
207 
208          if (!fetch_rgba
209              || !input_format_pack->pack_rgba_float
210              || input_format_desc->colorspace != UTIL_FORMAT_COLORSPACE_RGB
211              || input_format_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN
212              || !translate_is_output_format_supported(input_format))
213             continue;
214 
215          input_format_size = util_format_get_stride(input_format, 1);
216 
217          for(i = 0; i < input_format_desc->nr_channels; ++i)
218          {
219             if(input_format_desc->channel[i].type == UTIL_FORMAT_TYPE_FLOAT)
220             {
221                input_is_float = 1;
222                input_normalized |= 1 << 1;
223             }
224             else
225                input_normalized |= (1 << input_format_desc->channel[i].normalized);
226          }
227 
228          if(((input_normalized | output_normalized) == 3)
229                || ((input_normalized & 1) && (output_normalized & 1)
230                      && input_format_size * output_format_desc->nr_channels > output_format_size * input_format_desc->nr_channels))
231             continue;
232 
233          key.element[0].input_format = input_format;
234          key.element[0].output_format = output_format;
235          key.output_stride = output_format_size;
236          translate[0] = create_fn(&key);
237          if (!translate[0])
238             continue;
239 
240          key.element[0].input_format = output_format;
241          key.element[0].output_format = input_format;
242          key.output_stride = input_format_size;
243          translate[1] = create_fn(&key);
244          if(!translate[1])
245          {
246             used_generic = 1;
247             translate[1] = translate_generic_create(&key);
248             if(!translate[1])
249                continue;
250          }
251 
252          for(i = 1; i < 5; ++i)
253             memset(buffer[i], 0xcd - (0x22 * i), 4096);
254 
255          if(input_is_float && input_format_desc->channel[0].size == 32)
256             buffer[0] = (unsigned char*)float_buffer;
257          else if(input_is_float && input_format_desc->channel[0].size == 64)
258             buffer[0] = (unsigned char*)double_buffer;
259          else if(input_is_float && input_format_desc->channel[0].size == 16)
260             buffer[0] = (unsigned char*)half_buffer;
261          else if(input_is_float)
262             abort();
263          else
264             buffer[0] = byte_buffer;
265 
266          translate[0]->set_buffer(translate[0], 0, buffer[0], input_format_size, count - 1);
267          translate[0]->run_elts(translate[0], elts, count, 0, 0, buffer[1]);
268          translate[1]->set_buffer(translate[1], 0, buffer[1], output_format_size, count - 1);
269          translate[1]->run_elts(translate[1], elts, count, 0, 0, buffer[2]);
270          translate[0]->set_buffer(translate[0], 0, buffer[2], input_format_size, count - 1);
271          translate[0]->run_elts(translate[0], elts, count, 0, 0, buffer[3]);
272          translate[1]->set_buffer(translate[1], 0, buffer[3], output_format_size, count - 1);
273          translate[1]->run_elts(translate[1], elts, count, 0, 0, buffer[4]);
274 
275          for (i = 0; i < count; ++i)
276          {
277             float a[4];
278             float b[4];
279             fetch_rgba(a, buffer[2] + i * input_format_size, 0, 0);
280             fetch_rgba(b, buffer[4] + i * input_format_size, 0, 0);
281 
282             for (j = 0; j < count; ++j)
283             {
284                float d = a[j] - b[j];
285                if (d > error || d < -error)
286                {
287                   fail = 1;
288                   break;
289                }
290             }
291          }
292 
293          printf("%s%s: %s -> %s -> %s -> %s -> %s\n",
294                fail ? "FAIL" : "PASS",
295                used_generic ? "[GENERIC]" : "",
296                input_format_desc->name, output_format_desc->name, input_format_desc->name, output_format_desc->name, input_format_desc->name);
297 
298          if (1)
299          {
300             for (i = 0; i < ARRAY_SIZE(buffer); ++i)
301             {
302                unsigned format_size = (i & 1) ? output_format_size : input_format_size;
303                printf("%c ", (i == 2 || i == 4) ? '*' : ' ');
304                for (j = 0; j < count; ++j)
305                {
306                   for (k = 0; k < format_size; ++k)
307                   {
308                      printf("%02x", buffer[i][j * format_size + k]);
309                   }
310                   printf(" ");
311                }
312                printf("\n");
313             }
314          }
315 
316          if (!fail)
317             ++passed;
318          ++total;
319 
320          if(translate[1])
321             translate[1]->release(translate[1]);
322          translate[0]->release(translate[0]);
323       }
324    }
325 
326    printf("%u/%u tests passed for translate_%s\n", passed, total, argv[1]);
327    return passed != total;
328 }
329