Make more libc improvements

- Make memmem() faster
- Make readdir() thread safe
- Remove 64kb limit from mkdeps.com
- Add old crypt() function from Musl
- Improve new fix-third-party.py tool
- Improve libc/isystem/ headers and fix bugs
This commit is contained in:
Justine Tunney 2022-08-06 17:18:40 -07:00
parent a8cf0f7e89
commit 6a5717a48f
50 changed files with 3783 additions and 280 deletions

View file

@ -16,8 +16,13 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/bits/likely.h"
#include "libc/dce.h"
#include "libc/intrin/asan.internal.h"
#include "libc/str/str.h"
typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(16)));
/**
* Searches for fixed-length substring in memory region.
*
@ -27,26 +32,41 @@
* @param needlelen is its character count
* @return pointer to first result or NULL if not found
*/
void *memmem(const void *haystack, size_t haystacklen, const void *needle,
size_t needlelen) {
size_t i, j;
const char *p;
noasan void *memmem(const void *haystack, size_t haystacklen,
const void *needle, size_t needlelen) {
char c;
xmm_t n, *v;
unsigned i, k, m;
const char *p, *q, *e;
if (IsAsan()) __asan_verify(needle, needlelen);
if (IsAsan()) __asan_verify(haystack, haystacklen);
if (!needlelen) return haystack;
if (needlelen <= haystacklen) {
p = memchr(haystack, *(const char *)needle, haystacklen);
if (needlelen == 1) return p;
if (p) {
haystacklen -= p - (const char *)haystack;
haystack = p;
if (UNLIKELY(needlelen > haystacklen)) return 0;
q = needle;
c = *q;
n = (xmm_t){c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c};
p = haystack;
e = p + haystacklen;
k = (uintptr_t)p & 15;
v = (const xmm_t *)((uintptr_t)p & -16);
m = __builtin_ia32_pmovmskb128(*v == n);
m >>= k;
m <<= k;
for (;;) {
while (!m) {
++v;
if ((const char *)v >= e) return 0;
m = __builtin_ia32_pmovmskb128(*v == n);
}
/* TODO: make not quadratic */
for (i = 0; i < haystacklen; ++i) {
for (j = 0;; ++j) {
if (j == needlelen) return (/*unconst*/ char *)haystack + i;
if (i + j == haystacklen) break;
if (((char *)needle)[j] != ((char *)haystack)[i + j]) break;
do {
k = __builtin_ctzl(m);
p = (const char *)v + k;
if (UNLIKELY(p + needlelen > e)) return 0;
for (i = 1;; ++i) {
if (i == needlelen) return (/*unconst*/ char *)p;
if (p[i] != q[i]) break;
}
}
m &= ~(1 << k);
} while (m);
}
return NULL;
}