summaryrefslogtreecommitdiff
path: root/testsuite/stack_align.c
blob: b2c1a694fd1ffb042169aa86b46196ecde965e22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
 * Copyright (c) 2004 David A. Schleef <ds@schleef.org>
 * Copyright (c) 2005 Eric Anholt <anholt@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <liboil/liboil.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif

#include <liboil/liboilprototype.h>
#include <liboil/liboiltest.h>
#include <liboil/liboilcpu.h>

int verbose = 1;

/* Amount by which results of different types are allowed to deviate from the
 * reference.
 */
#define INT_EPSILON 1
#define FLOAT_EPSILON 0.0001

void
dump_array (void *data, void *ref_data, OilType type, int pre_n, int stride,
    int post_n)
{
  int i, j;
  int s2 = oil_type_sizeof (type);
  double x;

#define DUMP(type, format, int) do { \
  for(i=0;i<post_n;i++){ \
    float epsilon = (int) ? INT_EPSILON : FLOAT_EPSILON; \
    printf("    "); \
    for(j=0;j<pre_n;j++){ \
      x = fabs(OIL_GET(data, i*stride + j*s2, type) - \
          OIL_GET(ref_data, i*stride + j*s2, type)); \
      if (x > epsilon) { \
        printf("*" format "* (" format ") ", \
	    OIL_GET(data, i*stride + j*s2, type), \
	    OIL_GET(ref_data, i*stride + j*s2, type)); \
      } else { \
        printf(" " format " ", OIL_GET(data, i*stride + j*s2, type)); \
      } \
    } \
    printf("\n"); \
  } \
} while(0)

  switch(type) {
    case OIL_TYPE_s8p:
    case OIL_TYPE_u8p:
      DUMP(int8_t, "0x%02" PRIx8, 1);
      break;
    case OIL_TYPE_s16p:
    case OIL_TYPE_u16p:
      DUMP(uint16_t, "0x%04" PRIx16, 1);
      break;
    case OIL_TYPE_s32p:
    case OIL_TYPE_u32p:
      DUMP(uint32_t, "0x%08" PRIx32, 1);
      break;
    case OIL_TYPE_f32p:
      DUMP(float, "%g", 0);
      break;
    case OIL_TYPE_s64p:
    case OIL_TYPE_u64p:
      DUMP(uint64_t, "0x%016" PRIx64, 1);
      break;
    case OIL_TYPE_f64p:
      DUMP(double, "%g", 0);
      break;
    default:
      break;
  }
}

void
dump_source (OilTest *test)
{
  int i;
  for(i=0;i<OIL_ARG_LAST;i++){
    OilParameter *p = &test->params[i];
    if (p->is_pointer) {
      if (p->direction == 'i' || p->direction == 's') {
        printf ("  %s:\n", p->parameter_name);
        dump_array (p->src_data + p->test_header,
            p->src_data + p->test_header,
            p->type, p->pre_n, p->stride, p->post_n);
      }
    }
  }
}

void
dump_dest_ref (OilTest *test)
{
  int i;
  for(i=0;i<OIL_ARG_LAST;i++){
    OilParameter *p = &test->params[i];
    if (p->is_pointer) {
      if (p->direction == 'd') {
        printf ("  %s:\n", p->parameter_name);
        dump_array (p->ref_data + p->test_header,
            p->ref_data + p->test_header,
            p->type, p->pre_n, p->stride, p->post_n);
      }
    }
  }
}

int
test_difference (void *data, void *ref_data, OilType type, int pre_n, int stride,
    int post_n)
{
  int i, j;
  int s2 = oil_type_sizeof (type);
  double x;

#define CHECK(type, is_int) do { \
  float epsilon = (is_int) ? INT_EPSILON : FLOAT_EPSILON; \
  for(i=0;i<post_n;i++){ \
    for(j=0;j<pre_n;j++){ \
      x = fabs(OIL_GET(data, i*stride + j*s2, type) - \
          OIL_GET(ref_data, i*stride + j*s2, type)); \
      if (x > epsilon) { \
        return 1; \
      } \
    } \
  } \
  return 0; \
} while(0)

  switch(type) {
    case OIL_TYPE_s8p:
      CHECK(int8_t, 1);
      break;
    case OIL_TYPE_u8p:
      CHECK(uint8_t, 1);
      break;
    case OIL_TYPE_s16p:
      CHECK(int16_t, 1);
      break;
    case OIL_TYPE_u16p:
      CHECK(uint16_t, 1);
      break;
    case OIL_TYPE_s32p:
      CHECK(int32_t, 1);
      break;
    case OIL_TYPE_u32p:
      CHECK(uint32_t, 1);
      break;
    case OIL_TYPE_s64p:
      CHECK(int64_t, 1);
      break;
    case OIL_TYPE_u64p:
      CHECK(uint64_t, 1);
      break;
    case OIL_TYPE_f32p:
      CHECK(float, 0);
      break;
    case OIL_TYPE_f64p:
      CHECK(double, 0);
      break;
    default:
      return 1;
  }
}

