@@ -115,129 +115,127 @@ static void checkVectorFunction(Fn2Ty<RetTy, Ty> ScalarFn,
115
115
int main (void ) {
116
116
rng = std::mt19937 (15 );
117
117
118
+ #define INC_COND (Start, Step, RetTy ) for (RetTy I = Start; I < TC; I += Step)
119
+ #define DEC_COND (End, Step, RetTy ) for (RetTy I = TC; I > End; I -= Step)
120
+
121
+ #define DEFINE_FINDLAST_LOOP_BODY (TrueVal, FalseVal, ForCond ) \
122
+ ForCond { Rdx = A[I] > B[I] ? TrueVal : FalseVal; } \
123
+ return Rdx;
124
+
118
125
{
119
126
// Find the last index where A[I] > B[I] and update Rdx when the condition
120
127
// is true.
121
128
DEFINE_SCALAR_AND_VECTOR_FN2_TYPE (
122
- int32_t Rdx = -1 ;,
123
- for (int32_t I = 0 ; I < TC; I++) {
124
- Rdx = A[I] > B[I] ? I : Rdx;
125
- }
126
- return Rdx;,
127
- int32_t
128
- );
129
+ int32_t Rdx = -1 ;,
130
+ DEFINE_FINDLAST_LOOP_BODY (
131
+ /* TrueVal= */ I, /* FalseVal= */ Rdx,
132
+ /* ForCond= */
133
+ INC_COND (/* Start= */ 0 , /* Step= */ 1 , /* RetTy= */ int32_t )),
134
+ int32_t );
129
135
checkVectorFunction<int32_t , int32_t >(ScalarFn, VectorFn,
130
- " findlast_true_update" );
136
+ " findlast_true_update" );
131
137
}
132
138
133
139
{
134
140
// Update Rdx when the condition A[I] > B[I] is false.
135
141
DEFINE_SCALAR_AND_VECTOR_FN2_TYPE (
136
- int32_t Rdx = -1 ;,
137
- for (int32_t I = 0 ; I < TC; I++) {
138
- Rdx = A[I] > B[I] ? Rdx : I;
139
- }
140
- return Rdx;,
141
- int32_t
142
- );
142
+ int32_t Rdx = -1 ;,
143
+ DEFINE_FINDLAST_LOOP_BODY (
144
+ /* TrueVal= */ Rdx, /* FalseVal= */ I,
145
+ /* ForCond= */
146
+ INC_COND (/* Start= */ 0 , /* Step= */ 1 , /* RetTy= */ int32_t )),
147
+ int32_t );
143
148
checkVectorFunction<int32_t , int32_t >(ScalarFn, VectorFn,
144
149
" findlast_false_update" );
145
150
}
146
151
147
152
{
148
153
// Find the last index with the start value TC.
149
154
DEFINE_SCALAR_AND_VECTOR_FN2_TYPE (
150
- int32_t Rdx = TC;,
151
- for (int32_t I = 0 ; I < TC; I++) {
152
- Rdx = A[I] > B[I] ? I : Rdx;
153
- }
154
- return Rdx;,
155
- int32_t
156
- );
155
+ int32_t Rdx = TC;,
156
+ DEFINE_FINDLAST_LOOP_BODY (
157
+ /* TrueVal= */ I, /* FalseVal= */ Rdx,
158
+ /* ForCond= */
159
+ INC_COND (/* Start= */ 0 , /* Step= */ 1 , /* RetTy= */ int32_t )),
160
+ int32_t );
157
161
checkVectorFunction<int32_t , int32_t >(ScalarFn, VectorFn,
158
162
" findlast_start_TC" );
159
163
}
160
164
161
165
{
162
166
// Increment the induction variable by 2.
163
167
DEFINE_SCALAR_AND_VECTOR_FN2_TYPE (
164
- int32_t Rdx = -1 ;,
165
- for (int32_t I = 0 ; I < TC; I += 2 ) {
166
- Rdx = A[I] > B[I] ? I : Rdx;
167
- }
168
- return Rdx;,
169
- int32_t
170
- );
168
+ int32_t Rdx = -1 ;,
169
+ DEFINE_FINDLAST_LOOP_BODY (
170
+ /* TrueVal= */ I, /* FalseVal= */ Rdx,
171
+ /* ForCond= */
172
+ INC_COND (/* Start= */ 0 , /* Step= */ 2 , /* RetTy= */ int32_t )),
173
+ int32_t );
171
174
checkVectorFunction<int32_t , int32_t >(ScalarFn, VectorFn,
172
175
" findlast_inc_2" );
173
176
}
174
177
175
178
{
176
179
// Check with decreasing induction variable.
177
180
DEFINE_SCALAR_AND_VECTOR_FN2_TYPE (
178
- int32_t Rdx = -1 ;,
179
- for (int32_t I = TC; I > 0 ; I--) {
180
- Rdx = A[I] > B[I] ? I : Rdx;
181
- }
182
- return Rdx;,
183
- int32_t
184
- );
181
+ int32_t Rdx = -1 ;,
182
+ DEFINE_FINDLAST_LOOP_BODY (
183
+ /* TrueVal= */ I, /* FalseVal= */ Rdx,
184
+ /* ForCond= */
185
+ DEC_COND (/* End= */ 0 , /* Step= */ 1 , /* RetTy= */ int32_t )),
186
+ int32_t );
185
187
checkVectorFunction<int32_t , int32_t >(
186
188
ScalarFn, VectorFn, " findlast_start_decreasing_induction" );
187
189
}
188
190
189
191
{
190
192
// Check with the induction variable starts from 3.
191
193
DEFINE_SCALAR_AND_VECTOR_FN2_TYPE (
192
- int32_t Rdx = -1 ;,
193
- for (int32_t I = 3 ; I < TC; I++) {
194
- Rdx = A[I] > B[I] ? I : Rdx;
195
- }
196
- return Rdx;,
197
- int32_t
198
- );
194
+ int32_t Rdx = -1 ;,
195
+ DEFINE_FINDLAST_LOOP_BODY (
196
+ /* TrueVal= */ I, /* FalseVal= */ Rdx,
197
+ /* ForCond= */
198
+ INC_COND (/* Start= */ 3 , /* Step= */ 1 , /* RetTy= */ int32_t )),
199
+ int32_t );
199
200
checkVectorFunction<int32_t , int32_t >(ScalarFn, VectorFn,
200
201
" findlast_iv_start_3" );
201
202
}
202
203
203
204
{
204
205
// Check with start value of 3 and induction variable starts at 3.
205
206
DEFINE_SCALAR_AND_VECTOR_FN2_TYPE (
206
- int32_t Rdx = 3 ;,
207
- for (int32_t I = 3 ; I < TC; I++) {
208
- Rdx = A[I] > B[I] ? I : Rdx;
209
- }
210
- return Rdx;,
211
- int32_t
212
- );
207
+ int32_t Rdx = 3 ;,
208
+ DEFINE_FINDLAST_LOOP_BODY (
209
+ /* TrueVal= */ I, /* FalseVal= */ Rdx,
210
+ /* ForCond= */
211
+ INC_COND (/* Start= */ 3 , /* Step= */ 1 , /* RetTy= */ int32_t )),
212
+ int32_t );
213
213
checkVectorFunction<int32_t , int32_t >(ScalarFn, VectorFn,
214
214
" findlast_start_3_iv_start_3" );
215
215
}
216
216
217
217
{
218
218
// Check with start value of 2 and induction variable starts at 3.
219
219
DEFINE_SCALAR_AND_VECTOR_FN2_TYPE (
220
- int32_t Rdx = 2 ;,
221
- for (int32_t I = 3 ; I < TC; I++) {
222
- Rdx = A[I] > B[I] ? I : Rdx;
223
- }
224
- return Rdx;,
225
- int32_t
226
- );
220
+ int32_t Rdx = 2 ;,
221
+ DEFINE_FINDLAST_LOOP_BODY (
222
+ /* TrueVal= */ I, /* FalseVal= */ Rdx,
223
+ /* ForCond= */
224
+ INC_COND (/* Start= */ 3 , /* Step= */ 1 , /* RetTy= */ int32_t )),
225
+ int32_t );
227
226
checkVectorFunction<int32_t , int32_t >(ScalarFn, VectorFn,
228
227
" findlast_start_2_iv_start_3" );
229
228
}
230
229
231
230
{
232
231
// Check with start value of 4 and induction variable starts at 3.
233
232
DEFINE_SCALAR_AND_VECTOR_FN2_TYPE (
234
- int32_t Rdx = 4 ;,
235
- for (int32_t I = 3 ; I < TC; I++) {
236
- Rdx = A[I] > B[I] ? I : Rdx;
237
- }
238
- return Rdx;,
239
- int32_t
240
- );
233
+ int32_t Rdx = 4 ;,
234
+ DEFINE_FINDLAST_LOOP_BODY (
235
+ /* TrueVal= */ I, /* FalseVal= */ Rdx,
236
+ /* ForCond= */
237
+ INC_COND (/* Start= */ 3 , /* Step= */ 1 , /* RetTy= */ int32_t )),
238
+ int32_t );
241
239
checkVectorFunction<int32_t , int32_t >(ScalarFn, VectorFn,
242
240
" findlast_start_4_iv_start_3" );
243
241
}
0 commit comments