1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/liboil/tsrc/examples/oil-test/src/oil-test.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,317 @@
1.4 +/*
1.5 + * LIBOIL - Library of Optimized Inner Loops
1.6 + * Copyright (c) 2004 David A. Schleef <ds@schleef.org>
1.7 + * All rights reserved.
1.8 + *
1.9 + * Redistribution and use in source and binary forms, with or without
1.10 + * modification, are permitted provided that the following conditions
1.11 + * are met:
1.12 + * 1. Redistributions of source code must retain the above copyright
1.13 + * notice, this list of conditions and the following disclaimer.
1.14 + * 2. Redistributions in binary form must reproduce the above copyright
1.15 + * notice, this list of conditions and the following disclaimer in the
1.16 + * documentation and/or other materials provided with the distribution.
1.17 + *
1.18 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1.19 + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1.20 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1.21 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
1.22 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1.23 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
1.24 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1.25 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1.26 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
1.27 + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
1.28 + * POSSIBILITY OF SUCH DAMAGE.
1.29 + */
1.30 +
1.31 +#ifdef HAVE_CONFIG_H
1.32 +#include "config.h"
1.33 +#endif
1.34 +
1.35 +#include <liboil/liboil.h>
1.36 +#include <liboil/liboilfunction.h>
1.37 +#include <liboil/liboiltest.h>
1.38 +#include <liboil/liboilrandom.h>
1.39 +#include <liboil/liboilcpu.h>
1.40 +#include <string.h>
1.41 +#include <math.h>
1.42 +#include <stdio.h>
1.43 +#include <time.h>
1.44 +#include "std_log_result.h"
1.45 +
1.46 +#include <liboil/globals.h>
1.47 +#ifdef HAVE_INTTYPES_H
1.48 +#include <inttypes.h>
1.49 +#endif
1.50 +
1.51 +#ifndef PRIx8
1.52 +#define PRIx8 "x"
1.53 +#define PRIx16 "x"
1.54 +#define PRIx32 "x"
1.55 +#define PRIx64 "llx"
1.56 +#define PRId8 "d"
1.57 +#define PRId16 "d"
1.58 +#define PRId32 "d"
1.59 +#define PRId64 "lld"
1.60 +#define PRIu8 "u"
1.61 +#define PRIu16 "u"
1.62 +#define PRIu32 "u"
1.63 +#define PRIu64 "llu"
1.64 +#endif
1.65 +
1.66 +#define LOG_FILE "c:\\logs\\examples_oil-test_log.txt"
1.67 +#include "std_log_result.h"
1.68 +#define LOG_FILENAME_LINE __FILE__, __LINE__
1.69 +double new_des[10][10];
1.70 +int ind1=0,j;
1.71 +
1.72 +void create_xml(int result)
1.73 +{
1.74 + if(result)
1.75 + assert_failed = 1;
1.76 +
1.77 + testResultXml("examples_oil-test");
1.78 + close_log_file();
1.79 +}
1.80 +
1.81 +int hex;
1.82 +
1.83 +void register_impls(void);
1.84 +
1.85 +void test(void)
1.86 +{
1.87 + int32_t dest[1];
1.88 + uint8_t src[100];
1.89 + int i;
1.90 +
1.91 + for(i=0;i<100;i++){
1.92 + src[i] = oil_rand_u8() & 0x7f;
1.93 + }
1.94 + dest[0] = 0;
1.95 +
1.96 + oil_utf8_validate (dest, src, 100);
1.97 +
1.98 +#if 0
1.99 + for(i=0;i<100;i++){
1.100 + printf("%d %d\n",dest[i],src[i]);
1.101 + }
1.102 +#endif
1.103 + printf("%d\n", dest[0]);
1.104 +
1.105 +}
1.106 +
1.107 +void
1.108 +dump_array (void *data, void *ref_data, OilType type, int pre_n, int stride,
1.109 + int post_n)
1.110 +{
1.111 + int i, j;
1.112 + int s2 = oil_type_sizeof (type);
1.113 + double x;
1.114 +
1.115 +
1.116 +#define DUMP(type, format) do { \
1.117 + for(i=0;i<post_n;i++){ \
1.118 + printf(" "); \
1.119 + for(j=0;j<pre_n;j++){ \
1.120 + x = fabs(OIL_GET(data, i*stride + j*s2, type) - \
1.121 + OIL_GET(ref_data, i*stride + j*s2, type)); \
1.122 + if (x >= 0.00001) { \
1.123 + std_log(LOG_FILENAME_LINE,"*" format "* ", OIL_GET(data, i*stride + j*s2, type)); \
1.124 + new_des[ind1][i]=OIL_GET(data, i*stride + j*s2, type);\
1.125 + } else { \
1.126 + std_log(LOG_FILENAME_LINE,format " ", OIL_GET(data, i*stride + j*s2, type)); \
1.127 + new_des[ind1][i]=OIL_GET(data, i*stride + j*s2, type);\
1.128 + } \
1.129 + } \
1.130 + printf("\n"); \
1.131 + } \
1.132 +} while(0)
1.133 +
1.134 + if (hex) {
1.135 + switch(type) {
1.136 + case OIL_TYPE_s8p:
1.137 + case OIL_TYPE_u8p:
1.138 + DUMP(uint8_t, "%02" PRIx8);
1.139 + break;
1.140 + case OIL_TYPE_s16p:
1.141 + case OIL_TYPE_u16p:
1.142 + DUMP(uint16_t, "%04" PRIx16);
1.143 + break;
1.144 + case OIL_TYPE_s32p:
1.145 + case OIL_TYPE_u32p:
1.146 + case OIL_TYPE_f32p:
1.147 + DUMP(uint32_t, "%08" PRIx32);
1.148 + break;
1.149 + case OIL_TYPE_s64p:
1.150 + case OIL_TYPE_u64p:
1.151 + case OIL_TYPE_f64p:
1.152 + DUMP(uint64_t, "%016" PRIx64);
1.153 + break;
1.154 + default:
1.155 + break;
1.156 + }
1.157 + } else {
1.158 + switch(type) {
1.159 + case OIL_TYPE_s8p:
1.160 + DUMP(int8_t, "%" PRId8);
1.161 + break;
1.162 + case OIL_TYPE_u8p:
1.163 + DUMP(uint8_t, "%" PRIu8);
1.164 + break;
1.165 + case OIL_TYPE_s16p:
1.166 + DUMP(int16_t, "%" PRId16);
1.167 + break;
1.168 + case OIL_TYPE_u16p:
1.169 + DUMP(uint16_t, "%" PRIu16);
1.170 + break;
1.171 + case OIL_TYPE_s32p:
1.172 + DUMP(int32_t, "%" PRId32);
1.173 + break;
1.174 + case OIL_TYPE_u32p:
1.175 + DUMP(uint32_t, "%" PRIu32);
1.176 + break;
1.177 + case OIL_TYPE_s64p:
1.178 + DUMP(int64_t, "%" PRId64);
1.179 + break;
1.180 + case OIL_TYPE_u64p:
1.181 + DUMP(uint64_t, "%" PRIu64);
1.182 + break;
1.183 + case OIL_TYPE_f32p:
1.184 + DUMP(float, "%g");
1.185 + break;
1.186 + case OIL_TYPE_f64p:
1.187 + DUMP(double, "%g");
1.188 + break;
1.189 + default:
1.190 + break;
1.191 + }
1.192 + }
1.193 +}
1.194 +
1.195 +void
1.196 +dump_test (OilTest *test)
1.197 +{
1.198 + int i;
1.199 + for(i=0;i<OIL_ARG_LAST;i++){
1.200 + OilParameter *p = &test->params[i];
1.201 + if (p->is_pointer) {
1.202 + if (p->direction == 'i' || p->direction == 'd') {
1.203 + std_log(LOG_FILENAME_LINE," %s:\n", p->parameter_name);
1.204 + dump_array (p->test_data + OIL_TEST_HEADER,
1.205 + p->ref_data + OIL_TEST_HEADER,
1.206 + p->type, p->pre_n, p->stride, p->post_n);
1.207 + }
1.208 + }
1.209 + }
1.210 +}
1.211 +
1.212 +void
1.213 +dump_source (OilTest *test)
1.214 +{
1.215 + int i;
1.216 + for(i=0;i<OIL_ARG_LAST;i++){
1.217 + OilParameter *p = &test->params[i];
1.218 + if (p->is_pointer) {
1.219 + if (p->direction == 'i' || p->direction == 's') {
1.220 + std_log(LOG_FILENAME_LINE," %s:\n", p->parameter_name);
1.221 + dump_array (p->src_data + OIL_TEST_HEADER,
1.222 + p->src_data + OIL_TEST_HEADER,
1.223 + p->type, p->pre_n, p->stride, p->post_n);
1.224 + }
1.225 + }
1.226 + }
1.227 +}
1.228 +
1.229 +void
1.230 +help (void)
1.231 +{
1.232 +std_log(LOG_FILENAME_LINE,"oil-test [-x] <class_name>\n");
1.233 + //exit(0);
1.234 +}
1.235 +
1.236 +int main (int argc, char *argv[])
1.237 +{
1.238 + OilFunctionClass *klass;
1.239 + OilFunctionImpl *impl;
1.240 + OilTest *test;
1.241 + double ave, std;
1.242 + char *class_name = NULL;
1.243 + int i;
1.244 + int n = 10;
1.245 +
1.246 + srand(time(NULL));
1.247 + oil_init ();
1.248 +
1.249 + for (i=1;i<argc;i++){
1.250 + if (!strcmp(argv[i],"-x")) {
1.251 + hex = 1;
1.252 + } else if (!strcmp(argv[i],"-n")) {
1.253 + if (i + 1 < argc) {
1.254 + n = strtol (argv[i+1], NULL, 0);
1.255 + i++;
1.256 + }
1.257 + } else {
1.258 + if (class_name != NULL) {
1.259 + help();
1.260 + }
1.261 + class_name = argv[i];
1.262 + }
1.263 + }
1.264 + if (class_name == NULL) {
1.265 + help();
1.266 + }
1.267 +
1.268 + klass = oil_class_get (class_name);
1.269 + if (klass == NULL) {
1.270 + std_log(LOG_FILENAME_LINE,"class not found: %s\n", class_name);
1.271 + exit(0);
1.272 + }
1.273 + oil_class_optimize (klass);
1.274 +
1.275 + test = oil_test_new(klass);
1.276 + oil_test_set_iterations(test, 1);
1.277 + test->n = n;
1.278 + test->m = n;
1.279 +
1.280 + impl = klass->reference_impl;
1.281 + ave = impl->profile_ave;
1.282 + std = impl->profile_std;
1.283 + oil_test_check_impl (test, impl);
1.284 + std_log(LOG_FILENAME_LINE,"source array\n");
1.285 + dump_source(test);
1.286 + std_log(LOG_FILENAME_LINE,"reference impl %s\n", impl->name);
1.287 + std_log(LOG_FILENAME_LINE," ave=%g std=%g\n", ave, std);
1.288 + std_log(LOG_FILENAME_LINE," (this test) ave=%g std=%g\n", test->profile_ave, test->profile_std);
1.289 + dump_test(test);
1.290 +
1.291 + for (impl = klass->first_impl; impl; impl = impl->next) {
1.292 + if (impl == klass->reference_impl) continue;
1.293 + std_log(LOG_FILENAME_LINE,"impl %s\n", impl->name);
1.294 + ind1++;
1.295 + if (oil_impl_is_runnable (impl)) {
1.296 + std_log(LOG_FILENAME_LINE," ave=%g std=%g\n", impl->profile_ave, impl->profile_std);
1.297 + oil_test_check_impl (test, impl);
1.298 + std_log(LOG_FILENAME_LINE," (this test) ave=%g std=%g\n", test->profile_ave, test->profile_std);
1.299 + std_log(LOG_FILENAME_LINE," abs diff=%g\n", test->sum_abs_diff);
1.300 + dump_test(test);
1.301 + }
1.302 + }
1.303 + for(i=0;i<10;i++)
1.304 + {
1.305 + for(j=1;j<ind1;j++)
1.306 + {
1.307 + if(new_des[0][i]!=new_des[j][i])
1.308 + {
1.309 + assert_failed=1;
1.310 + }
1.311 + }
1.312 + }
1.313 + if(assert_failed)
1.314 + std_log(LOG_FILENAME_LINE,"Test Fail");
1.315 + else
1.316 + std_log(LOG_FILENAME_LINE,"Test Successful");
1.317 + create_xml(0);
1.318 + return 0;
1.319 +}
1.320 +