1*5d9d9091SRichard Lowe/* 2*5d9d9091SRichard Lowe * CDDL HEADER START 3*5d9d9091SRichard Lowe * 4*5d9d9091SRichard Lowe * The contents of this file are subject to the terms of the 5*5d9d9091SRichard Lowe * Common Development and Distribution License (the "License"). 6*5d9d9091SRichard Lowe * You may not use this file except in compliance with the License. 7*5d9d9091SRichard Lowe * 8*5d9d9091SRichard Lowe * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*5d9d9091SRichard Lowe * or http://www.opensolaris.org/os/licensing. 10*5d9d9091SRichard Lowe * See the License for the specific language governing permissions 11*5d9d9091SRichard Lowe * and limitations under the License. 12*5d9d9091SRichard Lowe * 13*5d9d9091SRichard Lowe * When distributing Covered Code, include this CDDL HEADER in each 14*5d9d9091SRichard Lowe * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*5d9d9091SRichard Lowe * If applicable, add the following below this CDDL HEADER, with the 16*5d9d9091SRichard Lowe * fields enclosed by brackets "[]" replaced with your own identifying 17*5d9d9091SRichard Lowe * information: Portions Copyright [yyyy] [name of copyright owner] 18*5d9d9091SRichard Lowe * 19*5d9d9091SRichard Lowe * CDDL HEADER END 20*5d9d9091SRichard Lowe */ 21*5d9d9091SRichard Lowe 22*5d9d9091SRichard Lowe/* 23*5d9d9091SRichard Lowe * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24*5d9d9091SRichard Lowe * Use is subject to license terms. 25*5d9d9091SRichard Lowe */ 26*5d9d9091SRichard Lowe 27*5d9d9091SRichard Lowe .file "strcmp.s" 28*5d9d9091SRichard Lowe 29*5d9d9091SRichard Lowe/* strcmp(s1, s2) 30*5d9d9091SRichard Lowe * 31*5d9d9091SRichard Lowe * Compare strings: s1>s2: >0 s1==s2: 0 s1<s2: <0 32*5d9d9091SRichard Lowe * 33*5d9d9091SRichard Lowe * Fast assembler language version of the following C-program for strcmp 34*5d9d9091SRichard Lowe * which represents the `standard' for the C-library. 35*5d9d9091SRichard Lowe * 36*5d9d9091SRichard Lowe * int 37*5d9d9091SRichard Lowe * strcmp(s1, s2) 38*5d9d9091SRichard Lowe * register const char *s1; 39*5d9d9091SRichard Lowe * register const char *s2; 40*5d9d9091SRichard Lowe * { 41*5d9d9091SRichard Lowe * 42*5d9d9091SRichard Lowe * if(s1 == s2) 43*5d9d9091SRichard Lowe * return(0); 44*5d9d9091SRichard Lowe * while(*s1 == *s2++) 45*5d9d9091SRichard Lowe * if(*s1++ == '\0') 46*5d9d9091SRichard Lowe * return(0); 47*5d9d9091SRichard Lowe * return(*s1 - s2[-1]); 48*5d9d9091SRichard Lowe * } 49*5d9d9091SRichard Lowe */ 50*5d9d9091SRichard Lowe 51*5d9d9091SRichard Lowe#include <sys/asm_linkage.h> 52*5d9d9091SRichard Lowe 53*5d9d9091SRichard Lowe ! This strcmp implementation first determines whether s1 is aligned. 54*5d9d9091SRichard Lowe ! If it is not, it attempts to align it and then checks the 55*5d9d9091SRichard Lowe ! alignment of the destination string. If it is possible to 56*5d9d9091SRichard Lowe ! align s2, this also happens and then the compare begins. Otherwise, 57*5d9d9091SRichard Lowe ! a different compare for non-aligned strings is used. 58*5d9d9091SRichard Lowe 59*5d9d9091SRichard Lowe ENTRY(strcmp) 60*5d9d9091SRichard Lowe 61*5d9d9091SRichard Lowe .align 32 62*5d9d9091SRichard Lowe 63*5d9d9091SRichard Lowe subcc %o0, %o1, %o2 ! s1 == s2 ? 64*5d9d9091SRichard Lowe bz,pn %xcc, .stringsequal ! yup, same string, done 65*5d9d9091SRichard Lowe sethi %hi(0x01010101), %o5 ! magic2<31:13> 66*5d9d9091SRichard Lowe andcc %o0, 7, %o3 ! s1 sword-aligned ? 67*5d9d9091SRichard Lowe or %o5, %lo(0x01010101),%o5! magic2<31:0> 68*5d9d9091SRichard Lowe bz,pn %xcc, .s1aligned ! yup 69*5d9d9091SRichard Lowe sllx %o5, 32, %o4 ! magic2<63:32> 70*5d9d9091SRichard Lowe sub %o3, 8, %o3 ! number of bytes till s1 aligned 71*5d9d9091SRichard Lowe 72*5d9d9091SRichard Lowe.aligns1: 73*5d9d9091SRichard Lowe ldub [%o1 + %o2], %o0 ! s1[] 74*5d9d9091SRichard Lowe ldub [%o1], %g1 ! s2[] 75*5d9d9091SRichard Lowe subcc %o0, %g1, %o0 ! s1[] != s2[] ? 76*5d9d9091SRichard Lowe bne,pn %xcc, .done ! yup, done 77*5d9d9091SRichard Lowe addcc %o0, %g1, %g0 ! s1[] == 0 ? 78*5d9d9091SRichard Lowe bz,pn %xcc, .done ! yup, done 79*5d9d9091SRichard Lowe inccc %o3 ! s1 aligned yet? 80*5d9d9091SRichard Lowe bnz,pt %xcc, .aligns1 ! nope, compare another pair of bytes 81*5d9d9091SRichard Lowe inc %o1 ! s1++, s2++ 82*5d9d9091SRichard Lowe 83*5d9d9091SRichard Lowe.s1aligned: 84*5d9d9091SRichard Lowe andcc %o1, 7, %o3 ! s2 dword aligned ? 85*5d9d9091SRichard Lowe or %o5, %o4, %o5 ! magic2<63:0> 86*5d9d9091SRichard Lowe bz,pn %xcc, .s2aligned ! yup 87*5d9d9091SRichard Lowe sllx %o5, 7, %o4 ! magic1 88*5d9d9091SRichard Lowe sllx %o3, 3, %g5 ! leftshift = 8*ofs 89*5d9d9091SRichard Lowe orn %g0, %g0, %g1 ! all ones 90*5d9d9091SRichard Lowe and %o1, -8, %o1 ! round s1 down to next aligned dword 91*5d9d9091SRichard Lowe srlx %g1, %g5, %g1 ! mask for fixing up bytes 92*5d9d9091SRichard Lowe ldx [%o1], %o0 ! new lower dword in s2 93*5d9d9091SRichard Lowe orn %o0, %g1, %o0 ! force start bytes to non-zero 94*5d9d9091SRichard Lowe sub %g0, %g5, %g4 ! rightshift = -(8*ofs) mod 64 95*5d9d9091SRichard Lowe sllx %o0, %g5, %g1 ! partial unaligned word from s2 96*5d9d9091SRichard Lowe add %o2, %o3, %o2 ! adjust pointers 97*5d9d9091SRichard Lowe nop ! align loop to 16-byte boundary 98*5d9d9091SRichard Lowe nop ! align loop to 16-byte boundary 99*5d9d9091SRichard Lowe 100*5d9d9091SRichard Lowe.cmp: 101*5d9d9091SRichard Lowe andn %o4, %o0, %o3 ! ~word & 0x80808080 102*5d9d9091SRichard Lowe sub %o0, %o5, %o0 ! word - 0x01010101 103*5d9d9091SRichard Lowe andcc %o0, %o3, %g0 ! (word - 0x01010101) & ~word & 0x80808080 104*5d9d9091SRichard Lowe bz,a,pt %xcc, .doload ! no null byte in previous word from s2 105*5d9d9091SRichard Lowe ldx [%o1+8], %o0 ! next aligned word in s2 106*5d9d9091SRichard Lowe.doload: 107*5d9d9091SRichard Lowe srlx %o0, %g4, %o3 ! bytes from aligned word from s2 108*5d9d9091SRichard Lowe or %g1, %o3, %g1 ! merge to get unaligned word from s2 109*5d9d9091SRichard Lowe ldx [%o1 + %o2], %o3 ! word from s1 110*5d9d9091SRichard Lowe cmp %o3, %g1 ! *s1 != *s2 ? 111*5d9d9091SRichard Lowe bne,pn %xcc, .wordsdiffer ! yup, find the byte that is different 112*5d9d9091SRichard Lowe add %o1, 8, %o1 ! s1+=8, s2+=8 113*5d9d9091SRichard Lowe andn %o4, %o3, %g1 ! ~word & 0x80808080 114*5d9d9091SRichard Lowe sub %o3, %o5, %o3 ! word - 0x01010101 115*5d9d9091SRichard Lowe andcc %o3, %g1, %g0 ! (word - 0x01010101) & ~word & 0x80808080 116*5d9d9091SRichard Lowe bz,pt %xcc, .cmp ! no null-byte in s1 yet 117*5d9d9091SRichard Lowe sllx %o0, %g5, %g1 ! partial unaligned word from s2 118*5d9d9091SRichard Lowe 119*5d9d9091SRichard Lowe ! words are equal but the end of s1 has been reached 120*5d9d9091SRichard Lowe ! this means the strings must be equal 121*5d9d9091SRichard Lowe.strequal: 122*5d9d9091SRichard Lowe retl ! return from leaf function 123*5d9d9091SRichard Lowe mov %g0, %o0 ! return 0, i.e. strings are equal 124*5d9d9091SRichard Lowe nop 125*5d9d9091SRichard Lowe 126*5d9d9091SRichard Lowe.s2aligned: 127*5d9d9091SRichard Lowe ldx [%o1 + %o2], %o3 ! load word from s1 128*5d9d9091SRichard Lowe 129*5d9d9091SRichard Lowe.cmpaligned: 130*5d9d9091SRichard Lowe ldx [%o1], %g1 ! load word from s2 131*5d9d9091SRichard Lowe cmp %o3, %g1 ! *scr1 == *src2 ? 132*5d9d9091SRichard Lowe bne,pn %xcc, .wordsdiffer ! nope, find mismatching character 133*5d9d9091SRichard Lowe add %o1, 8, %o1 ! src1 += 8, src2 += 8 134*5d9d9091SRichard Lowe andn %o4, %o3, %o0 ! ~word & 0x80808080 135*5d9d9091SRichard Lowe sub %o3, %o5, %o3 ! word - 0x01010101 136*5d9d9091SRichard Lowe andcc %o3, %o0, %g0 ! (word - 0x01010101) & ~word & 0x80808080 137*5d9d9091SRichard Lowe bz,a,pt %xcc, .cmpaligned ! no null-byte in s1 yet 138*5d9d9091SRichard Lowe ldx [%o1 + %o2], %o3 ! load word from s1 139*5d9d9091SRichard Lowe 140*5d9d9091SRichard Lowe ! words are equal but the end of s1 has been reached 141*5d9d9091SRichard Lowe ! this means the strings must be equal 142*5d9d9091SRichard Lowe 143*5d9d9091SRichard Lowe.stringsequal: 144*5d9d9091SRichard Lowe retl ! return from leaf function 145*5d9d9091SRichard Lowe mov %g0, %o0 ! return 0, i.e. strings are equal 146*5d9d9091SRichard Lowe nop ! align loop on 16-byte boundary 147*5d9d9091SRichard Lowe nop ! align loop on 16-byte boundary 148*5d9d9091SRichard Lowe nop ! align loop on 16-byte boundary 149*5d9d9091SRichard Lowe 150*5d9d9091SRichard Lowe.wordsdiffer: 151*5d9d9091SRichard Lowe mov 56, %o4 ! initial shift count 152*5d9d9091SRichard Lowe srlx %g1, %o4, %o2 ! first byte of mismatching word in s2 153*5d9d9091SRichard Lowe.cmpbytes: 154*5d9d9091SRichard Lowe srlx %o3, %o4, %o1 ! first byte of mismatching word in s1 155*5d9d9091SRichard Lowe subcc %o1, %o2, %o0 ! *s1-*s2 156*5d9d9091SRichard Lowe bnz,pn %xcc, .done ! bytes differ, return difference 157*5d9d9091SRichard Lowe nop 158*5d9d9091SRichard Lowe andcc %o1, 0xff, %o0 ! *s1 == 0 ? 159*5d9d9091SRichard Lowe bz,pn %xcc, .done ! yup, strings match 160*5d9d9091SRichard Lowe subcc %o4, 8, %o4 ! shift_count -= 8 161*5d9d9091SRichard Lowe bpos,pt %xcc, .cmpbytes ! until all bytes processed 162*5d9d9091SRichard Lowe srlx %g1, %o4, %o2 ! first byte of mismatching word in s2 163*5d9d9091SRichard Lowe 164*5d9d9091SRichard Lowe.done: 165*5d9d9091SRichard Lowe retl ! return from leaf routine 166*5d9d9091SRichard Lowe nop ! padding 167*5d9d9091SRichard Lowe 168*5d9d9091SRichard Lowe SET_SIZE(strcmp) 169