diff options
Diffstat (limited to 'rgo/src/memeq.c')
-rw-r--r-- | rgo/src/memeq.c | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/rgo/src/memeq.c b/rgo/src/memeq.c new file mode 100644 index 0000000..03ae2c2 --- /dev/null +++ b/rgo/src/memeq.c @@ -0,0 +1,106 @@ +/* + 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_memeq\n" + + "rgo_memeq:\n" + /* + void const * lptr + sus_typ_usz num + void const * rptr + */ +#if defined(sus_arch_amd64) + /* rdi: Address of the current left element. */ + /* rsi: Number of remaining elements. */ + /* rdx: Address of the current right element. */ + /* rax: Current left element. */ + /* rcx: Current right element. */ + ".wrdcmp:\n" + "cmpq $0x8,%rsi\n" + "jl .bytecmp\n" + "movq (%rdi),%rax\n" + "movq (%rdx),%rcx\n" + "cmpq %rax,%rcx\n" + "jne .neq\n" + "addq $0x8,%rdi\n" + "addq $0x8,%rdx\n" + "subq $0x8,%rsi\n" + "jmp .wrdcmp\n" + ".bytecmp:\n" + "testq %rsi,%rsi\n" + "jne .eq\n" /* If we have reached the final element, all previous elements have compared equal, and the memory sequences are equal. */ + "movb (%rdi),%al\n" + "movb (%rdx),%cl\n" + "cmpb %al,%cl\n" + "jne .neq\n" + "incq %rdi\n" + "incq %rdx\n" + "decq %rsi\n" + "jmp .bytecmp\n" + ".eq:\n" + "movb $0x1,%al\n" + "ret\n" + ".neq:\n" + "movb $0x0,%al\n" + "ret\n" +#elif defined(sus_arch_ia32) + /* eax: Address of the current left element. */ + "movl 0x4(%esp),%eax\n" + /* ecx: Number of remaining elements. */ + "movl 0x8(%esp),%ecx\n" + /* edx: Address of the current right element. */ + "movl 0xC(%esp),%edx\n" + /* ebx: Current left element. */ + "pushl %ebx\n" + /* ebx/esi: Current right element. */ + "pushl %esi\n" + ".wrdcmp:\n" + "cmpl $0x4,%ecx\n" + "jl .bytecmp\n" + "movl (%eax),%ebx\n" + "movl (%edx),%esi\n" + "cmpl %ebx,%esi\n" + "jne .neq\n" + "addl $0x4,%eax\n" + "addl $0x4,%edx\n" + "subl $0x4,%ecx\n" + "jmp .wrdcmp\n" + ".bytecmp:\n" + "testl %ecx,%ecx\n" + "jne .eq\n" /* If we have reached the final element, all previous elements have compared equal, and the memory sequences are equal. */ + "movb (%eax),%bl\n" + "movb (%edx),%bh\n" + "cmpb %bl,%bh\n" + "jne .neq\n" + "incl %eax\n" + "incl %edx\n" + "decl %ecx\n" + "jmp .bytecmp\n" + ".eq:\n" + "popl %ebx\n" + "popl %esi\n" + "movb $0x1,%al\n" + "ret\n" + ".neq:\n" + "popl %ebx\n" + "popl %esi\n" + "movb $0x0,%al\n" + "ret\n" +#endif +); +#else +sus_typ_u8 rgo_memeq(void const * const sus_restr _lptr,sus_typ_usz const _num,void const * const sus_restr _rptr) { + sus_typ_u8 const * lpos = (sus_typ_u8 const *)_lptr; + sus_typ_u8 const * sus_restr rpos = (sus_typ_u8 const *)_rptr; + sus_typ_u8 const * const afterbuf = lpos + _num; + for (;lpos != afterbuf;++lpos,++rpos) {sus_likely (*lpos != *rpos) {return sus_typlit_u8(0x0);}} + return sus_typlit_u8(0x1); +} +#endif |