diff options
Diffstat (limited to 'rgo/src/strcpy.c')
-rw-r--r-- | rgo/src/strcpy.c | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/rgo/src/strcpy.c b/rgo/src/strcpy.c new file mode 100644 index 0000000..4e26312 --- /dev/null +++ b/rgo/src/strcpy.c @@ -0,0 +1,67 @@ +/* + 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_strcpy\n" + + "rgo_strcpy:\n" + /* + char const * in + char const * out + */ +#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 character. */ + ".loop:\n" + "movb (%rax),%dl\n" /* Move current the character into a register... */ + "movb %dl,(%rsi)\n" /* ... and then back into memory. */ + "testb %dl,%dl\n" /* Check if we have reached the null-terminator... */ + "jz .done\n" /* ... and if so, we are finished copying. */ + "incq %rax\n" /* Increment the positions. */ + "incq %rsi\n" + "jmp .loop\n" /* Restart the loop. */ + ".done:\n" + "subq %rdi,%rax\n" /* Get the length of the string we copyied. */ + "decq %rdi\n" /* We do not count the null-terminator in the string length. */ + "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 character. */ + ".loop:\n" + "movb (%eax),%dl\n" /* Move current the character into a register... */ + "movb %dl,(%ecx)\n" /* ... and then back into memory. */ + "testb %dl,%dl\n" /* Check if we have reached the null-terminator... */ + "jz .done\n" /* ... and if so, we are finished copying. */ + "incl %eax\n" /* Increment the positions. */ + "incl %ecx\n" + "jmp .loop\n" /* Restart the loop. */ + ".done:\n" + "subl 0x4(%esp),%eax\n" /* Get the length of the string we copyied. */ + "decl %ecx \n" /* We do not count the null-terminator in the string length. */ + "ret\n" +#endif +); +#else +sus_typ_usz rgo_strcpy(char const * const sus_restr _in,char * const sus_restr _out) { + char const * sus_restr inpos = _in; + char * sus_restr outpos = _out; + for (;;++inpos,++outpos) { + char const chr = *inpos; + *outpos = chr; + if (chr == '\x0') {return (sus_typ_usz)(inpos - _in);} + } + sus_unreach(); +} +#endif |