Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<algorithm>: Vectorize reverse_copy() #181

Closed
StephanTLavavej opened this issue Oct 18, 2019 · 0 comments · Fixed by #804
Closed

<algorithm>: Vectorize reverse_copy() #181

StephanTLavavej opened this issue Oct 18, 2019 · 0 comments · Fixed by #804
Labels
fixed Something works now, yay! performance Must go faster

Comments

@StephanTLavavej
Copy link
Member

Currently, we vectorize reverse():

STL/stl/inc/xutility

Lines 4599 to 4623 in 957fe99

template <class _BidIt>
void reverse(const _BidIt _First, const _BidIt _Last) { // reverse elements in [_First, _Last)
_Adl_verify_range(_First, _Last);
auto _UFirst = _Get_unwrapped(_First);
auto _ULast = _Get_unwrapped(_Last);
#if (defined(_M_IX86) || defined(_M_X64)) && !defined(_M_CEE_PURE) && !defined(_M_HYBRID)
using _Elem = remove_pointer_t<decltype(_UFirst)>;
constexpr bool _Allow_vectorization =
conjunction_v<is_pointer<decltype(_UFirst)>, _Is_trivially_swappable<_Elem>, negation<is_volatile<_Elem>>>;
if constexpr (_Allow_vectorization && sizeof(_Elem) == 1) {
__std_reverse_trivially_swappable_1(_UFirst, _ULast);
} else if constexpr (_Allow_vectorization && sizeof(_Elem) == 2) {
__std_reverse_trivially_swappable_2(_UFirst, _ULast);
} else if constexpr (_Allow_vectorization && sizeof(_Elem) == 4) {
__std_reverse_trivially_swappable_4(_UFirst, _ULast);
} else if constexpr (_Allow_vectorization && sizeof(_Elem) == 8) {
__std_reverse_trivially_swappable_8(_UFirst, _ULast);
} else
#endif // (defined(_M_IX86) || defined(_M_X64)) && !defined(_M_CEE_PURE) && !defined(_M_HYBRID)
{
for (; _UFirst != _ULast && _UFirst != --_ULast; ++_UFirst) {
_STD iter_swap(_UFirst, _ULast);
}
}
}

We should extend this significant optimization to reverse_copy():

STL/stl/inc/algorithm

Lines 1975 to 1988 in 957fe99

template <class _BidIt, class _OutIt>
_OutIt reverse_copy(_BidIt _First, _BidIt _Last, _OutIt _Dest) {
// copy reversing elements in [_First, _Last)
_Adl_verify_range(_First, _Last);
const auto _UFirst = _Get_unwrapped(_First);
auto _ULast = _Get_unwrapped(_Last);
auto _UDest = _Get_unwrapped_n(_Dest, _Idl_distance<_BidIt>(_UFirst, _ULast));
for (; _UFirst != _ULast; ++_UDest) {
*_UDest = *--_ULast;
}
_Seek_wrapped(_Dest, _UDest);
return _Dest;
}

@StephanTLavavej StephanTLavavej added enhancement Something can be improved performance Must go faster labels Oct 18, 2019
@StephanTLavavej StephanTLavavej removed the enhancement Something can be improved label Feb 6, 2020
@CaseyCarter CaseyCarter added the fixed Something works now, yay! label Jul 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed Something works now, yay! performance Must go faster
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants