diff options
Diffstat (limited to 'rgo/src/streq.c')
-rw-r--r-- | rgo/src/streq.c | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/rgo/src/streq.c b/rgo/src/streq.c new file mode 100644 index 0000000..7cf2f21 --- /dev/null +++ b/rgo/src/streq.c @@ -0,0 +1,78 @@ +/* + 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_streq\n" + + "rgo_streq:\n" + /* + char const * lstr + char const * rstr + */ +#if defined(sus_arch_amd64) + /* rax: Address of the current input character. */ + "movq %rdi,%rax\n" + /* rsi: Address of the current output character. */ + "movq %rsi,%rsi\n" + /* rdx: Current input character. */ + /* rcx: Current output character. */ + ".loop:\n" + "movb (%rax),%dl\n" /* Move the characters into registers. */ + "movb (%rsi),%cl\n" + "cmpb %dl,%cl\n" /* Check if the characters are equal... */ + "jne .neq\n" /* ... indicate inequality if they are not. */ + "testb %dl,%dl\n" /* Check if we have reached the null-terminator... */ + "jz .eq\n" /* ... indicate equality if we have. */ + "incq %rax\n" /* Increment positions. */ + "incq %rsi\n" + "jmp .loop\n" /* Restart loop. */ + ".eq:\n" /* Indicate equality. */ + "movb $0x1,%al\n" + "ret\n" + ".neq:\n" /* Indicate inequality. */ + "movb $0x0,%al\n" + "ret\n" +#elif defined(sus_arch_ia32) + /* eax: Address of the current input character. */ + "movl 0x4(%esp),%eax\n" + /* ecx: Address of the current output character. */ + "movl 0x8(%esp),%ecx\n" + /* edx: Current input character. */ + /* edx: Current output character. */ + ".loop:\n" + "movb (%eax),%dl\n" /* Move the characters into registers. */ + "movb (%ecx),%dh\n" + "cmpb %dl,%dh\n" /* Check if the characters are equal... */ + "jne .neq\n" /* ... indicate inequality if they are not. */ + "testb %dl,%dl\n" /* Check if we have reached the null-terminator... */ + "jz .eq\n" /* ... indicate equality if we have. */ + "incl %eax\n" /* Increment positions. */ + "incl %ecx\n" + "jmp .loop\n" /* Restart loop. */ + ".eq:\n" /* Indicate equality. */ + "movb $0x1,%al\n" + "ret\n" + ".neq:\n" /* Indicate inequality. */ + "movb $0x0,%al\n" + "ret\n" +#endif +); +#else +sus_typ_u8 rgo_streq(char const * const sus_restr _lstr,char const * const sus_restr _rstr) { + char const * sus_restr lpos = _lstr; + char const * sus_restr rpos = _rstr; + for (;;++lpos,++rpos) { + char const lchr = *lpos; + char const rchr = *rpos; + sus_likely (lchr != rchr) {return sus_typlit_u8(0x0);} + if (lchr == '\x0') {return sus_typlit_u8(0x1);} + } + sus_unreach(); +} +#endif |