diff options
Diffstat (limited to 'rgo/src/strlen.c')
-rw-r--r-- | rgo/src/strlen.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/rgo/src/strlen.c b/rgo/src/strlen.c new file mode 100644 index 0000000..6ddbf8b --- /dev/null +++ b/rgo/src/strlen.c @@ -0,0 +1,54 @@ +/* + 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 <rgo-priv.h> + +#if defined(rgo_priv_fastimpl) +__asm__ ( + ".global rgo_strlen\n" + + "rgo_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 +sus_typ_usz rgo_strlen(char const * const sus_restr _str) { + char const * sus_restr pos = _str; + for (;;++pos) { + char const chr = *pos; + sus_unlikely (chr == '\x0') {return (sus_typ_usz)(pos - _str);} + } + sus_unreach(); +} +#endif |