@@ -115,129 +115,127 @@ static void checkVectorFunction(Fn2Ty<RetTy, Ty> ScalarFn,
115115int main (void ) {
116116 rng = std::mt19937 (15 );
117117
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+
118125 {
119126 // Find the last index where A[I] > B[I] and update Rdx when the condition
120127 // is true.
121128 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 );
129135 checkVectorFunction<int32_t , int32_t >(ScalarFn, VectorFn,
130- " findlast_true_update" );
136+ " findlast_true_update" );
131137 }
132138
133139 {
134140 // Update Rdx when the condition A[I] > B[I] is false.
135141 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 );
143148 checkVectorFunction<int32_t , int32_t >(ScalarFn, VectorFn,
144149 " findlast_false_update" );
145150 }
146151
147152 {
148153 // Find the last index with the start value TC.
149154 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 );
157161 checkVectorFunction<int32_t , int32_t >(ScalarFn, VectorFn,
158162 " findlast_start_TC" );
159163 }
160164
161165 {
162166 // Increment the induction variable by 2.
163167 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 );
171174 checkVectorFunction<int32_t , int32_t >(ScalarFn, VectorFn,
172175 " findlast_inc_2" );
173176 }
174177
175178 {
176179 // Check with decreasing induction variable.
177180 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 );
185187 checkVectorFunction<int32_t , int32_t >(
186188 ScalarFn, VectorFn, " findlast_start_decreasing_induction" );
187189 }
188190
189191 {
190192 // Check with the induction variable starts from 3.
191193 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 );
199200 checkVectorFunction<int32_t , int32_t >(ScalarFn, VectorFn,
200201 " findlast_iv_start_3" );
201202 }
202203
203204 {
204205 // Check with start value of 3 and induction variable starts at 3.
205206 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 );
213213 checkVectorFunction<int32_t , int32_t >(ScalarFn, VectorFn,
214214 " findlast_start_3_iv_start_3" );
215215 }
216216
217217 {
218218 // Check with start value of 2 and induction variable starts at 3.
219219 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 );
227226 checkVectorFunction<int32_t , int32_t >(ScalarFn, VectorFn,
228227 " findlast_start_2_iv_start_3" );
229228 }
230229
231230 {
232231 // Check with start value of 4 and induction variable starts at 3.
233232 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 );
241239 checkVectorFunction<int32_t , int32_t >(ScalarFn, VectorFn,
242240 " findlast_start_4_iv_start_3" );
243241 }
0 commit comments