• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "usr_lib_define.h"
2 #include <string.h>
3 #include <stdint.h>
4 #include <limits.h>
5 
6 #define ALIGN (sizeof(size_t))
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 
strlen(const char * s)11 _LIBC_TEXT_SECTION size_t strlen(const char *s)
12 {
13 	const char *a = s;
14 #ifdef __GNUC__
15 	typedef size_t __attribute__((__may_alias__)) word;
16 	const word *w;
17 	for (; (uintptr_t)s % ALIGN; s++) if (!*s) return s-a;
18 	for (w = (const void *)s; !HASZERO(*w); w++);
19 	s = (const void *)w;
20 #endif
21 	for (; *s; s++);
22 	return s-a;
23 }
24