summaryrefslogtreecommitdiff
path: root/zap/source/amd64/mem/memeq.S
diff options
context:
space:
mode:
Diffstat (limited to 'zap/source/amd64/mem/memeq.S')
-rw-r--r--zap/source/amd64/mem/memeq.S67
1 files changed, 67 insertions, 0 deletions
diff --git a/zap/source/amd64/mem/memeq.S b/zap/source/amd64/mem/memeq.S
new file mode 100644
index 0000000..ba43dfc
--- /dev/null
+++ b/zap/source/amd64/mem/memeq.S
@@ -0,0 +1,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