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 ! In this case, we have multiple routines depending upon the
2N/A ! degree to which a string is mis-aligned.
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 .stringsequal1 ! yup, same string, done
2N/A sethi %hi(0x01010101), %o5 ! start loading Mycroft's magic2
2N/A andcc %o0, 3, %o3 ! s1 word-aligned ?
2N/A or %o5, %lo(0x01010101),%o5! finish loading Mycroft's magic2
2N/A bz .s1aligned ! yup
2N/A sll %o5, 7, %o4 ! load Mycroft's magic1
2N/A sub %o3, 4, %o3 ! number of bytes till 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 .done ! yup, done
2N/A addcc %o0, %g1, %g0 ! s1[] == 0 ?
2N/A bz .done ! yup, done
2N/A inccc %o3 ! s1 aligned yet?
2N/A bnz .aligns1 ! nope, compare another pair of bytes
2N/A inc %o1 ! s1++, s2++
2N/A
2N/A.s1aligned:
2N/A andcc %o1, 3, %o3 ! s2 word aligned ?
2N/A bz .word4 ! yup
2N/A cmp %o3, 2 ! s2 half-word aligned ?
2N/A be .word2 ! yup
2N/A cmp %o3, 3 ! s2 offset to dword == 3 ?
2N/A be,a .word3 ! yup
2N/A ldub [%o1], %o0 ! new lower word in s2
2N/A
2N/A.word1:
2N/A lduw [%o1 - 1], %o0 ! new lower word in s2
2N/A sethi %hi(0xff000000), %o3 ! mask for forcing byte 1 non-zero
2N/A sll %o0, 8, %g1 ! partial unaligned word from s2
2N/A or %o0, %o3, %o0 ! force byte 1 non-zero
2N/A
2N/A.cmp1:
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 .doload1 ! no null byte in previous word from s2
2N/A lduw [%o1 + 3], %o0 ! load next aligned word from s2
2N/A.doload1:
2N/A srl %o0, 24, %o3 ! byte 1 of new aligned word from s2
2N/A or %g1, %o3, %g1 ! merge to get unaligned word from s2
2N/A lduw [%o1 + %o2], %o3 ! word from s1
2N/A cmp %o3, %g1 ! *s1 != *s2 ?
2N/A bne .wordsdiffer ! yup, find the byte that is different
2N/A add %o1, 4, %o1 ! s1+=4, s2+=4
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 .cmp1 ! no null-byte in s1 yet
2N/A sll %o0, 8, %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.stringsequal1:
2N/A retl ! return from leaf function
2N/A mov %g0, %o0 ! return 0, i.e. strings are equal
2N/A nop ! pad for optimal alignment of .cmp2
2N/A nop ! pad for optimal alignment of .cmp2
2N/A
2N/A.word2:
2N/A lduh [%o1], %o0 ! new lower word in s2
2N/A sethi %hi(0xffff0000), %o3 ! mask for forcing bytes 1,2 non-zero
2N/A sll %o0, 16, %g1 ! partial unaligned word from s2
2N/A or %o0, %o3, %o0 ! force bytes 1,2 non-zero
2N/A
2N/A.cmp2:
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 .doload2 ! no null byte in previous word from s2
2N/A lduw [%o1 + 2], %o0 ! load next aligned word from s2
2N/A.doload2:
2N/A srl %o0, 16, %o3 ! bytes 1,2 of new aligned word from s2
2N/A or %g1, %o3, %g1 ! merge to get unaligned word from s2
2N/A lduw [%o1 + %o2], %o3 ! word from s1
2N/A cmp %o3, %g1 ! *s1 != *s2 ?
2N/A bne .wordsdiffer ! yup, find the byte that is different
2N/A add %o1, 4, %o1 ! s1+=4, s2+=4
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 .cmp2 ! no null-byte in s1 yet
2N/A sll %o0, 16, %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.stringsequal2:
2N/A retl ! return from leaf function
2N/A mov %g0, %o0 ! return 0, i.e. strings are equal
2N/A
2N/A.word3:
2N/A sll %o0, 24, %g1 ! partial unaligned word from s2
2N/A nop ! pad for optimal alignment of .cmp3
2N/A.cmp3:
2N/A andcc %o0, 0xff, %g0 ! did previous word contain null-byte ?
2N/A bnz,a .doload3 ! nope, load next word from s2
2N/A lduw [%o1 + 1], %o0 ! load next aligned word from s2
2N/A.doload3:
2N/A srl %o0, 8, %o3 ! bytes 1,2,3 from new aligned s2 word
2N/A or %g1, %o3, %g1 ! merge to get unaligned word from s2
2N/A lduw [%o1 + %o2], %o3 ! word from s1
2N/A cmp %o3, %g1 ! *s1 != *s2 ?
2N/A bne .wordsdiffer ! yup, find the byte that is different
2N/A add %o1, 4, %o1 ! s1+=4, s2+=4
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 .cmp3 ! no null-byte in s1 yet
2N/A sll %o0, 24, %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.stringsequal3:
2N/A retl ! return from leaf function
2N/A mov %g0, %o0 ! return 0, i.e. strings are equal
2N/A
2N/A.word4:
2N/A lduw [%o1 + %o2], %o3 ! load word from s1
2N/A nop ! pad for optimal alignment of .cmp4
2N/A nop ! pad for optimal alignment of .cmp4
2N/A nop ! pad for optimal alignment of .cmp4
2N/A
2N/A.cmp4:
2N/A lduw [%o1], %g1 ! load word from s2
2N/A cmp %o3, %g1 ! *scr1 == *src2 ?
2N/A bne .wordsdiffer ! nope, find mismatching character
2N/A add %o1, 4, %o1 ! src1 += 4, src2 += 4
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 .cmp4 ! no null-byte in s1 yet
2N/A lduw [%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.stringsequal4:
2N/A retl ! return from leaf function
2N/A mov %g0, %o0 ! return 0, i.e. strings are equal
2N/A
2N/A.wordsdiffer:
2N/A srl %g1, 24, %o2 ! first byte of mismatching word in s2
2N/A srl %o3, 24, %o1 ! first byte of mismatching word in s1
2N/A subcc %o1, %o2, %o0 ! *s1-*s2
2N/A bnz .done ! bytes differ, return difference
2N/A srl %g1, 16, %o2 ! second byte of mismatching word in s2
2N/A andcc %o1, 0xff, %o0 ! *s1 == 0 ?
2N/A bz .done ! yup
2N/A
2N/A ! we know byte 1 is equal, so can compare bytes 1,2 as a group
2N/A
2N/A srl %o3, 16, %o1 ! second byte of mismatching word in s1
2N/A subcc %o1, %o2, %o0 ! *s1-*s2
2N/A bnz .done ! bytes differ, return difference
2N/A srl %g1, 8, %o2 ! third byte of mismatching word in s2
2N/A andcc %o1, 0xff, %o0 ! *s1 == 0 ?
2N/A bz .done ! yup
2N/A
2N/A ! we know bytes 1, 2 are equal, so can compare bytes 1,2,3 as a group
2N/A
2N/A srl %o3, 8, %o1 ! third byte of mismatching word in s1
2N/A subcc %o1, %o2, %o0 ! *s1-*s2
2N/A bnz .done ! bytes differ, return difference
2N/A andcc %o1, 0xff, %g0 ! *s1 == 0 ?
2N/A bz .stringsequal1 ! yup
2N/A
2N/A ! we know bytes 1,2,3 are equal, so can compare bytes 1,2,3,4 as group
2N/A
2N/A subcc %o3, %g1, %o0 ! *s1-*s2
2N/A bz,a .done ! bytes differ, return difference
2N/A andcc %o3, 0xff, %o0 ! *s1 == 0 ?
2N/A
2N/A.done:
2N/A retl ! return from leaf routine
2N/A nop ! padding
2N/A
2N/A
2N/A SET_SIZE(strcmp)