xref: /onnv-gate/usr/src/lib/libc/i386/gen/memcmp.s (revision 7298:b69e27387f74)
10Sstevel@tonic-gate/*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7298SMark.J.Nelson@Sun.COM * Common Development and Distribution License (the "License").
6*7298SMark.J.Nelson@Sun.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate/*
220Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
26*7298SMark.J.Nelson@Sun.COM	.file	"memcmp.s"
270Sstevel@tonic-gate
280Sstevel@tonic-gate/
290Sstevel@tonic-gate/ memcmp(s1, s2, n)
300Sstevel@tonic-gate/
310Sstevel@tonic-gate/ Compares n bytes:  s1>s2: >0  s1==s2:  0  s1<s2:  <0
320Sstevel@tonic-gate/
330Sstevel@tonic-gate/ Fast assembly language version of the following C-program strcat
340Sstevel@tonic-gate/ which represents the `standard' for the C-library.
350Sstevel@tonic-gate/
360Sstevel@tonic-gate/	int
370Sstevel@tonic-gate/	memcmp(const void *s1, const void *s2, size_t n)
380Sstevel@tonic-gate/	{
390Sstevel@tonic-gate/		if (s1 != s2 && n != 0) {
400Sstevel@tonic-gate/			const unsigned char	*ps1 = s1;
410Sstevel@tonic-gate/			const unsigned char	*ps2 = s2;
420Sstevel@tonic-gate/
430Sstevel@tonic-gate/			do {
440Sstevel@tonic-gate/				if (*ps1++ != *ps2++)
450Sstevel@tonic-gate/					return (ps1[-1] - ps2[-1]);
460Sstevel@tonic-gate/			} while (--n != 0);
470Sstevel@tonic-gate/		}
480Sstevel@tonic-gate/		return (NULL);
490Sstevel@tonic-gate/	}
500Sstevel@tonic-gate/
510Sstevel@tonic-gate/ This implementation conforms to SVID but does not implement
520Sstevel@tonic-gate/ the same algorithm as the portable version because it is
530Sstevel@tonic-gate/ inconvenient to get the difference of the differing characters.
540Sstevel@tonic-gate
550Sstevel@tonic-gate#include <sys/asm_linkage.h>
560Sstevel@tonic-gate
570Sstevel@tonic-gate	ANSI_PRAGMA_WEAK(memcmp,function)
580Sstevel@tonic-gate
590Sstevel@tonic-gate#include "SYS.h"
600Sstevel@tonic-gate
610Sstevel@tonic-gate	ENTRY(memcmp)
620Sstevel@tonic-gate	pushl	%edi		/ save register variable
630Sstevel@tonic-gate	movl	8(%esp), %eax	/ %eax = address of string 1
640Sstevel@tonic-gate	movl	12(%esp), %ecx	/ %ecx = address of string 2
650Sstevel@tonic-gate	cmpl	%eax, %ecx	/ if the same string
660Sstevel@tonic-gate	je	.equal		/ goto .equal
670Sstevel@tonic-gate	movl	16(%esp), %edi	/ %edi = length in bytes
680Sstevel@tonic-gate	cmpl	$4, %edi	/ if %edi < 4
690Sstevel@tonic-gate	jb	.byte_check	/ goto .byte_check
700Sstevel@tonic-gate	.align	4
710Sstevel@tonic-gate.word_loop:
720Sstevel@tonic-gate	movl	(%ecx), %edx	/ move 1 word from (%ecx) to %edx
730Sstevel@tonic-gate	leal	-4(%edi), %edi	/ %edi -= 4
740Sstevel@tonic-gate	cmpl	(%eax), %edx	/ compare 1 word from (%eax) with %edx
750Sstevel@tonic-gate	jne	.word_not_equal	/ if not equal, goto .word_not_equal
760Sstevel@tonic-gate	leal	4(%ecx), %ecx	/ %ecx += 4 (next word)
770Sstevel@tonic-gate	leal	4(%eax), %eax	/ %eax += 4 (next word)
780Sstevel@tonic-gate	cmpl	$4, %edi	/ if %edi >= 4
790Sstevel@tonic-gate	jae	.word_loop	/ goto .word_loop
800Sstevel@tonic-gate.byte_check:
810Sstevel@tonic-gate	cmpl	$0, %edi	/ if %edi == 0
820Sstevel@tonic-gate	je	.equal		/ goto .equal
830Sstevel@tonic-gate	jmp	.byte_loop	/ goto .byte_loop (checks in bytes)
840Sstevel@tonic-gate.word_not_equal:
850Sstevel@tonic-gate	leal	4(%edi), %edi	/ %edi += 4 (post-decremented)
860Sstevel@tonic-gate	.align	4
870Sstevel@tonic-gate.byte_loop:
880Sstevel@tonic-gate	movb	(%ecx),	%dl	/ move 1 byte from (%ecx) to %dl
890Sstevel@tonic-gate	cmpb	%dl, (%eax)	/ compare %dl with 1 byte from (%eax)
900Sstevel@tonic-gate	jne	.not_equal	/ if not equal, goto .not_equal
910Sstevel@tonic-gate	incl	%ecx		/ %ecx++ (next byte)
920Sstevel@tonic-gate	incl	%eax		/ %eax++ (next byte)
930Sstevel@tonic-gate	decl	%edi		/ %edi--
940Sstevel@tonic-gate	jnz	.byte_loop	/ if not zero, goto .byte_loop
950Sstevel@tonic-gate.equal:
960Sstevel@tonic-gate	xorl	%eax, %eax	/ %eax = 0
970Sstevel@tonic-gate	popl	%edi		/ restore register variable
980Sstevel@tonic-gate	ret			/ return (NULL)
990Sstevel@tonic-gate	.align	4
1000Sstevel@tonic-gate.not_equal:
1010Sstevel@tonic-gate	sbbl	%eax, %eax	/ %eax = 0 if no carry, %eax = -1 if carry
1020Sstevel@tonic-gate	orl	$1, %eax	/ %eax = 1 if no carry, %eax = -1 if carry
1030Sstevel@tonic-gate	popl	%edi		/ restore register variable
1040Sstevel@tonic-gate	ret			/ return (NULL)
1050Sstevel@tonic-gate	SET_SIZE(memcmp)
106