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

inline internal function that are called only once in vector_algorithms.cpp #4113

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 53 additions & 82 deletions stl/src/vector_algorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -543,33 +543,6 @@ namespace {
_Mode_both = _Mode_min | _Mode_max,
};

template <_Min_max_mode _Mode, class _STy, class _UTy>
auto _Minmax_tail(const void* _First, const void* _Last, _Min_max_element_t& _Res, bool _Sign, _UTy _Cur_min,
_UTy _Cur_max) noexcept {
constexpr _UTy _Correction = _UTy{1} << (sizeof(_UTy) * 8 - 1);

if constexpr (_Mode == _Mode_min) {
if (_Sign) {
return _Min_tail(_First, _Last, _Res._Min, static_cast<_STy>(_Cur_min));
} else {
return _Min_tail(_First, _Last, _Res._Min, static_cast<_UTy>(_Cur_min + _Correction));
}
} else if constexpr (_Mode == _Mode_max) {
if (_Sign) {
return _Max_tail(_First, _Last, _Res._Max, static_cast<_STy>(_Cur_max));
} else {
return _Max_tail(_First, _Last, _Res._Max, static_cast<_UTy>(_Cur_max + _Correction));
}
} else {
if (_Sign) {
return _Both_tail(_First, _Last, _Res, static_cast<_STy>(_Cur_min), static_cast<_STy>(_Cur_max));
} else {
return _Both_tail(_First, _Last, _Res, static_cast<_UTy>(_Cur_min + _Correction),
static_cast<_UTy>(_Cur_max + _Correction));
}
}
}

struct _Minmax_traits_1 {
using _Signed_t = int8_t;
using _Unsigned_t = uint8_t;
Expand Down Expand Up @@ -1063,9 +1036,32 @@ namespace {
}
}
#endif // !_M_ARM64EC
using _STy = _Traits::_Signed_t;
using _UTy = _Traits::_Unsigned_t;

constexpr _UTy _Correction = _UTy{1} << (sizeof(_UTy) * 8 - 1);

return _Minmax_tail<_Mode, typename _Traits::_Signed_t, typename _Traits::_Unsigned_t>(
_First, _Last, _Res, _Sign, _Cur_min_val, _Cur_max_val);
if constexpr (_Mode == _Mode_min) {
if (_Sign) {
return _Min_tail(_First, _Last, _Res._Min, static_cast<_STy>(_Cur_min_val));
} else {
return _Min_tail(_First, _Last, _Res._Min, static_cast<_UTy>(_Cur_min_val + _Correction));
}
} else if constexpr (_Mode == _Mode_max) {
if (_Sign) {
return _Max_tail(_First, _Last, _Res._Max, static_cast<_STy>(_Cur_max_val));
} else {
return _Max_tail(_First, _Last, _Res._Max, static_cast<_UTy>(_Cur_max_val + _Correction));
}
} else {
if (_Sign) {
return _Both_tail(
_First, _Last, _Res, static_cast<_STy>(_Cur_min_val), static_cast<_STy>(_Cur_max_val));
} else {
return _Both_tail(_First, _Last, _Res, static_cast<_UTy>(_Cur_min_val + _Correction),
static_cast<_UTy>(_Cur_max_val + _Correction));
}
}
}

} // unnamed namespace
Expand Down Expand Up @@ -1135,49 +1131,6 @@ _Min_max_element_t __stdcall __std_minmax_element_8(
} // extern "C"

namespace {
template <class _Ty>
const void* _Find_trivial_unsized_fallback(const void* _First, _Ty _Val) {
auto _Ptr = static_cast<const _Ty*>(_First);
while (*_Ptr != _Val) {
++_Ptr;
}
return _Ptr;
}

template <class _Ty>
const void* _Find_trivial_tail(const void* _First, const void* _Last, _Ty _Val) {
auto _Ptr = static_cast<const _Ty*>(_First);
while (_Ptr != _Last && *_Ptr != _Val) {
++_Ptr;
}
return _Ptr;
}

template <class _Ty>
const void* _Find_trivial_last_tail(const void* _First, const void* _Last, const void* _Real_last, _Ty _Val) {
auto _Ptr = static_cast<const _Ty*>(_Last);
for (;;) {
if (_Ptr == _First) {
return _Real_last;
}
--_Ptr;
if (*_Ptr == _Val) {
return _Ptr;
}
}
}

template <class _Ty>
__declspec(noalias) size_t _Count_trivial_tail(const void* _First, const void* _Last, size_t _Current, _Ty _Val) {
auto _Ptr = static_cast<const _Ty*>(_First);
for (; _Ptr != _Last; ++_Ptr) {
if (*_Ptr == _Val) {
++_Current;
}
}
return _Current;
}

struct _Find_traits_1 {
static constexpr size_t _Shift = 0;

Expand Down Expand Up @@ -1365,8 +1318,11 @@ namespace {
}
}
#endif // !_M_ARM64EC

return _Find_trivial_unsized_fallback(_First, _Val);
auto _Ptr = static_cast<const _Ty*>(_First);
while (*_Ptr != _Val) {
++_Ptr;
}
return _Ptr;
}

template <class _Traits, class _Ty>
Expand Down Expand Up @@ -1416,8 +1372,11 @@ namespace {
} while (_First != _Stop_at);
}
#endif // !_M_ARM64EC

return _Find_trivial_tail(_First, _Last, _Val);
auto _Ptr = static_cast<const _Ty*>(_First);
while (_Ptr != _Last && *_Ptr != _Val) {
++_Ptr;
}
return _Ptr;
}

template <class _Traits, class _Ty>
Expand All @@ -1443,7 +1402,6 @@ namespace {
_Advance_bytes(_Last, (31 - _Offset) - (sizeof(_Ty) - 1));
return _Last;
}

} while (_Last != _Stop_at);
_Size_bytes &= 0x1F;
}
Expand All @@ -1464,12 +1422,19 @@ namespace {
_Advance_bytes(_Last, _Offset - (sizeof(_Ty) - 1));
return _Last;
}

} while (_Last != _Stop_at);
}
#endif // !_M_ARM64EC

return _Find_trivial_last_tail(_First, _Last, _Real_last, _Val);
auto _Ptr = static_cast<const _Ty*>(_Last);
for (;;) {
if (_Ptr == _First) {
return _Real_last;
}
--_Ptr;
if (*_Ptr == _Val) {
return _Ptr;
}
}
}

template <class _Traits, class _Ty>
Expand Down Expand Up @@ -1509,8 +1474,14 @@ namespace {
} while (_First != _Stop_at);
}
#endif // !_M_ARM64EC

return _Count_trivial_tail(_First, _Last, _Result >> _Traits::_Shift, _Val);
_Result >>= _Traits::_Shift;
auto _Ptr = static_cast<const _Ty*>(_First);
for (; _Ptr != _Last; ++_Ptr) {
if (*_Ptr == _Val) {
++_Result;
}
}
return _Result;
}
} // unnamed namespace

Expand Down