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 * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 23*5d9d9091SRichard Lowe * Use is subject to license terms. 24*5d9d9091SRichard Lowe */ 25*5d9d9091SRichard Lowe 26*5d9d9091SRichard Lowe .file "memchr.s" 27*5d9d9091SRichard Lowe 28*5d9d9091SRichard Lowe/ 29*5d9d9091SRichard Lowe/ memchr(sptr, c1, n) 30*5d9d9091SRichard Lowe/ 31*5d9d9091SRichard Lowe/ Returns the pointer in sptr at which the character c1 appears; 32*5d9d9091SRichard Lowe/ or NULL if not found in chars; doesn't stop at \0. 33*5d9d9091SRichard Lowe/ 34*5d9d9091SRichard Lowe/ Fast assembly language version of the following C-program memchr 35*5d9d9091SRichard Lowe/ which represents the `standard' for the C-library. 36*5d9d9091SRichard Lowe/ 37*5d9d9091SRichard Lowe/ void * 38*5d9d9091SRichard Lowe/ memchr(const void *sptr, int c1, size_t n) 39*5d9d9091SRichard Lowe/ { 40*5d9d9091SRichard Lowe/ if (n != 0) { 41*5d9d9091SRichard Lowe/ unsigned char c = (unsigned char)c1; 42*5d9d9091SRichard Lowe/ const unsigned char *sp = sptr; 43*5d9d9091SRichard Lowe/ 44*5d9d9091SRichard Lowe/ do { 45*5d9d9091SRichard Lowe/ if (*sp++ == c) 46*5d9d9091SRichard Lowe/ return ((void *)--sp); 47*5d9d9091SRichard Lowe/ } while (--n != 0); 48*5d9d9091SRichard Lowe/ } 49*5d9d9091SRichard Lowe/ return (NULL); 50*5d9d9091SRichard Lowe/ } 51*5d9d9091SRichard Lowe/ 52*5d9d9091SRichard Lowe 53*5d9d9091SRichard Lowe#include "SYS.h" 54*5d9d9091SRichard Lowe 55*5d9d9091SRichard Lowe .globl memchr 56*5d9d9091SRichard Lowe .align 4 57*5d9d9091SRichard Lowe 58*5d9d9091SRichard Lowe ENTRY(memchr) /* (void *s, uchar_t c, size_t n) */ 59*5d9d9091SRichard Lowe movl %esi, %eax / move "c" to %eax 60*5d9d9091SRichard Lowe cmpq $4, %rdx / if number of bytes < 4 61*5d9d9091SRichard Lowe jb .L1 / goto .L1 62*5d9d9091SRichard Lowe testq $3, %rdi / if %rdi not word aligned 63*5d9d9091SRichard Lowe jnz .L2 / goto .L2 64*5d9d9091SRichard Lowe .align 4 65*5d9d9091SRichard Lowe.L3: 66*5d9d9091SRichard Lowe movl (%rdi), %ecx / move 1 word from (%rdi) to %ecx 67*5d9d9091SRichard Lowe cmpb %cl, %al / if the first byte is %al 68*5d9d9091SRichard Lowe je .L4 / goto .L4 (found) 69*5d9d9091SRichard Lowe cmpb %ch, %al / if the second byte is %al 70*5d9d9091SRichard Lowe je .L5 / goto .L5 (found) 71*5d9d9091SRichard Lowe shrl $16, %ecx / right shift 16-bit 72*5d9d9091SRichard Lowe cmpb %cl, %al / if the third byte is %al 73*5d9d9091SRichard Lowe je .L6 / goto .L6 (found) 74*5d9d9091SRichard Lowe cmpb %ch, %al / if the fourth is %al 75*5d9d9091SRichard Lowe je .L7 / goto .L7 (found) 76*5d9d9091SRichard Lowe subq $4, %rdx / decrement number of bytes by 4 77*5d9d9091SRichard Lowe addq $4, %rdi / next word 78*5d9d9091SRichard Lowe cmpq $4, %rdx / if number of bytes >= 4 79*5d9d9091SRichard Lowe jae .L3 / goto .L3 80*5d9d9091SRichard Lowe.L1: 81*5d9d9091SRichard Lowe cmpq $0, %rdx / if number of bytes == 0 82*5d9d9091SRichard Lowe jz .L8 / goto .L8 (not found) 83*5d9d9091SRichard Lowe cmpb (%rdi), %al / if a byte in (%rdi) is %al 84*5d9d9091SRichard Lowe je .L4 / goto .L4 (found) 85*5d9d9091SRichard Lowe decq %rdx / decrement number of bytes by 1 86*5d9d9091SRichard Lowe incq %rdi / next byte 87*5d9d9091SRichard Lowe jmp .L1 / goto .L1 88*5d9d9091SRichard Lowe .align 4 89*5d9d9091SRichard Lowe.L8: 90*5d9d9091SRichard Lowe xorl %eax, %eax / not found 91*5d9d9091SRichard Lowe ret / return (0) 92*5d9d9091SRichard Lowe .align 4 93*5d9d9091SRichard Lowe.L2: 94*5d9d9091SRichard Lowe cmpq $0, %rdx / if number of bytes == 0 95*5d9d9091SRichard Lowe jz .L8 / goto .L8 (not found) 96*5d9d9091SRichard Lowe cmpb (%rdi), %al / if a byte in (%rdi) is %al 97*5d9d9091SRichard Lowe je .L4 / goto .L4 (found) 98*5d9d9091SRichard Lowe incq %rdi / next byte 99*5d9d9091SRichard Lowe decq %rdx / decrement number of bytes by 1 100*5d9d9091SRichard Lowe testq $3, %rdi / if %rdi not word aligned 101*5d9d9091SRichard Lowe jnz .L2 / goto .L2 102*5d9d9091SRichard Lowe cmpq $4, %rdx / if number of bytes >= 4 103*5d9d9091SRichard Lowe jae .L3 / goto .L3 (word aligned) 104*5d9d9091SRichard Lowe jmp .L1 / goto .L1 105*5d9d9091SRichard Lowe .align 4 106*5d9d9091SRichard Lowe.L7: 107*5d9d9091SRichard Lowe / found at the fourth byte 108*5d9d9091SRichard Lowe incq %rdi 109*5d9d9091SRichard Lowe.L6: 110*5d9d9091SRichard Lowe / found at the third byte 111*5d9d9091SRichard Lowe incq %rdi 112*5d9d9091SRichard Lowe.L5: 113*5d9d9091SRichard Lowe / found at the second byte 114*5d9d9091SRichard Lowe incq %rdi 115*5d9d9091SRichard Lowe.L4: 116*5d9d9091SRichard Lowe / found at the first byte 117*5d9d9091SRichard Lowe movq %rdi,%rax 118*5d9d9091SRichard Lowe ret 119*5d9d9091SRichard Lowe SET_SIZE(memchr) 120