summaryrefslogtreecommitdiff
path: root/rgo/src/fndbyte.c
diff options
context:
space:
mode:
Diffstat (limited to 'rgo/src/fndbyte.c')
-rw-r--r--rgo/src/fndbyte.c74
1 files changed, 74 insertions, 0 deletions
diff --git a/rgo/src/fndbyte.c b/rgo/src/fndbyte.c
new file mode 100644
index 0000000..29945a0
--- /dev/null
+++ b/rgo/src/fndbyte.c
@@ -0,0 +1,74 @@
+/*
+ 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_fndbyte\n"
+
+ "rgo_fndbyte:\n"
+ /*
+ void const * ptr
+ sus_typ_usz num
+ sus_typ_u8 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
+sus_typ_usz rgo_fndbyte(void const * const sus_restr _ptr,sus_typ_usz const _num,sus_typ_u8 const _byte) {
+ sus_typ_u8 const * ptr = (sus_typ_u8 const *)_ptr;
+ sus_typ_u8 const * const afterbuf = ptr + _num;
+ for (;ptr != afterbuf;++ptr) {sus_unlikely (*ptr == _byte) {return ptr - (sus_typ_u8 const *)_ptr;}}
+ return sus_typlit_usz(-0x1);
+}
+#endif