-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[libc++] Add constant folding for optimized std::find variants #96491
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -132,6 +132,19 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp* __constexpr_memchr(_Tp* | |
| static_assert(sizeof(_Tp) == 1 && __libcpp_is_trivially_equality_comparable<_Tp, _Up>::value, | ||
| "Calling memchr on non-trivially equality comparable types is unsafe."); | ||
|
|
||
| if (__builtin_constant_p(__count) && __builtin_constant_p(__value)) { | ||
| for (; __count; --__count) { | ||
| if (!__builtin_constant_p(*__str)) | ||
| break; | ||
| if (*__str == __value) | ||
| return __str; | ||
| ++__str; | ||
| } | ||
| } | ||
|
|
||
| if (__builtin_constant_p(__count) && __count == 0) | ||
| return nullptr; | ||
|
|
||
| if (__libcpp_is_constant_evaluated()) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can get rid of this branch since |
||
| // use __builtin_char_memchr to optimize constexpr evaluation if we can | ||
| #if _LIBCPP_STD_VER >= 17 && __has_builtin(__builtin_char_memchr) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we file a bug report against Clang to complain that |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add some kind of comment explaining that this is to aid constant-folding and it also handles evaluation inside constant expressions, since that is not super obvious.