-
Notifications
You must be signed in to change notification settings - Fork 408
Expand file tree
/
Copy pathfind-last.cpp
More file actions
428 lines (393 loc) · 13.5 KB
/
find-last.cpp
File metadata and controls
428 lines (393 loc) · 13.5 KB
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
#include <algorithm>
#include <functional>
#include <iostream>
#include <limits>
#include <memory>
#include <stdint.h>
#include "common.h"
template <typename RetTy, typename Ty>
using Fn2Ty = std::function<RetTy(Ty *, Ty *, RetTy)>;
template <typename RetTy, typename Ty>
static void checkVectorFunction(Fn2Ty<RetTy, Ty> ScalarFn,
Fn2Ty<RetTy, Ty> VectorFn, const char *Name) {
std::cout << "Checking " << Name << "\n";
unsigned N = 1000;
std::unique_ptr<Ty[]> Src1(new Ty[N]);
std::unique_ptr<Ty[]> Src2(new Ty[N]);
init_data(Src1, N);
init_data(Src2, N);
// Test VectorFn with different input data.
{
// Check with random inputs.
auto Reference = ScalarFn(&Src1[0], &Src2[0], N);
auto ToCheck = VectorFn(&Src1[0], &Src2[0], N);
if (Reference != ToCheck) {
std::cerr << "Miscompare\n";
exit(1);
}
}
{
// Check with Src1 > Src2 for all elements.
for (unsigned I = 0; I != N; ++I) {
Src1[I] = std::numeric_limits<Ty>::max();
Src2[I] = std::numeric_limits<Ty>::min();
}
auto Reference = ScalarFn(&Src1[0], &Src2[0], N);
auto ToCheck = VectorFn(&Src1[0], &Src2[0], N);
if (Reference != ToCheck) {
std::cerr << "Miscompare\n";
exit(1);
}
}
{
// Check with Src1 < Src2 for all elements.
for (unsigned I = 0; I != N; ++I) {
Src1[I] = std::numeric_limits<Ty>::min();
Src2[I] = std::numeric_limits<Ty>::max();
}
auto Reference = ScalarFn(&Src1[0], &Src2[0], N);
auto ToCheck = VectorFn(&Src1[0], &Src2[0], N);
if (Reference != ToCheck) {
std::cerr << "Miscompare\n";
exit(1);
}
}
{
// Check with only Src1[998] > Src2[998].
for (unsigned I = 0; I != N; ++I)
Src1[I] = Src2[I] = std::numeric_limits<Ty>::min();
Src1[998] = std::numeric_limits<Ty>::max();
auto Reference = ScalarFn(&Src1[0], &Src2[0], N);
auto ToCheck = VectorFn(&Src1[0], &Src2[0], N);
if (Reference != ToCheck) {
std::cerr << "Miscompare\n";
exit(1);
}
}
{
// Check with only Src1[0] > Src2[0].
for (unsigned I = 0; I != N; ++I)
Src1[I] = Src2[I] = std::numeric_limits<Ty>::min();
Src1[0] = std::numeric_limits<Ty>::max();
auto Reference = ScalarFn(&Src1[0], &Src2[0], N);
auto ToCheck = VectorFn(&Src1[0], &Src2[0], N);
if (Reference != ToCheck) {
std::cerr << "Miscompare\n";
exit(1);
}
}
{
// Check with only Src1[N - 1] > Src2[N - 1].
for (unsigned I = 0; I != N; ++I)
Src1[I] = Src2[I] = std::numeric_limits<Ty>::min();
Src1[N - 1] = std::numeric_limits<Ty>::max();
auto Reference = ScalarFn(&Src1[0], &Src2[0], N);
auto ToCheck = VectorFn(&Src1[0], &Src2[0], N);
if (Reference != ToCheck) {
std::cerr << "Miscompare\n";
exit(1);
}
}
{
// Check with only Src1[0] > Src2[0] and Src1[N - 1] > Src2[N - 1].
for (unsigned I = 0; I != N; ++I)
Src1[I] = Src2[I] = std::numeric_limits<Ty>::min();
Src1[0] = Src1[N - 1] = std::numeric_limits<Ty>::max();
auto Reference = ScalarFn(&Src1[0], &Src2[0], N);
auto ToCheck = VectorFn(&Src1[0], &Src2[0], N);
if (Reference != ToCheck) {
std::cerr << "Miscompare\n";
exit(1);
}
}
// Test with small trip counts to stress tail-folding edge cases where
// inactive lanes in the last vector iteration must not update the reduction.
for (unsigned SmallN = 1; SmallN <= 16; ++SmallN) {
std::unique_ptr<Ty[]> SmallSrc1(new Ty[SmallN]);
std::unique_ptr<Ty[]> SmallSrc2(new Ty[SmallN]);
for (unsigned I = 0; I != SmallN; ++I) {
SmallSrc1[I] = std::numeric_limits<Ty>::max();
SmallSrc2[I] = std::numeric_limits<Ty>::min();
}
auto Reference = ScalarFn(&SmallSrc1[0], &SmallSrc2[0], SmallN);
auto ToCheck = VectorFn(&SmallSrc1[0], &SmallSrc2[0], SmallN);
if (Reference != ToCheck) {
std::cerr << "Miscompare for N=" << SmallN << "\n";
exit(1);
}
}
}
int main(void) {
rng = std::mt19937(15);
#define INC_COND(Start, Step, RetTy) for (RetTy I = Start; I < TC; I += Step)
#define DEC_COND(End, Step, RetTy) for (RetTy I = TC; I > End; I -= Step)
#define DEFINE_FINDLAST_LOOP_BODY(TrueVal, FalseVal, ForCond) \
ForCond { Rdx = A[I] > B[I] ? TrueVal : FalseVal; } \
return Rdx;
{
// Find the last index where A[I] > B[I] and update 32-bits Rdx when the
// condition is true.
DEFINE_SCALAR_AND_VECTOR_FN2_TYPE(
int32_t Rdx = -1;,
DEFINE_FINDLAST_LOOP_BODY(
/* TrueVal= */ I, /* FalseVal= */ Rdx,
/* ForCond= */
INC_COND(/* Start= */ 0, /* Step= */ 1, /* RetTy= */ int32_t)),
int32_t);
checkVectorFunction<int32_t, int32_t>(ScalarFn, VectorFn,
"findlast_icmp_s32_true_update");
checkVectorFunction<int32_t, float>(ScalarFn, VectorFn,
"findlast_fcmp_s32_true_update");
}
{
// Find the last index where A[I] > B[I] and update 16-bits Rdx when the
// condition is true.
DEFINE_SCALAR_AND_VECTOR_FN2_TYPE(
int16_t Rdx = -1;,
DEFINE_FINDLAST_LOOP_BODY(
/* TrueVal= */ I, /* FalseVal= */ Rdx,
/* ForCond= */
INC_COND(/* Start= */ 0, /* Step= */ 1, /* RetTy= */ int16_t)),
int16_t);
checkVectorFunction<int16_t, int16_t>(ScalarFn, VectorFn,
"findlast_icmp_s16_true_update");
}
{
// Update 32-bits Rdx when the condition A[I] > B[I] is false.
DEFINE_SCALAR_AND_VECTOR_FN2_TYPE(
int32_t Rdx = -1;,
DEFINE_FINDLAST_LOOP_BODY(
/* TrueVal= */ Rdx, /* FalseVal= */ I,
/* ForCond= */
INC_COND(/* Start= */ 0, /* Step= */ 1, /* RetTy= */ int32_t)),
int32_t);
checkVectorFunction<int32_t, int32_t>(ScalarFn, VectorFn,
"findlast_icmp_s32_false_update");
checkVectorFunction<int32_t, float>(ScalarFn, VectorFn,
"findlast_fcmp_s32_false_update");
}
{
// Update 16-bits Rdx when the condition A[I] > B[I] is false.
DEFINE_SCALAR_AND_VECTOR_FN2_TYPE(
int16_t Rdx = -1;,
DEFINE_FINDLAST_LOOP_BODY(
/* TrueVal= */ Rdx, /* FalseVal= */ I,
/* ForCond= */
INC_COND(/* Start= */ 0, /* Step= */ 1, /* RetTy= */ int16_t)),
int16_t);
checkVectorFunction<int16_t, int16_t>(ScalarFn, VectorFn,
"findlast_icmp_s16_false_update");
}
{
// Find the last 32-bits index with the start value TC.
DEFINE_SCALAR_AND_VECTOR_FN2_TYPE(
int32_t Rdx = TC;,
DEFINE_FINDLAST_LOOP_BODY(
/* TrueVal= */ I, /* FalseVal= */ Rdx,
/* ForCond= */
INC_COND(/* Start= */ 0, /* Step= */ 1, /* RetTy= */ int32_t)),
int32_t);
checkVectorFunction<int32_t, int32_t>(ScalarFn, VectorFn,
"findlast_icmp_s32_start_TC");
checkVectorFunction<int32_t, float>(ScalarFn, VectorFn,
"findlast_fcmp_s32_start_TC");
}
{
// Find the last 16-bits index with the start value TC.
DEFINE_SCALAR_AND_VECTOR_FN2_TYPE(
int16_t Rdx = TC;,
DEFINE_FINDLAST_LOOP_BODY(
/* TrueVal= */ I, /* FalseVal= */ Rdx,
/* ForCond= */
INC_COND(/* Start= */ 0, /* Step= */ 1, /* RetTy= */ int16_t)),
int16_t);
checkVectorFunction<int16_t, int16_t>(ScalarFn, VectorFn,
"findlast_icmp_s16_start_TC");
}
{
// Increment the 32-bits induction variable by 2.
DEFINE_SCALAR_AND_VECTOR_FN2_TYPE(
int32_t Rdx = -1;,
DEFINE_FINDLAST_LOOP_BODY(
/* TrueVal= */ I, /* FalseVal= */ Rdx,
/* ForCond= */
INC_COND(/* Start= */ 0, /* Step= */ 2, /* RetTy= */ int32_t)),
int32_t);
checkVectorFunction<int32_t, int32_t>(ScalarFn, VectorFn,
"findlast_icmp_s32_inc_2");
checkVectorFunction<int32_t, float>(ScalarFn, VectorFn,
"findlast_fcmp_s32_inc_2");
}
{
// Increment the 16-bits induction variable by 2.
DEFINE_SCALAR_AND_VECTOR_FN2_TYPE(
int16_t Rdx = -1;,
DEFINE_FINDLAST_LOOP_BODY(
/* TrueVal= */ I, /* FalseVal= */ Rdx,
/* ForCond= */
INC_COND(/* Start= */ 0, /* Step= */ 2, /* RetTy= */ int16_t)),
int16_t);
checkVectorFunction<int16_t, int16_t>(ScalarFn, VectorFn,
"findlast_icmp_s16_inc_2");
}
{
// Check with decreasing 32-bits induction variable.
DEFINE_SCALAR_AND_VECTOR_FN2_TYPE(
int32_t Rdx = -1;,
DEFINE_FINDLAST_LOOP_BODY(
/* TrueVal= */ I, /* FalseVal= */ Rdx,
/* ForCond= */
DEC_COND(/* End= */ 0, /* Step= */ 1, /* RetTy= */ int32_t)),
int32_t);
checkVectorFunction<int32_t, int32_t>(
ScalarFn, VectorFn, "findlast_icmp_s32_start_decreasing_induction");
checkVectorFunction<int32_t, float>(
ScalarFn, VectorFn, "findlast_fcmp_s32_start_decreasing_induction");
}
{
// Check with decreasing 16-bits induction variable.
DEFINE_SCALAR_AND_VECTOR_FN2_TYPE(
int16_t Rdx = -1;,
DEFINE_FINDLAST_LOOP_BODY(
/* TrueVal= */ I, /* FalseVal= */ Rdx,
/* ForCond= */
DEC_COND(/* End= */ 0, /* Step= */ 1, /* RetTy= */ int16_t)),
int16_t);
checkVectorFunction<int16_t, int16_t>(
ScalarFn, VectorFn, "findlast_icmp_s16_start_decreasing_induction");
}
{
// Check with 32-bits the induction variable starts from 3.
DEFINE_SCALAR_AND_VECTOR_FN2_TYPE(
int32_t Rdx = -1;,
DEFINE_FINDLAST_LOOP_BODY(
/* TrueVal= */ I, /* FalseVal= */ Rdx,
/* ForCond= */
INC_COND(/* Start= */ 3, /* Step= */ 1, /* RetTy= */ int32_t)),
int32_t);
checkVectorFunction<int32_t, int32_t>(ScalarFn, VectorFn,
"findlast_icmp_s32_iv_start_3");
checkVectorFunction<int32_t, float>(ScalarFn, VectorFn,
"findlast_fcmp_s32_iv_start_3");
}
{
// Check with 16-bits the induction variable starts from 3.
DEFINE_SCALAR_AND_VECTOR_FN2_TYPE(
int16_t Rdx = -1;,
DEFINE_FINDLAST_LOOP_BODY(
/* TrueVal= */ I, /* FalseVal= */ Rdx,
/* ForCond= */
INC_COND(/* Start= */ 3, /* Step= */ 1, /* RetTy= */ int16_t)),
int16_t);
checkVectorFunction<int16_t, int16_t>(ScalarFn, VectorFn,
"findlast_icmp_s16_iv_start_3");
}
{
// Check with start value of 3 and 32-bits induction variable starts at 3.
DEFINE_SCALAR_AND_VECTOR_FN2_TYPE(
int32_t Rdx = 3;,
DEFINE_FINDLAST_LOOP_BODY(
/* TrueVal= */ I, /* FalseVal= */ Rdx,
/* ForCond= */
INC_COND(/* Start= */ 3, /* Step= */ 1, /* RetTy= */ int32_t)),
int32_t);
checkVectorFunction<int32_t, int32_t>(
ScalarFn, VectorFn, "findlast_icmp_s32_start_3_iv_start_3");
checkVectorFunction<int32_t, float>(ScalarFn, VectorFn,
"findlast_fcmp_s32_start_3_iv_start_3");
}
{
// Check with start value of 3 and 16-bits induction variable starts at 3.
DEFINE_SCALAR_AND_VECTOR_FN2_TYPE(
int16_t Rdx = 3;,
DEFINE_FINDLAST_LOOP_BODY(
/* TrueVal= */ I, /* FalseVal= */ Rdx,
/* ForCond= */
INC_COND(/* Start= */ 3, /* Step= */ 1, /* RetTy= */ int16_t)),
int16_t);
checkVectorFunction<int16_t, int16_t>(
ScalarFn, VectorFn, "findlast_icmp_s16_start_3_iv_start_3");
}
{
// Check with start value of 2 and 32-bits induction variable starts at 3.
DEFINE_SCALAR_AND_VECTOR_FN2_TYPE(
int32_t Rdx = 2;,
DEFINE_FINDLAST_LOOP_BODY(
/* TrueVal= */ I, /* FalseVal= */ Rdx,
/* ForCond= */
INC_COND(/* Start= */ 3, /* Step= */ 1, /* RetTy= */ int32_t)),
int32_t);
checkVectorFunction<int32_t, int32_t>(
ScalarFn, VectorFn, "findlast_icmp_s32_start_2_iv_start_3");
checkVectorFunction<int32_t, float>(ScalarFn, VectorFn,
"findlast_fcmp_s32_start_2_iv_start_3");
}
{
// Check with start value of 2 and 16-bits induction variable starts at 3.
DEFINE_SCALAR_AND_VECTOR_FN2_TYPE(
int16_t Rdx = 2;,
DEFINE_FINDLAST_LOOP_BODY(
/* TrueVal= */ I, /* FalseVal= */ Rdx,
/* ForCond= */
INC_COND(/* Start= */ 3, /* Step= */ 1, /* RetTy= */ int16_t)),
int16_t);
checkVectorFunction<int16_t, int16_t>(
ScalarFn, VectorFn, "findlast_icmp_s16_start_2_iv_start_3");
}
{
// Check with start value of 4 and 32-bits induction variable starts at 3.
DEFINE_SCALAR_AND_VECTOR_FN2_TYPE(
int32_t Rdx = 4;,
DEFINE_FINDLAST_LOOP_BODY(
/* TrueVal= */ I, /* FalseVal= */ Rdx,
/* ForCond= */
INC_COND(/* Start= */ 3, /* Step= */ 1, /* RetTy= */ int32_t)),
int32_t);
checkVectorFunction<int32_t, int32_t>(
ScalarFn, VectorFn, "findlast_icmp_s32_start_4_iv_start_3");
checkVectorFunction<int32_t, float>(ScalarFn, VectorFn,
"findlast_fcmp_s32_start_4_iv_start_3");
}
{
// Check with start value of 4 and 16-bits induction variable starts at 3.
DEFINE_SCALAR_AND_VECTOR_FN2_TYPE(
int16_t Rdx = 4;,
DEFINE_FINDLAST_LOOP_BODY(
/* TrueVal= */ I, /* FalseVal= */ Rdx,
/* ForCond= */
INC_COND(/* Start= */ 3, /* Step= */ 1, /* RetTy= */ int16_t)),
int16_t);
checkVectorFunction<int16_t, int16_t>(
ScalarFn, VectorFn, "findlast_icmp_s16_start_4_iv_start_3");
}
{
// Check with decreasing 16-bits induction variable where Rdx gets updated when the condition is false.
DEFINE_SCALAR_AND_VECTOR_FN2_TYPE(
int16_t Rdx = -1;,
DEFINE_FINDLAST_LOOP_BODY(
/* TrueVal= */ Rdx, /* FalseVal= */ I,
/* ForCond= */
DEC_COND(/* End= */ 0, /* Step= */ 1, /* RetTy= */ int16_t)),
int16_t);
checkVectorFunction<int16_t, int16_t>(
ScalarFn, VectorFn,
"findlast_icmp_s16_false_update_decreasing_induction");
}
{
// Check with decreasing 32-bits induction variable where Rdx gets updated when the condition is false.
DEFINE_SCALAR_AND_VECTOR_FN2_TYPE(
int32_t Rdx = -1;,
DEFINE_FINDLAST_LOOP_BODY(
/* TrueVal= */ Rdx, /* FalseVal= */ I,
/* ForCond= */
DEC_COND(/* End= */ 0, /* Step= */ 1, /* RetTy= */ int32_t)),
int32_t);
checkVectorFunction<int32_t, int32_t>(
ScalarFn, VectorFn,
"findlast_icmp_s32_false_update_decreasing_induction");
checkVectorFunction<int32_t, float>(
ScalarFn, VectorFn,
"findlast_fcmp_s32_false_update_decreasing_induction");
}
return 0;
}