2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A .file "strcmp.s"
2N/A
2N/A/* strcmp(s1, s2)
2N/A *
2N/A * Compare strings: s1>s2: >0 s1==s2: 0 s1<s2: <0
2N/A *
2N/A * Fast assembler language version of the following C-program for strcmp
2N/A * which represents the `standard' for the C-library.
2N/A *
2N/A * int
2N/A * strcmp(s1, s2)
2N/A * register const char *s1;
2N/A * register const char *s2;
2N/A * {
2N/A *
2N/A * if(s1 == s2)
2N/A * return(0);
2N/A * while(*s1 == *s2++)
2N/A * if(*s1++ == '\0')
2N/A * return(0);
2N/A * return(*s1 - s2[-1]);
2N/A * }
2N/A */
2N/A
2N/A#include <sys/asm_linkage.h>
2N/A
2N/A ! This strcmp implementation first determines whether s1 is aligned.
2N/A ! If it is not, it attempts to align it and then checks the
2N/A ! alignment of the destination string. If it is possible to
2N/A ! align s2, this also happens and then the compare begins. Otherwise,
2N/A ! a different compare for non-aligned strings is used.
2N/A
2N/A ENTRY(strcmp)
2N/A
2N/A .align 32
2N/A
2N/A subcc %o0, %o1, %o2 ! s1 == s2 ?
2N/A bz,pn %xcc, .stringsequal ! yup, same string, done
2N/A sethi %hi(0x01010101), %o5 ! magic2<31:13>
2N/A andcc %o0, 7, %o3 ! s1 sword-aligned ?
2N/A or %o5, %lo(0x01010101),%o5! magic2<31:0>
2N/A bz,pn %xcc, .s1aligned ! yup
2N/A sllx %o5, 32, %o4 ! magic2<63:32>
2N/A sub %o3, 8, %o3 ! number of bytes till s1 aligned
2N/A
2N/A.aligns1:
2N/A ldub [%o1 + %o2], %o0 ! s1[]
2N/A ldub [%o1], %g1 ! s2[]
2N/A subcc %o0, %g1, %o0 ! s1[] != s2[] ?
2N/A bne,pn %xcc, .done ! yup, done
2N/A addcc %o0, %g1, %g0 ! s1[] == 0 ?
2N/A bz,pn %xcc, .done ! yup, done
2N/A inccc %o3 ! s1 aligned yet?
2N/A bnz,pt %xcc, .aligns1 ! nope, compare another pair of bytes
2N/A inc %o1 ! s1++, s2++
2N/A
2N/A.s1aligned:
2N/A andcc %o1, 7, %o3 ! s2 dword aligned ?
2N/A or %o5, %o4, %o5 ! magic2<63:0>
2N/A bz,pn %xcc, .s2aligned ! yup
2N/A sllx %o5, 7, %o4 ! magic1
2N/A sllx %o3, 3, %g5 ! leftshift = 8*ofs
2N/A orn %g0, %g0, %g1 ! all ones
2N/A and %o1, -8, %o1 ! round s1 down to next aligned dword
2N/A srlx %g1, %g5, %g1 ! mask for fixing up bytes
2N/A ldx [%o1], %o0 ! new lower dword in s2
2N/A orn %o0, %g1, %o0 ! force start bytes to non-zero
2N/A sub %g0, %g5, %g4 ! rightshift = -(8*ofs) mod 64
2N/A sllx %o0, %g5, %g1 ! partial unaligned word from s2
2N/A add %o2, %o3, %o2 ! adjust pointers
2N/A nop ! align loop to 16-byte boundary
2N/A nop ! align loop to 16-byte boundary
2N/A
2N/A.cmp:
2N/A andn %o4, %o0, %o3 ! ~word & 0x80808080
2N/A sub %o0, %o5, %o0 ! word - 0x01010101
2N/A andcc %o0, %o3, %g0 ! (word - 0x01010101) & ~word & 0x80808080
2N/A bz,a,pt %xcc, .doload ! no null byte in previous word from s2
2N/A ldx [%o1+8], %o0 ! next aligned word in s2
2N/A.doload:
2N/A srlx %o0, %g4, %o3 ! bytes from aligned word from s2
2N/A or %g1, %o3, %g1 ! merge to get unaligned word from s2
2N/A ldx [%o1 + %o2], %o3 ! word from s1
2N/A cmp %o3, %g1 ! *s1 != *s2 ?
2N/A bne,pn %xcc, .wordsdiffer ! yup, find the byte that is different
2N/A add %o1, 8, %o1 ! s1+=8, s2+=8
2N/A andn %o4, %o3, %g1 ! ~word & 0x80808080
2N/A sub %o3, %o5, %o3 ! word - 0x01010101
2N/A andcc %o3, %g1, %g0 ! (word - 0x01010101) & ~word & 0x80808080
2N/A bz,pt %xcc, .cmp ! no null-byte in s1 yet
2N/A sllx %o0, %g5, %g1 ! partial unaligned word from s2
2N/A
2N/A ! words are equal but the end of s1 has been reached
2N/A ! this means the strings must be equal
2N/A.strequal:
2N/A retl ! return from leaf function
2N/A mov %g0, %o0 ! return 0, i.e. strings are equal
2N/A nop
2N/A
2N/A.s2aligned:
2N/A ldx [%o1 + %o2], %o3 ! load word from s1
2N/A
2N/A.cmpaligned:
2N/A ldx [%o1], %g1 ! load word from s2
2N/A cmp %o3, %g1 ! *scr1 == *src2 ?
2N/A bne,pn %xcc, .wordsdiffer ! nope, find mismatching character
2N/A add %o1, 8, %o1 ! src1 += 8, src2 += 8
2N/A andn %o4, %o3, %o0 ! ~word & 0x80808080
2N/A sub %o3, %o5, %o3 ! word - 0x01010101
2N/A andcc %o3, %o0, %g0 ! (word - 0x01010101) & ~word & 0x80808080
2N/A bz,a,pt %xcc, .cmpaligned ! no null-byte in s1 yet
2N/A ldx [%o1 + %o2], %o3 ! load word from s1
2N/A
2N/A ! words are equal but the end of s1 has been reached
2N/A ! this means the strings must be equal
2N/A
2N/A.stringsequal:
2N/A retl ! return from leaf function
2N/A mov %g0, %o0 ! return 0, i.e. strings are equal
2N/A nop ! align loop on 16-byte boundary
2N/A nop ! align loop on 16-byte boundary
2N/A nop ! align loop on 16-byte boundary
2N/A
2N/A.wordsdiffer:
2N/A mov 56, %o4 ! initial shift count
2N/A srlx %g1, %o4, %o2 ! first byte of mismatching word in s2
2N/A.cmpbytes:
2N/A srlx %o3, %o4, %o1 ! first byte of mismatching word in s1
2N/A subcc %o1, %o2, %o0 ! *s1-*s2
2N/A bnz,pn %xcc, .done ! bytes differ, return difference
2N/A nop
2N/A andcc %o1, 0xff, %o0 ! *s1 == 0 ?
2N/A bz,pn %xcc, .done ! yup, strings match
2N/A subcc %o4, 8, %o4 ! shift_count -= 8
2N/A bpos,pt %xcc, .cmpbytes ! until all bytes processed
2N/A srlx %g1, %o4, %o2 ! first byte of mismatching word in s2
2N/A
2N/A.done:
2N/A retl ! return from leaf routine
2N/A nop ! padding
2N/A
2N/A SET_SIZE(strcmp)