/*
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_fndbyte\n"
"zap_fndbyte:\n"
/*
void const * pos
size_t num
unsigned char byte
*/
#if defined(sus_arch_amd64)
/* rax: Address of the current element. */
"movq %rdi,%rax\n"
/* rsi: Address of the element after the last element. */
"addq %rdi,%rsi\n"
/* rcx: Current element. */
".loop:\n"
"cmpq %rax,%rsi\n"
"je .nfnd\n" /* We have went through the entire array without finding the byte. */
"movb (%rax),%cl\n"
"cmpb %cl,%dl\n"
"je .fnd\n" /* We have found the byte. */
"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 element. */
"movl 0x4(%esp),%eax\n"
/* ecx: Address of the element after the last element. */
"movl 0x8(%esp),%ecx\n"
"addl %eax,%ecx\n"
/* edx: Byte value. */
"movb 0xC(%esp),%dl\n"
/* ebx: Current element. */
"pushl %ebx\n"
".loop:\n"
"cmpl %eax,%ecx\n"
"je .nfnd\n" /* We have went through the entire array without finding the byte. */
"movb (%eax),%bl\n"
"cmpb %bl,%dl\n"
"je .fnd\n" /* We have found the byte. */
"incl %eax\n"
"jmp .loop\n"
".fnd:\n"
"popl %ebx\n"
"subl 0x4(%esp),%eax\n"
"ret\n"
".nfnd:\n"
"popl %ebx\n"
"movl $0xFFFFFFFF,%eax\n"
"ret\n"
#endif
);
#else
size_t zap_fndbyte(void const * const _ptr,size_t const _num,unsigned char const _byte) {
unsigned char const * startpos = _ptr;
unsigned char const * pos = startpos;
unsigned char const * const afterbuf = pos + _num;
for (;pos != afterbuf;++pos) {sus_unlikely (*pos == _byte) {return pos - startpos;}}
return SIZE_MAX;
}
#endif