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