@@ -81,16 +81,19 @@ __minmax_element_loop(_Iter __first, _Sent __last, _Comp& __comp, _Proj& __proj)
8181 return __result;
8282}
8383
84-
84+ # if _LIBCPP_VECTORIZE_ALGORITHMS
8585template <class _Iter >
8686_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter, _Iter>
8787__minmax_element_vectorized (_Iter __first, _Iter __last) {
8888 using __value_type = __iter_value_type<_Iter>;
8989 constexpr size_t __unroll_count = 4 ;
9090 constexpr size_t __vec_size = __native_vector_size<__value_type>;
9191 using __vec_type = __simd_vector<__value_type, __vec_size>;
92- if (__last == __first) [[__unlikely__]] {
93- return {__first, __first};
92+
93+ auto __comp = std::__less<>{};
94+ std::__identity __proj;
95+ if (static_cast <size_t >(__last - __first) < __vec_size) [[__unlikely__]] {
96+ return std::__minmax_element_loop (__first, __last, __comp, __proj);
9497 }
9598
9699 __value_type __min_element = *__first;
@@ -114,7 +117,7 @@ __minmax_element_vectorized(_Iter __first, _Iter __last) {
114117 }
115118 // block max
116119 auto __block_max_element = __builtin_reduce_max (__vec[__i]);
117- if (__block_max_element > __max_element) {
120+ if (__block_max_element >= __max_element) {
118121 __max_element = __block_max_element;
119122 __max_block_start = __first + __i * __vec_size;
120123 __max_block_end = __first + (__i + 1 ) * __vec_size;
@@ -134,7 +137,7 @@ __minmax_element_vectorized(_Iter __first, _Iter __last) {
134137 }
135138 // max
136139 auto __block_max_element = __builtin_reduce_max (__vec);
137- if (__block_max_element > __max_element) {
140+ if (__block_max_element >= __max_element) {
138141 __max_element = __block_max_element;
139142 __max_block_start = __first;
140143 __max_block_end = __first + __vec_size;
@@ -143,34 +146,34 @@ __minmax_element_vectorized(_Iter __first, _Iter __last) {
143146 }
144147
145148 if (__last > __first) {
146- auto __comp = std::__less<>{};
147- std::__identity __proj;
148149 auto __epilogue = std::__minmax_element_loop (__first, __last, __comp, __proj);
149- auto __epilogue_min_element = *__epilogue.first ;
150- auto __epilogue_max_element = *__epilogue.second ;
151- if (__epilogue_min_element < __min_element && __epilogue_max_element > __max_element) {
150+ __value_type __epilogue_min_element = *__epilogue.first ;
151+ __value_type __epilogue_max_element = *__epilogue.second ;
152+ if (__epilogue_min_element < __min_element && __epilogue_max_element >= __max_element) {
152153 return __epilogue;
153154 } else if (__epilogue_min_element < __min_element) {
154155 __min_element = __epilogue_min_element;
155- __min_block_start = __first ;
156- __min_block_end = __first ; // this is global min_element
157- } else { // __epilogue_max_element > __max_element
156+ __min_block_start = __epilogue. first ;
157+ __min_block_end = __epilogue. first ; // this is global min_element
158+ } else if ( __epilogue_max_element >= __max_element) {
158159 __max_element = __epilogue_max_element;
159- __max_block_start = __first ;
160- __max_block_end = __first ; // this is global max_element
160+ __max_block_start = __epilogue. second ;
161+ __max_block_end = __epilogue. second ; // this is global max_element
161162 }
162163 }
163164
164165 // locate min
165166 for (; __min_block_start != __min_block_end; ++__min_block_start) {
166- if (*__min_block_start == __min_element)
167+ __value_type __cur_min_element = *__min_block_start;
168+ if ( __cur_min_element == __min_element)
167169 break ;
168170 }
169171
170172 // locate max
171- for (; __max_block_start != __max_block_end; ++__max_block_start) {
172- if (*__max_block_start == __max_element)
173- break ;
173+ for (_Iter __it = __max_block_start; __it != __max_block_end; ++__it) {
174+ __value_type __cur_max_element = *__it;
175+ if ( __cur_max_element == __max_element)
176+ __max_block_start = __it;
174177 }
175178
176179
@@ -211,6 +214,8 @@ __minmax_element_impl(_Iter __first, _Iter __last, _Comp& __comp, _Proj& __proj)
211214// } else {
212215// }
213216// }
217+ #endif // _LIBCPP_VECTORIZE_ALGORITHMS
218+
214219template <class _Iter , class _Sent , class _Proj , class _Comp >
215220_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter, _Iter>
216221__minmax_element_impl (_Iter __first, _Sent __last, _Comp& __comp, _Proj& __proj) {
0 commit comments