aboutsummaryrefslogtreecommitdiff
path: root/src/userspace/mlibc/string/strlen.c
diff options
context:
space:
mode:
authorMarvin Borner2019-12-21 12:25:14 +0100
committerMarvin Borner2019-12-21 12:25:14 +0100
commit38610cd06dc0b5a3a4ee46f5fe7c341191aa2bc1 (patch)
tree759378766ee6b22d72a0f2fded1b0dd530376a6e /src/userspace/mlibc/string/strlen.c
parent07530dd08e0b29573712b54543a7fc42672bb34b (diff)
Some userspace improvements
GAS is bad, NASM is awesome.
Diffstat (limited to 'src/userspace/mlibc/string/strlen.c')
-rw-r--r--src/userspace/mlibc/string/strlen.c32
1 files changed, 29 insertions, 3 deletions
diff --git a/src/userspace/mlibc/string/strlen.c b/src/userspace/mlibc/string/strlen.c
index 133ee3d..f6a06a4 100644
--- a/src/userspace/mlibc/string/strlen.c
+++ b/src/userspace/mlibc/string/strlen.c
@@ -2,7 +2,33 @@
size_t strlen(const char *str)
{
- size_t len = 0;
- while (str[len]) len++;
- return len;
+ const char *char_ptr;
+ const unsigned long int *longword_ptr;
+ unsigned long int longword, himagic, lomagic;
+
+ for (char_ptr = str; ((unsigned long int) char_ptr & (sizeof(longword) - 1)) != 0; ++char_ptr)
+ if (*char_ptr == '\0')
+ return char_ptr - str;
+
+ longword_ptr = (unsigned long int *) char_ptr;
+
+ himagic = 0x80808080L;
+ lomagic = 0x01010101L;
+
+ for (;;) {
+ longword = *longword_ptr++;
+
+ if (((longword - lomagic) & himagic) != 0) {
+ const char *cp = (const char *) (longword_ptr - 1);
+
+ if (cp[0] == 0)
+ return cp - str;
+ if (cp[1] == 0)
+ return cp - str + 1;
+ if (cp[2] == 0)
+ return cp - str + 2;
+ if (cp[3] == 0)
+ return cp - str + 3;
+ }
+ }
} \ No newline at end of file