summaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.target/powerpc/vsx-vector-5.c
blob: 65843e93fbd21ec21676a98686c107eb48148d26 (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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/* { dg-do compile { target { powerpc*-*-* && lp64 } } } */
/* { dg-skip-if "" { powerpc*-*-darwin* } { "*" } { "" } } */
/* { dg-require-effective-target powerpc_vsx_ok } */
/* { dg-options "-mvsx -O2" } */

/* This will run, and someday we should add the support to test whether we are
   running on VSX hardware.  */

#include <altivec.h>
#include <stdlib.h>

#ifdef DEBUG
#include <stdio.h>

static int errors = 0;
#endif

union args {
  double scalar[2];
  vector double vect;
};

union largs {
  unsigned long scalar[2];
  vector bool long vect;
};

static void
do_test (union args *expected, union args *got, const char *name)
{
  if (expected->scalar[0] != got->scalar[0]
      || expected->scalar[1] != got->scalar[1])
    {
#ifdef DEBUG
      printf ("%s failed!\n", name);
      errors++;
#else
      abort ();
#endif
    }
}

static void
do_ltest (union largs *expected, union largs *got, const char *name)
{
  if (expected->scalar[0] != got->scalar[0]
      || expected->scalar[1] != got->scalar[1])
    {
#ifdef DEBUG
      printf ("%s failed!\n", name);
      errors++;
#else
      abort ();
#endif
    }
}


/* Vec functions taking a single argument.  */
static vector double
vabs (vector double arg)
{
  return vec_abs (arg);
}

static vector double
vceil (vector double arg)
{
  return vec_ceil (arg);
}

static vector double
vfloor (vector double arg)
{
  return vec_floor (arg);
}

static vector double
vnearbyint (vector double arg)
{
  return vec_nearbyint (arg);
}

static vector double
vrint (vector double arg)
{
  return vec_rint (arg);
}

static vector double
vsqrt (vector double arg)
{
  return vec_sqrt (arg);
}

/* Single argument tests.  */
static struct
{
  union args result;
  union args input;
  vector double (*func) (vector double);
  const char *name;
} arg1_tests[] = {
  /* result		input			function	name */
  { {  1.0,  2.0 },	{ -1.0,  2.0 },		vabs,		"vabs" },
  { {  1.0,  2.0 },	{  1.0, -2.0 },		vabs,		"vabs" },
  { {  2.0,  2.0 },	{  1.1,  1.7 },		vceil,		"vceil" },
  { { -1.0, -1.0 },	{ -1.1, -1.7 },		vceil,		"vceil" },
  { { -1.0,  2.0 },	{ -1.5,  1.5 },		vceil,		"vceil" },
  { {  1.0,  1.0 },	{  1.1,  1.7 },		vfloor,		"vfloor" },
  { { -2.0, -2.0 },	{ -1.1, -1.7 },		vfloor,		"vfloor" },
  { { -2.0,  1.0 },	{ -1.5,  1.5 },		vfloor,		"vfloor" },
  { {  1.0,  2.0 },	{  1.1,  1.7 },		vnearbyint,	"vnearbyint" },
  { { -1.0, -2.0 },	{ -1.1, -1.7 },		vnearbyint,	"vnearbyint" },
  { { -2.0,  2.0 },	{ -1.5,  1.5 },		vnearbyint,	"vnearbyint" },
  { {  1.0,  2.0 },	{  1.1,  1.7 },		vrint,		"vrint" },
  { { -1.0, -2.0 },	{ -1.1, -1.7 },		vrint,		"vrint" },
  { { -2.0,  2.0 },	{ -1.5,  1.5 },		vrint,		"vrint" },
  { {  2.0,  4.0 },	{  4.0, 16.0 },		vsqrt,		"vsqrt" },
};

static void
test_arg1 (void)
{
  unsigned i;

#ifdef DEBUG
  printf ("Single argument tests:\n");
#endif

  for (i = 0; i < sizeof (arg1_tests) / sizeof (arg1_tests[0]); i++)
    {
      union args u;
      u.vect = arg1_tests[i].func (arg1_tests[i].input.vect);

#ifdef DEBUG
      printf ("test %-16s: expected { %4g, %4g }, got { %4g, %4g }, input { %4g, %4g }\n",
	      arg1_tests[i].name,
	      arg1_tests[i].result.scalar[0],
	      arg1_tests[i].result.scalar[1],
	      u.scalar[0],
	      u.scalar[1],
	      arg1_tests[i].input.scalar[0],
	      arg1_tests[i].input.scalar[1]);
#endif

      do_test (&arg1_tests[i].result, &u, arg1_tests[i].name);
    }

  return;
}


/* Vect functions taking 2 arguments.  */
static vector double
vadd (vector double arg1, vector double arg2)
{
  return vec_add (arg1, arg2);
}

static vector double
vadd2 (vector double arg1, vector double arg2)
{
  return arg1 + arg2;
}

static vector double
vsub (vector double arg1, vector double arg2)
{
  return vec_sub (arg1, arg2);
}

static vector double
vsub2 (vector double arg1, vector double arg2)
{
  return arg1 - arg2;
}

static vector double
vmul (vector double arg1, vector double arg2)
{
  return vec_mul (arg1, arg2);
}

static vector double
vmul2 (vector double arg1, vector double arg2)
{
  return arg1 * arg2;
}

static vector double
vdiv (vector double arg1, vector double arg2)
{
  return vec_div (arg1, arg2);
}

static vector double
vdiv2 (vector double arg1, vector double arg2)
{
  return arg1 / arg2;
}

static vector double
vmax (vector double arg1, vector double arg2)
{
  return vec_max (arg1, arg2);
}

static vector double
vmin (vector double arg1, vector double arg2)
{
  return vec_min (arg1, arg2);
}

/* 2 argument tests.  */
static struct
{
  union args result;
  union args input[2];
  vector double (*func) (vector double, vector double);
  const char *name;
} arg2_tests[] = {
  /* result */
  { {  4.0,  6.0 },	{ {  1.0,  2.0 }, {  3.0,  4.0 } },	vadd,	"vadd"  },
  { {  4.0, -6.0 },	{ {  1.0, -2.0 }, {  3.0, -4.0 } },	vadd,	"vadd"  },
  { {  4.0,  6.0 },	{ {  1.0,  2.0 }, {  3.0,  4.0 } },	vadd2,	"vadd2" },
  { {  4.0, -6.0 },	{ {  1.0, -2.0 }, {  3.0, -4.0 } },	vadd2,	"vadd2" },
  { { -2.0, -2.0 },	{ {  1.0,  2.0 }, {  3.0,  4.0 } },	vsub,	"vsub"  },
  { { -2.0,  2.0 },	{ {  1.0, -2.0 }, {  3.0, -4.0 } },	vsub,	"vsub"  },
  { { -2.0, -2.0 },	{ {  1.0,  2.0 }, {  3.0,  4.0 } },	vsub2,	"vsub2" },
  { { -2.0,  2.0 },	{ {  1.0, -2.0 }, {  3.0, -4.0 } },	vsub2,	"vsub2" },
  { {  6.0,  4.0 },	{ {  2.0,  8.0 }, {  3.0,  0.5 } },	vmul,	"vmul"  },
  { {  6.0,  4.0 },	{ {  2.0,  8.0 }, {  3.0,  0.5 } },	vmul2,	"vmul2" },
  { {  2.0,  0.5 },	{ {  6.0,  4.0 }, {  3.0,  8.0 } },	vdiv,	"vdiv"  },
  { {  2.0,  0.5 },	{ {  6.0,  4.0 }, {  3.0,  8.0 } },	vdiv2,	"vdiv2" },
  { {  3.0,  4.0 },	{ {  1.0,  2.0 }, {  3.0,  4.0 } },	vmax,	"vmax"  },
  { {  1.0,  4.0 },	{ {  1.0, -2.0 }, { -3.0,  4.0 } },	vmax,	"vmax"  },
  { {  1.0,  2.0 },	{ {  1.0,  2.0 }, {  3.0,  4.0 } },	vmin,	"vmin"  },
  { { -3.0, -2.0 },	{ {  1.0, -2.0 }, { -3.0,  4.0 } },	vmin,	"vmin"  },
};

static void
test_arg2 (void)
{
  unsigned i;

#ifdef DEBUG
  printf ("\nTwo argument tests:\n");
#endif

  for (i = 0; i < sizeof (arg2_tests) / sizeof (arg2_tests[0]); i++)
    {
      union args u;
      u.vect = arg2_tests[i].func (arg2_tests[i].input[0].vect,
				   arg2_tests[i].input[1].vect);

#ifdef DEBUG
      printf ("test %-16s: expected { %4g, %4g }, got { %4g, %4g }, input { %4g, %4g }, { %4g, %4g }\n",
	      arg2_tests[i].name,
	      arg2_tests[i].result.scalar[0],
	      arg2_tests[i].result.scalar[1],
	      u.scalar[0],
	      u.scalar[1],
	      arg2_tests[i].input[0].scalar[0],
	      arg2_tests[i].input[0].scalar[1],
	      arg2_tests[i].input[1].scalar[0],
	      arg2_tests[i].input[1].scalar[1]);
#endif

      do_test (&arg2_tests[i].result, &u, arg2_tests[i].name);
    }

  return;
}


/* Comparisons, returnning a boolean vector.  */
static vector bool long
vcmpeq (vector double arg1, vector double arg2)
{
  return vec_cmpeq (arg1, arg2);
}

static vector bool long
vcmplt (vector double arg1, vector double arg2)
{
  return vec_cmplt (arg1, arg2);
}

static vector bool long
vcmple (vector double arg1, vector double arg2)
{
  return vec_cmple (arg1, arg2);
}

static vector bool long
vcmpgt (vector double arg1, vector double arg2)
{
  return vec_cmpgt (arg1, arg2);
}

static vector bool long
vcmpge (vector double arg1, vector double arg2)
{
  return vec_cmpge (arg1, arg2);
}

#define ONE  0xffffffffffffffffUL
#define ZERO 0x0000000000000000UL

/* comparison tests.  */
static struct
{
  union largs result;
  union args input[2];
  vector bool long (*func) (vector double, vector double);
  const char *name;
} argcmp_tests[] = {
  { { ONE,  ZERO }, { {  1.0,  2.0 }, {  1.0, -2.0 } },	vcmpeq,	"vcmpeq" },
  { { ZERO, ONE  }, { { -1.0,  2.0 }, {  1.0,  2.0 } },	vcmpeq,	"vcmpeq" },

  { { ONE,  ONE  }, { {  1.0, -2.0 }, {  1.0, -2.0 } },	vcmple,	"vcmple" },
  { { ONE,  ONE  }, { {  1.0, -2.0 }, {  2.0, -1.0 } },	vcmple,	"vcmple" },
  { { ZERO, ZERO }, { {  2.0, -1.0 }, {  1.0, -2.0 } },	vcmple,	"vcmple" },

  { { ZERO, ZERO }, { {  1.0, -2.0 }, {  1.0, -2.0 } },	vcmplt,	"vcmplt" },
  { { ONE,  ONE  }, { {  1.0, -2.0 }, {  2.0, -1.0 } },	vcmplt,	"vcmplt" },
  { { ZERO, ZERO }, { {  2.0, -1.0 }, {  1.0, -2.0 } },	vcmplt,	"vcmplt" },

  { { ZERO, ZERO }, { {  1.0, -2.0 }, {  1.0, -2.0 } },	vcmpgt,	"vcmpgt" },
  { { ZERO, ZERO }, { {  1.0, -2.0 }, {  2.0, -1.0 } },	vcmpgt,	"vcmpgt" },
  { { ONE,  ONE  }, { {  2.0, -1.0 }, {  1.0, -2.0 } },	vcmpgt,	"vcmpgt" },

  { { ONE,  ONE  }, { {  1.0, -2.0 }, {  1.0, -2.0 } },	vcmpge,	"vcmpge" },
  { { ZERO, ZERO }, { {  1.0, -2.0 }, {  2.0, -1.0 } },	vcmpge,	"vcmpge" },
  { { ONE,  ONE  }, { {  2.0, -1.0 }, {  1.0, -2.0 } },	vcmpge,	"vcmpge" },
};

static void
test_argcmp (void)
{
  unsigned i;

#ifdef DEBUG
  printf ("\nComparison tests:\n");
#endif

  for (i = 0; i < sizeof (argcmp_tests) / sizeof (argcmp_tests[0]); i++)
    {
      union largs u;
      u.vect = argcmp_tests[i].func (argcmp_tests[i].input[0].vect,
				     argcmp_tests[i].input[1].vect);

#ifdef DEBUG
      printf ("test %-16s: expected { 0x%016lx, 0x%016lx }, got { 0x%016lx, 0x%016lx }, input { %4g, %4g }, { %4g, %4g }\n",
	      argcmp_tests[i].name,
	      argcmp_tests[i].result.scalar[0],
	      argcmp_tests[i].result.scalar[1],
	      u.scalar[0],
	      u.scalar[1],
	      argcmp_tests[i].input[0].scalar[0],
	      argcmp_tests[i].input[0].scalar[1],
	      argcmp_tests[i].input[1].scalar[0],
	      argcmp_tests[i].input[1].scalar[1]);
#endif

      do_ltest (&argcmp_tests[i].result, &u, argcmp_tests[i].name);
    }

  return;
}


int
main (int argc, char *argv[])
{
  test_arg1 ();
  test_arg2 ();
  test_argcmp ();

#ifdef DEBUG
  if (errors)
    {
      printf ("There were %d error(s)\n", errors);
      return errors;
    }
  else
    printf ("There were no errors\n");
#endif
  
  return 0;
}