summaryrefslogtreecommitdiff
path: root/zap/src/strcpy.c
blob: 616af7f8434e9673b0c4b00c702980bbb3c7815e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
	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>

#if zap_priv_fastimpl
__asm__ (
	".globl zap_strcpy\n"

	"zap_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
size_t zap_strcpy(char const * const _in,char * const _out) {
	char const * inpos  = _in;
	char *       outpos = _out;
	for (;;++inpos,++outpos) {
		char const chr = *inpos;
		*outpos = chr;
		if (chr == '\x0') {return inpos - _in;}
	}
	sus_unreach();
}
#endif