xref: /freebsd/lib/libc/i386/string/strncmp.S (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
12ceb2ce9SGarrett Wollman/*
22ceb2ce9SGarrett Wollman * Copyright (c) 1993,94 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
31e6d808aeSPeter Wemm#include <machine/asm.h>
322ceb2ce9SGarrett Wollman/*
332ceb2ce9SGarrett Wollman * strncmp(s1, s2, n)
342ceb2ce9SGarrett Wollman *	return an integer greater than, equal to, or less than 0,
352ceb2ce9SGarrett Wollman *	according as the first n characters of string s1 is greater
362ceb2ce9SGarrett Wollman *	than, equal to, or less than the string s2.
372ceb2ce9SGarrett Wollman *
382ceb2ce9SGarrett Wollman * %eax - pointer to s1
392ceb2ce9SGarrett Wollman * %ecx - pointer to s2
402ceb2ce9SGarrett Wollman * %edx - length
412ceb2ce9SGarrett Wollman *
422ceb2ce9SGarrett Wollman * Written by:
432ceb2ce9SGarrett Wollman *	J.T. Conklin (jtc@wimsey.com), Winning Strategies, Inc.
442ceb2ce9SGarrett Wollman */
452ceb2ce9SGarrett Wollman
462ceb2ce9SGarrett Wollman/*
472ceb2ce9SGarrett Wollman * I've unrolled the loop eight times: large enough to make a
482ceb2ce9SGarrett Wollman * significant difference, and small enough not to totally trash the
492ceb2ce9SGarrett Wollman * cache.
50be0264b9SBruce Evans *
51be0264b9SBruce Evans * TODO: change all the jz's back to je for consistency.
522ceb2ce9SGarrett Wollman */
532ceb2ce9SGarrett Wollman
542ceb2ce9SGarrett WollmanENTRY(strncmp)
552ceb2ce9SGarrett Wollman	pushl	%ebx
562ceb2ce9SGarrett Wollman	movl	8(%esp),%eax
572ceb2ce9SGarrett Wollman	movl	12(%esp),%ecx
582ceb2ce9SGarrett Wollman	movl	16(%esp),%edx
592ceb2ce9SGarrett Wollman	testl	%edx,%edx
602ceb2ce9SGarrett Wollman	jmp	L2			/* Jump into the loop! */
612ceb2ce9SGarrett Wollman
622ceb2ce9SGarrett Wollman	.align 2,0x90
632ceb2ce9SGarrett WollmanL1:	incl	%eax
642ceb2ce9SGarrett Wollman	incl	%ecx
652ceb2ce9SGarrett Wollman	decl	%edx
662ceb2ce9SGarrett WollmanL2:	jz	L4			/* strings are equal */
672ceb2ce9SGarrett Wollman	movb	(%eax),%bl
682ceb2ce9SGarrett Wollman	testb	%bl,%bl
692ceb2ce9SGarrett Wollman	jz	L3
702ceb2ce9SGarrett Wollman	cmpb	%bl,(%ecx)
712ceb2ce9SGarrett Wollman	jne	L3
722ceb2ce9SGarrett Wollman
73be0264b9SBruce Evans/*
74be0264b9SBruce Evans * XXX it might be best to move the next 4 instructions to the end of the
75be0264b9SBruce Evans * unrolled part of the loop.  The unrolled part would then be
76be0264b9SBruce Evans *	movb n(%eax),%bl; testb %bl, %bl; je L3; cmpb n(%ecx); jne L3
77be0264b9SBruce Evans * or maybe better
78be0264b9SBruce Evans *	movb n(%eax),%bl; cmpb n(%ecx); jne L3; testb %bl,%bl; je return_0
79be0264b9SBruce Evans * for n = 0, 1, ..., 8.  The end of the loop would be
80be0264b9SBruce Evans *	L1: addl $8,%eax; addl $8,%ecx; subl $8,%edx; cmpl $8,%edx; jae Lx
81be0264b9SBruce Evans * where residual counts of 0 to 7 are handled at Lx.  However, this would
82be0264b9SBruce Evans * be slower for short strings.  Cache effects are probably not so
83be0264b9SBruce Evans * important because we are only handling a byte at a time.
84be0264b9SBruce Evans */
852ceb2ce9SGarrett Wollman	incl	%eax
862ceb2ce9SGarrett Wollman	incl	%ecx
872ceb2ce9SGarrett Wollman	decl	%edx
882ceb2ce9SGarrett Wollman	jz	L4
892ceb2ce9SGarrett Wollman	movb	(%eax),%bl
902ceb2ce9SGarrett Wollman	testb	%bl,%bl
912ceb2ce9SGarrett Wollman	jz	L3
922ceb2ce9SGarrett Wollman	cmpb	%bl,(%ecx)
932ceb2ce9SGarrett Wollman	jne	L3
942ceb2ce9SGarrett Wollman
952ceb2ce9SGarrett Wollman	incl	%eax
962ceb2ce9SGarrett Wollman	incl	%ecx
972ceb2ce9SGarrett Wollman	decl	%edx
982ceb2ce9SGarrett Wollman	jz	L4
992ceb2ce9SGarrett Wollman	movb	(%eax),%bl
1002ceb2ce9SGarrett Wollman	testb	%bl,%bl
1012ceb2ce9SGarrett Wollman	jz	L3
1022ceb2ce9SGarrett Wollman	cmpb	%bl,(%ecx)
1032ceb2ce9SGarrett Wollman	jne	L3
1042ceb2ce9SGarrett Wollman
1052ceb2ce9SGarrett Wollman	incl	%eax
1062ceb2ce9SGarrett Wollman	incl	%ecx
1072ceb2ce9SGarrett Wollman	decl	%edx
1082ceb2ce9SGarrett Wollman	jz	L4
1092ceb2ce9SGarrett Wollman	movb	(%eax),%bl
1102ceb2ce9SGarrett Wollman	testb	%bl,%bl
1112ceb2ce9SGarrett Wollman	jz	L3
1122ceb2ce9SGarrett Wollman	cmpb	%bl,(%ecx)
1132ceb2ce9SGarrett Wollman	jne	L3
1142ceb2ce9SGarrett Wollman
1152ceb2ce9SGarrett Wollman	incl	%eax
1162ceb2ce9SGarrett Wollman	incl	%ecx
1172ceb2ce9SGarrett Wollman	decl	%edx
1182ceb2ce9SGarrett Wollman	jz	L4
1192ceb2ce9SGarrett Wollman	movb	(%eax),%bl
1202ceb2ce9SGarrett Wollman	testb	%bl,%bl
1212ceb2ce9SGarrett Wollman	jz	L3
1222ceb2ce9SGarrett Wollman	cmpb	%bl,(%ecx)
1232ceb2ce9SGarrett Wollman	jne	L3
1242ceb2ce9SGarrett Wollman
1252ceb2ce9SGarrett Wollman	incl	%eax
1262ceb2ce9SGarrett Wollman	incl	%ecx
1272ceb2ce9SGarrett Wollman	decl	%edx
1282ceb2ce9SGarrett Wollman	jz	L4
1292ceb2ce9SGarrett Wollman	movb	(%eax),%bl
1302ceb2ce9SGarrett Wollman	testb	%bl,%bl
1312ceb2ce9SGarrett Wollman	jz	L3
1322ceb2ce9SGarrett Wollman	cmpb	%bl,(%ecx)
1332ceb2ce9SGarrett Wollman	jne	L3
1342ceb2ce9SGarrett Wollman
1352ceb2ce9SGarrett Wollman	incl	%eax
1362ceb2ce9SGarrett Wollman	incl	%ecx
1372ceb2ce9SGarrett Wollman	decl	%edx
1382ceb2ce9SGarrett Wollman	jz	L4
1392ceb2ce9SGarrett Wollman	movb	(%eax),%bl
1402ceb2ce9SGarrett Wollman	testb	%bl,%bl
1412ceb2ce9SGarrett Wollman	jz	L3
1422ceb2ce9SGarrett Wollman	cmpb	%bl,(%ecx)
1432ceb2ce9SGarrett Wollman	jne	L3
1442ceb2ce9SGarrett Wollman
1452ceb2ce9SGarrett Wollman	incl	%eax
1462ceb2ce9SGarrett Wollman	incl	%ecx
1472ceb2ce9SGarrett Wollman	decl	%edx
1482ceb2ce9SGarrett Wollman	jz	L4
1492ceb2ce9SGarrett Wollman	movb	(%eax),%bl
1502ceb2ce9SGarrett Wollman	testb	%bl,%bl
1512ceb2ce9SGarrett Wollman	jz	L3
1522ceb2ce9SGarrett Wollman	cmpb	%bl,(%ecx)
1532ceb2ce9SGarrett Wollman	je	L1
1542ceb2ce9SGarrett Wollman
1552ceb2ce9SGarrett Wollman	.align 2,0x90
156be0264b9SBruce EvansL3:	movzbl	(%eax),%eax		/* unsigned comparison */
1572ceb2ce9SGarrett Wollman	movzbl	(%ecx),%ecx
1582ceb2ce9SGarrett Wollman	subl	%ecx,%eax
1592ceb2ce9SGarrett Wollman	popl	%ebx
1602ceb2ce9SGarrett Wollman	ret
1612ceb2ce9SGarrett Wollman	.align 2,0x90
1622ceb2ce9SGarrett WollmanL4:	xorl	%eax,%eax
1632ceb2ce9SGarrett Wollman	popl	%ebx
1642ceb2ce9SGarrett Wollman	ret
165ed820052SPeter WemmEND(strncmp)
166*93ab7586SKonstantin Belousov
167*93ab7586SKonstantin Belousov	.section .note.GNU-stack,"",%progbits
168