936fb5cf56da2d00f3947d0aed2644a9397daef8
[musl] / src / string / strlen.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <limits.h>
5
6 #define ALIGN (sizeof(size_t)-1)
7 #define ONES ((size_t)-1/UCHAR_MAX)
8 #define HIGHS (ONES * (UCHAR_MAX/2+1))
9 #define HASZERO(x) ((x)-ONES & ~(x) & HIGHS)
10
11 size_t strlen(const char *s)
12 {
13         const char *a = s;
14         const size_t *w;
15         for (; ((uintptr_t)s & ALIGN) && *s; s++);
16         if (*s) {
17                 for (w = (const void *)s; !HASZERO(*w); w++);
18                 for (s = (const void *)w; *s; s++);
19         }
20         return s-a;
21 }