Initial commit to Gerrit
[profile/ivi/orc.git] / testsuite / show_parse.c
1
2 #define ORC_ENABLE_UNSTABLE_API
3
4 #include <orc/orc.h>
5 #include <orc-test/orctest.h>
6 #include <orc-test/orcarray.h>
7 #include <orc/orcparse.h>
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <math.h>
12 #include <string.h>
13
14 #ifdef _MSC_VER
15 #define isnan(x) _isnan(x)
16 #endif
17
18 static char * read_file (const char *filename);
19 void output_code (OrcProgram *p, FILE *output);
20 void output_code_header (OrcProgram *p, FILE *output);
21 void output_code_test (OrcProgram *p, FILE *output);
22
23 void show (OrcProgram *p);
24
25 int error = FALSE;
26
27 enum {
28   FORMAT_SIGNED,
29   FORMAT_UNSIGNED,
30   FORMAT_HEX,
31   FORMAT_FLOAT
32 };
33
34 int format = FORMAT_SIGNED;
35 int array_n = 10;
36
37 int
38 main (int argc, char *argv[])
39 {
40   char *code;
41   int n = 0;
42   int i;
43   OrcProgram **programs;
44   const char *filename = NULL;
45
46   orc_init ();
47   orc_test_init ();
48
49   for(i=1;i<argc;i++){
50     if (strcmp("-x", argv[i]) == 0) {
51       format = FORMAT_HEX;
52     } else if (strcmp("-s", argv[i]) == 0) {
53       format = FORMAT_SIGNED;
54     } else if (strcmp("-u", argv[i]) == 0) {
55       format = FORMAT_UNSIGNED;
56     } else if (strcmp("-f", argv[i]) == 0) {
57       format = FORMAT_FLOAT;
58     } else if (strcmp("-n", argv[i]) == 0) {
59       if (i + 1 < argc) {
60         array_n = strtol (argv[i+1], NULL, 0);
61         i++;
62       }
63     } else {
64       filename = argv[i];
65     }
66   }
67
68   if (filename == NULL) {
69     filename = getenv ("testfile");
70   }
71   if (filename == NULL) {
72     filename = "test.orc";
73   }
74   code = read_file (filename);
75   if (code) {
76     n = orc_parse (code, &programs);
77   } else {
78     OrcStaticOpcode *opcode;
79
80     opcode = orc_opcode_find_by_name (filename);
81     if (opcode) {
82       programs = malloc(sizeof(void *));
83       programs[0] = orc_test_get_program_for_opcode (opcode);
84       n = 1;
85     } else {
86       printf("show_parse [-fsux] (<file.orc>|opcode)\n");
87       exit(1);
88     }
89   }
90
91   for(i=0;i<n;i++){
92     show (programs[i]);
93   }
94
95   if (error) return 1;
96   return 0;
97 }
98
99
100 static char *
101 read_file (const char *filename)
102 {
103   FILE *file = NULL;
104   char *contents = NULL;
105   long size;
106   int ret;
107
108   file = fopen (filename, "r");
109   if (file == NULL) return NULL;
110
111   ret = fseek (file, 0, SEEK_END);
112   if (ret < 0) goto bail;
113
114   size = ftell (file);
115   if (size < 0) goto bail;
116
117   ret = fseek (file, 0, SEEK_SET);
118   if (ret < 0) goto bail;
119
120   contents = malloc (size + 1);
121   if (contents == NULL) goto bail;
122
123   ret = fread (contents, size, 1, file);
124   if (ret < 0) goto bail;
125
126   contents[size] = 0;
127
128   return contents;
129 bail:
130   /* something failed */
131   if (file) fclose (file);
132   if (contents) free (contents);
133
134   return NULL;
135 }
136
137 int
138 print_array_val_signed (OrcArray *array, int i, int j)
139 {
140   void *ptr = ORC_PTR_OFFSET (array->data,
141       i*array->element_size + j*array->stride);
142
143   switch (array->element_size) {
144     case 1:
145       printf(" %4d", *(orc_int8 *)ptr);
146       return *(orc_int8 *)ptr;
147     case 2:
148       printf(" %5d", *(orc_int16 *)ptr);
149       return *(orc_int16 *)ptr;
150     case 4:
151       printf(" %10d", *(orc_int32 *)ptr);
152       return *(orc_int32 *)ptr;
153     case 8:
154       printf(" 0x%08x%08x", (orc_uint32)((*(orc_uint64 *)ptr)>>32),
155           (orc_uint32)((*(orc_uint64 *)ptr)));
156       return *(orc_int64 *)ptr;
157     default:
158       return -1;
159   }
160 }
161
162 int
163 print_array_val_unsigned (OrcArray *array, int i, int j)
164 {
165   void *ptr = ORC_PTR_OFFSET (array->data,
166       i*array->element_size + j*array->stride);
167
168   switch (array->element_size) {
169     case 1:
170       printf(" %4u", *(orc_uint8 *)ptr);
171       return *(orc_int8 *)ptr;
172     case 2:
173       printf(" %5u", *(orc_uint16 *)ptr);
174       return *(orc_int16 *)ptr;
175     case 4:
176       printf(" %10u", *(orc_uint32 *)ptr);
177       return *(orc_int32 *)ptr;
178     case 8:
179       printf(" 0x%08x%08x", (orc_uint32)((*(orc_uint64 *)ptr)>>32),
180           (orc_uint32)((*(orc_uint64 *)ptr)));
181       return *(orc_int64 *)ptr;
182     default:
183       return -1;
184   }
185 }
186
187 int
188 print_array_val_hex (OrcArray *array, int i, int j)
189 {
190   void *ptr = ORC_PTR_OFFSET (array->data,
191       i*array->element_size + j*array->stride);
192
193   switch (array->element_size) {
194     case 1:
195       printf(" %02x", *(orc_uint8 *)ptr);
196       return *(orc_int8 *)ptr;
197     case 2:
198       printf(" %04x", *(orc_uint16 *)ptr);
199       return *(orc_int16 *)ptr;
200     case 4:
201       printf(" %08x", *(orc_uint32 *)ptr);
202       return *(orc_int32 *)ptr;
203     case 8:
204       printf(" 0x%08x%08x", (orc_uint32)((*(orc_uint64 *)ptr)>>32),
205           (orc_uint32)((*(orc_uint64 *)ptr)));
206       return *(orc_int64 *)ptr;
207     default:
208       return -1;
209   }
210 }
211
212 int
213 print_array_val_float (OrcArray *array, int i, int j)
214 {
215   void *ptr = ORC_PTR_OFFSET (array->data,
216       i*array->element_size + j*array->stride);
217
218   switch (array->element_size) {
219     case 4:
220       if (isnan(*(float *)ptr)) {
221         printf(" nan %08x", *(orc_uint32 *)ptr);
222         /* This is to get around signaling/non-signaling nans in the output */
223         return (*(orc_uint32 *)ptr) & 0xffbfffff;
224       } else {
225         printf(" %12.5g", *(float *)ptr);
226         return *(orc_int32 *)ptr;
227       }
228     case 8:
229       printf(" %12.5g", *(double *)ptr);
230       return *(orc_int64 *)ptr;
231     default:
232       printf(" ERROR");
233       return -1;
234   }
235 }
236
237
238 void
239 show (OrcProgram *program)
240 {
241   OrcCompileResult result;
242   OrcTarget *target;
243   const char *target_name;
244   unsigned int target_flags;
245   int n, m;
246   OrcExecutor *ex;
247   OrcArray *dest[4] = { NULL, NULL, NULL, NULL };
248   OrcArray *src[8] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
249   int i,j;
250   OrcRandomContext rand_context = { 0 };
251
252
253   target_name = NULL;
254   target = orc_target_get_by_name (target_name);
255
256   target_flags = orc_target_get_default_flags (target);
257
258   result = orc_program_compile_full (program, target, target_flags);
259   if (!ORC_COMPILE_RESULT_IS_SUCCESSFUL(result)) {
260     printf("%s: compile failed\n", program->name);
261     return;
262   }
263
264   printf("%s:\n", program->name);
265
266   if (program->constant_n > 0) {
267     n = program->constant_n;
268   } else {
269     n = array_n;
270   }
271
272   ex = orc_executor_new (program);
273   orc_executor_set_n (ex, n);
274   if (program->is_2d) {
275     if (program->constant_m > 0) {
276       m = program->constant_m;
277     } else {
278       m = 2;
279     }
280   } else {
281     m = 1;
282   }
283   orc_executor_set_m (ex, m);
284
285   for(i=0;i<ORC_N_VARIABLES;i++){
286     if (program->vars[i].name == NULL) continue;
287
288     if (program->vars[i].vartype == ORC_VAR_TYPE_SRC) {
289       src[i-ORC_VAR_S1] = orc_array_new (n, m, program->vars[i].size, 0);
290       orc_array_set_random (src[i-ORC_VAR_S1], &rand_context);
291     } else if (program->vars[i].vartype == ORC_VAR_TYPE_DEST) {
292       dest[i-ORC_VAR_D1] = orc_array_new (n, m, program->vars[i].size, 0);
293       orc_array_set_pattern (dest[i], ORC_OOB_VALUE);
294     } else if (program->vars[i].vartype == ORC_VAR_TYPE_PARAM) {
295       switch (program->vars[i].param_type) {
296         case ORC_PARAM_TYPE_INT:
297           orc_executor_set_param (ex, i, 2);
298           break;
299         case ORC_PARAM_TYPE_FLOAT:
300           orc_executor_set_param_float (ex, i, 2.0);
301           break;
302         case ORC_PARAM_TYPE_INT64:
303           orc_executor_set_param_int64 (ex, i, 2);
304           break;
305         case ORC_PARAM_TYPE_DOUBLE:
306           orc_executor_set_param_double (ex, i, 2.0);
307           break;
308         default:
309           ORC_ASSERT(0);
310       }
311     }
312   }
313
314   orc_executor_set_n (ex, n);
315   orc_executor_set_m (ex, m);
316   for(j=0;j<ORC_N_VARIABLES;j++){
317     if (program->vars[j].vartype == ORC_VAR_TYPE_DEST) {
318       orc_executor_set_array (ex, j, dest[j-ORC_VAR_D1]->data);
319       orc_executor_set_stride (ex, j, dest[j-ORC_VAR_D1]->stride);
320     }
321     if (program->vars[j].vartype == ORC_VAR_TYPE_SRC) {
322       orc_executor_set_array (ex, j, src[j-ORC_VAR_S1]->data);
323       orc_executor_set_stride (ex, j, src[j-ORC_VAR_S1]->stride);
324     }
325   }
326
327   orc_executor_run (ex);
328
329   {
330     int i,j;
331
332     for(j=0;j<m;j++){
333       for(i=0;i<n;i++){
334         int l;
335
336         printf("%2d %2d:", i, j);
337
338         for(l=ORC_VAR_S1;l<ORC_VAR_S1+8;l++){
339           if (program->vars[l].size > 0) {
340             switch (format) {
341               case FORMAT_FLOAT:
342                 print_array_val_float (src[l-ORC_VAR_S1], i, j);
343                 break;
344               case FORMAT_HEX:
345                 print_array_val_hex (src[l-ORC_VAR_S1], i, j);
346                 break;
347               case FORMAT_SIGNED:
348                 print_array_val_signed (src[l-ORC_VAR_S1], i, j);
349                 break;
350               case FORMAT_UNSIGNED:
351                 print_array_val_unsigned (src[l-ORC_VAR_S1], i, j);
352                 break;
353             }
354           }
355         }
356
357         printf(" ->");
358         for(l=ORC_VAR_D1;l<ORC_VAR_D1+4;l++){
359           if (program->vars[l].size > 0) {
360             switch (format) {
361               case FORMAT_FLOAT:
362                 print_array_val_float (dest[l-ORC_VAR_D1], i, j);
363                 break;
364               case FORMAT_HEX:
365                 print_array_val_hex (dest[l-ORC_VAR_D1], i, j);
366                 break;
367               case FORMAT_SIGNED:
368                 print_array_val_signed (dest[l-ORC_VAR_D1], i, j);
369                 break;
370               case FORMAT_UNSIGNED:
371                 print_array_val_unsigned (dest[l-ORC_VAR_D1], i, j);
372                 break;
373             }
374           }
375         }
376
377         printf("\n");
378       }
379     }
380   }
381
382
383
384   for(i=0;i<4;i++){
385     if (dest[i]) orc_array_free (dest[i]);
386   }
387   for(i=0;i<8;i++){
388     if (src[i]) orc_array_free (src[i]);
389   }
390
391   orc_executor_free (ex);
392
393 }
394