os/ossrv/genericopenlibs/liboil/tsrc/examples/oil-test/src/oil-test.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2  * LIBOIL - Library of Optimized Inner Loops
     3  * Copyright (c) 2004 David A. Schleef <ds@schleef.org>
     4  * All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions
     8  * are met:
     9  * 1. Redistributions of source code must retain the above copyright
    10  *    notice, this list of conditions and the following disclaimer.
    11  * 2. Redistributions in binary form must reproduce the above copyright
    12  *    notice, this list of conditions and the following disclaimer in the
    13  *    documentation and/or other materials provided with the distribution.
    14  * 
    15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
    19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    23  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
    24  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    25  * POSSIBILITY OF SUCH DAMAGE.
    26  */
    27 
    28 #ifdef HAVE_CONFIG_H
    29 #include "config.h"
    30 #endif
    31 
    32 #include <liboil/liboil.h>
    33 #include <liboil/liboilfunction.h>
    34 #include <liboil/liboiltest.h>
    35 #include <liboil/liboilrandom.h>
    36 #include <liboil/liboilcpu.h>
    37 #include <string.h>
    38 #include <math.h>
    39 #include <stdio.h>
    40 #include <time.h>
    41 #include "std_log_result.h"
    42 
    43 #include <liboil/globals.h>
    44 #ifdef HAVE_INTTYPES_H
    45 #include <inttypes.h>
    46 #endif
    47 
    48 #ifndef PRIx8
    49 #define PRIx8  "x"
    50 #define PRIx16 "x"
    51 #define PRIx32 "x"
    52 #define PRIx64 "llx"
    53 #define PRId8  "d"
    54 #define PRId16 "d"
    55 #define PRId32 "d"
    56 #define PRId64 "lld"
    57 #define PRIu8  "u"
    58 #define PRIu16 "u"
    59 #define PRIu32 "u"
    60 #define PRIu64 "llu"
    61 #endif
    62 
    63 #define LOG_FILE "c:\\logs\\examples_oil-test_log.txt"
    64 #include "std_log_result.h"
    65 #define LOG_FILENAME_LINE __FILE__, __LINE__
    66 double new_des[10][10];
    67 int ind1=0,j;
    68 
    69 void create_xml(int result)
    70 {
    71     if(result)
    72         assert_failed = 1;
    73     
    74     testResultXml("examples_oil-test");
    75     close_log_file();
    76 }
    77 
    78 int hex;
    79 
    80 void register_impls(void);
    81 
    82 void test(void)
    83 {
    84   int32_t dest[1];
    85   uint8_t src[100];
    86   int i;
    87 
    88   for(i=0;i<100;i++){
    89     src[i] = oil_rand_u8() & 0x7f;
    90   }
    91   dest[0] = 0;
    92 
    93   oil_utf8_validate (dest, src, 100);
    94 
    95 #if 0
    96   for(i=0;i<100;i++){
    97     printf("%d %d\n",dest[i],src[i]);
    98   }
    99 #endif
   100   printf("%d\n", dest[0]);
   101 
   102 }
   103 
   104 void
   105 dump_array (void *data, void *ref_data, OilType type, int pre_n, int stride,
   106     int post_n)
   107 {
   108   int i, j;
   109   int s2 = oil_type_sizeof (type);
   110   double x;
   111 
   112 
   113 #define DUMP(type, format) do { \
   114   for(i=0;i<post_n;i++){ \
   115     printf("    "); \
   116     for(j=0;j<pre_n;j++){ \
   117       x = fabs(OIL_GET(data, i*stride + j*s2, type) - \
   118           OIL_GET(ref_data, i*stride + j*s2, type)); \
   119       if (x >= 0.00001) { \
   120       std_log(LOG_FILENAME_LINE,"*" format "* ", OIL_GET(data, i*stride + j*s2, type)); \
   121       new_des[ind1][i]=OIL_GET(data, i*stride + j*s2, type);\
   122       } else { \
   123       std_log(LOG_FILENAME_LINE,format " ", OIL_GET(data, i*stride + j*s2, type)); \
   124       new_des[ind1][i]=OIL_GET(data, i*stride + j*s2, type);\
   125       } \
   126     } \
   127     printf("\n"); \
   128   } \
   129 } while(0)
   130 
   131   if (hex) {
   132     switch(type) {
   133       case OIL_TYPE_s8p:
   134       case OIL_TYPE_u8p:
   135         DUMP(uint8_t, "%02" PRIx8);
   136         break;
   137       case OIL_TYPE_s16p:
   138       case OIL_TYPE_u16p:
   139         DUMP(uint16_t, "%04" PRIx16);
   140         break;
   141       case OIL_TYPE_s32p:
   142       case OIL_TYPE_u32p:
   143       case OIL_TYPE_f32p:
   144         DUMP(uint32_t, "%08" PRIx32);
   145         break;
   146       case OIL_TYPE_s64p:
   147       case OIL_TYPE_u64p:
   148       case OIL_TYPE_f64p:
   149         DUMP(uint64_t, "%016" PRIx64);
   150         break;
   151       default:
   152         break;
   153     }
   154   } else {
   155     switch(type) {
   156       case OIL_TYPE_s8p:
   157         DUMP(int8_t, "%" PRId8);
   158         break;
   159       case OIL_TYPE_u8p:
   160         DUMP(uint8_t, "%" PRIu8);
   161         break;
   162       case OIL_TYPE_s16p:
   163         DUMP(int16_t, "%" PRId16);
   164         break;
   165       case OIL_TYPE_u16p:
   166         DUMP(uint16_t, "%" PRIu16);
   167         break;
   168       case OIL_TYPE_s32p:
   169         DUMP(int32_t, "%" PRId32);
   170         break;
   171       case OIL_TYPE_u32p:
   172         DUMP(uint32_t, "%" PRIu32);
   173         break;
   174       case OIL_TYPE_s64p:
   175         DUMP(int64_t, "%" PRId64);
   176         break;
   177       case OIL_TYPE_u64p:
   178         DUMP(uint64_t, "%" PRIu64);
   179         break;
   180       case OIL_TYPE_f32p:
   181         DUMP(float, "%g");
   182         break;
   183       case OIL_TYPE_f64p:
   184         DUMP(double, "%g");
   185         break;
   186       default:
   187         break;
   188     }
   189   }
   190 }
   191 
   192 void
   193 dump_test (OilTest *test)
   194 {
   195   int i;
   196   for(i=0;i<OIL_ARG_LAST;i++){
   197     OilParameter *p = &test->params[i];
   198     if (p->is_pointer) {
   199       if (p->direction == 'i' || p->direction == 'd') {
   200       std_log(LOG_FILENAME_LINE,"  %s:\n", p->parameter_name);
   201         dump_array (p->test_data + OIL_TEST_HEADER,
   202             p->ref_data + OIL_TEST_HEADER,
   203             p->type, p->pre_n, p->stride, p->post_n);
   204       }
   205     }
   206   }
   207 }
   208 
   209 void
   210 dump_source (OilTest *test)
   211 {
   212   int i;
   213   for(i=0;i<OIL_ARG_LAST;i++){
   214     OilParameter *p = &test->params[i];
   215     if (p->is_pointer) {
   216       if (p->direction == 'i' || p->direction == 's') {
   217       std_log(LOG_FILENAME_LINE,"  %s:\n", p->parameter_name);
   218         dump_array (p->src_data + OIL_TEST_HEADER,
   219             p->src_data + OIL_TEST_HEADER,
   220             p->type, p->pre_n, p->stride, p->post_n);
   221       }
   222     }
   223   }
   224 }
   225 
   226 void
   227 help (void)
   228 {
   229 std_log(LOG_FILENAME_LINE,"oil-test [-x] <class_name>\n");
   230   //exit(0);
   231 }
   232 
   233 int main (int argc, char *argv[])
   234 {
   235   OilFunctionClass *klass;
   236   OilFunctionImpl *impl;
   237   OilTest *test;
   238   double ave, std;
   239   char *class_name = NULL;
   240   int i;
   241   int n = 10;
   242 
   243   srand(time(NULL));
   244   oil_init ();
   245 
   246   for (i=1;i<argc;i++){
   247     if (!strcmp(argv[i],"-x")) {
   248       hex = 1;
   249     } else if (!strcmp(argv[i],"-n")) {
   250       if (i + 1 < argc) {
   251         n = strtol (argv[i+1], NULL, 0);
   252         i++;
   253       }
   254     } else {
   255       if (class_name != NULL) {
   256         help();
   257       }
   258       class_name = argv[i];
   259     }
   260   }
   261   if (class_name == NULL) {
   262     help();
   263   }
   264 
   265   klass = oil_class_get (class_name);
   266   if (klass == NULL) {
   267   std_log(LOG_FILENAME_LINE,"class not found: %s\n", class_name);
   268     exit(0);
   269   }
   270   oil_class_optimize (klass);
   271 
   272   test = oil_test_new(klass);
   273   oil_test_set_iterations(test, 1);
   274   test->n = n;
   275   test->m = n;
   276 
   277   impl = klass->reference_impl;
   278   ave = impl->profile_ave;
   279   std = impl->profile_std;
   280   oil_test_check_impl (test, impl);
   281   std_log(LOG_FILENAME_LINE,"source array\n");
   282   dump_source(test);
   283   std_log(LOG_FILENAME_LINE,"reference impl %s\n", impl->name);
   284   std_log(LOG_FILENAME_LINE,"  ave=%g std=%g\n", ave, std);
   285   std_log(LOG_FILENAME_LINE,"  (this test) ave=%g std=%g\n", test->profile_ave, test->profile_std);
   286   dump_test(test);
   287 
   288   for (impl = klass->first_impl; impl; impl = impl->next) {
   289     if (impl == klass->reference_impl) continue;
   290     std_log(LOG_FILENAME_LINE,"impl %s\n", impl->name);
   291     ind1++;
   292     if (oil_impl_is_runnable (impl)) {
   293     std_log(LOG_FILENAME_LINE,"  ave=%g std=%g\n", impl->profile_ave, impl->profile_std);
   294       oil_test_check_impl (test, impl);
   295       std_log(LOG_FILENAME_LINE,"  (this test) ave=%g std=%g\n", test->profile_ave, test->profile_std);
   296       std_log(LOG_FILENAME_LINE,"  abs diff=%g\n", test->sum_abs_diff);
   297       dump_test(test);
   298     }
   299   }
   300   for(i=0;i<10;i++)
   301       {
   302       for(j=1;j<ind1;j++)
   303           {
   304           if(new_des[0][i]!=new_des[j][i])
   305               {
   306               assert_failed=1;
   307               }
   308           }
   309       }
   310   if(assert_failed)
   311       std_log(LOG_FILENAME_LINE,"Test Fail");
   312   else
   313       std_log(LOG_FILENAME_LINE,"Test Successful");
   314   create_xml(0);
   315   return 0;
   316 }
   317