xref: /titanic_50/usr/src/lib/libc/amd64/gen/memchr.s (revision 9a70fc3be3b1e966bf78825cdb8d509963a6f0a1)
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
5*9a70fc3bSMark J. Nelson * Common Development and Distribution License (the "License").
6*9a70fc3bSMark 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
26*9a70fc3bSMark J. Nelson	.file	"memchr.s"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate/
297c478bd9Sstevel@tonic-gate/ memchr(sptr, c1, n)
307c478bd9Sstevel@tonic-gate/
317c478bd9Sstevel@tonic-gate/ 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.
367c478bd9Sstevel@tonic-gate/
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) {
417c478bd9Sstevel@tonic-gate/			unsigned char	c = (unsigned char)c1;
427c478bd9Sstevel@tonic-gate/			const unsigned char	*sp = sptr;
437c478bd9Sstevel@tonic-gate/
447c478bd9Sstevel@tonic-gate/			do {
457c478bd9Sstevel@tonic-gate/				if (*sp++ == c)
467c478bd9Sstevel@tonic-gate/					return ((void *)--sp);
477c478bd9Sstevel@tonic-gate/			} while (--n != 0);
487c478bd9Sstevel@tonic-gate/		}
497c478bd9Sstevel@tonic-gate/		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) /* (void *s, uchar_t c, size_t n) */
597c478bd9Sstevel@tonic-gate	movl	%esi, %eax	/ move "c" to %eax
607c478bd9Sstevel@tonic-gate	cmpq	$4, %rdx	/ if number of bytes < 4
617c478bd9Sstevel@tonic-gate	jb	.L1		/ goto .L1
627c478bd9Sstevel@tonic-gate	testq	$3, %rdi	/ if %rdi not word aligned
637c478bd9Sstevel@tonic-gate	jnz	.L2		/ goto .L2
647c478bd9Sstevel@tonic-gate	.align	4
657c478bd9Sstevel@tonic-gate.L3:
667c478bd9Sstevel@tonic-gate	movl	(%rdi), %ecx	/ move 1 word from (%rdi) to %ecx
677c478bd9Sstevel@tonic-gate	cmpb	%cl, %al	/ if the first byte is %al
687c478bd9Sstevel@tonic-gate	je	.L4		/ goto .L4 (found)
697c478bd9Sstevel@tonic-gate	cmpb	%ch, %al	/ if the second byte is %al
707c478bd9Sstevel@tonic-gate	je	.L5		/ goto .L5 (found)
717c478bd9Sstevel@tonic-gate	shrl	$16, %ecx	/ right shift 16-bit
727c478bd9Sstevel@tonic-gate	cmpb	%cl, %al	/ if the third byte is %al
737c478bd9Sstevel@tonic-gate	je	.L6		/ goto .L6 (found)
747c478bd9Sstevel@tonic-gate	cmpb	%ch, %al	/ if the fourth is %al
757c478bd9Sstevel@tonic-gate	je	.L7		/ goto .L7 (found)
767c478bd9Sstevel@tonic-gate	subq	$4, %rdx	/ decrement number of bytes by 4
777c478bd9Sstevel@tonic-gate	addq	$4, %rdi	/ next word
787c478bd9Sstevel@tonic-gate	cmpq	$4, %rdx	/ if number of bytes >= 4
797c478bd9Sstevel@tonic-gate	jae	.L3		/ goto .L3
807c478bd9Sstevel@tonic-gate.L1:
817c478bd9Sstevel@tonic-gate	cmpq	$0, %rdx	/ if number of bytes == 0
827c478bd9Sstevel@tonic-gate	jz	.L8		/ goto .L8 (not found)
837c478bd9Sstevel@tonic-gate	cmpb	(%rdi), %al	/ if a byte in (%rdi) is %al
847c478bd9Sstevel@tonic-gate	je	.L4		/ goto .L4 (found)
857c478bd9Sstevel@tonic-gate	decq	%rdx		/ decrement number of bytes by 1
867c478bd9Sstevel@tonic-gate	incq	%rdi		/ next byte
877c478bd9Sstevel@tonic-gate	jmp	.L1		/ goto .L1
887c478bd9Sstevel@tonic-gate	.align	4
897c478bd9Sstevel@tonic-gate.L8:
907c478bd9Sstevel@tonic-gate	xorl	%eax, %eax	/ not found
917c478bd9Sstevel@tonic-gate	ret			/ return (0)
927c478bd9Sstevel@tonic-gate	.align	4
937c478bd9Sstevel@tonic-gate.L2:
947c478bd9Sstevel@tonic-gate	cmpq	$0, %rdx	/ if number of bytes == 0
957c478bd9Sstevel@tonic-gate	jz	.L8		/ goto .L8 (not found)
967c478bd9Sstevel@tonic-gate	cmpb	(%rdi), %al	/ if a byte in (%rdi) is %al
977c478bd9Sstevel@tonic-gate	je	.L4		/ goto .L4 (found)
987c478bd9Sstevel@tonic-gate	incq	%rdi		/ next byte
997c478bd9Sstevel@tonic-gate	decq	%rdx		/ decrement number of bytes by 1
1007c478bd9Sstevel@tonic-gate	testq	$3, %rdi	/ if %rdi not word aligned
1017c478bd9Sstevel@tonic-gate	jnz	.L2		/ goto .L2
1027c478bd9Sstevel@tonic-gate	cmpq	$4, %rdx	/ if number of bytes >= 4
1037c478bd9Sstevel@tonic-gate	jae	.L3		/ goto .L3 (word aligned)
1047c478bd9Sstevel@tonic-gate	jmp	.L1		/ goto .L1
1057c478bd9Sstevel@tonic-gate	.align	4
1067c478bd9Sstevel@tonic-gate.L7:
1077c478bd9Sstevel@tonic-gate	/ found at the fourth byte
1087c478bd9Sstevel@tonic-gate	incq	%rdi
1097c478bd9Sstevel@tonic-gate.L6:
1107c478bd9Sstevel@tonic-gate	/ found at the third byte
1117c478bd9Sstevel@tonic-gate	incq	%rdi
1127c478bd9Sstevel@tonic-gate.L5:
1137c478bd9Sstevel@tonic-gate	/ found at the second byte
1147c478bd9Sstevel@tonic-gate	incq	%rdi
1157c478bd9Sstevel@tonic-gate.L4:
1167c478bd9Sstevel@tonic-gate	/ found at the first byte
1177c478bd9Sstevel@tonic-gate	movq	%rdi,%rax
1187c478bd9Sstevel@tonic-gate	ret
1197c478bd9Sstevel@tonic-gate	SET_SIZE(memchr)
120