xref: /illumos-gate/usr/src/lib/libc/i386/gen/memchr.S (revision 55fea89d)
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
59a70fc3bSMark J. Nelson * Common Development and Distribution License (the "License").
69a70fc3bSMark J. Nelson * 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 */
217c478bd9Sstevel@tonic-gate/*
227c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
269a70fc3bSMark J. Nelson	.file	"memchr.s"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate/
297c478bd9Sstevel@tonic-gate/ memchr(sptr, c1, n)
307c478bd9Sstevel@tonic-gate/
31*55fea89dSDan Cross/ Returns the pointer in sptr at which the character c1 appears;
327c478bd9Sstevel@tonic-gate/ or NULL if not found in chars; doesn't stop at \0.
337c478bd9Sstevel@tonic-gate/
347c478bd9Sstevel@tonic-gate/ Fast assembly language version of the following C-program memchr
357c478bd9Sstevel@tonic-gate/ which represents the `standard' for the C-library.
36*55fea89dSDan Cross/
377c478bd9Sstevel@tonic-gate/	void *
387c478bd9Sstevel@tonic-gate/	memchr(const void *sptr, int c1, size_t n)
397c478bd9Sstevel@tonic-gate/	{
407c478bd9Sstevel@tonic-gate/		if (n != 0) {
41*55fea89dSDan Cross/			unsigned char	c = (unsigned char)c1;
42*55fea89dSDan Cross/			const unsigned char	*sp = sptr;
437c478bd9Sstevel@tonic-gate/
447c478bd9Sstevel@tonic-gate/			do {
457c478bd9Sstevel@tonic-gate/				if (*sp++ == c)
46*55fea89dSDan Cross/					return ((void *)--sp);
47*55fea89dSDan Cross/			} while (--n != 0);
487c478bd9Sstevel@tonic-gate/		}
49*55fea89dSDan Cross/		return (NULL);
507c478bd9Sstevel@tonic-gate/	}
517c478bd9Sstevel@tonic-gate/
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate#include "SYS.h"
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate	.globl	memchr
567c478bd9Sstevel@tonic-gate	.align	4
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate	ENTRY(memchr)
597c478bd9Sstevel@tonic-gate	pushl	%edi		/ save register variable
607c478bd9Sstevel@tonic-gate	movl	8(%esp), %eax	/ %eax = string address
617c478bd9Sstevel@tonic-gate	movl	12(%esp), %ecx	/ %cl = byte that is sought
627c478bd9Sstevel@tonic-gate	movl	16(%esp), %edi	/ %edi = number of bytes
637c478bd9Sstevel@tonic-gate	cmpl	$4, %edi	/ if number of bytes < 4
647c478bd9Sstevel@tonic-gate	jb	.L1		/ goto .L1
657c478bd9Sstevel@tonic-gate	testl	$3, %eax	/ if %eax not word aligned
667c478bd9Sstevel@tonic-gate	jnz	.L2		/ goto .L2
677c478bd9Sstevel@tonic-gate	.align	4
687c478bd9Sstevel@tonic-gate.L3:
697c478bd9Sstevel@tonic-gate	movl	(%eax), %edx	/ move 1 word from (%eax) to %edx
707c478bd9Sstevel@tonic-gate	cmpb	%dl, %cl	/ if the first byte is %cl
717c478bd9Sstevel@tonic-gate	je	.L4		/ goto .L4 (found)
727c478bd9Sstevel@tonic-gate	cmpb	%dh, %cl	/ if the second byte is %cl
737c478bd9Sstevel@tonic-gate	je	.L5		/ goto .L5 (found)
747c478bd9Sstevel@tonic-gate	shrl	$16, %edx	/ right shift 16-bit
757c478bd9Sstevel@tonic-gate	cmpb	%dl, %cl	/ if the third byte is %cl
767c478bd9Sstevel@tonic-gate	je	.L6		/ goto .L6 (found)
777c478bd9Sstevel@tonic-gate	cmpb	%dh, %cl	/ if the fourth is %cl
787c478bd9Sstevel@tonic-gate	je	.L7		/ goto .L7 (found)
797c478bd9Sstevel@tonic-gate	subl	$4, %edi	/ decrement number of bytes by 4
807c478bd9Sstevel@tonic-gate	addl	$4, %eax	/ next word
817c478bd9Sstevel@tonic-gate	cmpl	$4, %edi	/ if number of bytes >= 4
827c478bd9Sstevel@tonic-gate	jae	.L3		/ goto .L3
837c478bd9Sstevel@tonic-gate.L1:
847c478bd9Sstevel@tonic-gate	cmpl	$0, %edi	/ if number of bytes == 0
857c478bd9Sstevel@tonic-gate	jz	.L8		/ goto .L8 (not found)
867c478bd9Sstevel@tonic-gate	cmpb	(%eax), %cl	/ if a byte in (%eax) is %cl
877c478bd9Sstevel@tonic-gate	je	.L4		/ goto .L4 (found)
887c478bd9Sstevel@tonic-gate	decl	%edi		/ decrement number of bytes by 1
897c478bd9Sstevel@tonic-gate	incl	%eax		/ next byte
907c478bd9Sstevel@tonic-gate	jmp	.L1		/ goto .L1
917c478bd9Sstevel@tonic-gate	.align	4
92*55fea89dSDan Cross.L8:
937c478bd9Sstevel@tonic-gate	xorl	%eax, %eax	/ not found
947c478bd9Sstevel@tonic-gate	popl	%edi		/ restore register
957c478bd9Sstevel@tonic-gate	ret			/ return (0)
967c478bd9Sstevel@tonic-gate	.align	4
977c478bd9Sstevel@tonic-gate.L2:
987c478bd9Sstevel@tonic-gate	cmpl	$0, %edi	/ if number of bytes == 0
997c478bd9Sstevel@tonic-gate	jz	.L8		/ goto .L8 (not found)
1007c478bd9Sstevel@tonic-gate	cmpb	(%eax), %cl	/ if a byte in (%eax) is %cl
1017c478bd9Sstevel@tonic-gate	je	.L4		/ goto .L4 (found)
1027c478bd9Sstevel@tonic-gate	incl	%eax		/ next byte
1037c478bd9Sstevel@tonic-gate	decl	%edi		/ decrement number of bytes by 1
1047c478bd9Sstevel@tonic-gate	testl	$3, %eax	/ if %eax not word aligned
1057c478bd9Sstevel@tonic-gate	jnz	.L2		/ goto .L2
1067c478bd9Sstevel@tonic-gate	cmpl	$4, %edi	/ if number of bytes >= 4
1077c478bd9Sstevel@tonic-gate	jae	.L3		/ goto .L3 (word aligned)
1087c478bd9Sstevel@tonic-gate	jmp	.L1		/ goto .L1
1097c478bd9Sstevel@tonic-gate	.align	4
1107c478bd9Sstevel@tonic-gate.L7:
111*55fea89dSDan Cross	/ found at the fourth byte
1127c478bd9Sstevel@tonic-gate	incl	%eax
1137c478bd9Sstevel@tonic-gate.L6:
1147c478bd9Sstevel@tonic-gate	/ found at the third byte
1157c478bd9Sstevel@tonic-gate	incl	%eax
1167c478bd9Sstevel@tonic-gate.L5:
117*55fea89dSDan Cross	/ found at the second byte
1187c478bd9Sstevel@tonic-gate	incl	%eax
1197c478bd9Sstevel@tonic-gate.L4:
120*55fea89dSDan Cross	/ found at the first byte
1217c478bd9Sstevel@tonic-gate	popl	%edi		/ restore register variable
122*55fea89dSDan Cross	ret
1237c478bd9Sstevel@tonic-gate	SET_SIZE(memchr)
124