Skip to content

[libc++] Optimize std::find if types are integral and have the same signedness #70345

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

Merged
merged 1 commit into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
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
19 changes: 19 additions & 0 deletions libcxx/include/__algorithm/find.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
#include <__fwd/bit_reference.h>
#include <__iterator/segmented_iterator.h>
#include <__string/constexpr_c_functions.h>
#include <__type_traits/is_integral.h>
#include <__type_traits/is_same.h>
#include <__type_traits/is_signed.h>
#include <__utility/move.h>
#include <limits>

#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
# include <cwchar>
Expand Down Expand Up @@ -76,6 +79,22 @@ __find_impl(_Tp* __first, _Tp* __last, const _Up& __value, _Proj&) {
}
#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS

// TODO: This should also be possible to get right with different signedness
// cast integral types to allow vectorization
template <class _Tp,
class _Up,
class _Proj,
__enable_if_t<__is_identity<_Proj>::value && !__libcpp_is_trivially_equality_comparable<_Tp, _Up>::value &&
is_integral<_Tp>::value && is_integral<_Up>::value &&
is_signed<_Tp>::value == is_signed<_Up>::value,
int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp*
__find_impl(_Tp* __first, _Tp* __last, const _Up& __value, _Proj& __proj) {
if (__value < numeric_limits<_Tp>::min() || __value > numeric_limits<_Tp>::max())
return __last;
return std::__find_impl(__first, __last, _Tp(__value), __proj);
}

// __bit_iterator implementation
template <bool _ToFind, class _Cp, bool _IsConst>
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, _IsConst>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//

// ADDITIONAL_COMPILE_FLAGS(gcc): -Wno-bool-compare
// ADDITIONAL_COMPILE_FLAGS(gcc-style-warnings): -Wno-sign-compare
// MSVC warning C4389: '==': signed/unsigned mismatch
// ADDITIONAL_COMPILE_FLAGS(cl-style-warnings): /wd4389
Expand Down Expand Up @@ -162,6 +163,45 @@ void test_deque() {
}
}

template <class T>
struct TestIntegerPromotions1 {
template <class U>
TEST_CONSTEXPR_CXX20 void test(T val, U to_find) {
bool expect_match = val == to_find;
assert(std::find(&val, &val + 1, to_find) == (expect_match ? &val : &val + 1));
}

template <class U>
TEST_CONSTEXPR_CXX20 void operator()() {
test<U>(0, 0);
test<U>(0, 1);
test<U>(1, 1);
test<U>(0, -1);
test<U>(-1, -1);
test<U>(0, U(-127));
test<U>(T(-127), U(-127));
test<U>(T(-128), U(-128));
test<U>(T(-129), U(-129));
test<U>(T(255), U(255));
test<U>(T(256), U(256));
test<U>(T(257), U(257));
test<U>(0, std::numeric_limits<U>::min());
test<U>(T(std::numeric_limits<U>::min()), std::numeric_limits<U>::min());
test<U>(0, std::numeric_limits<U>::min() + 1);
test<U>(T(std::numeric_limits<U>::min() + 1), std::numeric_limits<U>::min() + 1);
test<U>(0, std::numeric_limits<U>::max());
test<U>(T(std::numeric_limits<U>::max()), std::numeric_limits<U>::max());
test<U>(T(std::numeric_limits<U>::max() - 1), std::numeric_limits<U>::max() - 1);
}
};

struct TestIntegerPromotions {
template <class T>
TEST_CONSTEXPR_CXX20 void operator()() {
types::for_each(types::integral_types(), TestIntegerPromotions1<T>());
}
};

TEST_CONSTEXPR_CXX20 bool test() {
types::for_each(types::integer_types(), TestTypes<char>());
types::for_each(types::integer_types(), TestTypes<int>());
Expand All @@ -181,6 +221,8 @@ TEST_CONSTEXPR_CXX20 bool test() {
}
#endif

types::for_each(types::integral_types(), TestIntegerPromotions());

return true;
}

Expand Down