/*1* Copyright (c) 1993,94 Winning Strategies, Inc.2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12* 3. All advertising materials mentioning features or use of this software13* must display the following acknowledgement:14* This product includes software developed by Winning Strategies, Inc.15* 4. The name of the author may not be used to endorse or promote products16* derived from this software without specific prior written permission17*18* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR19* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES20* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.21* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,22* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT23* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,24* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY25* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT26* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF27* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.28*/2930#include <machine/asm.h>31/*32* strncmp(s1, s2, n)33* return an integer greater than, equal to, or less than 0,34* according as the first n characters of string s1 is greater35* than, equal to, or less than the string s2.36*37* %eax - pointer to s138* %ecx - pointer to s239* %edx - length40*41* Written by:42* J.T. Conklin ([email protected]), Winning Strategies, Inc.43*/4445/*46* I've unrolled the loop eight times: large enough to make a47* significant difference, and small enough not to totally trash the48* cache.49*50* TODO: change all the jz's back to je for consistency.51*/5253ENTRY(strncmp)54pushl %ebx55movl 8(%esp),%eax56movl 12(%esp),%ecx57movl 16(%esp),%edx58testl %edx,%edx59jmp L2 /* Jump into the loop! */6061.align 2,0x9062L1: incl %eax63incl %ecx64decl %edx65L2: jz L4 /* strings are equal */66movb (%eax),%bl67testb %bl,%bl68jz L369cmpb %bl,(%ecx)70jne L37172/*73* XXX it might be best to move the next 4 instructions to the end of the74* unrolled part of the loop. The unrolled part would then be75* movb n(%eax),%bl; testb %bl, %bl; je L3; cmpb n(%ecx); jne L376* or maybe better77* movb n(%eax),%bl; cmpb n(%ecx); jne L3; testb %bl,%bl; je return_078* for n = 0, 1, ..., 8. The end of the loop would be79* L1: addl $8,%eax; addl $8,%ecx; subl $8,%edx; cmpl $8,%edx; jae Lx80* where residual counts of 0 to 7 are handled at Lx. However, this would81* be slower for short strings. Cache effects are probably not so82* important because we are only handling a byte at a time.83*/84incl %eax85incl %ecx86decl %edx87jz L488movb (%eax),%bl89testb %bl,%bl90jz L391cmpb %bl,(%ecx)92jne L39394incl %eax95incl %ecx96decl %edx97jz L498movb (%eax),%bl99testb %bl,%bl100jz L3101cmpb %bl,(%ecx)102jne L3103104incl %eax105incl %ecx106decl %edx107jz L4108movb (%eax),%bl109testb %bl,%bl110jz L3111cmpb %bl,(%ecx)112jne L3113114incl %eax115incl %ecx116decl %edx117jz L4118movb (%eax),%bl119testb %bl,%bl120jz L3121cmpb %bl,(%ecx)122jne L3123124incl %eax125incl %ecx126decl %edx127jz L4128movb (%eax),%bl129testb %bl,%bl130jz L3131cmpb %bl,(%ecx)132jne L3133134incl %eax135incl %ecx136decl %edx137jz L4138movb (%eax),%bl139testb %bl,%bl140jz L3141cmpb %bl,(%ecx)142jne L3143144incl %eax145incl %ecx146decl %edx147jz L4148movb (%eax),%bl149testb %bl,%bl150jz L3151cmpb %bl,(%ecx)152je L1153154.align 2,0x90155L3: movzbl (%eax),%eax /* unsigned comparison */156movzbl (%ecx),%ecx157subl %ecx,%eax158popl %ebx159ret160.align 2,0x90161L4: xorl %eax,%eax162popl %ebx163ret164END(strncmp)165166.section .note.GNU-stack,"",%progbits167168169