2020-06-15 14:18:57 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
2023-12-08 03:11:56 +00:00
|
|
|
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
2020-06-15 14:18:57 +00:00
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2023-04-27 03:45:01 +00:00
|
|
|
#include "libc/str/str.h"
|
2024-09-01 08:14:40 +00:00
|
|
|
#include "libc/str/kmp.h"
|
|
|
|
#include "third_party/aarch64/arm_neon.internal.h"
|
|
|
|
#include "third_party/intel/immintrin.internal.h"
|
2021-10-04 13:24:56 +00:00
|
|
|
|
2020-06-15 14:18:57 +00:00
|
|
|
/**
|
|
|
|
* Searches for substring.
|
|
|
|
*
|
2024-09-01 08:14:40 +00:00
|
|
|
* This function offers assurances against pathological cases, using KMP
|
|
|
|
* if no progress is being made on the O(nm) vectorized fast path. It is
|
|
|
|
* important to note that, if `needle` is untrusted, that it not be long
|
|
|
|
* enough to overflow the stack. That's because KMP needs to allocate an
|
|
|
|
* array of longs the same length as `needle` and it needs to do it with
|
|
|
|
* stack memory since POSIX requires this function to be safe to call in
|
|
|
|
* signal handlers.
|
|
|
|
*
|
2020-06-15 14:18:57 +00:00
|
|
|
* @param haystack is the search area, as a NUL-terminated string
|
|
|
|
* @param needle is the desired substring, also NUL-terminated
|
|
|
|
* @return pointer to first substring within haystack, or NULL
|
|
|
|
* @asyncsignalsafe
|
2022-08-06 16:56:17 +00:00
|
|
|
* @see strcasestr()
|
2020-06-15 14:18:57 +00:00
|
|
|
* @see memmem()
|
|
|
|
*/
|
2024-02-01 11:39:46 +00:00
|
|
|
__vex char *strstr(const char *haystack, const char *needle) {
|
2024-09-01 08:14:40 +00:00
|
|
|
if (haystack == needle || !*needle)
|
|
|
|
return (char *)haystack;
|
2023-07-30 18:05:05 +00:00
|
|
|
#if defined(__x86_64__) && !defined(__chibicc__)
|
2021-02-07 14:11:44 +00:00
|
|
|
size_t i;
|
2021-10-04 13:24:56 +00:00
|
|
|
unsigned k, m;
|
2024-09-01 08:14:40 +00:00
|
|
|
const __m128i *p;
|
|
|
|
long progress = 0;
|
|
|
|
__m128i v, n, z = _mm_setzero_si128();
|
|
|
|
const char *hay = haystack;
|
|
|
|
n = _mm_set1_epi8(*needle);
|
2021-02-07 14:11:44 +00:00
|
|
|
for (;;) {
|
2024-09-01 08:14:40 +00:00
|
|
|
k = (uintptr_t)hay & 15;
|
|
|
|
p = (const __m128i *)((uintptr_t)hay & -16);
|
|
|
|
v = _mm_load_si128(p);
|
|
|
|
m = _mm_movemask_epi8(
|
|
|
|
_mm_or_si128(_mm_cmpeq_epi8(v, z), _mm_cmpeq_epi8(v, n)));
|
2021-10-04 13:24:56 +00:00
|
|
|
m >>= k;
|
|
|
|
m <<= k;
|
|
|
|
while (!m) {
|
2024-09-01 08:14:40 +00:00
|
|
|
progress += 16;
|
|
|
|
v = _mm_load_si128(++p);
|
|
|
|
m = _mm_movemask_epi8(
|
|
|
|
_mm_or_si128(_mm_cmpeq_epi8(v, z), _mm_cmpeq_epi8(v, n)));
|
2021-10-04 13:24:56 +00:00
|
|
|
}
|
2024-09-01 08:14:40 +00:00
|
|
|
int offset = __builtin_ctzl(m);
|
|
|
|
progress += offset;
|
|
|
|
hay = (const char *)p + offset;
|
2021-10-04 13:24:56 +00:00
|
|
|
for (i = 0;; ++i) {
|
2024-09-01 08:14:40 +00:00
|
|
|
if (--progress <= -512)
|
|
|
|
goto OfferPathologicalAssurances;
|
Apply clang-format update to repo (#1154)
Commit bc6c183 introduced a bunch of discrepancies between what files
look like in the repo and what clang-format says they should look like.
However, there were already a few discrepancies prior to that. Most of
these discrepancies seemed to be unintentional, but a few of them were
load-bearing (e.g., a #include that violated header ordering needing
something to have been #defined by a 'later' #include.)
I opted to take what I hope is a relatively smooth-brained approach: I
reverted the .clang-format change, ran clang-format on the whole repo,
reapplied the .clang-format change, reran clang-format again, and then
reverted the commit that contained the first run. Thus the full effect
of this PR should only be to apply the changed formatting rules to the
repo, and from skimming the results, this seems to be the case.
My work can be checked by applying the short, manual commits, and then
rerunning the command listed in the autogenerated commits (those whose
messages I have prefixed auto:) and seeing if your results agree.
It might be that the other diffs should be fixed at some point but I'm
leaving that aside for now.
fd '\.c(c|pp)?$' --print0| xargs -0 clang-format -i
2024-04-25 17:38:00 +00:00
|
|
|
if (!needle[i])
|
2024-09-01 08:14:40 +00:00
|
|
|
return (/*unconst*/ char *)hay;
|
|
|
|
if (!hay[i])
|
Apply clang-format update to repo (#1154)
Commit bc6c183 introduced a bunch of discrepancies between what files
look like in the repo and what clang-format says they should look like.
However, there were already a few discrepancies prior to that. Most of
these discrepancies seemed to be unintentional, but a few of them were
load-bearing (e.g., a #include that violated header ordering needing
something to have been #defined by a 'later' #include.)
I opted to take what I hope is a relatively smooth-brained approach: I
reverted the .clang-format change, ran clang-format on the whole repo,
reapplied the .clang-format change, reran clang-format again, and then
reverted the commit that contained the first run. Thus the full effect
of this PR should only be to apply the changed formatting rules to the
repo, and from skimming the results, this seems to be the case.
My work can be checked by applying the short, manual commits, and then
rerunning the command listed in the autogenerated commits (those whose
messages I have prefixed auto:) and seeing if your results agree.
It might be that the other diffs should be fixed at some point but I'm
leaving that aside for now.
fd '\.c(c|pp)?$' --print0| xargs -0 clang-format -i
2024-04-25 17:38:00 +00:00
|
|
|
break;
|
2024-09-01 08:14:40 +00:00
|
|
|
if (needle[i] != hay[i])
|
Apply clang-format update to repo (#1154)
Commit bc6c183 introduced a bunch of discrepancies between what files
look like in the repo and what clang-format says they should look like.
However, there were already a few discrepancies prior to that. Most of
these discrepancies seemed to be unintentional, but a few of them were
load-bearing (e.g., a #include that violated header ordering needing
something to have been #defined by a 'later' #include.)
I opted to take what I hope is a relatively smooth-brained approach: I
reverted the .clang-format change, ran clang-format on the whole repo,
reapplied the .clang-format change, reran clang-format again, and then
reverted the commit that contained the first run. Thus the full effect
of this PR should only be to apply the changed formatting rules to the
repo, and from skimming the results, this seems to be the case.
My work can be checked by applying the short, manual commits, and then
rerunning the command listed in the autogenerated commits (those whose
messages I have prefixed auto:) and seeing if your results agree.
It might be that the other diffs should be fixed at some point but I'm
leaving that aside for now.
fd '\.c(c|pp)?$' --print0| xargs -0 clang-format -i
2024-04-25 17:38:00 +00:00
|
|
|
break;
|
2021-02-07 14:11:44 +00:00
|
|
|
}
|
2024-09-01 08:14:40 +00:00
|
|
|
if (!*hay++)
|
Apply clang-format update to repo (#1154)
Commit bc6c183 introduced a bunch of discrepancies between what files
look like in the repo and what clang-format says they should look like.
However, there were already a few discrepancies prior to that. Most of
these discrepancies seemed to be unintentional, but a few of them were
load-bearing (e.g., a #include that violated header ordering needing
something to have been #defined by a 'later' #include.)
I opted to take what I hope is a relatively smooth-brained approach: I
reverted the .clang-format change, ran clang-format on the whole repo,
reapplied the .clang-format change, reran clang-format again, and then
reverted the commit that contained the first run. Thus the full effect
of this PR should only be to apply the changed formatting rules to the
repo, and from skimming the results, this seems to be the case.
My work can be checked by applying the short, manual commits, and then
rerunning the command listed in the autogenerated commits (those whose
messages I have prefixed auto:) and seeing if your results agree.
It might be that the other diffs should be fixed at some point but I'm
leaving that aside for now.
fd '\.c(c|pp)?$' --print0| xargs -0 clang-format -i
2024-04-25 17:38:00 +00:00
|
|
|
break;
|
2021-02-07 14:11:44 +00:00
|
|
|
}
|
2021-10-04 13:24:56 +00:00
|
|
|
return 0;
|
2024-09-01 08:14:40 +00:00
|
|
|
OfferPathologicalAssurances:
|
|
|
|
#elif defined(__aarch64__) && defined(__ARM_NEON)
|
2023-05-02 20:38:16 +00:00
|
|
|
size_t i;
|
2024-09-01 08:14:40 +00:00
|
|
|
const char *hay = haystack;
|
|
|
|
uint8x16_t n = vdupq_n_u8(*needle);
|
|
|
|
uint8x16_t z = vdupq_n_u8(0);
|
|
|
|
long progress = 0;
|
2023-05-02 20:38:16 +00:00
|
|
|
for (;;) {
|
2024-09-01 08:14:40 +00:00
|
|
|
int k = (uintptr_t)hay & 15;
|
|
|
|
hay = (const char *)((uintptr_t)hay & -16);
|
|
|
|
uint8x16_t v = vld1q_u8((const uint8_t *)hay);
|
|
|
|
uint8x16_t cmp = vorrq_u8(vceqq_u8(v, z), vceqq_u8(v, n));
|
|
|
|
uint8x8_t mask = vshrn_n_u16(vreinterpretq_u16_u8(cmp), 4);
|
|
|
|
uint64_t m;
|
|
|
|
vst1_u8((uint8_t *)&m, mask);
|
|
|
|
m >>= k * 4;
|
|
|
|
m <<= k * 4;
|
|
|
|
while (!m) {
|
|
|
|
hay += 16;
|
|
|
|
progress += 16;
|
|
|
|
v = vld1q_u8((const uint8_t *)hay);
|
|
|
|
cmp = vorrq_u8(vceqq_u8(v, z), vceqq_u8(v, n));
|
|
|
|
mask = vshrn_n_u16(vreinterpretq_u16_u8(cmp), 4);
|
|
|
|
vst1_u8((uint8_t *)&m, mask);
|
|
|
|
}
|
|
|
|
int offset = __builtin_ctzll(m) >> 2;
|
|
|
|
progress += offset;
|
|
|
|
hay += offset;
|
2023-05-02 20:38:16 +00:00
|
|
|
for (i = 0;; ++i) {
|
2024-09-01 08:14:40 +00:00
|
|
|
if (--progress <= -512)
|
|
|
|
goto OfferPathologicalAssurances;
|
Apply clang-format update to repo (#1154)
Commit bc6c183 introduced a bunch of discrepancies between what files
look like in the repo and what clang-format says they should look like.
However, there were already a few discrepancies prior to that. Most of
these discrepancies seemed to be unintentional, but a few of them were
load-bearing (e.g., a #include that violated header ordering needing
something to have been #defined by a 'later' #include.)
I opted to take what I hope is a relatively smooth-brained approach: I
reverted the .clang-format change, ran clang-format on the whole repo,
reapplied the .clang-format change, reran clang-format again, and then
reverted the commit that contained the first run. Thus the full effect
of this PR should only be to apply the changed formatting rules to the
repo, and from skimming the results, this seems to be the case.
My work can be checked by applying the short, manual commits, and then
rerunning the command listed in the autogenerated commits (those whose
messages I have prefixed auto:) and seeing if your results agree.
It might be that the other diffs should be fixed at some point but I'm
leaving that aside for now.
fd '\.c(c|pp)?$' --print0| xargs -0 clang-format -i
2024-04-25 17:38:00 +00:00
|
|
|
if (!needle[i])
|
2024-09-01 08:14:40 +00:00
|
|
|
return (/*unconst*/ char *)hay;
|
|
|
|
if (!hay[i])
|
Apply clang-format update to repo (#1154)
Commit bc6c183 introduced a bunch of discrepancies between what files
look like in the repo and what clang-format says they should look like.
However, there were already a few discrepancies prior to that. Most of
these discrepancies seemed to be unintentional, but a few of them were
load-bearing (e.g., a #include that violated header ordering needing
something to have been #defined by a 'later' #include.)
I opted to take what I hope is a relatively smooth-brained approach: I
reverted the .clang-format change, ran clang-format on the whole repo,
reapplied the .clang-format change, reran clang-format again, and then
reverted the commit that contained the first run. Thus the full effect
of this PR should only be to apply the changed formatting rules to the
repo, and from skimming the results, this seems to be the case.
My work can be checked by applying the short, manual commits, and then
rerunning the command listed in the autogenerated commits (those whose
messages I have prefixed auto:) and seeing if your results agree.
It might be that the other diffs should be fixed at some point but I'm
leaving that aside for now.
fd '\.c(c|pp)?$' --print0| xargs -0 clang-format -i
2024-04-25 17:38:00 +00:00
|
|
|
break;
|
2024-09-01 08:14:40 +00:00
|
|
|
if (needle[i] != hay[i])
|
Apply clang-format update to repo (#1154)
Commit bc6c183 introduced a bunch of discrepancies between what files
look like in the repo and what clang-format says they should look like.
However, there were already a few discrepancies prior to that. Most of
these discrepancies seemed to be unintentional, but a few of them were
load-bearing (e.g., a #include that violated header ordering needing
something to have been #defined by a 'later' #include.)
I opted to take what I hope is a relatively smooth-brained approach: I
reverted the .clang-format change, ran clang-format on the whole repo,
reapplied the .clang-format change, reran clang-format again, and then
reverted the commit that contained the first run. Thus the full effect
of this PR should only be to apply the changed formatting rules to the
repo, and from skimming the results, this seems to be the case.
My work can be checked by applying the short, manual commits, and then
rerunning the command listed in the autogenerated commits (those whose
messages I have prefixed auto:) and seeing if your results agree.
It might be that the other diffs should be fixed at some point but I'm
leaving that aside for now.
fd '\.c(c|pp)?$' --print0| xargs -0 clang-format -i
2024-04-25 17:38:00 +00:00
|
|
|
break;
|
2023-05-02 20:38:16 +00:00
|
|
|
}
|
2024-09-01 08:14:40 +00:00
|
|
|
if (!*hay++)
|
Apply clang-format update to repo (#1154)
Commit bc6c183 introduced a bunch of discrepancies between what files
look like in the repo and what clang-format says they should look like.
However, there were already a few discrepancies prior to that. Most of
these discrepancies seemed to be unintentional, but a few of them were
load-bearing (e.g., a #include that violated header ordering needing
something to have been #defined by a 'later' #include.)
I opted to take what I hope is a relatively smooth-brained approach: I
reverted the .clang-format change, ran clang-format on the whole repo,
reapplied the .clang-format change, reran clang-format again, and then
reverted the commit that contained the first run. Thus the full effect
of this PR should only be to apply the changed formatting rules to the
repo, and from skimming the results, this seems to be the case.
My work can be checked by applying the short, manual commits, and then
rerunning the command listed in the autogenerated commits (those whose
messages I have prefixed auto:) and seeing if your results agree.
It might be that the other diffs should be fixed at some point but I'm
leaving that aside for now.
fd '\.c(c|pp)?$' --print0| xargs -0 clang-format -i
2024-04-25 17:38:00 +00:00
|
|
|
break;
|
2023-05-02 20:38:16 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2024-09-01 08:14:40 +00:00
|
|
|
OfferPathologicalAssurances:
|
2023-05-02 20:38:16 +00:00
|
|
|
#endif
|
2024-09-01 08:14:40 +00:00
|
|
|
return __memmem_kmp(haystack, strlen(haystack), needle, strlen(needle));
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|