aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.target/powerpc/vec-setup.h
blob: 9e251646d2d5a62385b4d3961b78cd85297326e5 (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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include <altivec.h>

/* Test various ways of creating vectors with 2 double words and accessing the
   elements.  This include files supports:

	 testing double
	 testing long on 64-bit systems
	 testing long long on 32-bit systems.

   The endian support is:

	big endian
	little endian with little endian element ordering
	little endian with big endian element ordering.  */

#ifdef DEBUG
#include <stdio.h>
#define DEBUG0(STR)		fputs (STR, stdout)
#define DEBUG2(STR,A,B)		printf (STR, A, B)

static int errors = 0;

#else
#include <stdlib.h>
#define DEBUG0(STR)
#define DEBUG2(STR,A,B)
#endif

#if defined(DO_DOUBLE)
#define TYPE	double
#define STYPE	"double"
#define ZERO	0.0
#define ONE	1.0
#define TWO	2.0
#define THREE	3.0
#define FOUR	4.0
#define FIVE	5.0
#define SIX	6.0
#define FMT	"g"

#elif defined(_ARCH_PPC64)
#define TYPE	long
#define STYPE	"long"
#define ZERO	0L
#define ONE	1L
#define TWO	2L
#define THREE	3L
#define FOUR	4L
#define FIVE	5L
#define SIX	6L
#define FMT	"ld"

#else
#define TYPE	long long
#define STYPE	"long long"
#define ZERO	0LL
#define ONE	1LL
#define TWO	2LL
#define THREE	3LL
#define FOUR	4LL
#define FIVE	5LL
#define SIX	6LL
#define FMT	"lld"
#endif

/* Macros to order the left/right values correctly.  Note, -maltivec=be does
   not change the order for static initializations, so we have to handle it
   specially.  */

#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define INIT_ORDER(A, B)	(TYPE) A, (TYPE) B
#define ELEMENT_ORDER(A, B)	(TYPE) A, (TYPE) B
#define ENDIAN			"-mbig"

#elif __VEC_ELEMENT_REG_ORDER__ == __ORDER_BIG_ENDIAN__
#define NO_ARRAY
#define INIT_ORDER(A, B)	(TYPE) B, (TYPE) A
#define ELEMENT_ORDER(A, B)	(TYPE) A, (TYPE) B
#define ENDIAN			"-mlittle -maltivec=be"

#else
#define INIT_ORDER(A, B)	(TYPE) B, (TYPE) A
#define ELEMENT_ORDER(A, B)	(TYPE) B, (TYPE) A
#define ENDIAN			"-mlittle"
#endif

static volatile TYPE		five	= FIVE;
static volatile TYPE		six	= SIX;
static volatile vector TYPE	s_v12 = { ONE,   TWO };
static volatile vector TYPE	g_v34 = { THREE, FOUR };


__attribute__((__noinline__))
static void
vector_check (vector TYPE v, TYPE expect_hi, TYPE expect_lo)
{
  TYPE actual_hi, actual_lo;
#ifdef DEBUG
  const char *pass_fail;
#endif

  __asm__ ("xxlor %x0,%x1,%x1"		: "=&wa" (actual_hi) : "wa" (v));
  __asm__ ("xxpermdi %x0,%x1,%x1,3"	: "=&wa" (actual_lo) : "wa" (v));

#ifdef DEBUG
  if ((actual_hi == expect_hi) && (actual_lo == expect_lo))
    pass_fail = ", pass";
  else
    {
      pass_fail = ", fail";
      errors++;
    }

  printf ("Expected %" FMT ", %" FMT ", got %" FMT ", %" FMT "%s\n",
	  expect_hi, expect_lo,
	  actual_hi, actual_lo,
	  pass_fail);
#else
  if ((actual_hi != expect_hi) || (actual_lo != expect_lo))
    abort ();
#endif
}

__attribute__((__noinline__))
static vector TYPE
combine (TYPE op0, TYPE op1)
{
  return (vector TYPE) { op0, op1 };
}

__attribute__((__noinline__))
static vector TYPE
combine_insert (TYPE op0, TYPE op1)
{
  vector TYPE ret = (vector TYPE) { ZERO, ZERO };
  ret = vec_insert (op0, ret, 0);
  ret = vec_insert (op1, ret, 1);
  return ret;
}

__attribute__((__noinline__))
static vector TYPE
concat_extract_00 (vector TYPE a, vector TYPE b)
{
  return (vector TYPE) { vec_extract (a, 0), vec_extract (b, 0) };
}

__attribute__((__noinline__))
static vector TYPE
concat_extract_01 (vector TYPE a, vector TYPE b)
{
  return (vector TYPE) { vec_extract (a, 0), vec_extract (b, 1) };
}

__attribute__((__noinline__))
static vector TYPE
concat_extract_10 (vector TYPE a, vector TYPE b)
{
  return (vector TYPE) { vec_extract (a, 1), vec_extract (b, 0) };
}

__attribute__((__noinline__))
static vector TYPE
concat_extract_11 (vector TYPE a, vector TYPE b)
{
  return (vector TYPE) { vec_extract (a, 1), vec_extract (b, 1) };
}

__attribute__((__noinline__))
static vector TYPE
concat_extract2_0s (vector TYPE a, TYPE b)
{
  return (vector TYPE) { vec_extract (a, 0), b };
}

__attribute__((__noinline__))
static vector TYPE
concat_extract2_1s (vector TYPE a, TYPE b)
{
  return (vector TYPE) { vec_extract (a, 1), b };
}

__attribute__((__noinline__))
static vector TYPE
concat_extract2_s0 (TYPE a, vector TYPE b)
{
  return (vector TYPE) { a, vec_extract (b, 0) };
}

__attribute__((__noinline__))
static vector TYPE
concat_extract2_s1 (TYPE a, vector TYPE b)
{
  return (vector TYPE) { a, vec_extract (b, 1) };
}

__attribute__((__noinline__))
static vector TYPE
concat_extract_nn (vector TYPE a, vector TYPE b, size_t i, size_t j)
{
  return (vector TYPE) { vec_extract (a, i), vec_extract (b, j) };
}

#ifndef NO_ARRAY
__attribute__((__noinline__))
static vector TYPE
array_0 (vector TYPE v, TYPE a)
{
  v[0] = a;
  return v;
}

__attribute__((__noinline__))
static vector TYPE
array_1 (vector TYPE v, TYPE a)
{
  v[1] = a;
  return v;
}

__attribute__((__noinline__))
static vector TYPE
array_01 (vector TYPE v, TYPE a, TYPE b)
{
  v[0] = a;
  v[1] = b;
  return v;
}

__attribute__((__noinline__))
static vector TYPE
array_01b (TYPE a, TYPE b)
{
  vector TYPE v = (vector TYPE) { 0, 0 };
  v[0] = a;
  v[1] = b;
  return v;
}
#endif

int
main (void)
{
  vector TYPE a = (vector TYPE) { ONE,   TWO  };
  vector TYPE b = (vector TYPE) { THREE, FOUR };
  size_t i, j;

#ifndef NO_ARRAY
  vector TYPE z = (vector TYPE) { ZERO,  ZERO };
#endif

  DEBUG2 ("Endian: %s, type: %s\n", ENDIAN, STYPE);
  DEBUG0 ("\nStatic/global initialization\n");
  vector_check (s_v12, INIT_ORDER (1, 2));
  vector_check (g_v34, INIT_ORDER (3, 4));

  DEBUG0 ("\nVector via constant runtime intiialization\n");
  vector_check (a, INIT_ORDER (1, 2));
  vector_check (b, INIT_ORDER (3, 4));

  DEBUG0 ("\nCombine scalars using vector initialization\n");
  vector_check (combine (1, 2), INIT_ORDER (1, 2));
  vector_check (combine (3, 4), INIT_ORDER (3, 4));

  DEBUG0 ("\nSetup with vec_insert\n");
  a = combine_insert (1, 2);
  b = combine_insert (3, 4);
  vector_check (a, ELEMENT_ORDER (1, 2));
  vector_check (b, ELEMENT_ORDER (3, 4));

#ifndef NO_ARRAY
  DEBUG0 ("\nTesting array syntax\n");
  vector_check (array_0   (a, FIVE),      ELEMENT_ORDER (5, 2));
  vector_check (array_1   (b, SIX),       ELEMENT_ORDER (3, 6));
  vector_check (array_01  (z, FIVE, SIX), ELEMENT_ORDER (5, 6));
  vector_check (array_01b (FIVE, SIX),    ELEMENT_ORDER (5, 6));

  vector_check (array_0   (a, five),      ELEMENT_ORDER (5, 2));
  vector_check (array_1   (b, six),       ELEMENT_ORDER (3, 6));
  vector_check (array_01  (z, five, six), ELEMENT_ORDER (5, 6));
  vector_check (array_01b (five, six),    ELEMENT_ORDER (5, 6));
#else
  DEBUG0 ("\nSkipping array syntax on -maltivec=be\n");
#endif

  DEBUG0 ("\nTesting concat and extract\n");
  vector_check (concat_extract_00 (a, b), INIT_ORDER (1, 3));
  vector_check (concat_extract_01 (a, b), INIT_ORDER (1, 4));
  vector_check (concat_extract_10 (a, b), INIT_ORDER (2, 3));
  vector_check (concat_extract_11 (a, b), INIT_ORDER (2, 4));

  DEBUG0 ("\nTesting concat and extract #2\n");
  vector_check (concat_extract2_0s (a, FIVE), INIT_ORDER (1, 5));
  vector_check (concat_extract2_1s (a, FIVE), INIT_ORDER (2, 5));
  vector_check (concat_extract2_s0 (SIX, a),  INIT_ORDER (6, 1));
  vector_check (concat_extract2_s1 (SIX, a),  INIT_ORDER (6, 2));

  DEBUG0 ("\nTesting variable concat and extract\n");
  for (i = 0; i < 2; i++)
    {
      for (j = 0; j < 2; j++)
	{
	  static struct {
	    TYPE hi;
	    TYPE lo;
	  } hilo[2][2] =
	      { { { ONE, THREE }, { ONE, FOUR } },
		{ { TWO, THREE }, { TWO, FOUR } } };

	  vector_check (concat_extract_nn (a, b, i, j),
			INIT_ORDER (hilo[i][j].hi, hilo[i][j].lo));
	}
    }

  DEBUG0 ("\nTesting separate function\n");
  vector_check (combine (vec_extract (a, 0), vec_extract (b, 0)),
		INIT_ORDER (1, 3));

  vector_check (combine (vec_extract (a, 0), vec_extract (b, 1)),
		INIT_ORDER (1, 4));

  vector_check (combine (vec_extract (a, 1), vec_extract (b, 0)),
		INIT_ORDER (2, 3));

  vector_check (combine (vec_extract (a, 1), vec_extract (b, 1)),
		INIT_ORDER (2, 4));

  vector_check (combine_insert (vec_extract (a, 0), vec_extract (b, 0)),
		ELEMENT_ORDER (1, 3));

  vector_check (combine_insert (vec_extract (a, 0), vec_extract (b, 1)),
		ELEMENT_ORDER (1, 4));

  vector_check (combine_insert (vec_extract (a, 1), vec_extract (b, 0)),
		ELEMENT_ORDER (2, 3));

  vector_check (combine_insert (vec_extract (a, 1), vec_extract (b, 1)),
		ELEMENT_ORDER (2, 4));


#if defined(DO_DOUBLE)
  DEBUG0 ("\nTesting explicit 2df concat\n");
  vector_check (__builtin_vsx_concat_2df (FIVE, SIX), INIT_ORDER (5, 6));
  vector_check (__builtin_vsx_concat_2df (five, six), INIT_ORDER (5, 6));

#elif defined(_ARCH_PPC64)
  DEBUG0 ("\nTesting explicit 2di concat\n");
  vector_check (__builtin_vsx_concat_2di (FIVE, SIX), INIT_ORDER (5, 6));
  vector_check (__builtin_vsx_concat_2di (five, six), INIT_ORDER (5, 6));

#else
  DEBUG0 ("\nSkip explicit 2di concat on 32-bit\n");
#endif

#ifdef DEBUG
  if (errors)
    printf ("\n%d error%s were found", errors, (errors == 1) ? "" : "s");
  else
    printf ("\nNo errors were found.\n");

  return errors;

#else
  return 0;
#endif
}