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 "strchr.s" 280Sstevel@tonic-gate 290Sstevel@tonic-gate/* 300Sstevel@tonic-gate * The strchr() function returns a pointer to the first occurrence of c 310Sstevel@tonic-gate * (converted to a char) in string s, or a null pointer if c does not occur 320Sstevel@tonic-gate * in the string. 330Sstevel@tonic-gate */ 340Sstevel@tonic-gate 350Sstevel@tonic-gate#include <sys/asm_linkage.h> 360Sstevel@tonic-gate 370Sstevel@tonic-gate ! Here, we start by checking to see if we're searching the dest 380Sstevel@tonic-gate ! string for a null byte. We have fast code for this, so it's 390Sstevel@tonic-gate ! an important special case. Otherwise, if the string is not 400Sstevel@tonic-gate ! word aligned, we check a for the search char a byte at a time 410Sstevel@tonic-gate ! until we've reached a word boundary. Once this has happened 420Sstevel@tonic-gate ! some zero-byte finding values are initialized and the string 430Sstevel@tonic-gate ! is checked a word at a time 440Sstevel@tonic-gate 450Sstevel@tonic-gate ENTRY(strchr) 460Sstevel@tonic-gate 470Sstevel@tonic-gate .align 32 480Sstevel@tonic-gate 490Sstevel@tonic-gate andcc %o1, 0xff, %o1 ! search only for this one byte 500Sstevel@tonic-gate bz .searchnullbyte ! faster code for searching null 510Sstevel@tonic-gate andcc %o0, 3, %o4 ! str word aligned ? 520Sstevel@tonic-gate bz,a .prepword2 ! yup, prepare for word-wise search 530Sstevel@tonic-gate sll %o1, 8, %g1 ! start spreading findchar across word 540Sstevel@tonic-gate 550Sstevel@tonic-gate ldub [%o0], %o2 ! str[0] 560Sstevel@tonic-gate cmp %o2, %o1 ! str[0] == findchar ? 570Sstevel@tonic-gate be .done ! yup, done 580Sstevel@tonic-gate tst %o2 ! str[0] == 0 ? 590Sstevel@tonic-gate bz .notfound ! yup, return null pointer 600Sstevel@tonic-gate cmp %o4, 3 ! only one byte needed to align? 610Sstevel@tonic-gate bz .prepword ! yup, prepare for word-wise search 620Sstevel@tonic-gate inc %o0 ! str++ 630Sstevel@tonic-gate ldub [%o0], %o2 ! str[1] 640Sstevel@tonic-gate cmp %o2, %o1 ! str[1] == findchar ? 650Sstevel@tonic-gate be .done ! yup, done 660Sstevel@tonic-gate tst %o2 ! str[1] == 0 ? 670Sstevel@tonic-gate bz .notfound ! yup, return null pointer 680Sstevel@tonic-gate cmp %o4, 2 ! only two bytes needed to align? 690Sstevel@tonic-gate bz .prepword ! yup, prepare for word-wise search 700Sstevel@tonic-gate inc %o0 ! str++ 710Sstevel@tonic-gate ldub [%o0], %o2 ! str[2] 720Sstevel@tonic-gate cmp %o2, %o1 ! str[2] == findchar ? 730Sstevel@tonic-gate be .done ! yup, done 740Sstevel@tonic-gate tst %o2 ! str[2] == 0 ? 750Sstevel@tonic-gate bz .notfound ! yup, return null pointer 760Sstevel@tonic-gate inc %o0 ! str++ 770Sstevel@tonic-gate 780Sstevel@tonic-gate.prepword: 790Sstevel@tonic-gate sll %o1, 8, %g1 ! spread findchar ------+ 800Sstevel@tonic-gate.prepword2: ! 810Sstevel@tonic-gate sethi %hi(0x01010101), %o4 ! Alan Mycroft's magic1 ! 820Sstevel@tonic-gate or %o1, %g1, %o1 ! across all <---------+ 830Sstevel@tonic-gate sethi %hi(0x80808080), %o5 ! Alan Mycroft's magic2 ! 840Sstevel@tonic-gate sll %o1, 16, %g1 ! four bytes <--------+ 850Sstevel@tonic-gate or %o4, %lo(0x01010101), %o4 ! 860Sstevel@tonic-gate or %o1, %g1, %o1 ! of a word <--------+ 870Sstevel@tonic-gate or %o5, %lo(0x80808080), %o5 880Sstevel@tonic-gate 890Sstevel@tonic-gate.searchchar: 900Sstevel@tonic-gate lduw [%o0], %o2 ! src word 910Sstevel@tonic-gate andn %o5, %o2, %o3 ! ~word & 0x80808080 920Sstevel@tonic-gate sub %o2, %o4, %g1 ! word = (word - 0x01010101) 930Sstevel@tonic-gate andcc %o3, %g1, %g0 ! ((word - 0x01010101) & ~word & 0x80808080) 940Sstevel@tonic-gate bnz .haszerobyte ! zero byte if magic expression != 0 950Sstevel@tonic-gate xor %o2, %o1, %g1 ! tword = word ^ findchar 960Sstevel@tonic-gate andn %o5, %g1, %o3 ! ~tword & 0x80808080 970Sstevel@tonic-gate sub %g1, %o4, %o2 ! (tword - 0x01010101) 980Sstevel@tonic-gate andcc %o3, %o2, %g0 ! ((tword - 0x01010101) & ~tword & 0x80808080) 990Sstevel@tonic-gate bz,a .searchchar ! no findchar if magic expression == 0 1000Sstevel@tonic-gate add %o0, 4, %o0 ! str += 4 1010Sstevel@tonic-gate 1020Sstevel@tonic-gate ! here we know "word" contains the searched character, but no null 1030Sstevel@tonic-gate ! byte. if there was a null byte, we would have gone to .haszerobyte 1040Sstevel@tonic-gate ! "tword" has null bytes where "word" had findchar. Examine "tword" 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate.foundchar: 1070Sstevel@tonic-gate set 0xff000000, %o4 ! mask for 1st byte 1080Sstevel@tonic-gate andcc %g1, %o4, %g0 ! first byte zero (= found search char) ? 1090Sstevel@tonic-gate bz .done ! yup, done 1100Sstevel@tonic-gate set 0x00ff0000, %o5 ! mask for 2nd byte 1110Sstevel@tonic-gate inc %o0 ! str++ 1120Sstevel@tonic-gate andcc %g1, %o5, %g0 ! second byte zero (= found search char) ? 1130Sstevel@tonic-gate bz .done ! yup, done 1140Sstevel@tonic-gate srl %o4, 16, %o4 ! 0x0000ff00 = mask for 3rd byte 1150Sstevel@tonic-gate inc %o0 ! str++ 1160Sstevel@tonic-gate andcc %g1, %o4, %g0 ! third byte zero (= found search char) ? 1170Sstevel@tonic-gate bnz,a .done ! nope, increment in delay slot 1180Sstevel@tonic-gate inc %o0 ! str++ 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate.done: 1210Sstevel@tonic-gate retl ! done with leaf function 1220Sstevel@tonic-gate nop ! padding 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate ! Here we know that "word" contains a null byte indicating the 1250Sstevel@tonic-gate ! end of the string. However, "word" might also contain findchar 1260Sstevel@tonic-gate ! "tword" (in %g1) has null bytes where "word" had findchar. So 1270Sstevel@tonic-gate ! check both "tword" and "word" 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate.haszerobyte: 1300Sstevel@tonic-gate set 0xff000000, %o4 ! mask for 1st byte 1310Sstevel@tonic-gate andcc %g1, %o4, %g0 ! first byte == findchar ? 1320Sstevel@tonic-gate bz .done ! yup, done 1330Sstevel@tonic-gate andcc %o2, %o4, %g0 ! first byte == 0 ? 1340Sstevel@tonic-gate bz .notfound ! yup, return null pointer 1350Sstevel@tonic-gate set 0x00ff0000, %o4 ! mask for 2nd byte 1360Sstevel@tonic-gate inc %o0 ! str++ 1370Sstevel@tonic-gate andcc %g1, %o4, %g0 ! second byte == findchar ? 1380Sstevel@tonic-gate bz .done ! yup, done 1390Sstevel@tonic-gate andcc %o2, %o4, %g0 ! second byte == 0 ? 1400Sstevel@tonic-gate bz .notfound ! yup, return null pointer 1410Sstevel@tonic-gate srl %o4, 8, %o4 ! mask for 3rd byte = 0x0000ff00 1420Sstevel@tonic-gate inc %o0 ! str++ 1430Sstevel@tonic-gate andcc %g1, %o4, %g0 ! third byte == findchar ? 1440Sstevel@tonic-gate bz .done ! yup, done 1450Sstevel@tonic-gate andcc %o2, %o4, %g0 ! third byte == 0 ? 1460Sstevel@tonic-gate bz .notfound ! yup, return null pointer 1470Sstevel@tonic-gate andcc %g1, 0xff, %g0 ! fourth byte == findchar ? 1480Sstevel@tonic-gate bz .done ! yup, done 1490Sstevel@tonic-gate inc %o0 ! str++ 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate.notfound: 1520Sstevel@tonic-gate retl ! done with leaf function 1530Sstevel@tonic-gate xor %o0, %o0, %o0 ! return null pointer 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate ! since findchar == 0, we only have to do one test per item 1560Sstevel@tonic-gate ! instead of two. This makes the search much faster. 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate.searchnullbyte: 1590Sstevel@tonic-gate bz .straligned ! str is word aligned 1600Sstevel@tonic-gate nop ! padding 1610Sstevel@tonic-gate 1620Sstevel@tonic-gate cmp %o4, 2 ! str halfword aligned ? 1630Sstevel@tonic-gate be .s2aligned ! yup 1640Sstevel@tonic-gate ldub [%o0], %o1 ! str[0] 1650Sstevel@tonic-gate tst %o1 ! byte zero? 1660Sstevel@tonic-gate bz .done2 ! yup, done 1670Sstevel@tonic-gate cmp %o4, 3 ! only one byte needed to align? 1680Sstevel@tonic-gate bz .straligned ! yup 1690Sstevel@tonic-gate inc %o0 ! str++ 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate ! check to see if we're half word aligned, which it better than 1720Sstevel@tonic-gate ! not being aligned at all. Search the first half of the word 1730Sstevel@tonic-gate ! if we are, and then search by whole word. 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate.s2aligned: 1760Sstevel@tonic-gate lduh [%o0], %o1 ! str[] 1770Sstevel@tonic-gate srl %o1, 8, %o4 ! %o4<7:0> = first byte 1780Sstevel@tonic-gate tst %o4 ! first byte zero ? 1790Sstevel@tonic-gate bz .done2 ! yup, done 1800Sstevel@tonic-gate andcc %o1, 0xff, %g0 ! second byte zero ? 1810Sstevel@tonic-gate bz,a .done2 ! yup, done 1820Sstevel@tonic-gate inc %o0 ! str++ 1830Sstevel@tonic-gate add %o0, 2, %o0 ! str+=2 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate.straligned: 1860Sstevel@tonic-gate sethi %hi(0x01010101), %o4 ! Alan Mycroft's magic1 1870Sstevel@tonic-gate sethi %hi(0x80808080), %o5 ! Alan Mycroft's magic2 1880Sstevel@tonic-gate or %o4, %lo(0x01010101), %o4 1890Sstevel@tonic-gate or %o5, %lo(0x80808080), %o5 1900Sstevel@tonic-gate 1910Sstevel@tonic-gate.searchword: 1920Sstevel@tonic-gate lduw [%o0], %o1 ! src word 1930Sstevel@tonic-gate andn %o5, %o1, %o3 ! ~word & 0x80808080 1940Sstevel@tonic-gate sub %o1, %o4, %g1 ! word = (word - 0x01010101) 1950Sstevel@tonic-gate andcc %o3, %g1, %g0 ! ((word - 0x01010101) & ~word & 0x80808080) 1960Sstevel@tonic-gate bz,a .searchword ! no zero byte if magic expression == 0 1970Sstevel@tonic-gate add %o0, 4, %o0 ! str += 4 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate.zerobyte: 2000Sstevel@tonic-gate set 0xff000000, %o4 ! mask for 1st byte 2010Sstevel@tonic-gate andcc %o1, %o4, %g0 ! first byte zero? 2020Sstevel@tonic-gate bz .done2 ! yup, done 2030Sstevel@tonic-gate set 0x00ff0000, %o5 ! mask for 2nd byte 2040Sstevel@tonic-gate inc %o0 ! str++ 2050Sstevel@tonic-gate andcc %o1, %o5, %g0 ! second byte zero? 2060Sstevel@tonic-gate bz .done2 ! yup, done 2070Sstevel@tonic-gate srl %o4, 16, %o4 ! 0x0000ff00 = mask for 3rd byte 2080Sstevel@tonic-gate inc %o0 ! str++ 2090Sstevel@tonic-gate andcc %o1, %o4, %g0 ! third byte zero? 2100Sstevel@tonic-gate bnz,a .done2 ! nope, increment in delay slot 2110Sstevel@tonic-gate inc %o0 ! str++ 2120Sstevel@tonic-gate.done2: 2130Sstevel@tonic-gate retl ! return from leaf function 2140Sstevel@tonic-gate nop ! padding 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate SET_SIZE(strchr) 217