sl@0: /* sl@0: * LIBOIL - Library of Optimized Inner Loops sl@0: * Copyright (c) 2003,2004 David A. Schleef sl@0: * All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR sl@0: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED sl@0: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, sl@0: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES sl@0: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR sl@0: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING sl@0: * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE sl@0: * POSSIBILITY OF SUCH DAMAGE. sl@0: */ sl@0: //Portions Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. sl@0: sl@0: #ifdef HAVE_CONFIG_H sl@0: #include sl@0: #endif sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #ifdef __SYMBIAN32__ sl@0: #ifdef __WINSCW__ sl@0: #pragma warn_unusedarg off sl@0: #endif//__WINSCW__ sl@0: #endif//__SYMBIAN32__ sl@0: sl@0: #define MAX_PARAMS 20 sl@0: sl@0: /** sl@0: * SECTION:liboiltest sl@0: * @title:OilTest sl@0: * @short_description: Test and profile function implementations. sl@0: * sl@0: */ sl@0: static void oil_test_init_params (OilTest *test); sl@0: static void fill_array (void *ptr, OilType type, int pre_n, int stride, sl@0: int post_n); sl@0: static double check_array (void *data, void *ref, OilType type, int pre_n, sl@0: int stride, int post_n); sl@0: static int check_holes (void *data, OilType type, int pre_n, sl@0: int stride, int post_n, int guard); sl@0: sl@0: /** sl@0: * oil_test_new: sl@0: * @klass: an OilFunctionClass sl@0: * sl@0: * Creates a new OilTest for the OilFunctionClass represented by @klass. sl@0: * sl@0: * Returns: the new OilTest sl@0: */ sl@0: #ifdef __SYMBIAN32__ sl@0: EXPORT_C sl@0: #endif sl@0: OilTest * sl@0: oil_test_new (OilFunctionClass *klass) sl@0: { sl@0: OilTest *test; sl@0: OilPrototype *proto; sl@0: int i; sl@0: sl@0: if (klass == NULL) return NULL; sl@0: sl@0: proto = oil_prototype_from_string (klass->prototype); sl@0: if (proto == NULL) return NULL; sl@0: sl@0: test = malloc (sizeof (OilTest)); sl@0: memset (test, 0, sizeof (OilTest)); sl@0: sl@0: test->klass = klass; sl@0: test->proto = proto; sl@0: test->impl = klass->reference_impl; sl@0: test->tolerance = 0.0; sl@0: sl@0: for (i=0;in_params;i++){ sl@0: if (proto->params[i].parameter_type == OIL_ARG_UNKNOWN) { sl@0: return NULL; sl@0: } sl@0: if (oil_type_is_floating_point(proto->params[i].type)) { sl@0: test->tolerance = 0.001; sl@0: } sl@0: memcpy (&test->params[proto->params[i].parameter_type], &proto->params[i], sl@0: sizeof(OilParameter)); sl@0: } sl@0: for (i=0;iparams[i].src_data = NULL; sl@0: test->params[i].ref_data = NULL; sl@0: test->params[i].test_data = NULL; sl@0: test->params[i].test_header = OIL_TEST_HEADER; sl@0: test->params[i].test_footer = OIL_TEST_FOOTER; sl@0: } sl@0: sl@0: test->iterations = 10; sl@0: test->n = 100; sl@0: test->m = 100; sl@0: sl@0: return test; sl@0: } sl@0: sl@0: /** sl@0: * oil_test_free: sl@0: * @test: the OilTest sl@0: * sl@0: * Frees memory associated with @test. sl@0: */ sl@0: #ifdef __SYMBIAN32__ sl@0: EXPORT_C sl@0: #endif sl@0: void sl@0: oil_test_free (OilTest *test) sl@0: { sl@0: int i; sl@0: sl@0: if (test->proto) { sl@0: oil_prototype_free (test->proto); sl@0: } sl@0: sl@0: for(i=0;iparams[i].src_data) { sl@0: free (test->params[i].src_data); sl@0: } sl@0: if (test->params[i].ref_data) { sl@0: free (test->params[i].ref_data); sl@0: } sl@0: if (test->params[i].test_data) { sl@0: free (test->params[i].test_data); sl@0: } sl@0: } sl@0: sl@0: free(test); sl@0: } sl@0: sl@0: /** sl@0: * oil_test_set_impl: sl@0: * @test: the OilTest sl@0: * @impl: an OilFunctionImpl to set sl@0: * sl@0: * Sets the current implementation of @test to @impl. sl@0: */ sl@0: #ifdef __SYMBIAN32__ sl@0: EXPORT_C sl@0: #endif sl@0: void sl@0: oil_test_set_impl (OilTest *test, OilFunctionImpl *impl) sl@0: { sl@0: test->impl = impl; sl@0: } sl@0: sl@0: /** sl@0: * oil_test_set_iterations: sl@0: * @test: the OilTest sl@0: * @iterations: the number of iterations sl@0: * sl@0: * Sets the number of iterations of @test to @iterations. sl@0: */ sl@0: #ifdef __SYMBIAN32__ sl@0: EXPORT_C sl@0: #endif sl@0: void sl@0: oil_test_set_iterations (OilTest *test, int iterations) sl@0: { sl@0: test->iterations = iterations; sl@0: } sl@0: sl@0: /** sl@0: * oil_test_set_test_header: sl@0: * @test: the OilTest sl@0: * @p: the OilParameter to change the header for sl@0: * @test_header: the number of bytes of guard header sl@0: * sl@0: * Sets the number of bytes of guard header for @p to @test_header. sl@0: */ sl@0: #ifdef __SYMBIAN32__ sl@0: EXPORT_C sl@0: #endif sl@0: void sl@0: oil_test_set_test_header (OilTest *test, OilParameter *p, int test_header) sl@0: { sl@0: p->test_header = test_header; sl@0: } sl@0: sl@0: /** sl@0: * oil_test_set_test_footer: sl@0: * @test: the OilTest sl@0: * @p: the OilParameter to change the footer for sl@0: * @test_footer: the number of bytes of guard footer sl@0: * sl@0: * Sets the number of bytes of guard footer for @p to @test_footer. sl@0: */ sl@0: #ifdef __SYMBIAN32__ sl@0: EXPORT_C sl@0: #endif sl@0: void sl@0: oil_test_set_test_footer (OilTest *test, OilParameter *p, int test_footer) sl@0: { sl@0: p->test_footer = test_footer; sl@0: } sl@0: sl@0: /** sl@0: * oil_test_init: sl@0: * @test: the OilTest sl@0: * sl@0: * Intializes @test. sl@0: * sl@0: * FIXME: needs work sl@0: */ sl@0: #ifdef __SYMBIAN32__ sl@0: EXPORT_C sl@0: #endif sl@0: void sl@0: oil_test_init (OilTest *test) sl@0: { sl@0: if (test->inited) { sl@0: return; sl@0: } sl@0: sl@0: oil_test_init_params(test); sl@0: sl@0: test->params[OIL_ARG_N].value = test->n; sl@0: sl@0: test->inited = 1; sl@0: sl@0: if (test->klass->test_func) { sl@0: test->klass->test_func (test); sl@0: } sl@0: } sl@0: sl@0: #ifdef __WINSCW__ sl@0: #pragma suppress_warnings on sl@0: #endif//__WINSCW__ sl@0: #ifdef __ARMCC__ sl@0: #pragma diag_remark 188 sl@0: #endif//__ARMCC__ sl@0: sl@0: static void sl@0: oil_test_check_function (void * priv) sl@0: { sl@0: OilTest *test = priv; sl@0: int i; sl@0: int j; sl@0: unsigned long args[MAX_PARAMS]; sl@0: unsigned int pointer_mask; sl@0: sl@0: oil_test_init (test); sl@0: sl@0: OIL_LOG("calling function %s", test->impl->name); sl@0: sl@0: pointer_mask = 1; sl@0: for(i=0;iproto->n_params;i++){ sl@0: OilParameter *p; sl@0: j = test->proto->params[i].parameter_type; sl@0: p = &test->params[j]; sl@0: sl@0: pointer_mask <<= 1; sl@0: OIL_LOG(" %s: 0x%08lx (%ld)", oil_arg_type_name (j), p->value, p->value); sl@0: if (p->is_pointer) { sl@0: pointer_mask |= 1; sl@0: if (p->direction == 's') { sl@0: args[i] = (unsigned long)p->src_data + p->test_header; sl@0: } else if (p->direction == 'i') { sl@0: memcpy (p->test_data, p->src_data, p->size); sl@0: args[i] = (unsigned long)p->test_data + p->test_header; sl@0: } else if (p->direction == 'd') { sl@0: memset (p->test_data, p->guard, p->size); sl@0: args[i] = (unsigned long)p->test_data + p->test_header; sl@0: } else { sl@0: OIL_ERROR ("not reached"); sl@0: } sl@0: } else { sl@0: args[i] = p->value; sl@0: } sl@0: } sl@0: #ifdef __WINSCW__ sl@0: #pragma suppress_warnings off sl@0: #endif//__WINSCW__ sl@0: sl@0: oil_profile_init (&test->prof); sl@0: for(i=0;iiterations;i++){ sl@0: int k; sl@0: sl@0: for(k=0;kproto->n_params;k++){ sl@0: OilParameter *p; sl@0: j = test->proto->params[k].parameter_type; sl@0: p = &test->params[j]; sl@0: if (p->direction == 'i') { sl@0: memcpy (p->test_data, p->src_data, p->size); sl@0: } sl@0: } sl@0: _oil_test_marshal_function (test->impl->func, args, test->proto->n_params, sl@0: pointer_mask, &test->prof); sl@0: } sl@0: sl@0: oil_profile_get_ave_std (&test->prof, &test->profile_ave, sl@0: &test->profile_std); sl@0: } sl@0: sl@0: /** sl@0: * oil_test_check_ref: sl@0: * @test: the OilTest sl@0: * sl@0: * Runs the test specified by @test on the reference function of the sl@0: * class being tested. sl@0: */ sl@0: #ifdef __SYMBIAN32__ sl@0: EXPORT_C sl@0: #endif sl@0: void sl@0: oil_test_check_ref (OilTest *test) sl@0: { sl@0: int i; sl@0: sl@0: if (test->proto->n_params > MAX_PARAMS) { sl@0: OIL_ERROR ("function class %s has too many parameters", sl@0: test->klass->name); sl@0: return; sl@0: } sl@0: if (test->klass->reference_impl == NULL) { sl@0: OIL_ERROR ("function class %s has no reference implementation", sl@0: test->klass->name); sl@0: return; sl@0: } sl@0: sl@0: test->impl = test->klass->reference_impl; sl@0: sl@0: oil_test_check_function (test); sl@0: sl@0: for(i=0;iparams[i]; sl@0: sl@0: if (p->is_pointer) { sl@0: if (p->direction == 'i' || p->direction == 'd') { sl@0: memcpy (p->ref_data, p->test_data, p->size); sl@0: } sl@0: } sl@0: } sl@0: sl@0: test->tested_ref = 1; sl@0: } sl@0: sl@0: static int sl@0: check_guard (uint8_t *data, int n, int guard) sl@0: { sl@0: int i; sl@0: for(i=0;iproto->n_params > MAX_PARAMS) { sl@0: OIL_ERROR ("function has too many parameters"); sl@0: return 0; sl@0: } sl@0: sl@0: if (!test->inited || !test->tested_ref) { sl@0: oil_test_check_ref(test); sl@0: } sl@0: sl@0: test->impl = impl; sl@0: ret = oil_fault_check_try (oil_test_check_function, test); sl@0: if (!ret) { sl@0: OIL_ERROR ("illegal instruction in %s", test->impl->name); sl@0: test->profile_ave = 0; sl@0: test->profile_std = 0; sl@0: sl@0: return 0; sl@0: } sl@0: sl@0: x = 0; sl@0: n = 0; sl@0: for(i=0;iparams[i]; sl@0: sl@0: if (p->is_pointer) { sl@0: if (p->direction == 'i' || p->direction == 'd') { sl@0: x += check_array (p->test_data + p->test_header, sl@0: p->ref_data + p->test_header, p->type, p->pre_n, p->stride, sl@0: p->post_n); sl@0: n += p->pre_n * p->post_n; sl@0: if (!check_guard (p->test_data, p->test_header, p->guard)) { sl@0: fail = 1; sl@0: OIL_ERROR("function %s wrote before area for parameter %s", sl@0: test->impl->name, p->parameter_name); sl@0: } sl@0: if (!check_guard ((uint8_t *)p->test_data + p->size - p->test_footer, sl@0: p->test_footer, p->guard)) { sl@0: fail = 1; sl@0: OIL_ERROR("function %s wrote after area for parameter %s", sl@0: test->impl->name, p->parameter_name); sl@0: } sl@0: if (!check_holes (p->test_data, p->type, p->pre_n, p->stride, sl@0: p->post_n, p->guard)) { sl@0: fail = 1; sl@0: OIL_ERROR("function %s wrote in interstitial area for parameter %s", sl@0: test->impl->name, p->parameter_name); sl@0: } sl@0: } sl@0: } sl@0: } sl@0: OIL_DEBUG("sum of absolute differences %g for %d values", x, n); sl@0: test->sum_abs_diff = x; sl@0: test->n_points = n; sl@0: sl@0: if (x > test->tolerance * n || fail) { sl@0: OIL_ERROR ("function %s in class %s failed check (%g > %g) || (outside=%d)", sl@0: test->impl->name, test->klass->name, x, test->tolerance * n, fail); sl@0: return 0; sl@0: } sl@0: sl@0: return 1; sl@0: } sl@0: sl@0: /** sl@0: * oil_test_cleanup sl@0: * @test: the OilTest sl@0: * sl@0: * Cleans up @test. sl@0: * sl@0: * FIXME: needs work sl@0: */ sl@0: #ifdef __SYMBIAN32__ sl@0: EXPORT_C sl@0: #endif sl@0: void sl@0: oil_test_cleanup (OilTest *test) sl@0: { sl@0: OilParameter *params = test->params; sl@0: sl@0: /* src1 */ sl@0: if(params[OIL_ARG_SRC1].type) { sl@0: if (!params[OIL_ARG_SSTR1].type) { sl@0: params[OIL_ARG_SSTR1].value = oil_type_sizeof (params[OIL_ARG_SRC1].type); sl@0: } sl@0: } sl@0: sl@0: /* src2 */ sl@0: if(params[OIL_ARG_SRC2].type) { sl@0: if (!params[OIL_ARG_SSTR2].type) { sl@0: params[OIL_ARG_SSTR2].value = oil_type_sizeof (params[OIL_ARG_SRC2].type); sl@0: } sl@0: } sl@0: sl@0: /* src3 */ sl@0: if(params[OIL_ARG_SRC3].type) { sl@0: if (!params[OIL_ARG_SSTR3].type) { sl@0: params[OIL_ARG_SSTR3].value = oil_type_sizeof (params[OIL_ARG_SRC3].type); sl@0: } sl@0: } sl@0: sl@0: /* dest1 */ sl@0: if(params[OIL_ARG_DEST1].type) { sl@0: if (!params[OIL_ARG_DSTR1].type) { sl@0: params[OIL_ARG_DSTR1].value = oil_type_sizeof (params[OIL_ARG_DEST1].type); sl@0: } sl@0: } sl@0: sl@0: /* dest2 */ sl@0: if(params[OIL_ARG_DEST2].type) { sl@0: if (!params[OIL_ARG_DSTR2].type) { sl@0: params[OIL_ARG_DSTR2].value = oil_type_sizeof (params[OIL_ARG_DEST2].type); sl@0: } sl@0: } sl@0: sl@0: /* dest3 */ sl@0: if(params[OIL_ARG_DEST3].type) { sl@0: if (!params[OIL_ARG_DSTR3].type) { sl@0: params[OIL_ARG_DSTR3].value = oil_type_sizeof (params[OIL_ARG_DEST3].type); sl@0: } sl@0: } sl@0: sl@0: } sl@0: sl@0: sl@0: static void sl@0: init_parameter (OilTest *test, OilParameter *p, OilParameter *ps) sl@0: { sl@0: if (!p->type) return; sl@0: sl@0: p->pre_n = p->prestride_length; sl@0: if (p->prestride_var == 1) { sl@0: p->pre_n += test->n; sl@0: } sl@0: if (p->prestride_var == 2) { sl@0: p->pre_n += test->m; sl@0: } sl@0: sl@0: if (ps->value) { sl@0: p->stride = ps->value; sl@0: } else { sl@0: p->stride = oil_type_sizeof (p->type) * p->pre_n; sl@0: ps->value = p->stride; sl@0: } sl@0: sl@0: p->post_n = p->poststride_length; sl@0: if (p->poststride_var == 1) { sl@0: p->post_n += test->n; sl@0: } sl@0: if (p->poststride_var == 2) { sl@0: p->post_n += test->m; sl@0: } sl@0: sl@0: p->size = p->stride * p->post_n + p->test_header + p->test_footer; sl@0: p->guard = oil_rand_u8(); sl@0: sl@0: if (p->direction == 'i' || p->direction == 's') { sl@0: if (p->src_data) free (p->src_data); sl@0: sl@0: OIL_DEBUG("allocating %d bytes for src_data for %s", p->size, p->parameter_name); sl@0: p->src_data = malloc (p->size); sl@0: memset (p->src_data, p->guard, p->size); sl@0: fill_array (p->src_data + p->test_header, p->type, p->pre_n, p->stride, p->post_n); sl@0: } sl@0: sl@0: if (p->direction == 'i' || p->direction == 'd') { sl@0: if (p->ref_data) free (p->ref_data); sl@0: p->ref_data = malloc (p->size); sl@0: memset (p->ref_data, p->guard, p->size); sl@0: OIL_DEBUG("allocating %d bytes for ref_data and test_data for %s", p->size, p->parameter_name); sl@0: sl@0: if (p->test_data) free (p->test_data); sl@0: p->test_data = malloc (p->size); sl@0: memset (p->test_data, p->guard, p->size); sl@0: } sl@0: } sl@0: sl@0: static void sl@0: oil_test_init_params (OilTest *test) sl@0: { sl@0: init_parameter (test, &test->params[OIL_ARG_DEST1], sl@0: &test->params[OIL_ARG_DSTR1]); sl@0: init_parameter (test, &test->params[OIL_ARG_DEST2], sl@0: &test->params[OIL_ARG_DSTR2]); sl@0: init_parameter (test, &test->params[OIL_ARG_DEST3], sl@0: &test->params[OIL_ARG_DSTR3]); sl@0: sl@0: init_parameter (test, &test->params[OIL_ARG_SRC1], sl@0: &test->params[OIL_ARG_SSTR1]); sl@0: init_parameter (test, &test->params[OIL_ARG_SRC2], sl@0: &test->params[OIL_ARG_SSTR2]); sl@0: init_parameter (test, &test->params[OIL_ARG_SRC3], sl@0: &test->params[OIL_ARG_SSTR3]); sl@0: init_parameter (test, &test->params[OIL_ARG_SRC4], sl@0: &test->params[OIL_ARG_SSTR4]); sl@0: init_parameter (test, &test->params[OIL_ARG_SRC5], sl@0: &test->params[OIL_ARG_SSTR5]); sl@0: sl@0: init_parameter (test, &test->params[OIL_ARG_INPLACE1], sl@0: &test->params[OIL_ARG_ISTR1]); sl@0: init_parameter (test, &test->params[OIL_ARG_INPLACE2], sl@0: &test->params[OIL_ARG_ISTR2]); sl@0: } sl@0: sl@0: static void sl@0: fill_array (void *data, OilType type, int pre_n, int stride, int post_n) sl@0: { sl@0: int i; sl@0: sl@0: #define FILL(type,func) do {\ sl@0: for(i=0;iparams[arg_type].src_data; sl@0: ptr += test->params[arg_type].test_header; sl@0: sl@0: return ptr; sl@0: } sl@0: sl@0: #ifdef __SYMBIAN32__ sl@0: EXPORT_C sl@0: #endif sl@0: int sl@0: oil_test_get_arg_pre_n (OilTest *test, OilArgType arg_type) sl@0: { sl@0: return test->params[arg_type].pre_n; sl@0: } sl@0: sl@0: #ifdef __SYMBIAN32__ sl@0: EXPORT_C sl@0: #endif sl@0: int sl@0: oil_test_get_arg_post_n (OilTest *test, OilArgType arg_type) sl@0: { sl@0: return test->params[arg_type].post_n; sl@0: } sl@0: sl@0: #ifdef __SYMBIAN32__ sl@0: EXPORT_C sl@0: #endif sl@0: int sl@0: oil_test_get_arg_stride (OilTest *test, OilArgType arg_type) sl@0: { sl@0: return test->params[arg_type].stride; sl@0: } sl@0: sl@0: #ifdef __SYMBIAN32__ sl@0: EXPORT_C sl@0: #endif sl@0: int sl@0: oil_test_get_value (OilTest *test, OilArgType arg_type) sl@0: { sl@0: return test->params[arg_type].value; sl@0: }