int
check_test (OilTest *test)
{
  int i, failed = 0;
  for(i=0;i<OIL_ARG_LAST;i++){
    OilParameter *p = &test->params[i];
    if (p->is_pointer) {
      if (p->direction == 'i' || p->direction == 'd') {
	if (!test_difference(p->test_data + p->test_header,
            p->ref_data + p->test_header,
            p->type, p->pre_n, p->stride, p->post_n))
	  continue;
        printf (" Failure in %s (marked by *, ref in ()):\n",
	    p->parameter_name);
        dump_array (p->test_data + p->test_header,
            p->ref_data + p->test_header,
            p->type, p->pre_n, p->stride, p->post_n);
	failed = 1;
      }
    }
  }
  return failed;
}

void print_align(void *ptr)
{
  int i[4];

  if (verbose)printf("stack addr %p\n", &i);
}

OilFunctionClass *realign_klass;
int realign_align;
int realign_return;

void realign(int align)
{
#ifdef HAVE_GCC_ASM
#ifdef HAVE_I386
  __asm__ __volatile__ (
      "  sub %%ebx, %%esp\n"
#ifdef HAVE_SYMBOL_UNDERSCORE
      "  call _check_class_with_alignment\n"
#else
      "  call check_class_with_alignment\n"
#endif
      "  add %%ebx, %%esp\n"
      :: "b" (align)
  );
#endif
#ifdef HAVE_AMD64
  __asm__ __volatile__ (
      "  sub %%rbx, %%rsp\n"
#ifdef HAVE_SYMBOL_UNDERSCORE
      "  call _check_class_with_alignment\n"
#else
      "  call check_class_with_alignment\n"
#endif
      "  add %%rbx, %%rsp\n"
      :: "b" (align)
  );
#endif
#endif
}

void check_class_with_alignment (void)
{
  OilFunctionClass *klass = realign_klass;
  int align = realign_align;
  int test_failed = 0;
  OilTest *test;
  OilFunctionImpl *impl;

  test = oil_test_new(klass);

  oil_test_set_iterations(test, 1);
  test->n = 100;

  impl = klass->reference_impl;
  oil_test_check_impl (test, impl);

  for (impl = klass->first_impl; impl; impl = impl->next) {
    if (impl == klass->reference_impl)
      continue;
    if (oil_impl_is_runnable (impl)) {
      if (!oil_test_check_impl (test, impl)) {
        printf ("impl %s with align %d\n", impl->name, align);
        printf("dests for %s:\n", klass->name);
        dump_dest_ref(test);
        printf("sources for %s:\n", klass->name);
        dump_source(test);
      }
    }
  }
  oil_test_free(test);

  realign_return = test_failed;
}

int check_class(OilFunctionClass *klass)
{
  OilTest *test;
  int failed = 0;
  int align;
  int step = 4;

  oil_class_optimize (klass);

  if(verbose) printf("checking class %s\n", klass->name);
  
  test = oil_test_new(klass);

#ifdef HAVE_AMD64
  step = 16;
#endif

  for (align = 0; align <= 32; align += step) {
    printf("  alignment %d\n", align);
    realign_klass = klass;
    realign_align = align;
    realign(align);
    failed |= realign_return;
  }
  oil_test_free (test);

  return failed;
}

int main (int argc, char *argv[])
{
  int failed = 0;
  int i, n;

  oil_init ();

  n = oil_class_get_n_classes ();
  for (i = 0; i < n; i++) {
    OilFunctionClass *klass = oil_class_get_by_index(i);
    failed |= check_class(klass);
  }

  return failed;
}