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 * See the License for the specific language governing permissions 2N/A * and limitations under the License. 2N/A * When distributing Covered Code, include this CDDL HEADER in each 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 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 2N/A * Use is subject to license terms. 2N/A * Compare strings: s1>s2: >0 s1==s2: 0 s1<s2: <0 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 * register const char *s1; 2N/A * register const char *s2; 2N/A * while(*s1 == *s2++) 2N/A * return(*s1 - s2[-1]); 2N/A andcc %o0, 3, %o3 ! s1 word-aligned ? 2N/A or %o5, %lo(0x01010101),%o5! finish loading Mycroft's
magic2 2N/A sub %o3, 4, %o3 ! number of bytes till aligned 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 andcc %o1, 3, %o3 ! s2 word aligned ? 2N/A cmp %o3, 2 ! s2 half-word aligned ? 2N/A cmp %o3, 3 ! s2 offset to dword == 3 ? 2N/A ldub [%o1], %o0 ! new lower word in s2 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 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 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 ! words are equal but the end of s1 has been reached 2N/A ! this means the strings must be equal 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 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 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 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 ! words are equal but the end of s1 has been reached 2N/A ! this means the strings must be equal 2N/A retl ! return from leaf function 2N/A mov %g0, %o0 ! return 0, i.e. strings are equal 2N/A sll %o0, 24, %g1 ! partial unaligned word from s2 2N/A nop ! pad for optimal alignment of .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 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 ! words are equal but the end of s1 has been reached 2N/A ! this means the strings must be equal 2N/A retl ! return from leaf function 2N/A mov %g0, %o0 ! return 0, i.e. strings are equal 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 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 ! words are equal but the end of s1 has been reached 2N/A ! this means the strings must be equal 2N/A retl ! return from leaf function 2N/A mov %g0, %o0 ! return 0, i.e. strings are equal 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 ! we know byte 1 is equal, so can compare bytes 1,2 as a group 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 ! we know bytes 1, 2 are equal, so can compare bytes 1,2,3 as a group 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 ! we know bytes 1,2,3 are equal, so can compare bytes 1,2,3,4 as group 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 retl ! return from leaf routine