xref: /titanic_41/usr/src/lib/libc/amd64/gen/memccpy.s (revision 82629e3015252bf18319ba3815c773df23e21436)
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27	.file	"memccpy.s"
28
29#include <sys/asm_linkage.h>
30
31	ANSI_PRAGMA_WEAK(memccpy,function)
32
33	ENTRY(memccpy)	/* (void *dst, void *src, uchar_t c, size_t) */
34.loop:
35	decq	%rcx		/ decrement bytes to go
36	jl	.notfound
37	movb	(%rsi),%dh
38	movb	%dh,(%rdi)	/ move byte
39	cmpb	%dh,%dl		/ is it the byte sought?
40	je	.found		/ yes
41
42	decq	%rcx		/ decrement bytes to go
43	jl	.notfound
44	movb	1(%rsi),%dh
45	movb	%dh,1(%rdi)	/ move byte
46	cmpb	%dh,%dl		/ is it the byte sought?
47	je	.found1		/ yes
48
49	decq	%rcx		/ decrement bytes to go
50	jl	.notfound
51	movb	2(%rsi),%dh
52	movb	%dh,2(%rdi)	/ move byte
53	cmpb	%dh,%dl		/ is it the byte sought?
54	je	.found2		/ yes
55
56	decq	%rcx		/ decrement bytes to go
57	jl	.notfound
58	movb	3(%rsi),%dh
59	movb	%dh,3(%rdi)	/ move byte
60	addq	$4,%rsi
61	addq	$4,%rdi
62	cmpb	%dh,%dl		/ is it the byte sought?
63	jne	.loop		/ no
64	decq	%rdi
65
66.found:
67	incq	%rdi		/ return pointer to next byte in dest
68	movq	%rdi,%rax
69	ret
70
71	.align	4
72.found2:
73	incq	%rdi
74.found1:
75	addq	$2,%rdi		/ return pointer to next byte in dest
76	movq	%rdi,%rax
77	ret
78
79	.align	4
80.notfound:
81	xorl	%eax,%eax	/ search fails
82	ret
83	SET_SIZE(memccpy)
84