• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <string.h>
2 #include <stdint.h>
3 #include <limits.h>
4 
5 #ifdef __LITEOS__
6 #undef ALIGN
7 #endif
8 #define ALIGN (sizeof(size_t))
9 #define ONES ((size_t)-1/UCHAR_MAX)
10 #define HIGHS (ONES * (UCHAR_MAX/2+1))
11 #define HASZERO(x) (((x)-ONES) & ~(x) & HIGHS)
12 
strlen(const char * s)13 size_t strlen(const char *s)
14 {
15 	const char *a = s;
16 #ifdef __GNUC__
17 	typedef size_t __attribute__((__may_alias__)) word;
18 	const word *w;
19 	for (; (uintptr_t)s % ALIGN; s++) if (!*s) return s-a;
20 	for (w = (const void *)s; !HASZERO(*w); w++);
21 	s = (const void *)w;
22 #endif
23 	for (; *s; s++);
24 	return s-a;
25 }
26