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