diff options
Diffstat (limited to 'zap/src/mem/memeq.S')
-rw-r--r-- | zap/src/mem/memeq.S | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/zap/src/mem/memeq.S b/zap/src/mem/memeq.S new file mode 100644 index 0000000..cf554c2 --- /dev/null +++ b/zap/src/mem/memeq.S @@ -0,0 +1,82 @@ +/* + 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> + +.globl zap_memeq + +zap_memeq: + + /* + void const * lptr + zap_sz num + void const * rptr + */ + +#if defined(__amd64__) + + /* 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 + +#endif |