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
72
73
|
/*
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>
#include <stdint.h>
#if zap_priv_fastimpl
__asm__ (
".globl zap_fndchr\n"
"zap_fndchr:\n"
/*
char const * str
char chr
*/
#if defined(sus_arch_amd64)
/* rax: Address of the current character. */
"movq %rdi,%rax\n"
/* rdx: Current character. */
".loop:\n"
"movb (%rax),%dl\n"
"cmpb %dl,%sil\n"
"je .fnd\n" /* Exit loop if we have found the character. */
"testb %dl,%dl\n"
"je .nfnd\n" /* We encountered the null-terminator but not the specified character. */
"incq %rax\n"
"jmp .loop\n"
".fnd:\n"
"subq %rdi,%rax\n"
"ret\n"
".nfnd:\n"
"movq $0xFFFFFFFFFFFFFFFF,%rax\n"
"ret\n"
#elif defined(sus_arch_ia32)
/* eax: Address of the current character. */
"movl 0x4(%esp),%eax\n"
/* ecx: Character. */
"movb 0x8(%esp),%cl\n"
/* edx: Current character. */
".loop:\n"
"movb (%eax),%dl\n"
"cmpb %dl,%cl\n"
"je .fnd\n" /* Exit loop if we have found the character. */
"testb %dl,%dl\n"
"je .nfnd\n" /* We encountered the null-terminator but not the specified character. */
"incl %eax\n"
"jmp .loop\n"
".fnd:\n"
"subl 0x4(%esp),%eax\n"
"ret\n"
".nfnd:\n"
"movl $0xFFFFFFFF,%eax\n"
"ret\n"
#endif
);
#else
size_t zap_fndchr(char const * const _str,char const _chr) {
char const * pos = _str;
for (;;++pos) {
char const chr = *pos;
sus_unlikely (chr == _chr) {return (size_t)(pos - _str);}
sus_unlikely (chr == '\x0') {return SIZE_MAX;}
}
sus_unreach();
}
#endif
|