summaryrefslogtreecommitdiff
path: root/zap/source/amd64/mem/memeq.S
blob: ba43dfceceaa4779080bca89d68891b50b33748b (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
# 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/.

.globl zap_memeq

zap_memeq:
	/* rdi: Left pointer. */
	/* rsi: Number of remaining elements. */
	/* rdx: Right pointer. */
	/* rax: Current left element. */
	/* rcx: Current right element. */

	/* Compare words: */
.wrdcmp:

	/* Check if there's at least one word left: */
	cmpq $0x8,%rsi
	jl .bytecmp /* If not, skip to byte checks: */
	
	/* Copy the values into registers: */
	movq (%rdi),%rax
	movq (%rdx),%rcx

	/* Check if the words are equal: */
	cmpq %rax,%rcx
	jne .neq

	/* Mark eight more bytes as equal: */	
	addq $0x8,%rdi
	addq $0x8,%rdx
	subq $0x8,%rsi

	/* Continue to the next word: */
	jmp .wrdcmp

	/* Compare bytes: */
.bytecmp:

	/* Check if there are any bytes left: */
	testq %rsi,%rsi
	jz .eq /* If we have reached the final element, all previous elements have compared equal, and the memory sequences are equal. */

	/* Copy the values into registers: */
	movb (%rdi),%al
	movb (%rdx),%cl

	cmpb %al,%cl
	jne .neq
	
	/* Mark another byte as equal: */
	incq %rdi
	incq %rdx
	decq %rsi
	
	/* Continue to the next byte: */
	jmp .bytecmp

	/* The memory sequences have compared equal: */
.eq:
	movb $0xFF,%al
	ret

	/* The memory sequences have compared NOT equal: */
.neq:
	movb $0x0,%al
	ret