xref: /titanic_41/usr/src/lib/libc/sparcv9/gen/memset.s (revision c138f478d2bc94e73ab8f6a084e323bec25e62f5)
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, Version 1.0 only
6 * (the "License").  You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*
23 * Copyright 1997-2003 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27	.ident	"%Z%%M%	%I%	%E% SMI"
28
29	.file	"%M%"
30
31/*
32 * memset(sp, c, n)
33 *
34 * Set an array of n chars starting at sp to the character c.
35 * Return sp.
36 *
37 * Fast assembler language version of the following C-program for memset
38 * which represents the `standard' for the C-library.
39 *
40 *	void *
41 *	memset(void *sp1, int c, size_t n)
42 *	{
43 *	    if (n != 0) {
44 *		char *sp = sp1;
45 *		do {
46 *		    *sp++ = (char)c;
47 *		} while (--n != 0);
48 *	    }
49 *	    return (sp1);
50 *	}
51 *
52 *
53 *
54 * Algorithm used:
55 *	For small stores (6 or fewer bytes), bytes will be stored one at a time.
56 *
57 *	When setting 15 or more bytes, there will be at least 8 bytes aligned
58 *	on an 8-byte boundary.  So, leading bytes will be set, then as many
59 *	8-byte aligned chunks as possible will be set, followed by any trailing
60 *	bytes.
61 *
62 *	For between 8 and 14 bytes (inclusive), leading odd bytes will be
63 *	set, followed by 4-byte chunks, followed by trailing bytes.
64 *
65 * Inputs:
66 *	o0:  pointer to start of area to be set to a given value
67 *	o1:  character used to set memory at location in i0
68 *	o2:  number of bytes to be set
69 *
70 * Outputs:
71 *	o0:  pointer to start of area set (same as input value in o0)
72 *
73 */
74
75#include <sys/asm_linkage.h>
76
77	ANSI_PRAGMA_WEAK(memset,function)
78
79#include "synonyms.h"
80
81	.weak	_private_memset
82	.type	_private_memset, #function
83	_private_memset = memset
84
85	ENTRY(memset)
86	mov	%o0, %o5		! need to return this value
87	cmp	%o2, 7
88	blu,pn	%xcc, .wrchar		! small count:  just set bytes
89	and	%o1, 0xff, %o1
90
91	sll	%o1, 8, %o4		! generate 4 bytes filled with char
92	or 	%o1, %o4, %o1
93	sll	%o1, 16, %o4
94	cmp	%o2, 15
95	blu,pn	%xcc, .walign		! not enough to guarantee 8-byte align
96	or	%o1, %o4, %o1
97
98	sllx	%o1, 32, %o4		! now fill the other 4 bytes with char
99	or 	%o1, %o4, %o1
100
101.dalign:			! Set bytes until 8-byte aligned
102	btst	7, %o5			! 8-byte aligned?
103	bz,a,pn	%icc, .wrdbl
104	andn	%o2, 7, %o3		! o3 has 8-byte multiple
105
106	dec	%o2
107	stb	%o1, [%o5]		! clear a byte
108	b	.dalign			! go see if aligned yet
109	inc	%o5
110
111	.align	32
112.wrdbl:
113	stx	%o1, [%o5]		! write aligned 8 bytes
114	subcc	%o3, 8, %o3
115	bnz,pt	%xcc, .wrdbl
116	inc	8, %o5
117
118	b	.wrchar			! write the remaining bytes
119	and	%o2, 7, %o2		! leftover count, if any
120
121.walign:			! Set bytes until 4-byte aligned
122	btst	3, %o5			! if bigger, align to 4 bytes
123	bz,pn	%icc, .wrword
124	andn	%o2, 3, %o3		! create word sized count in %o3
125
126	dec	%o2			! decrement count
127	stb	%o1, [%o5]		! clear a byte
128	b	.walign
129	inc	%o5			! next byte
130
131.wrword:
132	st	%o1, [%o5]		! 4-byte writing loop
133	subcc	%o3, 4, %o3
134	bnz,pn	%xcc, .wrword
135	inc	4, %o5
136
137	and	%o2, 3, %o2		! leftover count, if any
138
139.wrchar:
140	deccc	%o2			! byte clearing loop
141	inc	%o5
142	bgeu,a,pt %xcc, .wrchar
143	stb	%o1, [%o5 + -1]		! we've already incremented the address
144
145	retl
146	sub	%o0, %g0, %o0
147
148	SET_SIZE(memset)
149