aboutsummaryrefslogtreecommitdiff
path: root/SingleSource/UnitTests/Vector/AVX512F/m512_test_util.h
blob: c98e174bdb26d4e39a6d6847f0b03965d7c940cf (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
#ifndef M512_TEST_UTIL_H_INCLUDED
#define M512_TEST_UTIL_H_INCLUDED

/*
 * Common declarations useful for writing 512-bit unit tests.
 */

#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <x86intrin.h>

#define ALIGNTO(n) __declspec(align(n))

/*
 * For purposes of unit tests it can be beneficial to suppress inlining
 * simply so that only a single instance of a test function is emitted.
 * Makes it easier to diff A/B assembly output.
 */
#define NOINLINE __declspec(noinline)

/*
 * FULL_IREG(ax) expands to either eax or rax depending on the target.
 */
#if defined(__x86_64) || defined(_M_X64)
#define FULL_IREG(reg) r##reg
#else
#define FULL_IREG(reg) e##reg
#endif

/* Number of elements in an array. */
#define ASIZE(a) (sizeof((a)) / sizeof((a)[0]))

typedef __int64 I64;
typedef unsigned __int64 U64;

typedef union ALIGNTO(64) {

  __m512 zmm;
  __m512d zmmd;
  __m512i zmmi;

  __m256 ymm[2];
  __m256d ymmd[2];
  __m256i ymmi[2];

  __m128 xmm[4];
  __m128d xmmd[4];
  __m128i xmmi[4];

  char c[64];
  signed char s8[64];
  unsigned char u8[64];
  short s16[32];
  unsigned short u16[32];
  int s32[16];
  unsigned int u32[16];
  float f32[16];
  I64 s64[8];
  U64 u64[8];
  double f64[8];

} V512;

int n_errs = 0;

/*
 * Print the low N 32-bit unsigned integers from p.
 */

void NOINLINE display_pd(const V512 *p, const char *banner, int n_elems) {
  int i = 15;

  if (banner) {
    printf("%s", banner);
  }

  for (i = n_elems; i >= 0; i--) {
    printf(" %0.8x", p->u32[i]);
    if (i > 0 && i % 4 == 0) {
      printf("\n");
      if (banner) {
        printf("%*s", (int)strlen((void *)banner), "");
      }
    }
  }
  printf("\n");
}

/*
 * Print the low N 64-bit unsigned integers from p.
 */
void NOINLINE display_pq(const V512 *p, const char *banner, int n_elems) {
  int i = 7;

  if (banner) {
    printf("%s", banner);
  }

  for (i = n_elems; i >= 0; i--) {
    printf(" %0.16llx", p->u64[i]);
    if (i > 0 && i % 4 == 0) {
      printf("\n");
      if (banner) {
        printf("%*s", (int)strlen((void *)banner), "");
      }
    }
  }
  printf("\n");
}

/*
 * Print the low N single precision floats from p.
 */

void NOINLINE display_psf(const V512 *p, const char *banner, int n_elems) {
  int i = 15;

  if (banner) {
    printf("%s", banner);
  }

  for (i = n_elems; i >= 0; i--) {
    printf(" %7g", p->f32[i]);
    if (i > 0 && i % 4 == 0) {
      printf("\n");
      if (banner) {
        printf("%*s", (int)strlen((void *)banner), "");
      }
    }
  }
  printf("\n");
}

/*
 * Print the low N double precision floats from p.
 */

void NOINLINE display_pdf(const V512 *p, const char *banner, int n_elems) {
  int i = 15;

  if (banner) {
    printf("%s", banner);
  }

  for (i = n_elems; i >= 0; i--) {
    printf(" %7g", p->f64[i]);
    if (i > 0 && i % 4 == 0) {
      printf("\n");
      if (banner) {
        printf("%*s", (int)strlen((void *)banner), "");
      }
    }
  }
  printf("\n");
}

/*
 * Check that the low N 32-bit elements of "got" and "expected" are the same.
 */
int NOINLINE check_equal_nd(void *got, void *expected, int n_elems,
                            char *banner, int line) {
  int i, fail = 0;
  V512 *v1 = (V512 *)got;
  V512 *v2 = (V512 *)expected;

  for (i = 0; i < n_elems; i++) {
    if (v1->u32[i] != v2->u32[i]) {
      printf("ERROR(%d): %s failed at %d'th element:  0x%0.8x != 0x%0.8x\n",
             line, banner ? banner : "", i, v1->u32[i], v2->u32[i]);
      display_pd(got, "got:", n_elems);
      display_pd(expected, "exp:", n_elems);
      n_errs++;
      fail = 1;
      break;
    }
  }
  return fail;
}

/*
 * Check that the low N 64-bit elements of "got" and "expected" are the same.
 */
int NOINLINE check_equal_nq(void *got, void *expected, int n_elems,
                            char *banner, int line) {
  int i, fail = 0;
  V512 *v1 = (V512 *)got;
  V512 *v2 = (V512 *)expected;

  for (i = 0; i < n_elems; i++) {
    if (v1->u64[i] != v2->u64[i]) {
      printf(
          "ERROR(%d): %s failed at %d'th element:  0x%0.16llx != 0x%0.16llx\n",
          line, banner ? banner : "", i, v1->u64[i], v2->u64[i]);
      display_pq(got, "got:", n_elems);
      display_pq(expected, "exp:", n_elems);
      n_errs++;
      fail = 1;
      break;
    }
  }
  return fail;
}

double delta = 1e-4;

#define EQUAL_FP(v1, v2)                                                       \
  ((v1) < (v2) ? ((v2) - (v1) < delta) : ((v1) - (v2) < delta))

/*
 * Check that the low N single precision float elements of "got" and "expected"
 * are the same.
 */
int NOINLINE check_equal_nsf(void *got, void *expected, int n_elems,
                             char *banner, int line) {
  int i, fail = 0;
  V512 *v1 = (V512 *)got;
  V512 *v2 = (V512 *)expected;

  for (i = 0; i < n_elems; i++) {
    if (!EQUAL_FP(v1->f32[i], v2->f32[i])) {
      printf("ERROR(%d): %s failed at %d'th element:  %7g != %7g \n", line,
             banner ? banner : "", i, v1->f32[i], v2->f32[i]);
      display_psf(got, "got:", n_elems);
      display_psf(expected, "exp:", n_elems);
      n_errs++;
      fail = 1;
      break;
    }
  }
  return fail;
}

/*
 * Check that the low N double precision float elements of "got" and "expected"
 * are the same.
 */
int NOINLINE check_equal_ndf(void *got, void *expected, int n_elems,
                             char *banner, int line) {
  int i, fail = 0;
  V512 *v1 = (V512 *)got;
  V512 *v2 = (V512 *)expected;

  for (i = 0; i < n_elems; i++) {
    if (!EQUAL_FP(v1->f64[i], v2->f64[i])) {
      printf("ERROR(%d): %s failed at %d'th element:  %7g != %7g \n", line,
             banner ? banner : "", i, v1->f64[i], v2->f64[i]);
      display_pdf(got, "got:", n_elems);
      display_pdf(expected, "exp:", n_elems);
      n_errs++;
      fail = 1;
      break;
    }
  }
  return fail;
}

#endif /* M512_TEST_UTIL_H_INCLUDED */