2 * Copyright (c) 2004 David A. Schleef <ds@schleef.org>
3 * Copyright (c) 2005 Eric Anholt <anholt@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
28 /* Tests math functions against the reference, failing if they differ by more
29 * than some epsilon, and printing the difference.
36 #include <liboil/liboil.h>
41 #ifdef HAVE_INTTYPES_H
45 #include <liboil/liboilprototype.h>
46 #include <liboil/liboiltest.h>
47 #include <liboil/liboilcpu.h>
49 /* Amount by which results of different types are allowed to deviate from the
53 #define FLOAT_EPSILON 0.0001
56 dump_array (void *data, void *ref_data, OilType type, int pre_n, int stride,
60 int s2 = oil_type_sizeof (type);
63 #define DUMP(type, format, int) do { \
64 for(i=0;i<post_n;i++){ \
65 float epsilon = (int) ? INT_EPSILON : FLOAT_EPSILON; \
67 for(j=0;j<pre_n;j++){ \
68 x = fabs(OIL_GET(data, i*stride + j*s2, type) - \
69 OIL_GET(ref_data, i*stride + j*s2, type)); \
71 printf("*" format "* (" format ") ", \
72 OIL_GET(data, i*stride + j*s2, type), \
73 OIL_GET(ref_data, i*stride + j*s2, type)); \
75 printf(" " format " ", OIL_GET(data, i*stride + j*s2, type)); \
85 DUMP(int8_t, "0x%02" PRIx8, 1);
89 DUMP(uint16_t, "0x%04" PRIx16, 1);
93 DUMP(uint32_t, "0x%08" PRIx32, 1);
100 DUMP(uint64_t, "0x%016" PRIx64, 1);
103 DUMP(double, "%g", 0);
111 dump_source (OilTest *test)
114 for(i=0;i<OIL_ARG_LAST;i++){
115 OilParameter *p = &test->params[i];
117 if (p->direction == 'i' || p->direction == 's') {
118 printf (" %s:\n", p->parameter_name);
119 dump_array (p->src_data + p->test_header,
120 p->src_data + p->test_header,
121 p->type, p->pre_n, p->stride, p->post_n);
128 dump_dest_ref (OilTest *test)
131 for(i=0;i<OIL_ARG_LAST;i++){
132 OilParameter *p = &test->params[i];
134 if (p->direction == 'd') {
135 printf (" %s:\n", p->parameter_name);
136 dump_array (p->ref_data + p->test_header,
137 p->ref_data + p->test_header,
138 p->type, p->pre_n, p->stride, p->post_n);
145 test_difference (void *data, void *ref_data, OilType type, int pre_n, int stride,
149 int s2 = oil_type_sizeof (type);
152 #define CHECK(type, is_int) do { \
153 float epsilon = (is_int) ? INT_EPSILON : FLOAT_EPSILON; \
154 for(i=0;i<post_n;i++){ \
155 for(j=0;j<pre_n;j++){ \
156 x = fabs(OIL_GET(data, i*stride + j*s2, type) - \
157 OIL_GET(ref_data, i*stride + j*s2, type)); \
203 check_test (OilTest *test)
206 for(i=0;i<OIL_ARG_LAST;i++){
207 OilParameter *p = &test->params[i];
209 if (p->direction == 'i' || p->direction == 'd') {
210 if (!test_difference(p->test_data + p->test_header,
211 p->ref_data + p->test_header,
212 p->type, p->pre_n, p->stride, p->post_n))
214 printf (" Failure in %s (marked by *, ref in ()):\n",
216 dump_array (p->test_data + p->test_header,
217 p->ref_data + p->test_header,
218 p->type, p->pre_n, p->stride, p->post_n);
226 int check_class_with_alignment (OilFunctionClass *klass,
227 OilArgType arg, int n, int align)
233 OilFunctionImpl *impl;
235 test = oil_test_new(klass);
237 p = &test->params[arg];
238 align_offset = align * oil_type_sizeof(p->type);
239 oil_test_set_test_header(test, p, OIL_TEST_HEADER + align_offset);
241 oil_test_set_iterations(test, 1);
245 impl = klass->reference_impl;
246 oil_test_check_impl (test, impl);
248 for (impl = klass->first_impl; impl; impl = impl->next) {
249 if (impl == klass->reference_impl)
251 if (oil_impl_is_runnable (impl)) {
252 if (!oil_test_check_impl (test, impl)) {
253 printf ("impl %s with arg %d offset %d, n=%d\n", impl->name, arg,
255 printf("dests for %s:\n", klass->name);
257 printf("sources for %s:\n", klass->name);
267 /* Check a function class for all implementations matching the reference when
268 * each parameter is varied in its offset from malloc's alignment by 0 - 3 units
269 * times size of the type, and with the number of elements varying between 8 and
272 int check_class(OilFunctionClass *klass)
278 oil_class_optimize (klass);
280 printf("checking class %s\n", klass->name);
282 test = oil_test_new(klass);
283 for (i=0; i < OIL_ARG_LAST; i++) {
287 p = &test->params[i];
288 if (!p->is_pointer) {
292 for (n = 8; n <= 11; n++) {
293 for (align = 0; align <= 3; align++) {
294 failed |= check_class_with_alignment (klass, i, n, align);
298 oil_test_free (test);
303 int main (int argc, char *argv[])
310 n = oil_class_get_n_classes ();
311 for (i = 0; i < n; i++) {
312 OilFunctionClass *klass = oil_class_get_by_index(i);
313 failed |= check_class(klass);