10Sstevel@tonic-gate/* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 56812Sraf * Common Development and Distribution License (the "License"). 66812Sraf * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 216812Sraf 220Sstevel@tonic-gate/* 236812Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 27*7298SMark.J.Nelson@Sun.COM .file "strcmp.s" 280Sstevel@tonic-gate 290Sstevel@tonic-gate/* strcmp(s1, s2) 300Sstevel@tonic-gate * 310Sstevel@tonic-gate * Compare strings: s1>s2: >0 s1==s2: 0 s1<s2: <0 320Sstevel@tonic-gate * 330Sstevel@tonic-gate * Fast assembler language version of the following C-program for strcmp 340Sstevel@tonic-gate * which represents the `standard' for the C-library. 350Sstevel@tonic-gate * 360Sstevel@tonic-gate * int 370Sstevel@tonic-gate * strcmp(s1, s2) 380Sstevel@tonic-gate * register const char *s1; 390Sstevel@tonic-gate * register const char *s2; 400Sstevel@tonic-gate * { 410Sstevel@tonic-gate * 420Sstevel@tonic-gate * if(s1 == s2) 430Sstevel@tonic-gate * return(0); 440Sstevel@tonic-gate * while(*s1 == *s2++) 450Sstevel@tonic-gate * if(*s1++ == '\0') 460Sstevel@tonic-gate * return(0); 470Sstevel@tonic-gate * return(*s1 - s2[-1]); 480Sstevel@tonic-gate * } 490Sstevel@tonic-gate */ 500Sstevel@tonic-gate 510Sstevel@tonic-gate#include <sys/asm_linkage.h> 520Sstevel@tonic-gate 530Sstevel@tonic-gate ! This strcmp implementation first determines whether s1 is aligned. 540Sstevel@tonic-gate ! If it is not, it attempts to align it and then checks the 550Sstevel@tonic-gate ! alignment of the destination string. If it is possible to 560Sstevel@tonic-gate ! align s2, this also happens and then the compare begins. Otherwise, 570Sstevel@tonic-gate ! a different compare for non-aligned strings is used. 580Sstevel@tonic-gate ! In this case, we have multiple routines depending upon the 590Sstevel@tonic-gate ! degree to which a string is mis-aligned. 600Sstevel@tonic-gate 610Sstevel@tonic-gate ENTRY(strcmp) 620Sstevel@tonic-gate 630Sstevel@tonic-gate .align 32 640Sstevel@tonic-gate 650Sstevel@tonic-gate subcc %o0, %o1, %o2 ! s1 == s2 ? 660Sstevel@tonic-gate bz .stringsequal1 ! yup, same string, done 670Sstevel@tonic-gate sethi %hi(0x01010101), %o5 ! start loading Mycroft's magic2 680Sstevel@tonic-gate andcc %o0, 3, %o3 ! s1 word-aligned ? 690Sstevel@tonic-gate or %o5, %lo(0x01010101),%o5! finish loading Mycroft's magic2 700Sstevel@tonic-gate bz .s1aligned ! yup 710Sstevel@tonic-gate sll %o5, 7, %o4 ! load Mycroft's magic1 720Sstevel@tonic-gate sub %o3, 4, %o3 ! number of bytes till aligned 730Sstevel@tonic-gate 740Sstevel@tonic-gate.aligns1: 750Sstevel@tonic-gate ldub [%o1 + %o2], %o0 ! s1[] 760Sstevel@tonic-gate ldub [%o1], %g1 ! s2[] 770Sstevel@tonic-gate subcc %o0, %g1, %o0 ! s1[] != s2[] ? 780Sstevel@tonic-gate bne .done ! yup, done 790Sstevel@tonic-gate addcc %o0, %g1, %g0 ! s1[] == 0 ? 800Sstevel@tonic-gate bz .done ! yup, done 810Sstevel@tonic-gate inccc %o3 ! s1 aligned yet? 820Sstevel@tonic-gate bnz .aligns1 ! nope, compare another pair of bytes 830Sstevel@tonic-gate inc %o1 ! s1++, s2++ 840Sstevel@tonic-gate 850Sstevel@tonic-gate.s1aligned: 860Sstevel@tonic-gate andcc %o1, 3, %o3 ! s2 word aligned ? 870Sstevel@tonic-gate bz .word4 ! yup 880Sstevel@tonic-gate cmp %o3, 2 ! s2 half-word aligned ? 890Sstevel@tonic-gate be .word2 ! yup 900Sstevel@tonic-gate cmp %o3, 3 ! s2 offset to dword == 3 ? 910Sstevel@tonic-gate be,a .word3 ! yup 920Sstevel@tonic-gate ldub [%o1], %o0 ! new lower word in s2 930Sstevel@tonic-gate 940Sstevel@tonic-gate.word1: 950Sstevel@tonic-gate lduw [%o1 - 1], %o0 ! new lower word in s2 960Sstevel@tonic-gate sethi %hi(0xff000000), %o3 ! mask for forcing byte 1 non-zero 970Sstevel@tonic-gate sll %o0, 8, %g1 ! partial unaligned word from s2 980Sstevel@tonic-gate or %o0, %o3, %o0 ! force byte 1 non-zero 990Sstevel@tonic-gate 1000Sstevel@tonic-gate.cmp1: 1010Sstevel@tonic-gate andn %o4, %o0, %o3 ! ~word & 0x80808080 1020Sstevel@tonic-gate sub %o0, %o5, %o0 ! word - 0x01010101 1030Sstevel@tonic-gate andcc %o0, %o3, %g0 ! (word - 0x01010101) & ~word & 0x80808080 1040Sstevel@tonic-gate bz,a .doload1 ! no null byte in previous word from s2 1050Sstevel@tonic-gate lduw [%o1 + 3], %o0 ! load next aligned word from s2 1060Sstevel@tonic-gate.doload1: 1070Sstevel@tonic-gate srl %o0, 24, %o3 ! byte 1 of new aligned word from s2 1080Sstevel@tonic-gate or %g1, %o3, %g1 ! merge to get unaligned word from s2 1090Sstevel@tonic-gate lduw [%o1 + %o2], %o3 ! word from s1 1100Sstevel@tonic-gate cmp %o3, %g1 ! *s1 != *s2 ? 1110Sstevel@tonic-gate bne .wordsdiffer ! yup, find the byte that is different 1120Sstevel@tonic-gate add %o1, 4, %o1 ! s1+=4, s2+=4 1130Sstevel@tonic-gate andn %o4, %o3, %g1 ! ~word & 0x80808080 1140Sstevel@tonic-gate sub %o3, %o5, %o3 ! word - 0x01010101 1150Sstevel@tonic-gate andcc %o3, %g1, %g0 ! (word - 0x01010101) & ~word & 0x80808080 1160Sstevel@tonic-gate bz .cmp1 ! no null-byte in s1 yet 1170Sstevel@tonic-gate sll %o0, 8, %g1 ! partial unaligned word from s2 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate ! words are equal but the end of s1 has been reached 1200Sstevel@tonic-gate ! this means the strings must be equal 1210Sstevel@tonic-gate.stringsequal1: 1220Sstevel@tonic-gate retl ! return from leaf function 1230Sstevel@tonic-gate mov %g0, %o0 ! return 0, i.e. strings are equal 1240Sstevel@tonic-gate nop ! pad for optimal alignment of .cmp2 1250Sstevel@tonic-gate nop ! pad for optimal alignment of .cmp2 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate.word2: 1280Sstevel@tonic-gate lduh [%o1], %o0 ! new lower word in s2 1290Sstevel@tonic-gate sethi %hi(0xffff0000), %o3 ! mask for forcing bytes 1,2 non-zero 1300Sstevel@tonic-gate sll %o0, 16, %g1 ! partial unaligned word from s2 1310Sstevel@tonic-gate or %o0, %o3, %o0 ! force bytes 1,2 non-zero 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate.cmp2: 1340Sstevel@tonic-gate andn %o4, %o0, %o3 ! ~word & 0x80808080 1350Sstevel@tonic-gate sub %o0, %o5, %o0 ! word - 0x01010101 1360Sstevel@tonic-gate andcc %o0, %o3, %g0 ! (word - 0x01010101) & ~word & 0x80808080 1370Sstevel@tonic-gate bz,a .doload2 ! no null byte in previous word from s2 1380Sstevel@tonic-gate lduw [%o1 + 2], %o0 ! load next aligned word from s2 1390Sstevel@tonic-gate.doload2: 1400Sstevel@tonic-gate srl %o0, 16, %o3 ! bytes 1,2 of new aligned word from s2 1410Sstevel@tonic-gate or %g1, %o3, %g1 ! merge to get unaligned word from s2 1420Sstevel@tonic-gate lduw [%o1 + %o2], %o3 ! word from s1 1430Sstevel@tonic-gate cmp %o3, %g1 ! *s1 != *s2 ? 1440Sstevel@tonic-gate bne .wordsdiffer ! yup, find the byte that is different 1450Sstevel@tonic-gate add %o1, 4, %o1 ! s1+=4, s2+=4 1460Sstevel@tonic-gate andn %o4, %o3, %g1 ! ~word & 0x80808080 1470Sstevel@tonic-gate sub %o3, %o5, %o3 ! word - 0x01010101 1480Sstevel@tonic-gate andcc %o3, %g1, %g0 ! (word - 0x01010101) & ~word & 0x80808080 1490Sstevel@tonic-gate bz .cmp2 ! no null-byte in s1 yet 1500Sstevel@tonic-gate sll %o0, 16, %g1 ! partial unaligned word from s2 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate ! words are equal but the end of s1 has been reached 1530Sstevel@tonic-gate ! this means the strings must be equal 1540Sstevel@tonic-gate.stringsequal2: 1550Sstevel@tonic-gate retl ! return from leaf function 1560Sstevel@tonic-gate mov %g0, %o0 ! return 0, i.e. strings are equal 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate.word3: 1590Sstevel@tonic-gate sll %o0, 24, %g1 ! partial unaligned word from s2 1600Sstevel@tonic-gate nop ! pad for optimal alignment of .cmp3 1610Sstevel@tonic-gate.cmp3: 1620Sstevel@tonic-gate andcc %o0, 0xff, %g0 ! did previous word contain null-byte ? 1630Sstevel@tonic-gate bnz,a .doload3 ! nope, load next word from s2 1640Sstevel@tonic-gate lduw [%o1 + 1], %o0 ! load next aligned word from s2 1650Sstevel@tonic-gate.doload3: 1660Sstevel@tonic-gate srl %o0, 8, %o3 ! bytes 1,2,3 from new aligned s2 word 1670Sstevel@tonic-gate or %g1, %o3, %g1 ! merge to get unaligned word from s2 1680Sstevel@tonic-gate lduw [%o1 + %o2], %o3 ! word from s1 1690Sstevel@tonic-gate cmp %o3, %g1 ! *s1 != *s2 ? 1700Sstevel@tonic-gate bne .wordsdiffer ! yup, find the byte that is different 1710Sstevel@tonic-gate add %o1, 4, %o1 ! s1+=4, s2+=4 1720Sstevel@tonic-gate andn %o4, %o3, %g1 ! ~word & 0x80808080 1730Sstevel@tonic-gate sub %o3, %o5, %o3 ! word - 0x01010101 1740Sstevel@tonic-gate andcc %o3, %g1, %g0 ! (word - 0x01010101) & ~word & 0x80808080 1750Sstevel@tonic-gate bz .cmp3 ! no null-byte in s1 yet 1760Sstevel@tonic-gate sll %o0, 24, %g1 ! partial unaligned word from s2 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate ! words are equal but the end of s1 has been reached 1790Sstevel@tonic-gate ! this means the strings must be equal 1800Sstevel@tonic-gate.stringsequal3: 1810Sstevel@tonic-gate retl ! return from leaf function 1820Sstevel@tonic-gate mov %g0, %o0 ! return 0, i.e. strings are equal 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate.word4: 1850Sstevel@tonic-gate lduw [%o1 + %o2], %o3 ! load word from s1 1860Sstevel@tonic-gate nop ! pad for optimal alignment of .cmp4 1870Sstevel@tonic-gate nop ! pad for optimal alignment of .cmp4 1880Sstevel@tonic-gate nop ! pad for optimal alignment of .cmp4 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate.cmp4: 1910Sstevel@tonic-gate lduw [%o1], %g1 ! load word from s2 1920Sstevel@tonic-gate cmp %o3, %g1 ! *scr1 == *src2 ? 1930Sstevel@tonic-gate bne .wordsdiffer ! nope, find mismatching character 1940Sstevel@tonic-gate add %o1, 4, %o1 ! src1 += 4, src2 += 4 1950Sstevel@tonic-gate andn %o4, %o3, %o0 ! ~word & 0x80808080 1960Sstevel@tonic-gate sub %o3, %o5, %o3 ! word - 0x01010101 1970Sstevel@tonic-gate andcc %o3, %o0, %g0 ! (word - 0x01010101) & ~word & 0x80808080 1980Sstevel@tonic-gate bz,a .cmp4 ! no null-byte in s1 yet 1990Sstevel@tonic-gate lduw [%o1 + %o2], %o3 ! load word from s1 2000Sstevel@tonic-gate 2010Sstevel@tonic-gate ! words are equal but the end of s1 has been reached 2020Sstevel@tonic-gate ! this means the strings must be equal 2030Sstevel@tonic-gate.stringsequal4: 2040Sstevel@tonic-gate retl ! return from leaf function 2050Sstevel@tonic-gate mov %g0, %o0 ! return 0, i.e. strings are equal 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate.wordsdiffer: 2080Sstevel@tonic-gate srl %g1, 24, %o2 ! first byte of mismatching word in s2 2090Sstevel@tonic-gate srl %o3, 24, %o1 ! first byte of mismatching word in s1 2100Sstevel@tonic-gate subcc %o1, %o2, %o0 ! *s1-*s2 2110Sstevel@tonic-gate bnz .done ! bytes differ, return difference 2120Sstevel@tonic-gate srl %g1, 16, %o2 ! second byte of mismatching word in s2 2130Sstevel@tonic-gate andcc %o1, 0xff, %o0 ! *s1 == 0 ? 2140Sstevel@tonic-gate bz .done ! yup 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate ! we know byte 1 is equal, so can compare bytes 1,2 as a group 2170Sstevel@tonic-gate 2180Sstevel@tonic-gate srl %o3, 16, %o1 ! second byte of mismatching word in s1 2190Sstevel@tonic-gate subcc %o1, %o2, %o0 ! *s1-*s2 2200Sstevel@tonic-gate bnz .done ! bytes differ, return difference 2210Sstevel@tonic-gate srl %g1, 8, %o2 ! third byte of mismatching word in s2 2220Sstevel@tonic-gate andcc %o1, 0xff, %o0 ! *s1 == 0 ? 2230Sstevel@tonic-gate bz .done ! yup 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate ! we know bytes 1, 2 are equal, so can compare bytes 1,2,3 as a group 2260Sstevel@tonic-gate 2270Sstevel@tonic-gate srl %o3, 8, %o1 ! third byte of mismatching word in s1 2280Sstevel@tonic-gate subcc %o1, %o2, %o0 ! *s1-*s2 2290Sstevel@tonic-gate bnz .done ! bytes differ, return difference 2300Sstevel@tonic-gate andcc %o1, 0xff, %g0 ! *s1 == 0 ? 2310Sstevel@tonic-gate bz .stringsequal1 ! yup 2320Sstevel@tonic-gate 2330Sstevel@tonic-gate ! we know bytes 1,2,3 are equal, so can compare bytes 1,2,3,4 as group 2340Sstevel@tonic-gate 2350Sstevel@tonic-gate subcc %o3, %g1, %o0 ! *s1-*s2 2360Sstevel@tonic-gate bz,a .done ! bytes differ, return difference 2370Sstevel@tonic-gate andcc %o3, 0xff, %o0 ! *s1 == 0 ? 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate.done: 2400Sstevel@tonic-gate retl ! return from leaf routine 2410Sstevel@tonic-gate nop ! padding 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate 2440Sstevel@tonic-gate SET_SIZE(strcmp) 245