summaryrefslogtreecommitdiff
path: root/zap/src/strlen.c
diff options
context:
space:
mode:
Diffstat (limited to 'zap/src/strlen.c')
-rw-r--r--zap/src/strlen.c56
1 files changed, 56 insertions, 0 deletions
diff --git a/zap/src/strlen.c b/zap/src/strlen.c
new file mode 100644
index 0000000..c1e8959
--- /dev/null
+++ b/zap/src/strlen.c
@@ -0,0 +1,56 @@
+/*
+ Copyright 2022 Gabriel Jensen.
+ This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
+ If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
+*/
+
+#include <zap/priv.h>
+
+#include <stddef.h>
+
+#if defined(zap_priv_fastimpl)
+__asm__ (
+ ".global zap_strlen\n"
+
+ "zap_strlen:\n"
+ /*
+ char const * str
+ */
+#if defined(sus_arch_amd64)
+ /* rax: Address of the current character. */
+ "movq %rdi,%rax\n"
+ /* rdx: Current character. */
+ ".loop:\n"
+ "movb (%rax),%dl\n"
+ "testb %dl,%dl\n"
+ "jz .done\n" /* Exit loop if we have reached the null-terminator. */
+ "incq %rax\n" /* Continue to the next character. */
+ "jmp .loop\n"
+ ".done:\n"
+ "subq %rdi,%rax\n"
+ "ret\n"
+#elif defined(sus_arch_ia32)
+ /* eax: Address of the current character. */
+ "movl 0x4(%esp),%eax\n"
+ /* ecx: Current character. */
+ ".loop:\n"
+ "movb (%eax),%cl\n"
+ "testb %cl,%cl\n"
+ "jz .done\n" /* Exit loop if we have reached the null-terminator. */
+ "incl %eax\n" /* Continue to the next character. */
+ "jmp .loop\n"
+ ".done:\n"
+ "subl 0x4(%esp),%eax\n"
+ "ret\n"
+#endif
+);
+#else
+size_t zap_strlen(char const * const _str) {
+ char const * pos = _str;
+ for (;;++pos) {
+ char const chr = *pos;
+ sus_unlikely (chr == '\x0') {return (size_t)(pos - _str);}
+ }
+ sus_unreach();
+}
+#endif