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
|
/* { dg-do run { target { powerpc*-*-linux* && lp64 } } } */
/* { dg-skip-if "" { powerpc*-*-darwin* } } */
/* { dg-skip-if "" { powerpc*-*-*spe* } } */
/* { dg-require-effective-target p8vector_hw } */
/* { dg-skip-if "do not override -mcpu" { powerpc*-*-* } { "-mcpu=*" } { "-mcpu=power8" } } */
/* { dg-options "-mcpu=power8 -O2" } */
#include <stddef.h>
#include <stdlib.h>
#include <altivec.h>
#ifdef DEBUG
#include <stdio.h>
#define UNUSED
#ifdef __LITTLE_ENDIAN__
#define HI_WORD 1
#define LO_WORD 0
#else
#define HI_WORD 0
#define LO_WORD 1
#endif
#else
#define UNUSED __attribute__((__unused__))
#endif
#ifndef S_TYPE
#define S_TYPE __uint128_t
#endif
#ifndef V_TYPE
#define V_TYPE vector S_TYPE
#endif
static int compare (S_TYPE, V_TYPE, const char *, const char *)
__attribute__((__noinline__));
static int
compare (S_TYPE scalar,
V_TYPE vect,
const char *nl UNUSED,
const char *which UNUSED)
{
unsigned long scalar_lo = (unsigned long) scalar;
unsigned long scalar_hi = (unsigned long) (scalar >> 64);
unsigned long vect_lo;
unsigned long vect_hi;
vector long long tmp;
int ret;
__asm__ ("mfvsrd %0,%x3\n\t"
"xxpermdi %x2,%x3,%x3,3\n\t"
"mfvsrd %1,%x2"
: "=r" (vect_hi),
"=r" (vect_lo),
"=wa" (tmp)
: "wa" (vect));
ret = (scalar_lo != vect_lo) || (scalar_hi != vect_hi);
#ifdef DEBUG
printf ("%s%s: 0x%.16lx %.16lx %s 0x%.16lx %.16lx\n",
nl, which,
scalar_hi, scalar_lo,
(ret) ? "!=" : "==",
vect_hi, vect_lo);
fflush (stdout);
#endif
return ret;
}
static void convert_via_mem (V_TYPE *, S_TYPE *)
__attribute__((__noinline__));
static void
convert_via_mem (V_TYPE *v, S_TYPE *s)
{
*v = (V_TYPE) { *s };
__asm__ volatile ("nop"
: "+m" (*s), "+m" (*v)
:
: "memory");
}
/* Check if vadduqm returns the same values as normal 128-bit add. */
/* Values to add together. */
const static struct {
unsigned long hi_1;
unsigned long lo_1;
unsigned long hi_2;
unsigned long lo_2;
} values[] = {
{ 0x0000000000000000UL, 0xfffffffffffffffeUL,
0x0000000000000000UL, 0x0000000000000002UL },
{ 0x0000000000000000UL, 0x0000000000000002UL,
0x0000000000000000UL, 0xfffffffffffffffeUL },
{ 0xffffffffffffffffUL, 0xfffffffffffffffeUL,
0x0000000000000000UL, 0x0000000000000002UL },
{ 0xfffffffffffffff2UL, 0xffffffffffffffffUL,
0x0000000000000002UL, 0x0000000000000000UL },
{ 0x7fffffffffffffffUL, 0xfffffffffffffffeUL,
0x0000000000000000UL, 0x0000000000000002UL },
{ 0x7ffffffffffffff2UL, 0xffffffffffffffffUL,
0x0000000000000002UL, 0x0000000000000000UL },
};
int
main (void)
{
int reg_errors = 0;
int mem_errors = 0;
size_t i;
const char *nl = "";
for (i = 0; i < sizeof (values) / sizeof (values[0]); i++)
{
S_TYPE s_reg_res, s_reg_in1, s_reg_in2, s_mem_res, s_mem_in1, s_mem_in2;
V_TYPE v_reg_res, v_reg_in1, v_reg_in2, v_mem_res, v_mem_in1, v_mem_in2;
s_reg_in1 = ((((S_TYPE)values[i].hi_1 << 64)) + ((S_TYPE)values[i].lo_1));
reg_errors += compare (s_reg_in1, (V_TYPE) { s_reg_in1 }, nl, "reg, in1");
s_reg_in2 = ((((S_TYPE)values[i].hi_2 << 64)) + ((S_TYPE)values[i].lo_2));
reg_errors += compare (s_reg_in2, (V_TYPE) { s_reg_in2 }, "", "reg, in2");
s_reg_res = s_reg_in1 + s_reg_in2;
v_reg_in1 = (V_TYPE) { s_reg_in1 };
v_reg_in2 = (V_TYPE) { s_reg_in2 };
v_reg_res = vec_vadduqm (v_reg_in1, v_reg_in2);
reg_errors += compare (s_reg_res, v_reg_res, "", "reg, res");
s_mem_in1 = s_reg_in1;
convert_via_mem (&v_mem_in1, &s_mem_in1);
mem_errors += compare (s_mem_in1, (V_TYPE) { s_mem_in1 }, "\n", "mem, in1");
s_mem_in2 = s_reg_in2;
convert_via_mem (&v_mem_in2, &s_mem_in2);
mem_errors += compare (s_mem_in2, (V_TYPE) { s_mem_in2 }, "", "mem, in2");
s_mem_res = s_mem_in1 + s_mem_in2;
v_mem_res = vec_vadduqm (v_mem_in1, v_mem_in2);
mem_errors += compare (s_mem_res, v_mem_res, "", "mem, res");
nl = "\n";
}
#ifdef DEBUG
putchar ('\n');
if (!reg_errors)
fputs ("no errors found on register operations\n", stdout);
else
printf ("%d error%s found on register operations\n",
reg_errors,
(reg_errors == 1) ? "s" : "");
if (!mem_errors)
fputs ("no errors found on memory operations\n", stdout);
else
printf ("%d error%s found on memory operations\n",
mem_errors,
(mem_errors == 1) ? "s" : "");
fflush (stdout);
#endif
if ((reg_errors + mem_errors) != 0)
abort ();
return 0;
}
|