Skip to content
Snippets Groups Projects
Commit 5e6c7782 authored by Christoph Hellwig's avatar Christoph Hellwig
Browse files

add a (inefficient) rawmemchr implementation

parent 1ff4c079
No related branches found
No related tags found
No related merge requests found
......@@ -493,6 +493,7 @@ libc += string/memmove.o
libc += string/mempcpy.o
libc += string/memrchr.o
libc += string/memset.o
libc += string/rawmemchr.o
libc += string/rindex.o
libc += string/stpcpy.o
libc += string/stpncpy.o
......
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include "libc.h"
#define SS (sizeof(size_t))
#define ALIGN (sizeof(size_t)-1)
#define ONES ((size_t)-1/UCHAR_MAX)
#define HIGHS (ONES * (UCHAR_MAX/2+1))
#define HASZERO(x) (((x)-ONES) & ~(x) & HIGHS)
void *__rawmemchr(const void *src, int c)
{
long n = LONG_MAX;
const unsigned char *s = src;
c = (unsigned char)c;
for (; ((uintptr_t)s & ALIGN) && n && *s != c; s++, n--);
if (n && *s != c) {
const size_t *w;
size_t k = ONES * c;
for (w = (const void *)s; n>=SS && !HASZERO(*w^k); w++, n-=SS);
for (s = (const void *)w; n && *s != c; s++, n--);
}
return n ? (void *)s : 0;
}
weak_alias(__rawmemchr, rawmemchr);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment