xref: /titanic_50/usr/src/lib/libc/sparcv9/gen/memset.s (revision 9a70fc3be3b1e966bf78825cdb8d509963a6f0a1)
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	"memset.s"
28
29/*
30 * memset(sp, c, n)
31 *
32 * Set an array of n chars starting at sp to the character c.
33 * Return sp.
34 *
35 * Fast assembler language version of the following C-program for memset
36 * which represents the `standard' for the C-library.
37 *
38 *	void *
39 *	memset(void *sp1, int c, size_t n)
40 *	{
41 *	    if (n != 0) {
42 *		char *sp = sp1;
43 *		do {
44 *		    *sp++ = (char)c;
45 *		} while (--n != 0);
46 *	    }
47 *	    return (sp1);
48 *	}
49 *
50 *
51 *
52 * Algorithm used:
53 *	For small stores (6 or fewer bytes), bytes will be stored one at a time.
54 *
55 *	When setting 15 or more bytes, there will be at least 8 bytes aligned
56 *	on an 8-byte boundary.  So, leading bytes will be set, then as many
57 *	8-byte aligned chunks as possible will be set, followed by any trailing
58 *	bytes.
59 *
60 *	For between 8 and 14 bytes (inclusive), leading odd bytes will be
61 *	set, followed by 4-byte chunks, followed by trailing bytes.
62 *
63 * Inputs:
64 *	o0:  pointer to start of area to be set to a given value
65 *	o1:  character used to set memory at location in i0
66 *	o2:  number of bytes to be set
67 *
68 * Outputs:
69 *	o0:  pointer to start of area set (same as input value in o0)
70 *
71 */
72
73#include <sys/asm_linkage.h>
74
75	ANSI_PRAGMA_WEAK(memset,function)
76
77	ENTRY(memset)
78	mov	%o0, %o5		! need to return this value
79	cmp	%o2, 7
80	blu,pn	%xcc, .wrchar		! small count:  just set bytes
81	and	%o1, 0xff, %o1
82
83	sll	%o1, 8, %o4		! generate 4 bytes filled with char
84	or 	%o1, %o4, %o1
85	sll	%o1, 16, %o4
86	cmp	%o2, 15
87	blu,pn	%xcc, .walign		! not enough to guarantee 8-byte align
88	or	%o1, %o4, %o1
89
90	sllx	%o1, 32, %o4		! now fill the other 4 bytes with char
91	or 	%o1, %o4, %o1
92
93.dalign:			! Set bytes until 8-byte aligned
94	btst	7, %o5			! 8-byte aligned?
95	bz,a,pn	%icc, .wrdbl
96	andn	%o2, 7, %o3		! o3 has 8-byte multiple
97
98	dec	%o2
99	stb	%o1, [%o5]		! clear a byte
100	b	.dalign			! go see if aligned yet
101	inc	%o5
102
103	.align	32
104.wrdbl:
105	stx	%o1, [%o5]		! write aligned 8 bytes
106	subcc	%o3, 8, %o3
107	bnz,pt	%xcc, .wrdbl
108	inc	8, %o5
109
110	b	.wrchar			! write the remaining bytes
111	and	%o2, 7, %o2		! leftover count, if any
112
113.walign:			! Set bytes until 4-byte aligned
114	btst	3, %o5			! if bigger, align to 4 bytes
115	bz,pn	%icc, .wrword
116	andn	%o2, 3, %o3		! create word sized count in %o3
117
118	dec	%o2			! decrement count
119	stb	%o1, [%o5]		! clear a byte
120	b	.walign
121	inc	%o5			! next byte
122
123.wrword:
124	st	%o1, [%o5]		! 4-byte writing loop
125	subcc	%o3, 4, %o3
126	bnz,pn	%xcc, .wrword
127	inc	4, %o5
128
129	and	%o2, 3, %o2		! leftover count, if any
130
131.wrchar:
132	deccc	%o2			! byte clearing loop
133	inc	%o5
134	bgeu,a,pt %xcc, .wrchar
135	stb	%o1, [%o5 + -1]		! we've already incremented the address
136
137	retl
138	sub	%o0, %g0, %o0
139
140	SET_SIZE(memset)
141