1570af302Sopenharmony_ci#include <string.h>
2570af302Sopenharmony_ci#include <stdint.h>
3570af302Sopenharmony_ci#include <limits.h>
4570af302Sopenharmony_ci
5570af302Sopenharmony_ci#define ALIGN (sizeof(size_t))
6570af302Sopenharmony_ci#define ONES ((size_t)-1/UCHAR_MAX)
7570af302Sopenharmony_ci#define HIGHS (ONES * (UCHAR_MAX/2+1))
8570af302Sopenharmony_ci#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS)
9570af302Sopenharmony_ci
10570af302Sopenharmony_cichar *__strchrnul(const char *s, int c)
11570af302Sopenharmony_ci{
12570af302Sopenharmony_ci	c = (unsigned char)c;
13570af302Sopenharmony_ci	if (!c) return (char *)s + strlen(s);
14570af302Sopenharmony_ci
15570af302Sopenharmony_ci#ifdef __GNUC__
16570af302Sopenharmony_ci	typedef size_t __attribute__((__may_alias__)) word;
17570af302Sopenharmony_ci	const word *w;
18570af302Sopenharmony_ci	for (; (uintptr_t)s % ALIGN; s++)
19570af302Sopenharmony_ci		if (!*s || *(unsigned char *)s == c) return (char *)s;
20570af302Sopenharmony_ci	size_t k = ONES * c;
21570af302Sopenharmony_ci	for (w = (void *)s; !HASZERO(*w) && !HASZERO(*w^k); w++);
22570af302Sopenharmony_ci	s = (void *)w;
23570af302Sopenharmony_ci#endif
24570af302Sopenharmony_ci	for (; *s && *(unsigned char *)s != c; s++);
25570af302Sopenharmony_ci	return (char *)s;
26570af302Sopenharmony_ci}
27570af302Sopenharmony_ci
28570af302Sopenharmony_ciweak_alias(__strchrnul, strchrnul);
29