diff options
Diffstat (limited to 'zap/src/fndchr.c')
-rw-r--r-- | zap/src/fndchr.c | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/zap/src/fndchr.c b/zap/src/fndchr.c new file mode 100644 index 0000000..cd5458f --- /dev/null +++ b/zap/src/fndchr.c @@ -0,0 +1,71 @@ +/* + 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> +#include <stdint.h> + +#if defined(zap_priv_fastimpl) +__asm__ ( + ".global zap_fndchr\n" + + "zap_fndchr:\n" + /* + char const * str + char chr + */ +#if defined(sus_arch_amd64) + /* rax: Address of the current character. */ + "movq %rdi,%rax\n" + /* rdx: Current character. */ + ".loop:\n" + "movb (%rax),%dl\n" + "cmpb %dl,%sil\n" + "je .fnd\n" /* Exit loop if we have found the character. */ + "testb %dl,%dl\n" + "je .nfnd\n" /* We encountered the null-terminator but not the specified character. */ + "incq %rax\n" + "jmp .loop\n" + ".fnd:\n" + "subq %rdi,%rax\n" + "ret\n" + ".nfnd:\n" + "movq $0xFFFFFFFFFFFFFFFF,%rax\n" + "ret\n" +#elif defined(sus_arch_ia32) + /* eax: Address of the current character. */ + "movl 0x4(%esp),%eax\n" + /* ecx: Character. */ + "movb 0x8(%esp),%cl\n" + /* edx: Current character. */ + ".loop:\n" + "movb (%eax),%dl\n" + "cmpb %dl,%cl\n" + "je .fnd\n" /* Exit loop if we have found the character. */ + "testb %dl,%dl\n" + "je .nfnd\n" /* We encountered the null-terminator but not the specified character. */ + "incl %eax\n" + "jmp .loop\n" + ".fnd:\n" + "subl 0x4(%esp),%eax\n" + "ret\n" + ".nfnd:\n" + "movl $0xFFFFFFFF,%eax\n" + "ret\n" +#endif +); +#else +size_t zap_fndchr(char const * const _str,char const _chr) { + char const * pos = _str; + for (;;++pos) { + char const chr = *pos; + sus_unlikely (chr == _chr) {return (size_t)(pos - _str);} + sus_unlikely (chr == '\x0') {return zap_typlit_usz(-0x1);} + } + sus_unreach(); +} +#endif |