summaryrefslogtreecommitdiff
path: root/rgo/src/memeq.S
blob: bd57f43773d5963799305c343edce9402b998b60 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
	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.h>

.global rgo_memeq

rgo_memeq:
	/*
		void const * lptr
		size_t       num
		void const * rptr
	*/
#if defined(__i386__)
	/* eax: Address of the current left element. */
	movl 0x4(%esp),%eax
	/* ecx: Number of remaining elements. */
	movl 0x8(%esp),%ecx
	/* edx: Address of the current right element. */
	movl 0xC(%esp),%edx
	/* ebx: Current left element. */
	pushl %ebx
	/* ebx/esi: Current right element. */
	pushl %esi
.wrdcmp:
	cmpl $0x4,%ecx
	jl .bytecmp
	movl (%eax),%ebx
	movl (%edx),%esi
	cmpl %ebx,%esi
	jne .neq
	addl $0x4,%eax
	addl $0x4,%edx
	subl $0x4,%ecx
	jmp .wrdcmp
.bytecmp:
	testl %ecx,%ecx
	jne .eq /* If we have reached the final element, all previous elements have compared equal, and the memory sequences are equal. */
	movb (%eax),%bl
	movb (%edx),%bh
	cmpb %bl,%bh
	jne .neq
	incl %eax
	incl %edx
	decl %ecx
	jmp .bytecmp
.eq:
	popl %ebx
	popl %esi
	movb $0x1,%al
	ret
.neq:
	popl %ebx
	popl %esi
	movb $0x0,%al
	ret
#elif defined(__x86_64__)
	/* rdi: Address of the current left element. */
	/* rsi: Number of remaining elements. */
	/* rdx: Address of the current right element. */
	/* rax: Current left element. */
	/* rcx: Current right element. */
.wrdcmp:
	cmpq $0x8,%rsi
	jl .bytecmp
	movq (%rdi),%rax
	movq (%rdx),%rcx
	cmpq %rax,%rcx
	jne .neq
	addq $0x8,%rdi
	addq $0x8,%rdx
	subq $0x8,%rsi
	jmp .wrdcmp
.bytecmp:
	testq %rsi,%rsi
	jne .eq /* If we have reached the final element, all previous elements have compared equal, and the memory sequences are equal. */
	movb (%rdi),%al
	movb (%rdx),%cl
	cmpb %al,%cl
	jne .neq
	incq %rdi
	incq %rdx
	decq %rsi
	jmp .bytecmp
.eq:
	movb $0x1,%al
	ret
.neq:
	movb $0x0,%al
	ret
#endif