17c478bd9Sstevel@tonic-gate/* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57257d1b4Sraf * Common Development and Distribution License (the "License"). 67257d1b4Sraf * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217257d1b4Sraf 227c478bd9Sstevel@tonic-gate/* 237257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 27*9a70fc3bSMark J. Nelson .file "strcmp.s" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate/* strcmp(s1, s2) 307c478bd9Sstevel@tonic-gate * 317c478bd9Sstevel@tonic-gate * Compare strings: s1>s2: >0 s1==s2: 0 s1<s2: <0 327c478bd9Sstevel@tonic-gate * 337c478bd9Sstevel@tonic-gate * Fast assembler language version of the following C-program for strcmp 347c478bd9Sstevel@tonic-gate * which represents the `standard' for the C-library. 357c478bd9Sstevel@tonic-gate * 367c478bd9Sstevel@tonic-gate * int 377c478bd9Sstevel@tonic-gate * strcmp(s1, s2) 387c478bd9Sstevel@tonic-gate * register const char *s1; 397c478bd9Sstevel@tonic-gate * register const char *s2; 407c478bd9Sstevel@tonic-gate * { 417c478bd9Sstevel@tonic-gate * 427c478bd9Sstevel@tonic-gate * if(s1 == s2) 437c478bd9Sstevel@tonic-gate * return(0); 447c478bd9Sstevel@tonic-gate * while(*s1 == *s2++) 457c478bd9Sstevel@tonic-gate * if(*s1++ == '\0') 467c478bd9Sstevel@tonic-gate * return(0); 477c478bd9Sstevel@tonic-gate * return(*s1 - s2[-1]); 487c478bd9Sstevel@tonic-gate * } 497c478bd9Sstevel@tonic-gate */ 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate#include <sys/asm_linkage.h> 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate ! This strcmp implementation first determines whether s1 is aligned. 547c478bd9Sstevel@tonic-gate ! If it is not, it attempts to align it and then checks the 557c478bd9Sstevel@tonic-gate ! alignment of the destination string. If it is possible to 567c478bd9Sstevel@tonic-gate ! align s2, this also happens and then the compare begins. Otherwise, 577c478bd9Sstevel@tonic-gate ! a different compare for non-aligned strings is used. 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate ENTRY(strcmp) 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate .align 32 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate subcc %o0, %o1, %o2 ! s1 == s2 ? 647c478bd9Sstevel@tonic-gate bz,pn %xcc, .stringsequal ! yup, same string, done 657c478bd9Sstevel@tonic-gate sethi %hi(0x01010101), %o5 ! magic2<31:13> 667c478bd9Sstevel@tonic-gate andcc %o0, 7, %o3 ! s1 sword-aligned ? 677c478bd9Sstevel@tonic-gate or %o5, %lo(0x01010101),%o5! magic2<31:0> 687c478bd9Sstevel@tonic-gate bz,pn %xcc, .s1aligned ! yup 697c478bd9Sstevel@tonic-gate sllx %o5, 32, %o4 ! magic2<63:32> 707c478bd9Sstevel@tonic-gate sub %o3, 8, %o3 ! number of bytes till s1 aligned 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate.aligns1: 737c478bd9Sstevel@tonic-gate ldub [%o1 + %o2], %o0 ! s1[] 747c478bd9Sstevel@tonic-gate ldub [%o1], %g1 ! s2[] 757c478bd9Sstevel@tonic-gate subcc %o0, %g1, %o0 ! s1[] != s2[] ? 767c478bd9Sstevel@tonic-gate bne,pn %xcc, .done ! yup, done 777c478bd9Sstevel@tonic-gate addcc %o0, %g1, %g0 ! s1[] == 0 ? 787c478bd9Sstevel@tonic-gate bz,pn %xcc, .done ! yup, done 797c478bd9Sstevel@tonic-gate inccc %o3 ! s1 aligned yet? 807c478bd9Sstevel@tonic-gate bnz,pt %xcc, .aligns1 ! nope, compare another pair of bytes 817c478bd9Sstevel@tonic-gate inc %o1 ! s1++, s2++ 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate.s1aligned: 847c478bd9Sstevel@tonic-gate andcc %o1, 7, %o3 ! s2 dword aligned ? 857c478bd9Sstevel@tonic-gate or %o5, %o4, %o5 ! magic2<63:0> 867c478bd9Sstevel@tonic-gate bz,pn %xcc, .s2aligned ! yup 877c478bd9Sstevel@tonic-gate sllx %o5, 7, %o4 ! magic1 887c478bd9Sstevel@tonic-gate sllx %o3, 3, %g5 ! leftshift = 8*ofs 897c478bd9Sstevel@tonic-gate orn %g0, %g0, %g1 ! all ones 907c478bd9Sstevel@tonic-gate and %o1, -8, %o1 ! round s1 down to next aligned dword 917c478bd9Sstevel@tonic-gate srlx %g1, %g5, %g1 ! mask for fixing up bytes 927c478bd9Sstevel@tonic-gate ldx [%o1], %o0 ! new lower dword in s2 937c478bd9Sstevel@tonic-gate orn %o0, %g1, %o0 ! force start bytes to non-zero 947c478bd9Sstevel@tonic-gate sub %g0, %g5, %g4 ! rightshift = -(8*ofs) mod 64 957c478bd9Sstevel@tonic-gate sllx %o0, %g5, %g1 ! partial unaligned word from s2 967c478bd9Sstevel@tonic-gate add %o2, %o3, %o2 ! adjust pointers 977c478bd9Sstevel@tonic-gate nop ! align loop to 16-byte boundary 987c478bd9Sstevel@tonic-gate nop ! align loop to 16-byte boundary 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate.cmp: 1017c478bd9Sstevel@tonic-gate andn %o4, %o0, %o3 ! ~word & 0x80808080 1027c478bd9Sstevel@tonic-gate sub %o0, %o5, %o0 ! word - 0x01010101 1037c478bd9Sstevel@tonic-gate andcc %o0, %o3, %g0 ! (word - 0x01010101) & ~word & 0x80808080 1047c478bd9Sstevel@tonic-gate bz,a,pt %xcc, .doload ! no null byte in previous word from s2 1057c478bd9Sstevel@tonic-gate ldx [%o1+8], %o0 ! next aligned word in s2 1067c478bd9Sstevel@tonic-gate.doload: 1077c478bd9Sstevel@tonic-gate srlx %o0, %g4, %o3 ! bytes from aligned word from s2 1087c478bd9Sstevel@tonic-gate or %g1, %o3, %g1 ! merge to get unaligned word from s2 1097c478bd9Sstevel@tonic-gate ldx [%o1 + %o2], %o3 ! word from s1 1107c478bd9Sstevel@tonic-gate cmp %o3, %g1 ! *s1 != *s2 ? 1117c478bd9Sstevel@tonic-gate bne,pn %xcc, .wordsdiffer ! yup, find the byte that is different 1127c478bd9Sstevel@tonic-gate add %o1, 8, %o1 ! s1+=8, s2+=8 1137c478bd9Sstevel@tonic-gate andn %o4, %o3, %g1 ! ~word & 0x80808080 1147c478bd9Sstevel@tonic-gate sub %o3, %o5, %o3 ! word - 0x01010101 1157c478bd9Sstevel@tonic-gate andcc %o3, %g1, %g0 ! (word - 0x01010101) & ~word & 0x80808080 1167c478bd9Sstevel@tonic-gate bz,pt %xcc, .cmp ! no null-byte in s1 yet 1177c478bd9Sstevel@tonic-gate sllx %o0, %g5, %g1 ! partial unaligned word from s2 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate ! words are equal but the end of s1 has been reached 1207c478bd9Sstevel@tonic-gate ! this means the strings must be equal 1217c478bd9Sstevel@tonic-gate.strequal: 1227c478bd9Sstevel@tonic-gate retl ! return from leaf function 1237c478bd9Sstevel@tonic-gate mov %g0, %o0 ! return 0, i.e. strings are equal 1247c478bd9Sstevel@tonic-gate nop 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate.s2aligned: 1277c478bd9Sstevel@tonic-gate ldx [%o1 + %o2], %o3 ! load word from s1 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate.cmpaligned: 1307c478bd9Sstevel@tonic-gate ldx [%o1], %g1 ! load word from s2 1317c478bd9Sstevel@tonic-gate cmp %o3, %g1 ! *scr1 == *src2 ? 1327c478bd9Sstevel@tonic-gate bne,pn %xcc, .wordsdiffer ! nope, find mismatching character 1337c478bd9Sstevel@tonic-gate add %o1, 8, %o1 ! src1 += 8, src2 += 8 1347c478bd9Sstevel@tonic-gate andn %o4, %o3, %o0 ! ~word & 0x80808080 1357c478bd9Sstevel@tonic-gate sub %o3, %o5, %o3 ! word - 0x01010101 1367c478bd9Sstevel@tonic-gate andcc %o3, %o0, %g0 ! (word - 0x01010101) & ~word & 0x80808080 1377c478bd9Sstevel@tonic-gate bz,a,pt %xcc, .cmpaligned ! no null-byte in s1 yet 1387c478bd9Sstevel@tonic-gate ldx [%o1 + %o2], %o3 ! load word from s1 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate ! words are equal but the end of s1 has been reached 1417c478bd9Sstevel@tonic-gate ! this means the strings must be equal 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate.stringsequal: 1447c478bd9Sstevel@tonic-gate retl ! return from leaf function 1457c478bd9Sstevel@tonic-gate mov %g0, %o0 ! return 0, i.e. strings are equal 1467c478bd9Sstevel@tonic-gate nop ! align loop on 16-byte boundary 1477c478bd9Sstevel@tonic-gate nop ! align loop on 16-byte boundary 1487c478bd9Sstevel@tonic-gate nop ! align loop on 16-byte boundary 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate.wordsdiffer: 1517c478bd9Sstevel@tonic-gate mov 56, %o4 ! initial shift count 1527c478bd9Sstevel@tonic-gate srlx %g1, %o4, %o2 ! first byte of mismatching word in s2 1537c478bd9Sstevel@tonic-gate.cmpbytes: 1547c478bd9Sstevel@tonic-gate srlx %o3, %o4, %o1 ! first byte of mismatching word in s1 1557c478bd9Sstevel@tonic-gate subcc %o1, %o2, %o0 ! *s1-*s2 1567c478bd9Sstevel@tonic-gate bnz,pn %xcc, .done ! bytes differ, return difference 1577c478bd9Sstevel@tonic-gate nop 1587c478bd9Sstevel@tonic-gate andcc %o1, 0xff, %o0 ! *s1 == 0 ? 1597c478bd9Sstevel@tonic-gate bz,pn %xcc, .done ! yup, strings match 1607c478bd9Sstevel@tonic-gate subcc %o4, 8, %o4 ! shift_count -= 8 1617c478bd9Sstevel@tonic-gate bpos,pt %xcc, .cmpbytes ! until all bytes processed 1627c478bd9Sstevel@tonic-gate srlx %g1, %o4, %o2 ! first byte of mismatching word in s2 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate.done: 1657c478bd9Sstevel@tonic-gate retl ! return from leaf routine 1667c478bd9Sstevel@tonic-gate nop ! padding 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate SET_SIZE(strcmp) 169