blob: fc4eb2b396ea2f22f7e97b005c05bafb15533844 (
plain) (
tree)
|
|
/*
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 <zap/mem.h>
#include <stddef.h>
#include <stdint.h>
#if zap_priv_fastimpl
__asm__ (
".globl 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 SIZE_MAX;}
}
sus_unreach();
}
#endif
|