usr/src/lib/libc/i386/gen/memchr.s
author Mark J. Nelson <Mark.J.Nelson@Sun.COM>
Wed, 06 Aug 2008 16:29:39 -0600
changeset 7298 b69e27387f74
parent 0 68f95e015346
permissions -rw-r--r--
6733918 Teamware has retired, please welcome your new manager, Mercurial 4758439 some files use "current date" sccs keywords 6560843 asm sources should not rely on .file "%M%" for naming STT_FILE symbols 6560958 Solaris:: perl modules should not use SCCS keywords in version information 6729074 webrev doesn't deal well with remote ssh hg parents
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
     1
/*
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
     2
 * CDDL HEADER START
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
     3
 *
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
     4
 * The contents of this file are subject to the terms of the
7298
b69e27387f74 6733918 Teamware has retired, please welcome your new manager, Mercurial
Mark J. Nelson <Mark.J.Nelson@Sun.COM>
parents: 0
diff changeset
     5
 * Common Development and Distribution License (the "License").
b69e27387f74 6733918 Teamware has retired, please welcome your new manager, Mercurial
Mark J. Nelson <Mark.J.Nelson@Sun.COM>
parents: 0
diff changeset
     6
 * You may not use this file except in compliance with the License.
0
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
     7
 *
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
     8
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
     9
 * or http://www.opensolaris.org/os/licensing.
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    10
 * See the License for the specific language governing permissions
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    11
 * and limitations under the License.
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    12
 *
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    13
 * When distributing Covered Code, include this CDDL HEADER in each
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    14
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    15
 * If applicable, add the following below this CDDL HEADER, with the
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    16
 * fields enclosed by brackets "[]" replaced with your own identifying
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    17
 * information: Portions Copyright [yyyy] [name of copyright owner]
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    18
 *
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    19
 * CDDL HEADER END
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    20
 */
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    21
/*
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    22
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    23
 * Use is subject to license terms.
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    24
 */
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    25
7298
b69e27387f74 6733918 Teamware has retired, please welcome your new manager, Mercurial
Mark J. Nelson <Mark.J.Nelson@Sun.COM>
parents: 0
diff changeset
    26
	.file	"memchr.s"
0
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    27
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    28
/
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    29
/ memchr(sptr, c1, n)
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    30
/
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    31
/ Returns the pointer in sptr at which the character c1 appears; 
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    32
/ or NULL if not found in chars; doesn't stop at \0.
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    33
/
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    34
/ Fast assembly language version of the following C-program memchr
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    35
/ which represents the `standard' for the C-library.
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    36
/ 
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    37
/	void *
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    38
/	memchr(const void *sptr, int c1, size_t n)
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    39
/	{
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    40
/		if (n != 0) {
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    41
/			unsigned char	c = (unsigned char)c1; 
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    42
/			const unsigned char	*sp = sptr; 
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    43
/
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    44
/			do {
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    45
/				if (*sp++ == c)
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    46
/					return ((void *)--sp); 
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    47
/			} while (--n != 0); 
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    48
/		}
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    49
/		return (NULL); 
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    50
/	}
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    51
/
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    52
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    53
#include "SYS.h"
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    54
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    55
	.globl	memchr
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    56
	.align	4
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    57
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    58
	ENTRY(memchr)
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    59
	pushl	%edi		/ save register variable
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    60
	movl	8(%esp), %eax	/ %eax = string address
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    61
	movl	12(%esp), %ecx	/ %cl = byte that is sought
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    62
	movl	16(%esp), %edi	/ %edi = number of bytes
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    63
	cmpl	$4, %edi	/ if number of bytes < 4
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    64
	jb	.L1		/ goto .L1
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    65
	testl	$3, %eax	/ if %eax not word aligned
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    66
	jnz	.L2		/ goto .L2
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    67
	.align	4
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    68
.L3:
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    69
	movl	(%eax), %edx	/ move 1 word from (%eax) to %edx
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    70
	cmpb	%dl, %cl	/ if the first byte is %cl
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    71
	je	.L4		/ goto .L4 (found)
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    72
	cmpb	%dh, %cl	/ if the second byte is %cl
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    73
	je	.L5		/ goto .L5 (found)
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    74
	shrl	$16, %edx	/ right shift 16-bit
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    75
	cmpb	%dl, %cl	/ if the third byte is %cl
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    76
	je	.L6		/ goto .L6 (found)
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    77
	cmpb	%dh, %cl	/ if the fourth is %cl
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    78
	je	.L7		/ goto .L7 (found)
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    79
	subl	$4, %edi	/ decrement number of bytes by 4
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    80
	addl	$4, %eax	/ next word
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    81
	cmpl	$4, %edi	/ if number of bytes >= 4
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    82
	jae	.L3		/ goto .L3
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    83
.L1:
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    84
	cmpl	$0, %edi	/ if number of bytes == 0
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    85
	jz	.L8		/ goto .L8 (not found)
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    86
	cmpb	(%eax), %cl	/ if a byte in (%eax) is %cl
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    87
	je	.L4		/ goto .L4 (found)
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    88
	decl	%edi		/ decrement number of bytes by 1
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    89
	incl	%eax		/ next byte
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    90
	jmp	.L1		/ goto .L1
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    91
	.align	4
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    92
.L8:	
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    93
	xorl	%eax, %eax	/ not found
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    94
	popl	%edi		/ restore register
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    95
	ret			/ return (0)
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    96
	.align	4
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    97
.L2:
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    98
	cmpl	$0, %edi	/ if number of bytes == 0
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    99
	jz	.L8		/ goto .L8 (not found)
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
   100
	cmpb	(%eax), %cl	/ if a byte in (%eax) is %cl
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
   101
	je	.L4		/ goto .L4 (found)
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
   102
	incl	%eax		/ next byte
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
   103
	decl	%edi		/ decrement number of bytes by 1
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
   104
	testl	$3, %eax	/ if %eax not word aligned
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
   105
	jnz	.L2		/ goto .L2
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
   106
	cmpl	$4, %edi	/ if number of bytes >= 4
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
   107
	jae	.L3		/ goto .L3 (word aligned)
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
   108
	jmp	.L1		/ goto .L1
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
   109
	.align	4
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
   110
.L7:
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
   111
	/ found at the fourth byte	
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
   112
	incl	%eax
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
   113
.L6:
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
   114
	/ found at the third byte
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
   115
	incl	%eax
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
   116
.L5:
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
   117
	/ found at the second byte	
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
   118
	incl	%eax
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
   119
.L4:
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
   120
	/ found at the first byte	
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
   121
	popl	%edi		/ restore register variable
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
   122
	ret			 
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
   123
	SET_SIZE(memchr)