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
|
/* { dg-do run } */
/* { dg-options "-O2 -msse2" } */
/* { dg-require-effective-target sse2 } */
#include "sse2-check.h"
#include <emmintrin.h>
#include <string.h>
#define SHIFT (4)
typedef union {
__m128i v;
unsigned int s[4];
unsigned short int t[8];
unsigned long long u[2];
unsigned char c[16];
}vecInLong;
void sse2_tests (void) __attribute__((noinline));
void dump128_16 (char *, char *, vecInLong);
void dump128_32 (char *, char *, vecInLong);
void dump128_64 (char *, char *, vecInLong);
void dump128_128 (char *, char *, vecInLong);
int check (const char *, const char *[]);
char buf[8000];
char comparison[8000];
static int errors = 0;
vecInLong a128, b128, c128, d128, e128, f128;
__m128i m128_16, m128_32, s128, m128_64, m128_128;
__m64 m64_16, s64, m64_32, m64_64;
const char *reference_sse2[] = {
"_mm_srai_epi16 0012 0012 0012 0012 0012 0012 0012 0012 \n",
"_mm_sra_epi16 0012 0012 0012 0012 0012 0012 0012 0012 \n",
"_mm_srai_epi32 00123456 00123456 00123456 00123456 \n",
"_mm_sra_epi32 00123456 00123456 00123456 00123456 \n",
"_mm_srli_epi16 0012 0012 0012 0012 0012 0012 0012 0012 \n",
"_mm_srl_epi16 0012 0012 0012 0012 0012 0012 0012 0012 \n",
"_mm_srli_epi32 00123456 00123456 00123456 00123456 \n",
"_mm_srl_epi32 00123456 00123456 00123456 00123456 \n",
"_mm_srli_epi64 00123456789abcde 00123456789abcde \n",
"_mm_srl_epi64 00123456789abcde 00123456789abcde \n",
"_mm_srli_si128 (byte shift) 00000000ffeeddccbbaa998877665544\n",
"_mm_slli_epi16 1230 1230 1230 1230 1230 1230 1230 1230 \n",
"_mm_sll_epi16 1230 1230 1230 1230 1230 1230 1230 1230 \n",
"_mm_slli_epi32 12345670 12345670 12345670 12345670 \n",
"_mm_sll_epi32 12345670 12345670 12345670 12345670 \n",
"_mm_slli_epi64 123456789abcdef0 123456789abcdef0 \n",
"_mm_sll_epi64 123456789abcdef0 123456789abcdef0 \n",
"_mm_sll_si128 (byte shift) bbaa9988776655443322110000000000\n",
"_mm_shuffle_epi32 ffeeddcc bbaa9988 77665544 33221100 \n",
"_mm_shuffelo_epi16 7766 5544 3322 1100 9988 bbaa ddcc ffee \n",
"_mm_shuffehi_epi16 1100 3322 5544 7766 ffee ddcc bbaa 9988 \n",
""
};
static void
sse2_test (void)
{
a128.s[0] = 0x01234567;
a128.s[1] = 0x01234567;
a128.s[2] = 0x01234567;
a128.s[3] = 0x01234567;
m128_32 = a128.v;
d128.u[0] = 0x0123456789abcdefULL;
d128.u[1] = 0x0123456789abcdefULL;
m128_64 = d128.v;
/* This is the 128-bit constant 0x00112233445566778899aabbccddeeff,
expressed as two little-endian 64-bit words. */
e128.u[0] = 0x7766554433221100ULL;
e128.u[1] = 0xffeeddccbbaa9988ULL;
f128.t[0] = 0x0123;
f128.t[1] = 0x0123;
f128.t[2] = 0x0123;
f128.t[3] = 0x0123;
f128.t[4] = 0x0123;
f128.t[5] = 0x0123;
f128.t[6] = 0x0123;
f128.t[7] = 0x0123;
m128_16 = f128.v;
m128_128 = e128.v;
b128.s[0] = SHIFT;
b128.s[1] = 0;
b128.s[2] = 0;
b128.s[3] = 0;
s128 = b128.v;
sse2_tests();
check (buf, reference_sse2);
#ifdef DEBUG
printf ("sse2 testing:\n");
printf (buf);
printf ("\ncomparison:\n");
printf (comparison);
#endif
buf[0] = '\0';
if (errors != 0)
abort ();
}
void __attribute__((noinline))
sse2_tests (void)
{
/* psraw */
c128.v = _mm_srai_epi16 (m128_16, SHIFT);
dump128_16 (buf, "_mm_srai_epi16", c128);
c128.v = _mm_sra_epi16 (m128_16, s128);
dump128_16 (buf, "_mm_sra_epi16", c128);
/* psrad */
c128.v = _mm_srai_epi32 (m128_32, SHIFT);
dump128_32 (buf, "_mm_srai_epi32", c128);
c128.v = _mm_sra_epi32 (m128_32, s128);
dump128_32 (buf, "_mm_sra_epi32", c128);
/* psrlw */
c128.v = _mm_srli_epi16 (m128_16, SHIFT);
dump128_16 (buf, "_mm_srli_epi16", c128);
c128.v = _mm_srl_epi16 (m128_16, s128);
dump128_16 (buf, "_mm_srl_epi16", c128);
/* psrld */
c128.v = _mm_srli_epi32 (m128_32, SHIFT);
dump128_32 (buf, "_mm_srli_epi32", c128);
c128.v = _mm_srl_epi32 (m128_32, s128);
dump128_32 (buf, "_mm_srl_epi32", c128);
/* psrlq */
c128.v = _mm_srli_epi64 (m128_64, SHIFT);
dump128_64 (buf, "_mm_srli_epi64", c128);
c128.v = _mm_srl_epi64 (m128_64, s128);
dump128_64 (buf, "_mm_srl_epi64", c128);
/* psrldq */
c128.v = _mm_srli_si128 (m128_128, SHIFT);
dump128_128 (buf, "_mm_srli_si128 (byte shift) ", c128);
/* psllw */
c128.v = _mm_slli_epi16 (m128_16, SHIFT);
dump128_16 (buf, "_mm_slli_epi16", c128);
c128.v = _mm_sll_epi16 (m128_16, s128);
dump128_16 (buf, "_mm_sll_epi16", c128);
/* pslld */
c128.v = _mm_slli_epi32 (m128_32, SHIFT);
dump128_32 (buf, "_mm_slli_epi32", c128);
c128.v = _mm_sll_epi32 (m128_32, s128);
dump128_32 (buf, "_mm_sll_epi32", c128);
/* psllq */
c128.v = _mm_slli_epi64 (m128_64, SHIFT);
dump128_64 (buf, "_mm_slli_epi64", c128);
c128.v = _mm_sll_epi64 (m128_64, s128);
dump128_64 (buf, "_mm_sll_epi64", c128);
/* pslldq */
c128.v = _mm_slli_si128 (m128_128, SHIFT);
dump128_128 (buf, "_mm_sll_si128 (byte shift)", c128);
/* Shuffle constant 0x1b == 0b_00_01_10_11, e.g. swap words: ABCD => DCBA. */
/* pshufd */
c128.v = _mm_shuffle_epi32 (m128_128, 0x1b);
dump128_32 (buf, "_mm_shuffle_epi32", c128);
/* pshuflw */
c128.v = _mm_shufflelo_epi16 (m128_128, 0x1b);
dump128_16 (buf, "_mm_shuffelo_epi16", c128);
/* pshufhw */
c128.v = _mm_shufflehi_epi16 (m128_128, 0x1b);
dump128_16 (buf, "_mm_shuffehi_epi16", c128);
}
void
dump128_16 (char *buf, char *name, vecInLong x)
{
int i;
char *p = buf + strlen (buf);
sprintf (p, "%s ", name);
p += strlen (p);
for (i=0; i<8; i++)
{
sprintf (p, "%4.4x ", x.t[i]);
p += strlen (p);
}
strcat (p, "\n");
}
void
dump128_32 (char *buf, char *name, vecInLong x)
{
int i;
char *p = buf + strlen (buf);
sprintf (p, "%s ", name);
p += strlen (p);
for (i=0; i<4; i++)
{
sprintf (p, "%8.8x ", x.s[i]);
p += strlen (p);
}
strcat (p, "\n");
}
void
dump128_64 (char *buf, char *name, vecInLong x)
{
int i;
char *p = buf + strlen (buf);
sprintf (p, "%s ", name);
p += strlen (p);
for (i=0; i<2; i++)
{
#if defined(_WIN32) && !defined(__CYGWIN__)
sprintf (p, "%16.16I64x ", x.u[i]);
#else
sprintf (p, "%16.16llx ", x.u[i]);
#endif
p += strlen (p);
}
strcat (p, "\n");
}
void
dump128_128 (char *buf, char *name, vecInLong x)
{
int i;
char *p = buf + strlen (buf);
sprintf (p, "%s ", name);
p += strlen (p);
for (i=15; i>=0; i--)
{
/* This is cheating; we don't have a 128-bit int format code.
Running the loop backwards to compensate for the
little-endian layout. */
sprintf (p, "%2.2x", x.c[i]);
p += strlen (p);
}
strcat (p, "\n");
}
int
check (const char *input, const char *reference[])
{
int broken, i, j, len;
const char *p_input;
char *p_comparison;
int new_errors = 0;
p_comparison = &comparison[0];
p_input = input;
for (i = 0; *reference[i] != '\0'; i++)
{
broken = 0;
len = strlen (reference[i]);
for (j = 0; j < len; j++)
{
/* Ignore the terminating NUL characters at the end of every string in 'reference[]'. */
if (!broken && *p_input != reference[i][j])
{
*p_comparison = '\0';
strcat (p_comparison, " >>> ");
p_comparison += strlen (p_comparison);
new_errors++;
broken = 1;
}
*p_comparison = *p_input;
p_comparison++;
p_input++;
}
if (broken)
{
*p_comparison = '\0';
strcat (p_comparison, "expected:\n");
strcat (p_comparison, reference[i]);
p_comparison += strlen (p_comparison);
}
}
*p_comparison = '\0';
strcat (p_comparison, new_errors ? "failure\n\n" : "O.K.\n\n") ;
errors += new_errors;
return 0;
}
|