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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
|
#include <nmmintrin.h>
#include <string.h>
#define CFLAG 0x00000001
#define ZFLAG 0x00000002
#define SFLAG 0x00000004
#define OFLAG 0x00000008
#define AFLAG 0x00000010
#define PFLAG 0x00000020
#define PCMPSTR_EQ(X, Y, RES) \
{ \
int __size = (sizeof (*X) ^ 3) * 8; \
int __i, __j; \
for (__i = 0; __i < __size; __i++) \
for (__j = 0; __j < __size; __j++) \
RES[__j][__i] = (X[__i] == Y[__j]); \
}
#define PCMPSTR_RNG(X, Y, RES) \
{ \
int __size = (sizeof (*X) ^ 3) * 8; \
int __i, __j; \
for (__j = 0; __j < __size; __j++) \
for (__i = 0; __i < __size - 1; __i += 2) \
{ \
RES[__j][__i] = (Y[__j] >= X[__i]); \
RES[__j][__i+1] = (Y[__j] <= X[__i + 1]); \
} \
}
static void
override_invalid (unsigned char res[16][16], int la, int lb,
const int mode, int dim)
{
int i, j;
for (j = 0; j < dim; j++)
for (i = 0; i < dim; i++)
if (i < la && j >= lb)
res[j][i] = 0;
else if (i >= la)
switch ((mode & 0x0C))
{
case _SIDD_CMP_EQUAL_ANY:
case _SIDD_CMP_RANGES:
res[j][i] = 0;
break;
case _SIDD_CMP_EQUAL_EACH:
res[j][i] = (j >= lb) ? 1: 0;
break;
case _SIDD_CMP_EQUAL_ORDERED:
res[j][i] = 1;
break;
}
}
static void
calc_matrix (__m128i a, int la, __m128i b, int lb, const int mode,
unsigned char res[16][16])
{
union
{
__m128i x;
signed char sc[16];
unsigned char uc[16];
signed short ss[8];
unsigned short us[8];
} d, s;
d.x = a;
s.x = b;
switch ((mode & 3))
{
case _SIDD_UBYTE_OPS:
if ((mode & 0x0C) == _SIDD_CMP_RANGES)
{
PCMPSTR_RNG (d.uc, s.uc, res);
}
else
{
PCMPSTR_EQ (d.uc, s.uc, res);
}
break;
case _SIDD_UWORD_OPS:
if ((mode & 0x0C) == _SIDD_CMP_RANGES)
{
PCMPSTR_RNG (d.us, s.us, res);
}
else
{
PCMPSTR_EQ (d.us, s.us, res);
}
break;
case _SIDD_SBYTE_OPS:
if ((mode & 0x0C) == _SIDD_CMP_RANGES)
{
PCMPSTR_RNG (d.sc, s.sc, res);
}
else
{
PCMPSTR_EQ (d.sc, s.sc, res);
}
break;
case _SIDD_SWORD_OPS:
if ((mode & 0x0C) == _SIDD_CMP_RANGES)
{
PCMPSTR_RNG (d.ss, s.ss, res);
}
else
{
PCMPSTR_EQ (d.ss, s.ss, res);
}
break;
}
override_invalid (res, la, lb, mode, (mode & 1) == 0 ? 16 : 8);
}
static int
calc_res (__m128i a, int la, __m128i b, int lb, const int mode)
{
unsigned char mtx[16][16];
int i, j, k, dim, res = 0;
memset (mtx, 0, sizeof (mtx));
dim = (mode & 1) == 0 ? 16 : 8;
if (la < 0)
la = -la;
if (lb < 0)
lb = -lb;
if (la > dim)
la = dim;
if (lb > dim)
lb = dim;
calc_matrix (a, la, b, lb, mode, mtx);
switch ((mode & 0x0C))
{
case _SIDD_CMP_EQUAL_ANY:
for (i = 0; i < dim; i++)
for (j = 0; j < dim; j++)
if (mtx[i][j])
res |= (1 << i);
break;
case _SIDD_CMP_RANGES:
for (i = 0; i < dim; i += 2)
for(j = 0; j < dim; j++)
if (mtx[j][i] && mtx[j][i+1])
res |= (1 << j);
break;
case _SIDD_CMP_EQUAL_EACH:
for(i = 0; i < dim; i++)
if (mtx[i][i])
res |= (1 << i);
break;
case _SIDD_CMP_EQUAL_ORDERED:
for(i = 0; i < dim; i++)
{
unsigned char val = 1;
for (j = 0, k = i; j < dim - i && k < dim; j++, k++)
val &= mtx[k][j];
if (val)
res |= (1 << i);
else
res &= ~(1 << i);
}
break;
}
switch ((mode & 0x30))
{
case _SIDD_POSITIVE_POLARITY:
case _SIDD_MASKED_POSITIVE_POLARITY:
break;
case _SIDD_NEGATIVE_POLARITY:
res ^= -1;
break;
case _SIDD_MASKED_NEGATIVE_POLARITY:
for (i = 0; i < lb; i++)
if (res & (1 << i))
res &= ~(1 << i);
else
res |= (1 << i);
break;
}
return res & ((dim == 8) ? 0xFF : 0xFFFF);
}
static int
cmp_flags (__m128i a, int la, __m128i b, int lb,
int mode, int res2, int is_implicit)
{
int i;
int flags = 0;
int is_bytes_mode = (mode & 1) == 0;
union
{
__m128i x;
unsigned char uc[16];
unsigned short us[8];
} d, s;
d.x = a;
s.x = b;
/* CF: reset if (RES2 == 0), set otherwise. */
if (res2 != 0)
flags |= CFLAG;
if (is_implicit)
{
/* ZF: set if any byte/word of src xmm operand is null, reset
otherwise.
SF: set if any byte/word of dst xmm operand is null, reset
otherwise. */
if (is_bytes_mode)
{
for (i = 0; i < 16; i++)
{
if (s.uc[i] == 0)
flags |= ZFLAG;
if (d.uc[i] == 0)
flags |= SFLAG;
}
}
else
{
for (i = 0; i < 8; i++)
{
if (s.us[i] == 0)
flags |= ZFLAG;
if (d.us[i] == 0)
flags |= SFLAG;
}
}
}
else
{
/* ZF: set if abs value of EDX/RDX < 16 (8), reset otherwise.
SF: set if abs value of EAX/RAX < 16 (8), reset otherwise. */
int max_ind = is_bytes_mode ? 16 : 8;
if (la < 0)
la = -la;
if (lb < 0)
lb = -lb;
if (lb < max_ind)
flags |= ZFLAG;
if (la < max_ind)
flags |= SFLAG;
}
/* OF: equal to RES2[0]. */
if ((res2 & 0x1))
flags |= OFLAG;
/* AF: Reset.
PF: Reset. */
return flags;
}
static int
cmp_indexed (__m128i a, int la, __m128i b, int lb,
const int mode, int *res2)
{
int i, ndx;
int dim = (mode & 1) == 0 ? 16 : 8;
int r2;
r2 = calc_res (a, la, b, lb, mode);
ndx = dim;
if ((mode & 0x40))
{
for (i = dim - 1; i >= 0; i--)
if (r2 & (1 << i))
{
ndx = i;
break;
}
}
else
{
for (i = 0; i < dim; i++)
if ((r2 & (1 << i)))
{
ndx = i;
break;
}
}
*res2 = r2;
return ndx;
}
static __m128i
cmp_masked (__m128i a, int la, __m128i b, int lb,
const int mode, int *res2)
{
union
{
__m128i x;
char c[16];
short s[8];
} ret;
int i;
int dim = (mode & 1) == 0 ? 16 : 8;
union
{
int i;
char c[4];
short s[2];
} r2;
r2.i = calc_res (a, la, b, lb, mode);
memset (&ret, 0, sizeof (ret));
if (mode & 0x40)
{
for (i = 0; i < dim; i++)
if (dim == 8)
ret.s [i] = (r2.i & (1 << i)) ? -1 : 0;
else
ret.c [i] = (r2.i & (1 << i)) ? -1 : 0;
}
else
{
if (dim == 16)
ret.s[0] = r2.s[0];
else
ret.c[0] = r2.c[0];
}
*res2 = r2.i;
return ret.x;
}
static int
calc_str_len (__m128i a, const int mode)
{
union
{
__m128i x;
char c[16];
short s[8];
} s;
int i;
int dim = (mode & 1) == 0 ? 16 : 8;
s.x = a;
if ((mode & 1))
{
for (i = 0; i < dim; i++)
if (s.s[i] == 0)
break;
}
else
{
for (i = 0; i < dim; i++)
if (s.c[i] == 0)
break;
}
return i;
}
static inline int
cmp_ei (__m128i *a, int la, __m128i *b, int lb,
const int mode, int *flags)
{
int res2;
int index = cmp_indexed (*a, la, *b, lb, mode, &res2);
if (flags != NULL)
*flags = cmp_flags (*a, la, *b, lb, mode, res2, 0);
return index;
}
static inline int
cmp_ii (__m128i *a, __m128i *b, const int mode, int *flags)
{
int la, lb;
int res2;
int index;
la = calc_str_len (*a, mode);
lb = calc_str_len (*b, mode);
index = cmp_indexed (*a, la, *b, lb, mode, &res2);
if (flags != NULL)
*flags = cmp_flags (*a, la, *b, lb, mode, res2, 1);
return index;
}
static inline __m128i
cmp_em (__m128i *a, int la, __m128i *b, int lb,
const int mode, int *flags )
{
int res2;
__m128i mask = cmp_masked (*a, la, *b, lb, mode, &res2);
if (flags != NULL)
*flags = cmp_flags (*a, la, *b, lb, mode, res2, 0);
return mask;
}
static inline __m128i
cmp_im (__m128i *a, __m128i *b, const int mode, int *flags)
{
int la, lb;
int res2;
__m128i mask;
la = calc_str_len (*a, mode);
lb = calc_str_len (*b, mode);
mask = cmp_masked (*a, la, *b, lb, mode, &res2);
if (flags != NULL)
*flags = cmp_flags (*a, la, *b, lb, mode, res2, 1);
return mask;
}
|