1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/liboil/tsrc/testsuite/align/src/align.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,357 @@
1.4 +/*
1.5 + * Copyright (c) 2004 David A. Schleef <ds@schleef.org>
1.6 + * Copyright (c) 2005 Eric Anholt <anholt@FreeBSD.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 +/* Tests math functions against the reference, failing if they differ by more
1.32 + * than some epsilon, and printing the difference.
1.33 + */
1.34 +#ifdef HAVE_CONFIG_H
1.35 +#include "config.h"
1.36 +#endif
1.37 +
1.38 +#include <stdio.h>
1.39 +#include <liboil/liboil.h>
1.40 +#include <ctype.h>
1.41 +#include <stdlib.h>
1.42 +#include <string.h>
1.43 +#include <math.h>
1.44 +#ifdef HAVE_INTTYPES_H
1.45 +#include <inttypes.h>
1.46 +#endif
1.47 +
1.48 +#include <liboil/liboilprototype.h>
1.49 +#include <liboil/liboiltest.h>
1.50 +#include <liboil/liboilcpu.h>
1.51 +#include <liboil/liboilparameter.h>
1.52 +
1.53 +#define LOG_FILE "c:\\logs\\testsuite_align_log.txt"
1.54 +#include "std_log_result.h"
1.55 +#define LOG_FILENAME_LINE __FILE__, __LINE__
1.56 +
1.57 +void create_xml(int result)
1.58 +{
1.59 + if(result)
1.60 + assert_failed = 1;
1.61 +
1.62 + testResultXml("testsuite_align");
1.63 + close_log_file();
1.64 +}
1.65 +
1.66 +#ifndef PRIx8
1.67 +#define PRIx8 "x"
1.68 +#define PRIx16 "x"
1.69 +#define PRIx32 "x"
1.70 +#define PRIx64 "llx"
1.71 +#define PRId8 "d"
1.72 +#define PRId16 "d"
1.73 +#define PRId32 "d"
1.74 +#define PRId64 "lld"
1.75 +#define PRIu8 "u"
1.76 +#define PRIu16 "u"
1.77 +#define PRIu32 "u"
1.78 +#define PRIu64 "llu"
1.79 +#endif
1.80 +
1.81 +
1.82 +/* Amount by which results of different types are allowed to deviate from the
1.83 + * reference.
1.84 + */
1.85 +#define INT_EPSILON 1
1.86 +#define FLOAT_EPSILON 0.0001
1.87 +
1.88 +void
1.89 +dump_array (void *data, void *ref_data, OilType type, int pre_n, int stride,
1.90 + int post_n)
1.91 +{
1.92 + int i, j;
1.93 + int s2 = oil_type_sizeof (type);
1.94 + double x;
1.95 +
1.96 +#define DUMP(type, format, int) do { \
1.97 + for(i=0;i<post_n;i++){ \
1.98 + float epsilon = (int) ? INT_EPSILON : FLOAT_EPSILON; \
1.99 + std_log(LOG_FILENAME_LINE," "); \
1.100 + for(j=0;j<pre_n;j++){ \
1.101 + x = fabs(OIL_GET(data, i*stride + j*s2, type) - \
1.102 + OIL_GET(ref_data, i*stride + j*s2, type)); \
1.103 + if (x > epsilon) { \
1.104 + std_log(LOG_FILENAME_LINE,"*" format "* (" format ") ", \
1.105 + OIL_GET(data, i*stride + j*s2, type), \
1.106 + OIL_GET(ref_data, i*stride + j*s2, type)); \
1.107 + } else { \
1.108 + std_log(LOG_FILENAME_LINE," " format " ", OIL_GET(data, i*stride + j*s2, type)); \
1.109 + } \
1.110 + } \
1.111 + std_log(LOG_FILENAME_LINE,"\n"); \
1.112 + } \
1.113 +} while(0)
1.114 +
1.115 + switch(type) {
1.116 + case OIL_TYPE_s8p:
1.117 + case OIL_TYPE_u8p:
1.118 + DUMP(int8_t, "0x%02" PRIx8, 1);
1.119 + break;
1.120 + case OIL_TYPE_s16p:
1.121 + case OIL_TYPE_u16p:
1.122 + DUMP(uint16_t, "0x%04" PRIx16, 1);
1.123 + break;
1.124 + case OIL_TYPE_s32p:
1.125 + case OIL_TYPE_u32p:
1.126 + DUMP(uint32_t, "0x%08" PRIx32, 1);
1.127 + break;
1.128 + case OIL_TYPE_f32p:
1.129 + DUMP(float, "%g", 0);
1.130 + break;
1.131 + case OIL_TYPE_s64p:
1.132 + case OIL_TYPE_u64p:
1.133 + DUMP(uint64_t, "0x%016" PRIx64, 1);
1.134 + break;
1.135 + case OIL_TYPE_f64p:
1.136 + DUMP(double, "%g", 0);
1.137 + break;
1.138 + default:
1.139 + break;
1.140 + }
1.141 +}
1.142 +
1.143 +void
1.144 +dump_source (OilTest *test)
1.145 +{
1.146 + int i;
1.147 + for(i=0;i<OIL_ARG_LAST;i++){
1.148 + OilParameter *p = &test->params[i];
1.149 + if (p->is_pointer) {
1.150 + if (p->direction == 'i' || p->direction == 's') {
1.151 + printf (" %s:\n", p->parameter_name);
1.152 + dump_array (p->src_data + p->test_header,
1.153 + p->src_data + p->test_header,
1.154 + p->type, p->pre_n, p->stride, p->post_n);
1.155 + }
1.156 + }
1.157 + }
1.158 +}
1.159 +
1.160 +void
1.161 +dump_dest_ref (OilTest *test)
1.162 +{
1.163 + int i;
1.164 + for(i=0;i<OIL_ARG_LAST;i++){
1.165 + OilParameter *p = &test->params[i];
1.166 + if (p->is_pointer) {
1.167 + if (p->direction == 'd') {
1.168 + printf (" %s:\n", p->parameter_name);
1.169 + dump_array (p->ref_data + p->test_header,
1.170 + p->ref_data + p->test_header,
1.171 + p->type, p->pre_n, p->stride, p->post_n);
1.172 + }
1.173 + }
1.174 + }
1.175 +}
1.176 +
1.177 +int
1.178 +test_difference (void *data, void *ref_data, OilType type, int pre_n, int stride,
1.179 + int post_n)
1.180 +{
1.181 + int i, j;
1.182 + int s2 = oil_type_sizeof (type);
1.183 + double x;
1.184 +
1.185 +#define CHECK(type, is_int) do { \
1.186 + float epsilon = (is_int) ? INT_EPSILON : FLOAT_EPSILON; \
1.187 + for(i=0;i<post_n;i++){ \
1.188 + for(j=0;j<pre_n;j++){ \
1.189 + x = fabs(OIL_GET(data, i*stride + j*s2, type) - \
1.190 + OIL_GET(ref_data, i*stride + j*s2, type)); \
1.191 + if (x > epsilon) { \
1.192 + return 1; \
1.193 + } \
1.194 + } \
1.195 + } \
1.196 + return 0; \
1.197 +} while(0)
1.198 +
1.199 + switch(type) {
1.200 + case OIL_TYPE_s8p:
1.201 + CHECK(int8_t, 1);
1.202 + break;
1.203 + case OIL_TYPE_u8p:
1.204 + CHECK(uint8_t, 1);
1.205 + break;
1.206 + case OIL_TYPE_s16p:
1.207 + CHECK(int16_t, 1);
1.208 + break;
1.209 + case OIL_TYPE_u16p:
1.210 + CHECK(uint16_t, 1);
1.211 + break;
1.212 + case OIL_TYPE_s32p:
1.213 + CHECK(int32_t, 1);
1.214 + break;
1.215 + case OIL_TYPE_u32p:
1.216 + CHECK(uint32_t, 1);
1.217 + break;
1.218 + case OIL_TYPE_s64p:
1.219 + CHECK(int64_t, 1);
1.220 + break;
1.221 + case OIL_TYPE_u64p:
1.222 + CHECK(uint64_t, 1);
1.223 + break;
1.224 + case OIL_TYPE_f32p:
1.225 + CHECK(float, 0);
1.226 + break;
1.227 + case OIL_TYPE_f64p:
1.228 + CHECK(double, 0);
1.229 + break;
1.230 + default:
1.231 + return 1;
1.232 + }
1.233 +}
1.234 +
1.235 +int
1.236 +check_test (OilTest *test)
1.237 +{
1.238 + int i, failed = 0;
1.239 + for(i=0;i<OIL_ARG_LAST;i++){
1.240 + OilParameter *p = &test->params[i];
1.241 + if (p->is_pointer) {
1.242 + if (p->direction == 'i' || p->direction == 'd') {
1.243 + if (!test_difference(p->test_data + p->test_header,
1.244 + p->ref_data + p->test_header,
1.245 + p->type, p->pre_n, p->stride, p->post_n))
1.246 + continue;
1.247 + printf (" Failure in %s (marked by *, ref in ()):\n",
1.248 + p->parameter_name);
1.249 + dump_array (p->test_data + p->test_header,
1.250 + p->ref_data + p->test_header,
1.251 + p->type, p->pre_n, p->stride, p->post_n);
1.252 + failed = 1;
1.253 + }
1.254 + }
1.255 + }
1.256 + return failed;
1.257 +}
1.258 +
1.259 +int check_class_with_alignment (OilFunctionClass *klass,
1.260 + OilArgType arg, int n, int align)
1.261 +{
1.262 + OilParameter *p;
1.263 + int align_offset;
1.264 + int test_failed = 0;
1.265 + OilTest *test;
1.266 + OilFunctionImpl *impl;
1.267 +
1.268 + test = oil_test_new(klass);
1.269 +
1.270 + p = &test->params[arg];
1.271 + align_offset = align * oil_type_sizeof(p->type);
1.272 + oil_test_set_test_header(test, p, OIL_TEST_HEADER + align_offset);
1.273 +
1.274 + oil_test_set_iterations(test, 1);
1.275 + test->n = n;
1.276 + test->m = n;
1.277 +
1.278 + impl = klass->reference_impl;
1.279 + oil_test_check_impl (test, impl);
1.280 +
1.281 + for (impl = klass->first_impl; impl; impl = impl->next) {
1.282 + if (impl == klass->reference_impl)
1.283 + continue;
1.284 + if (oil_impl_is_runnable (impl)) {
1.285 + if (!oil_test_check_impl (test, impl)) {
1.286 + printf ("impl %s with arg %d offset %d, n=%d\n", impl->name, arg,
1.287 + align_offset, n);
1.288 + std_log(LOG_FILENAME_LINE,"dests for %s:\n", klass->name);
1.289 + dump_dest_ref(test);
1.290 + std_log(LOG_FILENAME_LINE,"sources for %s:\n", klass->name);
1.291 + dump_source(test);
1.292 + }
1.293 + }
1.294 + }
1.295 + oil_test_free(test);
1.296 +
1.297 + if (test_failed)
1.298 + std_log(LOG_FILENAME_LINE,"Failed in class %s", klass->name);
1.299 + return test_failed;
1.300 +}
1.301 +
1.302 +/* Check a function class for all implementations matching the reference when
1.303 + * each parameter is varied in its offset from malloc's alignment by 0 - 3 units
1.304 + * times size of the type, and with the number of elements varying between 8 and
1.305 + * 11.
1.306 + */
1.307 +int check_class(OilFunctionClass *klass)
1.308 +{
1.309 + OilTest *test;
1.310 + int failed = 0;
1.311 + int i, n;
1.312 +
1.313 + oil_class_optimize (klass);
1.314 +
1.315 + std_log(LOG_FILENAME_LINE,"checking class %s\n", klass->name);
1.316 +
1.317 + test = oil_test_new(klass);
1.318 + for (i=0; i < OIL_ARG_LAST; i++) {
1.319 + OilParameter *p;
1.320 + int align;
1.321 +
1.322 + p = &test->params[i];
1.323 + if (!p->is_pointer) {
1.324 + continue;
1.325 + }
1.326 +
1.327 + for (n = 8; n <= 11; n++) {
1.328 + for (align = 0; align <= 3; align++) {
1.329 + failed |= check_class_with_alignment (klass, i, n, align);
1.330 + }
1.331 + }
1.332 + }
1.333 + oil_test_free (test);
1.334 +
1.335 + return failed;
1.336 +}
1.337 +
1.338 +int main (int argc, char *argv[])
1.339 +{
1.340 + int failed = 0;
1.341 + int i, n;
1.342 +
1.343 + std_log(LOG_FILENAME_LINE,"Test started testsuite_align");
1.344 + oil_init ();
1.345 +
1.346 + n = oil_class_get_n_classes ();
1.347 + for (i = 0; i < n; i++) {
1.348 + OilFunctionClass *klass = oil_class_get_by_index(i);
1.349 + failed |= check_class(klass);
1.350 + }
1.351 +
1.352 + if(failed)
1.353 + assert_failed = failed;
1.354 + if(assert_failed)
1.355 + std_log(LOG_FILENAME_LINE,"Test Fail");
1.356 + else
1.357 + std_log(LOG_FILENAME_LINE,"Test Successful");
1.358 + create_xml(0);
1.359 + return 0;
1.360 +}