summaryrefslogblamecommitdiff
path: root/zap/src/mem/memeq.S
blob: cf554c2d9da27845f594d12d98c4cc95dc4fba14 (plain) (tree)

















































































                                                                                                                                         
/*
	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