summaryrefslogtreecommitdiff
path: root/zap/src/strlen.c
blob: 84b7d28eb3a0169602768b9197eb37c463ead295 (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
/*
	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_strlen\n"

	"zap_strlen:\n"
		/*
			char const * str
		*/
#if defined(sus_arch_amd64)
		/* rax: Address of the current character. */
		"movq %rdi,%rax\n"
		/* rdx: Current character. */
	".loop:\n"
		"movb (%rax),%dl\n"
		"testb %dl,%dl\n"
		"jz .done\n" /* Exit loop if we have reached the null-terminator. */
		"incq %rax\n" /* Continue to the next character. */
		"jmp .loop\n"
	".done:\n"
		"subq %rdi,%rax\n"
		"ret\n"
#elif defined(sus_arch_ia32)
		/* eax: Address of the current character. */
		"movl 0x4(%esp),%eax\n"
		/* ecx: Current character. */
	".loop:\n"
		"movb (%eax),%cl\n"
		"testb %cl,%cl\n"
		"jz .done\n" /* Exit loop if we have reached the null-terminator. */
		"incl %eax\n" /* Continue to the next character. */
		"jmp .loop\n"
	".done:\n"
		"subl 0x4(%esp),%eax\n"
		"ret\n"
#endif
);
#else
size_t zap_strlen(char const * const _str) {
	char const * pos = _str;
	for (;;++pos) {
		char const chr = *pos;
		sus_unlikely (chr == '\x0') {return (size_t)(pos - _str);}
	}
	sus_unreach();
}
#endif