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
74
75
76
77
78
79
80
81
82
83
|
/*
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 <stdbool.h>
#include <stdint.h>
#if zap_priv_fastimpl
__asm__ (
".globl zap_streq\n"
"zap_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
bool zap_streq(char const * const _lstr,char const * const _rstr) {
char const * lpos = _lstr;
char const * rpos = _rstr;
for (;;++lpos,++rpos) {
char const lchr = *lpos;
char const rchr = *rpos;
sus_likely (lchr != rchr) {return false;}
if (lchr == '\x0') {return true;}
}
sus_unreach();
}
#endif
|