2 #define ORC_ENABLE_UNSTABLE_API
5 #include <orc-test/orctest.h>
6 #include <orc-test/orcarray.h>
7 #include <orc/orcparse.h>
15 #define isnan(x) _isnan(x)
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);
23 void show (OrcProgram *p);
34 int format = FORMAT_SIGNED;
38 main (int argc, char *argv[])
43 OrcProgram **programs;
44 const char *filename = NULL;
50 if (strcmp("-x", argv[i]) == 0) {
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) {
60 array_n = strtol (argv[i+1], NULL, 0);
68 if (filename == NULL) {
69 filename = getenv ("testfile");
71 if (filename == NULL) {
72 filename = "test.orc";
74 code = read_file (filename);
76 n = orc_parse (code, &programs);
78 OrcStaticOpcode *opcode;
80 opcode = orc_opcode_find_by_name (filename);
82 programs = malloc(sizeof(void *));
83 programs[0] = orc_test_get_program_for_opcode (opcode);
86 printf("show_parse [-fsux] (<file.orc>|opcode)\n");
101 read_file (const char *filename)
104 char *contents = NULL;
108 file = fopen (filename, "r");
109 if (file == NULL) return NULL;
111 ret = fseek (file, 0, SEEK_END);
112 if (ret < 0) goto bail;
115 if (size < 0) goto bail;
117 ret = fseek (file, 0, SEEK_SET);
118 if (ret < 0) goto bail;
120 contents = malloc (size + 1);
121 if (contents == NULL) goto bail;
123 ret = fread (contents, size, 1, file);
124 if (ret < 0) goto bail;
130 /* something failed */
131 if (file) fclose (file);
132 if (contents) free (contents);
138 print_array_val_signed (OrcArray *array, int i, int j)
140 void *ptr = ORC_PTR_OFFSET (array->data,
141 i*array->element_size + j*array->stride);
143 switch (array->element_size) {
145 printf(" %4d", *(orc_int8 *)ptr);
146 return *(orc_int8 *)ptr;
148 printf(" %5d", *(orc_int16 *)ptr);
149 return *(orc_int16 *)ptr;
151 printf(" %10d", *(orc_int32 *)ptr);
152 return *(orc_int32 *)ptr;
154 printf(" 0x%08x%08x", (orc_uint32)((*(orc_uint64 *)ptr)>>32),
155 (orc_uint32)((*(orc_uint64 *)ptr)));
156 return *(orc_int64 *)ptr;
163 print_array_val_unsigned (OrcArray *array, int i, int j)
165 void *ptr = ORC_PTR_OFFSET (array->data,
166 i*array->element_size + j*array->stride);
168 switch (array->element_size) {
170 printf(" %4u", *(orc_uint8 *)ptr);
171 return *(orc_int8 *)ptr;
173 printf(" %5u", *(orc_uint16 *)ptr);
174 return *(orc_int16 *)ptr;
176 printf(" %10u", *(orc_uint32 *)ptr);
177 return *(orc_int32 *)ptr;
179 printf(" 0x%08x%08x", (orc_uint32)((*(orc_uint64 *)ptr)>>32),
180 (orc_uint32)((*(orc_uint64 *)ptr)));
181 return *(orc_int64 *)ptr;
188 print_array_val_hex (OrcArray *array, int i, int j)
190 void *ptr = ORC_PTR_OFFSET (array->data,
191 i*array->element_size + j*array->stride);
193 switch (array->element_size) {
195 printf(" %02x", *(orc_uint8 *)ptr);
196 return *(orc_int8 *)ptr;
198 printf(" %04x", *(orc_uint16 *)ptr);
199 return *(orc_int16 *)ptr;
201 printf(" %08x", *(orc_uint32 *)ptr);
202 return *(orc_int32 *)ptr;
204 printf(" 0x%08x%08x", (orc_uint32)((*(orc_uint64 *)ptr)>>32),
205 (orc_uint32)((*(orc_uint64 *)ptr)));
206 return *(orc_int64 *)ptr;
213 print_array_val_float (OrcArray *array, int i, int j)
215 void *ptr = ORC_PTR_OFFSET (array->data,
216 i*array->element_size + j*array->stride);
218 switch (array->element_size) {
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;
225 printf(" %12.5g", *(float *)ptr);
226 return *(orc_int32 *)ptr;
229 printf(" %12.5g", *(double *)ptr);
230 return *(orc_int64 *)ptr;
239 show (OrcProgram *program)
241 OrcCompileResult result;
243 const char *target_name;
244 unsigned int target_flags;
247 OrcArray *dest[4] = { NULL, NULL, NULL, NULL };
248 OrcArray *src[8] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
250 OrcRandomContext rand_context = { 0 };
254 target = orc_target_get_by_name (target_name);
256 target_flags = orc_target_get_default_flags (target);
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);
264 printf("%s:\n", program->name);
266 if (program->constant_n > 0) {
267 n = program->constant_n;
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;
283 orc_executor_set_m (ex, m);
285 for(i=0;i<ORC_N_VARIABLES;i++){
286 if (program->vars[i].name == NULL) continue;
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);
299 case ORC_PARAM_TYPE_FLOAT:
300 orc_executor_set_param_float (ex, i, 2.0);
302 case ORC_PARAM_TYPE_INT64:
303 orc_executor_set_param_int64 (ex, i, 2);
305 case ORC_PARAM_TYPE_DOUBLE:
306 orc_executor_set_param_double (ex, i, 2.0);
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);
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);
327 orc_executor_run (ex);
336 printf("%2d %2d:", i, j);
338 for(l=ORC_VAR_S1;l<ORC_VAR_S1+8;l++){
339 if (program->vars[l].size > 0) {
342 print_array_val_float (src[l-ORC_VAR_S1], i, j);
345 print_array_val_hex (src[l-ORC_VAR_S1], i, j);
348 print_array_val_signed (src[l-ORC_VAR_S1], i, j);
350 case FORMAT_UNSIGNED:
351 print_array_val_unsigned (src[l-ORC_VAR_S1], i, j);
358 for(l=ORC_VAR_D1;l<ORC_VAR_D1+4;l++){
359 if (program->vars[l].size > 0) {
362 print_array_val_float (dest[l-ORC_VAR_D1], i, j);
365 print_array_val_hex (dest[l-ORC_VAR_D1], i, j);
368 print_array_val_signed (dest[l-ORC_VAR_D1], i, j);
370 case FORMAT_UNSIGNED:
371 print_array_val_unsigned (dest[l-ORC_VAR_D1], i, j);
385 if (dest[i]) orc_array_free (dest[i]);
388 if (src[i]) orc_array_free (src[i]);
391 orc_executor_free (ex);