summaryrefslogtreecommitdiff
path: root/orc-test/orcarray.c
blob: 6e9c70f180bdfb020bcae67b2f7d7e12071ff90a (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

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

#include <orc-test/orctest.h>
#include <orc-test/orcarray.h>
#include <orc-test/orcrandom.h>
#include <orc/orc.h>
#include <orc/orcutils.h>
#include <orc/orcdebug.h>

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>

#ifdef HAVE_MMAP
#include <sys/mman.h>
#endif

#ifdef HAVE_MMAP
/* This can be used to test non-zero high-32-bits of pointers. */
//#define USE_MMAP
#endif

#define EXTEND_ROWS 16
#define EXTEND_STRIDE 256

#ifdef _MSC_VER
#define isnan(x) _isnan(x)
#endif

#define ALIGNMENT 64
#define MISALIGNMENT 0

OrcArray *
orc_array_new (int n, int m, int element_size, int misalignment,
    int alignment)
{
  OrcArray *ar;
  void *data;
#ifndef USE_MMAP
#ifdef HAVE_POSIX_MEMALIGN
  int ret;
#endif
#endif
  int offset;
#ifdef USE_MMAP
  static unsigned long idx = 1;
#endif

  ar = malloc (sizeof(OrcArray));
  memset (ar, 0, sizeof(OrcArray));

  ar->n = n;
  ar->m = m;
  ar->element_size = element_size;

  ar->stride = (n*element_size + EXTEND_STRIDE);
  ar->stride = (ar->stride + (ALIGNMENT-1)) & (~(ALIGNMENT-1));
  ar->alloc_len = ar->stride * (m+2*EXTEND_ROWS) + (ALIGNMENT * element_size);
  ar->alloc_len = (ar->alloc_len + 4095) & (~4095);

#ifdef USE_MMAP
  data = mmap ((void *)(idx<<32), ar->alloc_len, PROT_READ|PROT_WRITE,
      MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
  idx++;
#else
#ifdef HAVE_POSIX_MEMALIGN
  ret = posix_memalign (&data, ALIGNMENT, ar->alloc_len);
#else
  data = malloc (ar->alloc_len);
#endif
#endif
  ar->alloc_data = data;

  if (alignment == 0) alignment = element_size;
  offset = (alignment * misalignment) & (ALIGNMENT - 1);

  ar->data = ORC_PTR_OFFSET (ar->alloc_data,
      ar->stride * EXTEND_ROWS + offset);

  return ar;
}

void
orc_array_free (OrcArray *array)
{
#ifdef USE_MMAP
  munmap (array->alloc_data, array->alloc_len);
#else
  free (array->alloc_data);
#endif
  free (array);
}

void
orc_array_set_pattern (OrcArray *array, int value)
{
  memset (array->alloc_data, value, array->alloc_len);
}

void
orc_array_set_random (OrcArray *array, OrcRandomContext *context)
{
  orc_random_bits (context, array->alloc_data, array->alloc_len);
}

#define CREATE_FLOAT(sign,exp,mant) (((sign)<<31)|((exp)<<23)|((mant)<<0))

static const orc_uint32 special_floats[] = {
  CREATE_FLOAT(0,0,0), /* 0 */
  CREATE_FLOAT(1,0,0), /* -0 */
  CREATE_FLOAT(0,126,0), /* 0.5 */
  CREATE_FLOAT(0,127,0), /* 1 */
  CREATE_FLOAT(0,128,0), /* 2 */
  CREATE_FLOAT(1,126,0), /* -0.5 */
  CREATE_FLOAT(1,127,0), /* -1 */
  CREATE_FLOAT(1,128,0), /* -2 */
  CREATE_FLOAT(0,255,0), /* infinity */
  CREATE_FLOAT(1,255,0), /* -infinity */
  CREATE_FLOAT(0,255,1), /* nan */
  CREATE_FLOAT(1,255,1), /* -nan */
  CREATE_FLOAT(0,0,1), /* denormal */
  CREATE_FLOAT(1,0,1), /* -denormal */
  CREATE_FLOAT(0,127+31,0), /* MAX_INT+1 */
  CREATE_FLOAT(0,127+30,0x7fffff), /* largest float < MAX_INT */
  CREATE_FLOAT(0,127+23,0x7fffff), /* largest non-integer float */
  CREATE_FLOAT(1,127+31,0), /* MIN_INT */
  CREATE_FLOAT(1,127+31,1), /* MIN_INT-1 */
  CREATE_FLOAT(1,127+30,0x7fffff), /* largest float >= MIN_INT */
  CREATE_FLOAT(1,127+23,0x7fffff), /* (negative) largest non-integer float */
  CREATE_FLOAT(0,127+14,(32767-16384)<<(23-14)), /* 32767 */
  CREATE_FLOAT(0,127+15,(0)<<(23-15)), /* 32768 */
  CREATE_FLOAT(0,127+15,(1)<<(23-15)), /* -32769 */
  CREATE_FLOAT(1,127+14,(32767-16384)<<(23-14)), /* -32767 */
  CREATE_FLOAT(1,127+15,(0)<<(23-15)), /* -32768 */
  CREATE_FLOAT(1,127+15,(1)<<(23-15)), /* -32769 */
  CREATE_FLOAT(0,127+4,(27-16)<<(23-4)), /* 27 */
  CREATE_FLOAT(0,127+4,(28-16)<<(23-4)), /* 28 */
  CREATE_FLOAT(0,127+4,(29-16)<<(23-4)), /* 29 */
  CREATE_FLOAT(0,127+4,(30-16)<<(23-4)), /* 30 */
  CREATE_FLOAT(0,127+4,(31-16)<<(23-4)), /* 31 */
};

void
orc_array_set_pattern_2 (OrcArray *array, OrcRandomContext *context,
    int type)
{
  int i,j;

  switch (type) {
    case ORC_PATTERN_RANDOM:
      orc_random_bits (context, array->alloc_data, array->alloc_len);
      break;
    case ORC_PATTERN_FLOAT_SMALL:
      {
        if (array->element_size != 4) return;
        for(j=0;j<array->m;j++){
          orc_union32 *data;
          int exp;

          data = ORC_PTR_OFFSET(array->data, array->stride * j);

          for(i=0;i<array->n;i++){
            data[i].i = orc_random (context);
            exp = (data[i].i & 0x7f80000) >> 23;
            exp &= 0xf;
            exp += 122;
            data[i].i &= ~0x7f800000;
            data[i].i |= (exp&0xff) << 23;
          }
        }
      }
      break;
    case ORC_PATTERN_FLOAT_SPECIAL:
      {
        if (array->element_size != 4) return;
        for(j=0;j<array->m;j++){
          orc_union32 *data;
          int x;

          data = ORC_PTR_OFFSET(array->data, array->stride * j);

          for(i=0;i<array->n;i++){
            x = i&0x1f;
            data[i].i = special_floats[x];
          }
        }
      }
      break;
    case ORC_PATTERN_FLOAT_DENORMAL:
      {
        if (array->element_size != 4) return;
        for(j=0;j<array->m;j++){
          orc_union32 *data;

          data = ORC_PTR_OFFSET(array->data, array->stride * j);

          for(i=0;i<array->n;i++){
            data[i].i = orc_random (context);
            data[i].i &= ~0x7f800000;
          }
        }
      }
      break;
    default:
      break;
  }
}

#define MIN_NONDENORMAL (1.1754944909521339405e-38)
#define MIN_NONDENORMAL_D (2.2250738585072014e-308)

int
orc_array_compare (OrcArray *array1, OrcArray *array2, int flags)
{
  if ((flags & ORC_TEST_FLAGS_FLOAT)) {
    if (array1->element_size == 4) {
      int j;
      for(j=0;j<array1->m;j++){
        float *a, *b;
        int i;

        a = ORC_PTR_OFFSET (array1->data, j*array1->stride);
        b = ORC_PTR_OFFSET (array2->data, j*array2->stride);

        for (i=0;i<array1->n;i++){
          if (isnan(a[i]) && isnan(b[i])) continue;
          if (a[i] == b[i]) continue;
          if (fabs(a[i] - b[i]) < MIN_NONDENORMAL) continue;
          return FALSE;
        }
      }
      return TRUE;
    } else if (array1->element_size == 8) {
      int j;
      for(j=0;j<array1->m;j++){
        double *a, *b;
        int i;

        a = ORC_PTR_OFFSET (array1->data, j*array1->stride);
        b = ORC_PTR_OFFSET (array2->data, j*array2->stride);

        for (i=0;i<array1->n;i++){
          if (isnan(a[i]) && isnan(b[i])) continue;
          if (a[i] == b[i]) continue;
          if (abs(a[i] - b[i]) < MIN_NONDENORMAL_D) continue;
          return FALSE;
        }
      }
      return TRUE;
    }
  } else {
    if (memcmp (array1->alloc_data, array2->alloc_data,
          array1->alloc_len) == 0) {
      return TRUE;
    }
  }

  return FALSE;
}

int
orc_array_check_out_of_bounds (OrcArray *array)
{
  int i;
  int j;
  unsigned char *data;
  
  data = array->alloc_data;
  for(i=0;i<array->stride * EXTEND_ROWS;i++){
    if (data[i] != ORC_OOB_VALUE) {
      printf("OOB check failed at start-%d\n", array->stride * EXTEND_ROWS - i);
      return FALSE;
    }
  }

  for(j=0;j<array->m;j++){
    data = ORC_PTR_OFFSET(array->data, array->stride * j);
    for(i=array->element_size * array->n;i<array->stride;i++){
      if (data[i] != ORC_OOB_VALUE) {
        printf("OOB check failed on row %d, end+%d\n", j,
            i - array->element_size * array->n);
        return FALSE;
      }
    }
  }

  data = ORC_PTR_OFFSET (array->data, array->stride * array->m);
  for(i=0;i<array->stride * EXTEND_ROWS;i++){
    if (data[i] != ORC_OOB_VALUE) {
      printf("OOB check failed at end+%d\n", i);
      return FALSE;
    }
  }

  return TRUE;
}

#if 0
void
orc_array_print_compare (OrcArray *array1, OrcArray *array2)
{

  for(j=0;j<array1->m;j++){
    for(i=0;i<array1->n;i++){
      int a,b;
      int j;

      printf("%2d %2d:", i, j);

      for(k=0;k<ORC_N_VARIABLES;k++){
        if (program->vars[k].name == NULL) continue;
        if (program->vars[k].vartype == ORC_VAR_TYPE_SRC &&
            program->vars[k].size > 0) {
          print_array_val_signed (ex->arrays[k], program->vars[k].size, i);
        }
      }

      printf(" ->");
      a = print_array_val_signed (dest_emul[k], program->vars[k].size, i);
      b = print_array_val_signed (dest_exec[k], program->vars[k].size, i);

      if (a != b) {
        printf(" *");
      }

      printf("\n");
    }
  }
}
#endif