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