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 "memchr.s" 287257d1b4Sraf 297c478bd9Sstevel@tonic-gate/* 307c478bd9Sstevel@tonic-gate * Return the ptr in sptr at which the character c1 appears; 317c478bd9Sstevel@tonic-gate * or NULL if not found in n chars; don't stop at \0. 327c478bd9Sstevel@tonic-gate * void * 337c478bd9Sstevel@tonic-gate * memchr(const void *sptr, int c1, size_t n) 347c478bd9Sstevel@tonic-gate * { 357c478bd9Sstevel@tonic-gate * if (n != 0) { 367c478bd9Sstevel@tonic-gate * unsigned char c = (unsigned char)c1; 377c478bd9Sstevel@tonic-gate * const unsigned char *sp = sptr; 387c478bd9Sstevel@tonic-gate * 397c478bd9Sstevel@tonic-gate * do { 407c478bd9Sstevel@tonic-gate * if (*sp++ == c) 417c478bd9Sstevel@tonic-gate * return ((void *)--sp); 427c478bd9Sstevel@tonic-gate * } while (--n != 0); 437c478bd9Sstevel@tonic-gate * } 447c478bd9Sstevel@tonic-gate * return (NULL); 457c478bd9Sstevel@tonic-gate * } 467c478bd9Sstevel@tonic-gate */ 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate#include <sys/asm_linkage.h> 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate ! The first part of this algorithm focuses on determining 517c478bd9Sstevel@tonic-gate ! whether or not the desired character is in the first few bytes 527c478bd9Sstevel@tonic-gate ! of memory, aligning the memory for word-wise copies, and 537c478bd9Sstevel@tonic-gate ! initializing registers to detect zero bytes 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate ENTRY(memchr) 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate .align 32 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate tst %o2 ! n == 0 ? 607c478bd9Sstevel@tonic-gate bz .notfound ! yup, c not found, return null ptr 617c478bd9Sstevel@tonic-gate andcc %o0, 3, %o4 ! s word aligned ? 627c478bd9Sstevel@tonic-gate add %o0, %o2, %o0 ! s + n 637c478bd9Sstevel@tonic-gate sub %g0, %o2, %o2 ! n = -n 647c478bd9Sstevel@tonic-gate bz .prepword ! yup, prepare for word-wise search 657c478bd9Sstevel@tonic-gate and %o1, 0xff, %o1 ! search only for this one byte 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate ldub [%o0 + %o2], %o3 ! s[0] 687c478bd9Sstevel@tonic-gate cmp %o3, %o1 ! s[0] == c ? 697c478bd9Sstevel@tonic-gate be .done ! yup, done 707c478bd9Sstevel@tonic-gate nop ! 717c478bd9Sstevel@tonic-gate addcc %o2, 1, %o2 ! n++, s++ 727c478bd9Sstevel@tonic-gate bz .notfound ! c not found in first n bytes 737c478bd9Sstevel@tonic-gate cmp %o4, 3 ! only one byte needed to align? 747c478bd9Sstevel@tonic-gate bz .prepword2 ! yup, prepare for word-wise search 757c478bd9Sstevel@tonic-gate sll %o1, 8, %g1 ! start spreading c across word 767c478bd9Sstevel@tonic-gate ldub [%o0 + %o2], %o3 ! s[1] 777c478bd9Sstevel@tonic-gate cmp %o3, %o1 ! s[1] == c ? 787c478bd9Sstevel@tonic-gate be .done ! yup, done 797c478bd9Sstevel@tonic-gate nop ! 807c478bd9Sstevel@tonic-gate addcc %o2, 1, %o2 ! n++, s++ 817c478bd9Sstevel@tonic-gate bz .notfound ! c not found in first n bytes 827c478bd9Sstevel@tonic-gate cmp %o4, 2 ! only two bytes needed to align? 837c478bd9Sstevel@tonic-gate bz .prepword3 ! yup, prepare for word-wise search 847c478bd9Sstevel@tonic-gate sethi %hi(0x01010101), %o4 ! start loading Alan Mycroft's magic1 857c478bd9Sstevel@tonic-gate ldub [%o0 + %o2], %o3 ! s[1] 867c478bd9Sstevel@tonic-gate cmp %o3, %o1 ! s[1] == c ? 877c478bd9Sstevel@tonic-gate be .done ! yup, done 887c478bd9Sstevel@tonic-gate nop 897c478bd9Sstevel@tonic-gate addcc %o2, 1, %o2 ! n++, s++ 907c478bd9Sstevel@tonic-gate bz .notfound ! c not found in first n bytes 917c478bd9Sstevel@tonic-gate nop 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate.prepword: 947c478bd9Sstevel@tonic-gate sll %o1, 8, %g1 ! spread c -------------+ 957c478bd9Sstevel@tonic-gate.prepword2: ! ! 967c478bd9Sstevel@tonic-gate sethi %hi(0x01010101), %o4 ! Alan Mycroft's magic1 ! 977c478bd9Sstevel@tonic-gate.prepword3: ! ! 987c478bd9Sstevel@tonic-gate or %o1, %g1, %o1 ! across all <---------+ 997c478bd9Sstevel@tonic-gate or %o4, %lo(0x01010101),%o4! finish loading magic1 ! 1007c478bd9Sstevel@tonic-gate sll %o1, 16, %g1 ! four bytes <--------+ 1017c478bd9Sstevel@tonic-gate sll %o4, 7, %o5 ! Alan Mycroft's magic2 ! 1027c478bd9Sstevel@tonic-gate or %o1, %g1, %o1 ! of a word <--------+ 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate.searchchar: 1057c478bd9Sstevel@tonic-gate lduw [%o0 + %o2], %o3 ! src word 1067c478bd9Sstevel@tonic-gate.searchchar2: 1077c478bd9Sstevel@tonic-gate addcc %o2, 4, %o2 ! s+=4, n+=4 1087c478bd9Sstevel@tonic-gate bcs .lastword ! if counter wraps, last word 1097c478bd9Sstevel@tonic-gate xor %o3, %o1, %g1 ! tword = word ^ c 1107c478bd9Sstevel@tonic-gate andn %o5, %g1, %o3 ! ~tword & 0x80808080 1117c478bd9Sstevel@tonic-gate sub %g1, %o4, %g1 ! (tword - 0x01010101) 1127c478bd9Sstevel@tonic-gate andcc %o3, %g1, %g0 ! ((tword - 0x01010101) & ~tword & 0x80808080) 1137c478bd9Sstevel@tonic-gate bz,a .searchchar2 ! c not found if magic expression == 0 1147c478bd9Sstevel@tonic-gate lduw [%o0 + %o2], %o3 ! src word 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate ! here we know "word" contains the searched character, and no byte in 1177c478bd9Sstevel@tonic-gate ! "word" exceeds n. If we had exceeded n, we would have gone to label 1187c478bd9Sstevel@tonic-gate ! .lastword. "tword" has null bytes where "word" had c. After 1197c478bd9Sstevel@tonic-gate ! restoring "tword" from "(tword - 0x01010101)" in %g1, examine "tword" 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate.foundchar: 1227c478bd9Sstevel@tonic-gate add %g1, %o4, %g1 ! restore tword 1237c478bd9Sstevel@tonic-gate set 0xff000000, %o4 ! mask for 1st byte 1247c478bd9Sstevel@tonic-gate andcc %g1, %o4, %g0 ! first byte zero (= found c) ? 1257c478bd9Sstevel@tonic-gate bz,a .done ! yup, done 1267c478bd9Sstevel@tonic-gate sub %o2, 4, %o2 ! n -= 4 (undo counter bumping) 1277c478bd9Sstevel@tonic-gate set 0x00ff0000, %o5 ! mask for 2nd byte 1287c478bd9Sstevel@tonic-gate andcc %g1, %o5, %g0 ! second byte zero (= found c) ? 1297c478bd9Sstevel@tonic-gate bz,a .done ! yup, done 1307c478bd9Sstevel@tonic-gate sub %o2, 3, %o2 ! n -= 3 (undo counter bumping) 1317c478bd9Sstevel@tonic-gate srl %o4, 16, %o4 ! 0x0000ff00 = mask for 3rd byte 1327c478bd9Sstevel@tonic-gate andcc %g1, %o4, %g0 ! third byte zero (= found c) ? 1337c478bd9Sstevel@tonic-gate bz,a .done ! nope, must be fourth byte 1347c478bd9Sstevel@tonic-gate sub %o2, 2, %o2 ! n -= 2 (undo counter bumping) 1357c478bd9Sstevel@tonic-gate sub %o2, 1, %o2 ! n -= 1, if fourth byte 1367c478bd9Sstevel@tonic-gate retl ! done with leaf function 1377c478bd9Sstevel@tonic-gate add %o0, %o2, %o0 ! return pointer to c in s 1387c478bd9Sstevel@tonic-gate.done: 1397c478bd9Sstevel@tonic-gate retl ! done with leaf function 1407c478bd9Sstevel@tonic-gate add %o0, %o2, %o0 ! return pointer to c in s 1417c478bd9Sstevel@tonic-gate nop 1427c478bd9Sstevel@tonic-gate nop 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate ! Here we know that "word" is the last word in the search, and that 1457c478bd9Sstevel@tonic-gate ! some bytes possibly exceed n. However, "word" might also contain c. 1467c478bd9Sstevel@tonic-gate ! "tword" (in %g1) has null bytes where "word" had c. Examine "tword" 1477c478bd9Sstevel@tonic-gate ! while keeping track of number of remaining bytes 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate.lastword: 1507c478bd9Sstevel@tonic-gate set 0xff000000, %o4 ! mask for 1st byte 1517c478bd9Sstevel@tonic-gate sub %o2, 4, %o2 ! n -= 4 (undo counter bumping) 1527c478bd9Sstevel@tonic-gate andcc %g1, %o4, %g0 ! first byte zero (= found c) ? 1537c478bd9Sstevel@tonic-gate bz .done ! yup, done 1547c478bd9Sstevel@tonic-gate set 0x00ff0000, %o5 ! mask for 2nd byte 1557c478bd9Sstevel@tonic-gate addcc %o2, 1, %o2 ! n += 1 1567c478bd9Sstevel@tonic-gate bz .notfound ! c not found in first n bytes 1577c478bd9Sstevel@tonic-gate andcc %g1, %o5, %g0 ! second byte zero (= found c) ? 1587c478bd9Sstevel@tonic-gate bz .done ! yup, done 1597c478bd9Sstevel@tonic-gate srl %o4, 16, %o4 ! 0x0000ff00 = mask for 3rd byte 1607c478bd9Sstevel@tonic-gate addcc %o2, 1, %o2 ! n += 1 1617c478bd9Sstevel@tonic-gate bz .notfound ! c not found in first n bytes 1627c478bd9Sstevel@tonic-gate andcc %g1, %o4, %g0 ! third byte zero (= found c) ? 1637c478bd9Sstevel@tonic-gate bz .done ! yup, done 1647c478bd9Sstevel@tonic-gate nop ! 1657c478bd9Sstevel@tonic-gate addcc %o2, 1, %o2 ! n += 1 1667c478bd9Sstevel@tonic-gate bz .notfound ! c not found in first n bytes 1677c478bd9Sstevel@tonic-gate andcc %g1, 0xff, %g0 ! fourth byte zero (= found c) ? 1687c478bd9Sstevel@tonic-gate bz .done ! yup, done 1697c478bd9Sstevel@tonic-gate nop 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate.notfound: 1727c478bd9Sstevel@tonic-gate retl ! done with leaf function 1737c478bd9Sstevel@tonic-gate mov %g0, %o0 ! return null pointer 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate SET_SIZE(memchr) 176