xref: /freebsd/lib/libc/i386/string/memcmp.S (revision e6d808aee319c457c0f73f906b8a2b1d888f793b)
12ceb2ce9SGarrett Wollman/*
22ceb2ce9SGarrett Wollman * Copyright (c) 1993 Winning Strategies, Inc.
32ceb2ce9SGarrett Wollman * All rights reserved.
42ceb2ce9SGarrett Wollman *
52ceb2ce9SGarrett Wollman * Redistribution and use in source and binary forms, with or without
62ceb2ce9SGarrett Wollman * modification, are permitted provided that the following conditions
72ceb2ce9SGarrett Wollman * are met:
82ceb2ce9SGarrett Wollman * 1. Redistributions of source code must retain the above copyright
92ceb2ce9SGarrett Wollman *    notice, this list of conditions and the following disclaimer.
102ceb2ce9SGarrett Wollman * 2. Redistributions in binary form must reproduce the above copyright
112ceb2ce9SGarrett Wollman *    notice, this list of conditions and the following disclaimer in the
122ceb2ce9SGarrett Wollman *    documentation and/or other materials provided with the distribution.
132ceb2ce9SGarrett Wollman * 3. All advertising materials mentioning features or use of this software
142ceb2ce9SGarrett Wollman *    must display the following acknowledgement:
152ceb2ce9SGarrett Wollman *      This product includes software developed by Winning Strategies, Inc.
162ceb2ce9SGarrett Wollman * 4. The name of the author may not be used to endorse or promote products
172ceb2ce9SGarrett Wollman *    derived from this software without specific prior written permission
182ceb2ce9SGarrett Wollman *
192ceb2ce9SGarrett Wollman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
202ceb2ce9SGarrett Wollman * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
212ceb2ce9SGarrett Wollman * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
222ceb2ce9SGarrett Wollman * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
232ceb2ce9SGarrett Wollman * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
242ceb2ce9SGarrett Wollman * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
252ceb2ce9SGarrett Wollman * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
262ceb2ce9SGarrett Wollman * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
272ceb2ce9SGarrett Wollman * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
282ceb2ce9SGarrett Wollman * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
292ceb2ce9SGarrett Wollman */
302ceb2ce9SGarrett Wollman
312ceb2ce9SGarrett Wollman#if defined(LIBC_RCS) && !defined(lint)
32717d9cddSDavid Greenman	.text
337f3dea24SPeter Wemm        .asciz "$FreeBSD$"
342ceb2ce9SGarrett Wollman#endif /* LIBC_RCS and not lint */
352ceb2ce9SGarrett Wollman
36e6d808aeSPeter Wemm#include <machine/asm.h>
372ceb2ce9SGarrett Wollman
382ceb2ce9SGarrett Wollman/*
392ceb2ce9SGarrett Wollman * memcmp (void *b1, void *b2, size_t len)
402ceb2ce9SGarrett Wollman *
412ceb2ce9SGarrett Wollman * Written by:
422ceb2ce9SGarrett Wollman *	J.T. Conklin (jtc@wimsey.com), Winning Strategies, Inc.
432ceb2ce9SGarrett Wollman */
442ceb2ce9SGarrett Wollman
452ceb2ce9SGarrett WollmanENTRY(memcmp)
462ceb2ce9SGarrett Wollman	pushl	%edi
472ceb2ce9SGarrett Wollman	pushl	%esi
482ceb2ce9SGarrett Wollman	movl	12(%esp),%edi
492ceb2ce9SGarrett Wollman	movl	16(%esp),%esi
502ceb2ce9SGarrett Wollman	cld				/* set compare direction forward */
512ceb2ce9SGarrett Wollman
522ceb2ce9SGarrett Wollman	movl	20(%esp),%ecx		/* compare by words */
532ceb2ce9SGarrett Wollman	shrl	$2,%ecx
542ceb2ce9SGarrett Wollman	repe
552ceb2ce9SGarrett Wollman	cmpsl
562ceb2ce9SGarrett Wollman	jne	L5			/* do we match so far? */
572ceb2ce9SGarrett Wollman
582ceb2ce9SGarrett Wollman	movl	20(%esp),%ecx		/* compare remainder by bytes */
592ceb2ce9SGarrett Wollman	andl	$3,%ecx
602ceb2ce9SGarrett Wollman	repe
612ceb2ce9SGarrett Wollman	cmpsb
622ceb2ce9SGarrett Wollman	jne	L6			/* do we match? */
632ceb2ce9SGarrett Wollman
642ceb2ce9SGarrett Wollman	xorl	%eax,%eax		/* we match, return zero	*/
652ceb2ce9SGarrett Wollman	popl	%esi
662ceb2ce9SGarrett Wollman	popl	%edi
672ceb2ce9SGarrett Wollman	ret
682ceb2ce9SGarrett Wollman
692ceb2ce9SGarrett WollmanL5:	movl	$4,%ecx			/* We know that one of the next	*/
702ceb2ce9SGarrett Wollman	subl	%ecx,%edi		/* four pairs of bytes do not	*/
712ceb2ce9SGarrett Wollman	subl	%ecx,%esi		/* match.			*/
722ceb2ce9SGarrett Wollman	repe
732ceb2ce9SGarrett Wollman	cmpsb
742ceb2ce9SGarrett WollmanL6:	movzbl  -1(%edi),%eax		/* Perform unsigned comparison	*/
752ceb2ce9SGarrett Wollman        movzbl  -1(%esi),%edx
762ceb2ce9SGarrett Wollman        subl    %edx,%eax
772ceb2ce9SGarrett Wollman	popl	%esi
782ceb2ce9SGarrett Wollman	popl	%edi
792ceb2ce9SGarrett Wollman	ret
80